张量对象没有keras_shape属性。

12

使用以下方法构建模型:

model = Model(inputs=[input_text], outputs=pred)

出现错误:

AttributeError: 'Tensor'对象没有'_keras_shape'属性

我尝试的完整笔记本可以在这里找到... https://github.com/shantanuo/pandas_examples/blob/master/tensorflow/tf_learn.ipynb

同样的代码的纯文本版本可以在这里找到... https://gist.github.com/shantanuo/4e35baba144ee658e4dd4d1f87e19f3a

即使我使用了与此博客中显示的完全相同的代码,我仍想知道为什么会出现错误:

https://towardsdatascience.com/transfer-learning-using-elmo-embedding-c4a7e415103c

期望的输出-类似这样:

模型摘要如下: _________________________________________________________________ Layer (type) Output Shape Param #
================================================================= input_2 (InputLayer) (None, 1) 0
_________________________________________________________________ lambda_2 (Lambda) (None, 1024) 0
_________________________________________________________________ dense_3 (Dense) (None, 256) 262400
_________________________________________________________________ dense_4 (Dense) (None, 1) 257

我尝试升级tensorflow和keras,但仍然出现相同的错误:

!pip install --upgrade tensorflow

那里有一个超酷的错误。至少Keras模型部分看起来是完美的代码。当您在层外执行张量操作时,就会出现此错误,但我无法检测到任何操作,因为您的所有自定义操作都位于Lambda层内部。所以,我猜测您正在使用有缺陷/旧版的"Keras"(这与tensorflow无关)。 - Daniel Möller
1
你可以尝试重置你的内核。有时候,变量名相似、拼写错误或者奇怪的变量会以错误的状态锁定在内存中...这种情况很少见,但确实会发生。 - Daniel Möller
也许这会有所帮助 https://stackoverflow.com/a/54620221/10418812 - Manualmsdos
2个回答

18

当我更换了不同版本的tensorflow后,我也遇到了类似问题。

解决方法是使用.shape而不是._keras_shape

根据tensorflow/keras的版本和/或导入方式 (from keras, from tensorflow.kerasfrom tensorflow.python.keras),属性名称可能会有所不同。


9
如果你改变了这个:
from keras.models import Model

转换为:

from tensorflow.keras.models import Model

你的代码会没问题。
* 或 *
改为:
from tensorflow.python.keras.layers import Input

转换为:

from keras.layers import Input

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接