Tensorflow图形变得太大

问题描述:

我目前正在使用Tensorflow进行工作中正在开发的深度学习应用程序。我无法详细了解应用程序的具体情况,但代码类似于自定义的Word2Vec,但每个单词 - 矢量表示的成本需要单独计算。 :Tensorflow图形变得太大

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) 

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

动态创建TensorFlow图将不如重复使用图。不看代码就很难说更多,但将所有条件逻辑折叠到TensorFlow中会使问题消失。否则,试图以某种方式缓存生成的图表也应该有所帮助。

+0

谢谢......我结束了使用一个简单的倾盆楼梯下降的实现。无法用tensorflow解决它。任何想法,如果动态图形框架(如pytorch)会有所帮助? –

+0

倾斜梯度下降是tensorflow模型在默认情况下用于分布式和平行训练的模型。 –