首页 > 解决方案 > 将 doc2vec 从 feed-dict 转换为 tf.data 管道

问题描述

我正在尝试将doc2vec 示例从使用 feed-dict 转换为使用 tf.data 管道。我正在努力如何实现窗口序列生成。

我已经成功地在text_helpers.py中实现了所有数据方法的加载,但我不知道如何处理该generate_batch_data方法。任何指针将不胜感激。

我尝试使用 tf.string.split 将句子拆分为单词,但是我无法获得结果的形状以用于设置要遍历的范围ws。(见下文)它会引发错误,因为它是None而不是整数。我假设变量的行appendwindow_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错误。

标签: tensorflow-datasets

解决方案


推荐阅读