tensorflow-datasets - 将 doc2vec 从 feed-dict 转换为 tf.data 管道
问题描述
我正在尝试将doc2vec 示例从使用 feed-dict 转换为使用 tf.data 管道。我正在努力如何实现窗口序列生成。
我已经成功地在text_helpers.py中实现了所有数据方法的加载,但我不知道如何处理该generate_batch_data
方法。任何指针将不胜感激。
我尝试使用 tf.string.split 将句子拆分为单词,但是我无法获得结果的形状以用于设置要遍历的范围ws
。(见下文)它会引发错误,因为它是None
而不是整数。我假设变量的行append
也window_sequences
不起作用。
更新:将我的代码添加到 github 存储库 https://github.com/waldren/doc2vec-datapipeline
更新:我认为我正朝着正确的方向前进。我现在有一个 tf.while_loop
def generate_window_sequence(self, sentence_ix, sentence, label):
ws = tf.strings.split([sentence], sep=' ').values
offset = tf.constant(int(self.window_size/2))
i = tf.constant(int(self.window_size/2))
def while_condition (i, window_sequences, ws, offset):
return tf.less(i, tf.math.subtract(tf.size(ws), offset))
def body(i, window_sequences, ws, offset):
#TODO - move within offset if it is > 1
before = tf.math.subtract(i, offset)
after = tf.math.add(i, offset)
#window_sequences.append((tf.gather(ws, [before, i])))
#window_sequences.append((tf.gather(ws, [after, i])))
return i+1, window_sequences, ws, offset
window_sequences = []
window_sequences.append(("before1","test1"))
i, window_sequences, ws, offset = tf.while_loop(while_condition, body, [i, window_sequences, ws, offset])
return sentence_ix, window_sequences, label
这将“运行”,但我不知道循环是否多次运行。但是,一旦我取消注释body
函数中的两行(以 window_sequences 开头),就会出现ValueError: Number of inputs and outputs of body must match loop_vars: 5, 7
错误。
解决方案
推荐阅读
- postgresql - 加快对重叠日期范围的 postgres 读取窗口查询
- javascript - Javascript - 为什么我的回调不起作用?我得到一个空数组而不是偶数数组
- uwp - DataGrid - 有条件地选择 RowStyle
- python - 遍历不相等的数据帧以检查一个数据帧的元素是否存在于其他数据帧中
- c# - 如何调试在另一个程序中使用的 DLL?
- c++ - C++派生类构造函数调用基类构造函数错误
- javascript - 将项目添加到数组末尾,然后调用函数 - React
- javascript - 可以跨两张表以 16 行块将 CSV 导出到 Google 表格模板中吗?
- r - mapply 不会单独读取列表项
- python-3.x - 使用 Robot Framework 时如何访问类中的方法?