tensorflow - 张量对象没有属性 keras_shape

标签 tensorflow keras

使用以下方法构建模型时:

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

得到一个错误:

AttributeError: 'Tensor' object has no attribute '_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

预期的输出 - 像这样:

The model summary is: _________________________________________________________________ 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

最佳答案

如果你改变这个:

from keras.models import Model

对此:
from tensorflow.keras.models import Model

你的代码会很好。

* 或 *

改变这个:
from tensorflow.python.keras.layers import Input

对此:
from keras.layers import Input

关于tensorflow - 张量对象没有属性 keras_shape,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54614299/

相关文章:

python - Tensorflow Layers Api 线性激活函数

python-3.x - 尽管我的 CPU 支持 AVX,但 Tensorflow 2.4 仍无法工作

tensorflow - 如何使用 tf.summary.text?

python - key 错误 : "Can' t open attribute (Can't locate attribute: 'nb_layers' )"

python - Keras 在使用 TensorFlow-gpu 时出现错误

python - Pycharm 和 tensorflow GPU;未找到 DLL

python - 在卡住图上使用 optimize_for_inference.py 后使用模型时出错

python - Opencv DNN Net 1x1的大小

python - "tensorflow.math.multiply"和 "tensorflow.keras.layers.multiply"和有什么区别?

python - 拟合正弦函数的神经网络玩具模型失败,出了什么问题?