简体   繁体   English

tensorflow 中的 add 方法问题:AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

[英]Issue with add method in tensorflow : AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

import keras as K
from keras.models import Sequential
from keras.layers import Dense
from tensorflow import set_random_seed

for hidden_neuron in hidden_neurons:
  model = Sequential()

model.add(Dense(hidden_neuron, input_dim=61, activation='relu'))

-> i am getting error at this line. -> 我在这一行遇到错误。 I am not really sure what am i missing here.我不确定我在这里错过了什么。

Traceback (most recent call last):回溯(最近一次调用最后一次):

File "PycharmProjects/HW2/venv/bin/hw3q4.py", line 46, in model.add(Dense(hidden_neuron, input_dim=61, activation='relu')) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/sequential.py", line 165, in add layer(x) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 414, in call self.assert_input_compatibility(inputs) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/engine/base_layer.py", line 279, in assert_input_compatibility K.is_keras_tensor(x) File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 472, in is_keras_tensor if not is_tensor(x): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 480, in is_tensor return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x) A文件“PycharmProjects/HW2/venv/bin/hw3q4.py”,第 46 行,在 model.add(Dense(hidden_​​neuron, input_dim=61, activation='relu')) 文件“/Library/Frameworks/Python.framework/Versions”中/3.7/lib/python3.7/site-packages/keras/engine/sequential.py”,第 165 行,在添加层(x)文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3. 7/site-packages/keras/engine/base_layer.py”,第 414 行,调用self.assert_input_compatibility(inputs) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages /keras/engine/base_layer.py”,第 279 行,在 assert_input_compatibility K.is_keras_tensor(x) 文件“/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/ tensorflow_backend.py", line 472, in is_keras_tensor 如果不是 is_tensor(x): File "/Library/Frameworks/Python.framework/Versions/3.7/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py" ,第 480 行,在 is_tensor 中返回 isinstance(x, tf_ops._TensorLike) 或 tf_ops.is_dense_tensor_like(x) A ttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike' ttributeError:模块“tensorflow.python.framework.ops”没有属性“_TensorLike”

For me, the fix was importing对我来说,修复是导入

from tensorflow.keras import Sequential
from tensorflow.keras.layers import Conv2D, Flatten, Dense

instead of代替

from keras import Sequential
from keras.layers import Conv2D, Flatten, Dense

There seems to be some weird compatibility issues between keras and tensorflow.keras keras 和 tensorflow.keras 之间似乎存在一些奇怪的兼容性问题

It is due to version incompatibility.是因为版本不兼容。
Update keras to the latest version compatible with tensorflow :keras更新到与tensorflow兼容的最新版本:

pip install --upgrade keras==x.x.x

You can use the following import command:您可以使用以下导入命令:

from tensorflow.keras.layers import ... 

instead of the ' old ' one:而不是“”的:

from keras.layers import ....

As described here.如此处所述

对于那些偶然发现此问题的人,重新安装 Keras 和 Tensorflow 可以解决该问题。

!pip uninstall tensorflow 
!pip install tensorflow==1.14

!pip uninstall keras 
!pip install keras==2.2.4

Installing the above versions of keras and tensorflow have solved the problem for me.安装以上版本的keras和tensorflow已经为我解决了这个问题。

简单地同时更新 TensorFlow 和 Keras 可能会解决这个问题

It depends on how you are importing the preliminaries.这取决于您如何导入预赛。 If importing tensorflow as tf and importing keras within the tensorflow, you should start with tf.keras.如果将 tensorflow 作为 tf 导入并在 tensorflow 中导入 keras,则应从 tf.keras 开始。 otherwise, if you are importing directly keras.models then you can just start off with Input() or Conv().否则,如果您直接导入 keras.models,那么您可以从 Input() 或 Conv() 开始。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 AttributeError:模块'tensorflow.python.framework.ops'没有属性'_TensorLike' - AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike' 我无法理解这个 AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike' - I cannot understand this AttributeError : module 'tensorflow.python.framework.ops' has no attribute '_TensorLike' AttributeError: 模块“tensorflow.python.framework.ops”没有属性“RegisterShape” - AttributeError: module 'tensorflow.python.framework.ops' has no attribute 'RegisterShape' AttributeError:模块'tensorflow.python.framework.op_def_registry'没有属性'get_registered_ops' - AttributeError: module 'tensorflow.python.framework.op_def_registry' has no attribute 'get_registered_ops' Tensorflow模块导入错误:AttributeError:模块'tensorflow.python.ops.nn'没有属性'rnn_cell' - Tensorflow Module Import error: AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'rnn_cell' Tensorflow:AttributeError:模块'tensorflow.python'没有属性'control_flow_ops' - Tensorflow: AttributeError: module 'tensorflow.python' has no attribute 'control_flow_ops' Tensorflow:AttributeError:模块“tensorflow.python.ops.nn”没有属性“softmax_cross_entropy_with_logits_v2” - Tensorflow: AttributeError: module 'tensorflow.python.ops.nn' has no attribute 'softmax_cross_entropy_with_logits_v2' AttributeError: 'tensorflow.python.framework.ops.EagerTensor' 对象没有属性 'to_tensor' - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'to_tensor' AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object 没有属性 'ravel' - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'ravel' AttributeError: 'tensorflow.python.framework.ops.EagerTensor' 对象没有属性 'decode' - AttributeError: 'tensorflow.python.framework.ops.EagerTensor' object has no attribute 'decode'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM