TF 2.3中的错误.当混合急切和非急切的Keras模型时



我在尝试在tenserlflow 2.3中拟合模型时遇到了这个问题,是否有任何解决方法或解决方案?当我尝试使用TensorFlow神经网络模型预测一些记录时,也会发生此错误。我希望Tensorflow专家能找出问题所在!

代码:

import tensorflow as tf
import numpy as np
DO_BUG = True
inputs = tf.keras.Input((1,))
outputs = tf.keras.layers.Dense(10)(inputs)
model0 = tf.keras.Model(inputs=inputs, outputs=outputs)
if DO_BUG:
with tf.Graph().as_default():
inputs = tf.keras.Input((1,))
outputs = tf.keras.layers.Dense(10)(inputs)
model1 = tf.keras.Model(inputs=inputs, outputs=outputs)
model0.compile(optimizer=tf.optimizers.SGD(0.1), loss=tf.losses.mse)
model0.fit(np.zeros((4, 1)), np.zeros((4, 10)))

日志:

Traceback (most recent call last):
File ".../tmp.py", line 15, in <module>
model0.fit(np.zeros((4, 1)), np.zeros((4, 10)))
File "...tensorflowpythonkerasenginetraining_v1.py", line 807, in fit
use_multiprocessing=use_multiprocessing)
File "...tensorflowpythonkerasenginetraining_arrays.py", line 666, in fit
steps_name='steps_per_epoch')
File "...tensorflowpythonkerasenginetraining_arrays.py", line 189, in model_iteration
f = _make_execution_function(model, mode)
File "...tensorflowpythonkerasenginetraining_arrays.py", line 557, in _make_execution_function
return model._make_execution_function(mode)
File "...tensorflowpythonkerasenginetraining_v1.py", line 2072, in _make_execution_function
self._make_train_function()
File "...tensorflowpythonkerasenginetraining_v1.py", line 2021, in _make_train_function
**self._function_kwargs)
File "...tensorflowpythonkerasbackend.py", line 3933, in function
'eager execution. You passed: %s' % (updates,))
ValueError: `updates` argument is not supported during eager execution. You passed: [<tf.Operation 'training/SGD/SGD/AssignAddVariableOp' type=AssignAddVariableOp>]

下面的代码可以正常工作。使用下面注释部分的任何具体原因

import tensorflow as tf
import numpy as np
DO_BUG = True
inputs = tf.keras.Input((1,))
outputs = tf.keras.layers.Dense(10)(inputs)
model0 = tf.keras.Model(inputs=inputs, outputs=outputs)
"""
if DO_BUG:
with tf.Graph().as_default():
inputs = tf.keras.Input((1,))
outputs = tf.keras.layers.Dense(10)(inputs)
model1 = tf.keras.Model(inputs=inputs, outputs=outputs)
"""
model0.compile(optimizer=tf.optimizers.SGD(0.1), loss=tf.losses.mse)
model0.fit(np.zeros((4, 1)), np.zeros((4, 10)))

相关内容

  • 没有找到相关文章

最新更新