- RuntimeError:
loss
passed to Optimizer.compute_gradients should be a function when eager execution is enabled.
optimizer = tf.compat.v1.train.GradientDescentOptimizer(0.5)
train = optimizer.minimize(loss)
關(guān)于tf.compat.v1.train.GradientDescentOptimizer的介紹參考https://tensorflow.google.cn/api_docs/python/tf/compat/v1/train/GradientDescentOptimizer?hl=en
Methods中minimize
When eager execution is enabled, loss should be a Python function that takes no arguments and computes the value to be minimized.
當(dāng)eager execution開啟的時候漓糙,loss應(yīng)該是一個Python函數(shù)近忙。
關(guān)于eager execution的介紹给郊,參考https://tensorflow.google.cn/guide/eager?hl=en
In Tensorflow 2.0, eager execution is enabled by default.
在Tensorflow 2.0 中尖昏,eager execution 是默認(rèn)開啟的勒奇。
所以,需要先關(guān)閉eager execution
tf.compat.v1.disable_eager_execution()