onhuko 2023-04-13 17:09 采纳率: 58.3%
浏览 66
已结题

关于#tensorflow#的问题,如何解决?

tensorflow 报错内容如下 这是什么问题


class Encoder(tf.keras.Model) :
    def __init__(self, vocab_size, embedding_dim, enc_units, batch_sz) :
        super(Encoder, self).__init__()
        self.batch_sz = batch_sz

        self.enc_units = enc_units

        self.embedding = keras.layers.Embedding(vocab_size, embedding_dim)

        self.gru = keras.layers.GRU(self.enc_units, return_sequences=True, return_state=True, recurrent_initializer="glorot_uniform")

    @tf.function
    def call(self, x, hidden) :
        x = self.embedding(x)
        output, state = self.gru(x, initial_state=hidden)
        return output, state
    def initialize_hidden_state(self) :
        return tf.zeros((self.batch_sz, self.enc_units))


class BahdanauAttentionMechanism(tf.keras.layers.Layer) :

    def __init__(self, units) :
        super(BahdanauAttentionMechanism, self).__init__()

        self.W1 = layers.Dense(units)

        self.W2 = layers.Dense(units)

        self.V = layers.Dense(1)
    @tf.function
    def call(self, query, values) :
        hidden_with_time_axis = tf.expand_dims(query, 1)

        score = self.V(tf.nn.tanh(self.W1(values) + self.W2(hidden_with_time_axis)))

        attention_weights = tf.nn.softmax(score, axis=1)

        context_vector = attention_weights * values
        context_vector = tf.math.reduce_sum(context_vector, axis=1)

        return context_vector, attention_weights



class Decoder(tf.keras.Model) :
    def __init__(self, vocab_size, embedding_dim, dec_units, batch_sz) :
        super(Decoder, self).__init__()
        
        self.batch_sz = batch_sz

        self.dec_units = dec_units

        self.embedding = layers.Embedding(vocab_size, embedding_dim) 


        self.gru = layers.GRU(self.dec_units, return_sequences=True, return_state=True, recurrent_initializer="glorot_uniform")

        self.fc = layers.Dense(vocab_size)

        self.attention = BahdanauAttentionMechanism(self.dec_units)
    @tf.function
    def call(self, x, hidden, enc_output) :
        context_vector, attention_weights = self.attention(hidden, enc_output)
    
        x = self.embedding(x)
        x = tf.concat([tf.expand_dims(context_vector, 1), x], axis=-1)
        output, state = self.gru(x)

        output = tf.reshape(output, (-1, output.shape[2]))

        x = self.fc(output)
        return x, state, attention_weights
Current allocation summary follows.
Current allocation summary follows.
2023-04-13 17:02:03.448253: W tensorflow/core/framework/op_kernel.cc:1780] OP_REQUIRES failed at matmul_op_impl.h:728 : RESOURCE_EXHAUSTED: OOM when allocating tensor with shape[64,1024] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
2023-04-13 17:02:03.449923: W tensorflow/core/common_runtime/bfc_allocator.cc:491] ***************************************************************************************************x
2023-04-13 17:02:03.450152: W tensorflow/core/framework/op_kernel.cc:1780] OP_REQUIRES failed at resource_variable_ops.cc:725 : RESOURCE_EXHAUSTED: OOM when allocating tensor with shape[64,1,256] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
Traceback (most recent call last):
  File "C:\Users\DELL\Desktop\h\p1.py", line 326, in <module>
    train_model(q_hidden, encoder, decoder, q_index, BATCH_SIZE, dataset, steps_per_epoch, optimizer, checkpoint, checkpoint_prefix, summary_writer)
  File "C:\Users\DELL\Desktop\h\p1.py", line 208, in train_model
    batch_loss = optimizer_loss(q, a, q_hidden, encoder, decoder, q_index, BATCH_SIZE, optimizer)
  File "C:\Users\DELL\Desktop\h\p1.py", line 189, in optimizer_loss
    batch_loss, grads = grad_loss(q, a, q_hidden, encoder, decoder, q_index, BATCH_SIZE)
  File "C:\Users\DELL\Desktop\h\p1.py", line 175, in grad_loss
    predictions, a_hidden, _ = decoder(a_input, a_hidden, q_output)
  File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 70, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\tensorflow\python\eager\execute.py", line 54, in quick_execute
    tensors = pywrap_tfe.TFE_Py_Execute(ctx._handle, device_name, op_name,
tensorflow.python.framework.errors_impl.ResourceExhaustedError: Exception encountered when calling layer "decoder" "                 f"(type Decoder).

Graph execution error:

Detected at node 'dense_5/Tensordot/MatMul' defined at (most recent call last):
    File "C:\Users\DELL\Desktop\h\p1.py", line 319, in <module>
      a_output, _, _ = decoder(tf.random.uniform((64, 1)), q_hidden, q_output)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\engine\training.py", line 557, in __call__
      return super().__call__(*args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\engine\base_layer.py", line 1097, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\Desktop\h\p1.py", line 138, in call
      context_vector, attention_weights = self.attention(hidden, enc_output)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\engine\base_layer.py", line 1097, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\Desktop\h\p1.py", line 109, in call
      score = self.V(tf.nn.tanh(self.W1(values) + self.W2(hidden_with_time_axis)))
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 65, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\engine\base_layer.py", line 1097, in __call__
      outputs = call_fn(inputs, *args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\utils\traceback_utils.py", line 96, in error_handler
      return fn(*args, **kwargs)
    File "C:\Users\DELL\AppData\Roaming\Python\Python310\site-packages\keras\layers\core\dense.py", line 244, in call
      outputs = tf.tensordot(inputs, self.kernel, [[rank - 1], [0]])
Node: 'dense_5/Tensordot/MatMul'
OOM when allocating tensor with shape[64,1024] and type float on /job:localhost/replica:0/task:0/device:GPU:0 by allocator GPU_0_bfc
         [[{{node dense_5/Tensordot/MatMul}}]]
Hint: If you want to see a list of allocated tensors when OOM happens, add report_tensor_allocations_upon_oom to RunOptions for current allocation info. This isn't available when running in Eager mode.
 [Op:__forward_call_3520]

Call arguments received by layer "decoder" "                 f"(type Decoder):
  • x=tf.Tensor(shape=(64, 1), dtype=int32)
  • hidden=tf.Tensor(shape=(64, 1024), dtype=float32)
  • enc_output=tf.Tensor(shape=(64, 74, 1024), dtype=float32)

  • 写回答

6条回答 默认 最新

  • teellyy 2023-04-13 20:18
    关注

    这个错误提示是说在计算过程中,尝试在GPU上分配一个shape为[64,1024]的float型tensor时内存不足,导致程序崩溃。同样的原因也导致了另一个位置出现了类似的错误提示。

    解决这个问题的思路有以下几种:

    1.减小batch_size
    减小batch_size可以减少每次计算需要的内存,从而避免内存不足的情况。不过这样可能会降低训练效率。

    2.减小模型参数量
    减小模型参数量可以减少每次计算需要的内存,可以通过使用更小的embedding_dim或者减小模型层数等方式实现。但是这样会牺牲模型的表达能力。

    3.将模型移动到CPU上计算
    如果GPU内存真的非常有限,可以考虑将模型移动到CPU上计算。不过这样会大大降低训练速度,需谨慎考虑。

    4.增加GPU显存
    如果以上方法都不能解决问题,可以考虑增加GPU显存。这可以通过更换显卡或者在代码中调用tf.config.experimental.set_memory_growth方法来实现。

    下面是增加显存的示例代码:

    import tensorflow as tf
    gpu_options = tf.compat.v1.GPUOptions(per_process_gpu_memory_fraction=0.7)
    sess = tf.compat.v1.Session(config=tf.compat.v1.ConfigProto(gpu_options=gpu_options))
    tf.compat.v1.keras.backend.set_session(sess)
    
    
    

    将这段代码加入到你的程序中,并尝试减小per_process_gpu_memory_fraction的值,看是否能解决问题。

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
  • threenewbee 2023-04-13 17:25
    关注

    应该是爆显存了,你检查下。

    评论
  • Huouayi 2023-04-13 17:58
    关注

    OOM(out of memory)错误,分配GPU内存问题,导致TensorFlow无法继续执行,最终导致内存不足
    办法:
    1、减小batch size的值,减少内存压力
    2、减小模型大小(隐藏单元数)
    3、禁用GPU并使用CPU运行代码,这会降低速度,但内存压力会减小
    4、增加内存
    5、重新启动Python内核和TensorFlow会话,看人品会内存泄漏导致内存不足

    评论
  • 封尘绝念丶 2023-04-13 21:29
    关注

    你的计算机内存不支持一次训练那么多图片,把batchsize调小一点吧,另外训练时你可以使用任务管理器看你电脑的内存占用,找一个合适你电脑的batchsize。

    评论
  • Zyb0627 2023-04-13 19:30
    关注

    引用chatGPT作答,
    这个错误提示表明,当您的 TensorFlow 代码尝试分配张量时,GPU 内存不足。具体地,错误信息显示 TensorFlow 尝试在 GPU:0 上分配大小为 [64,1024]、类型为 float 的张量,但没有足够的内存可用。同样,它还尝试在 GPU:0 上分配大小为 [64,1,256]、类型为 float 的张量,但是内存不足。

    为了解决这个错误,您可以尝试以下一项或多项措施:

    1.减小模型的大小:您可以通过使用更小的词汇表大小、嵌入维度或隐藏层大小来减少模型参数的数量。

    2.减小批量大小:您可以尝试减小批量大小,以减少每个批次的内存使用量。

    3.使用更大的 GPU:如果您可以使用具有更多内存的更大的 GPU,您可以尝试在该 GPU 上运行代码。

    4.使用混合精度:您可以尝试使用混合精度训练,这允许您使用低精度的浮点数来减少模型的内存使用量。

    5.使用梯度检查点:您可以尝试使用梯度检查点,通过在反向传播期间重新计算中间激活值,以换取计算时间来减少内存使用量。当您的大型模型无法放入内存时,这种技术可能非常有用。

    6.减小序列长度:您可以尝试减小输入或输出的序列长度,以减少模型的内存使用量。

    评论
  • 美羊羊桑7890 2023-04-14 00:02
    关注

    内容来源与ChatGpt4及newbing和百度:


    这个问题看起来是代码中断了,可以尝试检查代码中是否有拼写错误或者缺失的符号。具体来说,检查Encoder类的构造函数是否完整,包括embeddi后面是否缺失了ng_dim这个参数。另外,也可以检查一下是否正确导入了tensorflow和keras库。


    祝您问题迎刃而解

    评论
查看更多回答(5条)

报告相同问题?

问题事件

  • 系统已结题 4月22日
  • 已采纳回答 4月14日
  • 创建了问题 4月13日

悬赏问题

  • ¥15 Qt 不小心删除了自带的类,该怎么办
  • ¥15 我需要在PC端 开两个抖店工作台客户端.(语言-java)
  • ¥15 有没有哪位厉害的人可以用C#可视化呀
  • ¥15 可以帮我看看代码哪里错了吗
  • ¥15 设计一个成绩管理系统
  • ¥15 PCL注册的选点等函数如何取消注册
  • ¥15 问一下各位,为什么我用蓝牙直接发送模拟输入的数据,接收端显示乱码呢,米思齐软件上usb串口显示正常的字符串呢?
  • ¥15 Python爬虫程序
  • ¥15 crypto 这种的应该怎么找flag?
  • ¥15 代码已写好,求帮我指出错误,有偿!