AttributeError: module ‘tensorflow.python.framework.ops‘ has no attribute ‘_TensorLike‘解决方法

贴出问题:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-17-c998649dcbf1> in <module>
----> 1 autoencoder, encoder = autoencoder(dims, init=init)

<ipython-input-12-a5ada2e5f0c7> in autoencoder(dims, act, init)
     15     # internal layers in encoder
     16     for i in range(n_stacks-1):
---> 17         x = Dense(dims[i + 1], activation=act, kernel_initializer=init, name='encoder_%d' % i)(x)
     18 
     19     # hidden layer

~/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in symbolic_fn_wrapper(*args, **kwargs)
     73         if _SYMBOLIC_SCOPE.value:
     74             with get_graph().as_default():
---> 75                 return func(*args, **kwargs)
     76         else:
     77             return func(*args, **kwargs)

~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in __call__(self, inputs, **kwargs)
    444                 # Raise exceptions in case the input is not compatible
    445                 # with the input_spec specified in the layer constructor.
--> 446                 self.assert_input_compatibility(inputs)
    447 
    448                 # Collect input shapes to build layer.

~/anaconda3/lib/python3.7/site-packages/keras/engine/base_layer.py in assert_input_compatibility(self, inputs)
    308         for x in inputs:
    309             try:
--> 310                 K.is_keras_tensor(x)
    311             except ValueError:
    312                 raise ValueError('Layer ' + self.name + ' was called with '

~/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in is_keras_tensor(x)
    693     ```
    694     """
--> 695     if not is_tensor(x):
    696         raise ValueError('Unexpectedly found an instance of type `' +
    697                          str(type(x)) + '`. '

~/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py in is_tensor(x)
    701 
    702 def is_tensor(x):
--> 703     return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)
    704 
    705 

AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'

这里出现的问题属于keras和tensorflow函数的不匹配,如果你打开/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py,你的函数定义是

702 def is_tensor(x):
703     return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)

但是函数中tf_ops._TensorLikelib/python3.7/site-packages/tensorflow/python/framework/ops.py的函数

def is_dense_tensor_like(t):
  return isinstance(t, core_tf_types.Tensor)

core_tf_types.Tensor 不匹配

改正方法:

Method1: 打开/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py,将tf_ops._TensorLike改成core_tf_types.Tensor不过要调用函数from tensorflow.python.framework import tensor_util,不过函数调用方式也要以lib/python3.7/site-packages/tensorflow/python/framework/ops.py的调用方式为准。
在我的这台电脑上,具体函数改成了

from tensorflow.python.types import core as core_tf_types
def is_tensor(x):
    return isinstance(x, core_tf_types.Tensor) or tf_ops.is_dense_tensor_like(x)

Method2: 暴力一点,直接改掉这个函数,因为我们其实在另一个包里也有用到is_tensor(x):

from tensorflow.python.framework import tensor_util
def is_tensor(x):
    return tensor_util.is_tensor(x)

Method3: 如果都不是以上原因,很可能是因为你没有引用tensorflow.keras而是用的keras这样就会出现一些问题,注意检查一下。

  • 16
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 8
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值