【问题标题】:Tracking counts of examples used in training跟踪训练中使用的示例计数
【发布时间】:2016-09-23 10:36:34
【问题描述】:

我正在尝试基于 tensorflow 存储库上的 skipgrams 实现来实现 CBOW word2vec 模型:

https://github.com/tensorflow/tensorflow/blob/v0.10.0/tensorflow/models/embedding/word2vec.py

我有previously implemented TensorFlow 教程之后的简化版本,所以我知道我必须修改数据批处理功能以及图表的一小部分才能获得上下文嵌入。

在skipgram实现中,数据批处理功能在348-351行中使用。

(words, counts, words_per_epoch, self._epoch, self._words, examples,
 labels) = word2vec.skipgram(filename=opts.train_data,
                             batch_size=opts.batch_size,
                             window_size=opts.window_size,
                             min_count=opts.min_count,
                             subsample=opts.subsample)

据我了解,赋值的变量如下:

  • words: 词汇中的术语
  • counts:语料库中使用的术语的相关计数
  • words_per_epoch: 语料库总字数
  • self._epoch: 当前使用的 epoch 计数
  • self._words:当前使用的训练示例数
  • examples:当前批次的训练样例
  • labels:当前批次的训练标签

我已经成功地复制了 wordscountswords_per_epochexampleslabels 的张量。然而,self._epochself._words 却让我望而却步。如果我的理解是正确的,我需要能够跟踪使用的训练示例的数量。但是,sample batching function 不提供此功能。这些计数稍后会以多线程方式用于终止训练循环,因此我不能简单地使用循环来将计数相加。

我知道一些 tensorflow 操作是用 C++ 实现的。但是,由于我不熟悉 C++,我将不得不使用 Python 复制这些部分。

如果我能得到一些建议来获得self._words 的张量,那就太好了。张量基本上只有在每次调用新一批示例/标签时才增加。有了这个,我可以简单地使用self._epoch = self._words // words_per_epoch 来获取另一个张量。

【问题讨论】:

    标签: tensorflow


    【解决方案1】:

    在查看tensorflow.models.embedding.word2vec_optimized.py 的源代码时找出了诀窍。具体来说,当在lines 218-225 中调用loss 时,global_step 如何递增。

    就我而言,我必须这样做:

    # codes to prepare features and labels tensors
    
    data_processed = tf.Variable(0, trainable=False, dtype=tf.int64)
    epochs_processed = data_processed // data_per_epoch
    
    inc_op = data_processed.assign_add(batch_size)
    with tf.control_dependencies([inc_op]):
        features_batch, labels_batch = tf.train.batch([features, labels],
                                                      batch_size=batch_size)
    

    在这种情况下,每当调用 features_batchlabels_batch 时,张量 data_processed 将始终增加 batch_sizeepochs_processed 也会相应增加。

    tf.control_dependencies(control_inputs) 的使用是这里的关键。它返回一个上下文管理器。 control_inputs 中指定的操作必须在上下文中定义的操作之前执行。

    【讨论】:

      猜你喜欢
      • 2020-10-01
      • 1970-01-01
      • 1970-01-01
      • 2019-09-09
      • 2018-02-02
      • 2018-03-31
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多