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

贴出问题:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
 in 
----> 1 autoencoder, encoder = autoencoder(dims, init=init)

 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这样就会出现一些问题,注意检查一下。

你可能感兴趣的:(Problems,tensorflow,anaconda,iops)