tensorflow2.0 解决ModuleNotFoundError: No module named ‘tensorflow.contrib.layers import flatten‘

tensorflow2.0 解决ModuleNotFoundError: No module named ‘tensorflow.contrib’

代码

    # flatten
    fc1 = flatten(pool5)

报错信息

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
 in ()
      5     ###
      6 # from tensorflow.contrib.layers import xavier_initializer_conv2d
----> 7 from tensorflow.contrib.layers import flatten
      8 

ModuleNotFoundError: No module named 'tensorflow.contrib'


解决方法1


def flatten(incoming, name=None):
    flat_shape = [-1, np.prod(incoming.shape[1:]).value]
    return tf.reshape(incoming, flat_shape)

使用自定义函数

解决方法2

fc1 = tf.layers.flatten(pool5)

fc1 = flatten(pool5) 替换为 fc1 = tf.layers.flatten(pool5)

你可能感兴趣的:(日常报错,tensorflow,深度学习,人工智能,bug)