Tensorflow图变得太大了

时间:2016-11-30 02:52:45

标签: python tensorflow word2vec

我目前正在使用Tensorflow进行深度学习,我正在开发工作。我不能详细介绍应用程序,但代码类似于自定义的Word2Vec,但每个单词矢量表示的成本需要单独计算。 :

def cost_function(word_vector, other_word_vectors):
    # cost for a single training example needs to be computed
    # separately, and it depends on the vector representations
    # of some other words, depending on the training instance
    return tf.nn.tanh(some_mathematical_computation)

这种方法的问题在于图形大小往往会爆炸,compute_gradients操作需要花费很多时间。不仅如此,compute_gradients所花费的时间与图表大小呈线性增长。 tf.stop_gradients由于某种原因没有帮助。帮助是为每个培训实例初始化一个新的tf.Graph和一个新的tf.Session()(称之为小批量),并行执行多个此类计算(称之为小批量集合) ,然后将结果合并并保存下一个小批量收集。

def learning(mini_batch):
     with tf.Graph().as_default() as g:
        with tf.Session() as sess:
            self.perform_learning_on_batch(mini_batch, sess)
            return resultant_model 

def collection_learn(mini_batch_collection):
    models = run_learning_in_parallel()
    return combine_model(models)

def master():
   initial_model = something
   for mini_batch_collection in dataset:
       new_model = collection_learn(mini_batch_collection)

有没有更好的方法为这样的应用程序执行并行学习?

1 个答案:

答案 0 :(得分:0)

动态创建TensorFlow图形不如重用图表有效。在不查看代码的情况下,很难说更多,但将所有条件逻辑折叠到TensorFlow中会使这个问题消失。如果做不到这一点,试图以某种方式缓存生成的图表也应该有所帮助。