首页 > 解决方案 > Keras vs tf.Keras,model.fit 给出了 tf.Keras 的运行时错误

问题描述

我对 TensorFlow 和 keras 很陌生,我搜索了很多,但找不到答案。我想训练一个神经网络。在导入语句中,当我使用 keras 时,一切正常。但是,当我将它们更改为 tensorflow.keras 时,会出现错误。由于某些原因,我必须升级我的代码以使用 tf.keras 而不是 keras

以下是我如何将 numpy 数组作为训练的输入:

model.fit(X2_upsampled_train, Y2_upsampled_train, batch_size=batch_size, epochs=nb_epoch,verbose=0, validation_data=(X2[test], Y2_test),callbacks=[monitor])

这是我在使用 tf.keras 时遇到的错误

RuntimeError                              Traceback (most recent call last)
<ipython-input-11-2ea1c5ab4362> in <module>()
145     monitor = EarlyStopping(monitor='val_loss', min_delta=1e-5, patience=5, verbose=1, mode='auto', restore_best_weights=True)
146 
--> 147     model.fit(X2_upsampled_train, Y2_upsampled_train, batch_size=batch_size, epochs=nb_epoch,verbose=0, validation_data=(X2[test], Y2_test),callbacks=[monitor])
149 

16 frames
/tensorflow-1.15.0/python3.6/tensorflow_core/python/ops/resource_variable_ops.py in __imul__(self, unused_other)
1227 
1228   def __imul__(self, unused_other):
-> 1229     raise RuntimeError("Variable *= value not supported. Use "
1230                        "`var.assign(var * value)` to modify the   variable or "
1231                        "`var = var * value` to get a new Tensor    object.")

RuntimeError: Variable *= value not supported. Use `var.assign(var * value)` to modify the variable or `var = var * value` to get a new Tensor object.

任何人都知道 tf.keras vs keras 发生了什么?如何更改我的代码以便我可以使用 tf.keras?

标签: tensorflowkeras

解决方案


确保您使用的是 tf.keras 而不是 keras。

在错误消息中,您似乎使用的是 tensorflow-1.15.0。使用 tensorflow 2+,它应该可以工作。


推荐阅读