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)