【问题标题】:Keras vs tf.Keras, model.fit gives Runtime Error With tf.KerasKeras vs tf.Keras,model.fit 给出了 tf.Keras 的运行时错误
【发布时间】:2020-03-25 19:34:04
【问题描述】:

我对 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 与 keras 发生了什么吗?如何更改我的代码以便我可以使用 tf.keras?

【问题讨论】:

  • 您是否将 keras 与 tf.keras 代码混合在一起?您能否添加扩展您的代码 sn-ps 并包括导入语句?
  • 在导入语句中,当我使用 keras 时,一切正常。但是,当我将它们更改为 tensorflow.keras 时,我会收到此错误。由于某些原因,我不得不使用 tf.keras。那么,如何将我的代码从 keras 更改为 tf.keras?
  • 你需要包含完整的回溯,不完整的没有多大意义。

标签: tensorflow keras


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 2020-06-12
    • 1970-01-01
    • 2020-09-14
    • 2021-06-02
    • 1970-01-01
    • 1970-01-01
    • 2020-02-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多