tensorflow - 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?
解决方案
确保您使用的是 tf.keras 而不是 keras。
在错误消息中,您似乎使用的是 tensorflow-1.15.0。使用 tensorflow 2+,它应该可以工作。
推荐阅读
- python - pyspark 列值是一个列表
- javascript - expo-video-player 在这个简单的库中使用 videoRef
- graphql - 带有打字稿的graphql - @FieldResolver 在compiled-to-js type-graphql 中不起作用
- spring - Kotlin 和 Spring MVC - HTTP 状态 400 - Null
- compilation - Fortran 错误:两个不相关的子例程调用之间的类型不匹配
- python - 只能将 size-1 数组转换为 Python 标量错误消息
- python - 从 txt 输入到列表的数据 - Python
- flutter - 颤动的 TabBarView 有时不会在手势滑动时触发
- javascript - C# 将属性添加到对象列表
- android - kotlin.Collections 似乎没有被导入。“listOf”标记为红色,而在其他项目中工作正常