【问题标题】:ResourceExhaustedError :OOM when allocating tensor with shape []ResourceExhaustedError :OOM 分配具有形状 [] 的张量时
【发布时间】:2017-02-28 03:21:49
【问题描述】:
def RNN(X, weights, biases):
    X = tf.reshape(X, [-1, n_inputs])
    X_in = tf.matmul(X, weights['in']) + biases['in']
    X_in = tf.reshape(X_in, [-1, n_steps, n_hidden_units])
    lstm_cell = tf.nn.rnn_cell.BasicLSTMCell(n_hidden_units, forget_bias=0.0, state_is_tuple=True)
    init_state = lstm_cell.zero_state(batch_size, dtype=tf.float32)
    outputs, final_state = tf.nn.dynamic_rnn(lstm_cell, X_in, initial_state=init_state, time_major=False)

    outputs = tf.unpack(tf.transpose(outputs, [1, 0, 2]))    # states is the last outputs
    results = tf.matmul(outputs[-1], weights['out']) + biases['out']
    del outputs,final_state,lstm_cell,init_state,X,X_in
    return results

def while_loop(s,e,step):
    while s+batch_size<ran:
        batch_id=file_id[s:e]
        batch_col=label_matrix[s:e]                                             

        batch_label = csc_matrix((data, (batch_row, batch_col)), shape=(batch_size, n_classes))
        batch_label = batch_label.toarray()
        batch_xs1=tf.nn.embedding_lookup(embedding_matrix,batch_id)
        batch_xs=sess.run(batch_xs1)
        del batch_xs1
        sess.run([train_op], feed_dict={x: batch_xs,
                                        y: batch_label})

        print(step,':',sess.run(accuracy, feed_dict={x: batch_xs,y: batch_label}),sess.run(cost,feed_dict={x: batch_xs,y: batch_label}))
        if step!=0 and step % 20 == 0:
            save_path = saver.save(sess, './model/lstm_classification.ckpt',write_meta_graph=False)
            print('Save to path', save_path)

        step += 1
        s+=batch_size
        e+=batch_size
        del batch_label,batch_xs,batch_id,batch_col
        print(hp.heap())
        print(hp.heap().more)

这是我的代码。它一直出现这个错误“ResourceExhaustedError:OOM when allocating tensor with shape” 我用了guppy。然后得到了这个。result of guppy

为什么tensorflow的变量占用这么多空间。

【问题讨论】:

  • 请参阅this advice,了解如何处理 TensorFlow 中的内存泄漏。特别是,在 Python 循环中对 tf.nn.embedding_lookup(embedding_matrix, ...) 的调用表明 embedding_matrix 正在被转换为 TensorFlow 常量并多次存储在图中,这可能是内存泄漏的根源。
  • 你建议使用 tf.graph.finalize() 。但之后我不能使用 tf.nn.embedding_lookup(embedding_matrix, ...) 。那我该怎么办?
  • 谢谢!措辞。还有一个问题,在这段代码中,我保存了模型。当我恢复它时,是否需要再次运行 init = tf.initialize_all_variables()?在恢复步骤之前或之后?
  • 假设您从中恢复的检查点具有所有变量,您不需要运行由tf.initialize_all_variables()(现在在 TF 1.0 中称为tf.global_variables_initializer())返回的操作。

标签: python tensorflow deep-learning lstm word-sense-disambiguation


【解决方案1】:

我最近在使用 TF + Keras 和之前使用 yolo v3 的 Darknet 时遇到了这个问题。 我的数据集包含非常大的图像,用于存储我的两台 GTX 1050。 我不得不将图像调整为更小。 平均而言,1024x1024 图像每个 GPU 需要 6GB。

【讨论】:

    【解决方案2】:

    问题是由训练循环中的这一行引起的:

    while s + batch_size < ran:
        # ...
        batch_xs1 = tf.nn.embedding_lookup(embedding_matrix, batch_id)
    

    调用 tf.nn.embedding_lookup() 函数会将节点添加到 TensorFlow 图中,并且——因为这些节点永远不会被垃圾回收——在循环中这样做会导致内存泄漏。

    内存泄漏的实际原因可能是tf.nn.embedding_lookup() 的参数中的embedding_matrix NumPy 数组。 TensorFlow 尝试提供帮助,并将参数中的所有 NumPy 数组转换为函数到 TensorFlow 图中的tf.constant() 节点。然而,在一个循环中,这将导致embedding_matrix 的多个单独副本被复制到 TensorFlow 中,然后复制到稀缺的 GPU 内存中。

    最简单的解决方案是将tf.nn.embedding_lookup() 调用移到训练循环之外。例如:

    def while_loop(s,e,step):
      batch_id_placeholder = tf.placeholder(tf.int32)
      batch_xs1 = tf.nn.embedding_lookup(embedding_matrix, batch_id_placeholder)
    
      while s+batch_size<ran:
        batch_id=file_id[s:e]
        batch_col=label_matrix[s:e]                                             
    
        batch_label = csc_matrix((data, (batch_row, batch_col)), shape=(batch_size, n_classes))
        batch_label = batch_label.toarray()
    
        batch_xs=sess.run(batch_xs1, feed_dict={batch_id_placeholder: batch_id})
    

    【讨论】:

    • 您好,我在训练 NN 时也遇到了 OOM 问题,但我的是 GCN,没有嵌入功能。不过,我想我的问题也可能出在某些功能上,因为我可以进行一轮没有错误,而第二轮肯定会带来一些OOM错误。如果您能看看我的帖子 (stackoverflow.com/questions/67178061/…),将不胜感激!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-09-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    相关资源
    最近更新 更多