Python 属性错误:模块';tensorflow_core.compat.v1';没有属性';contrib&x27; x=tf.placeholder(dtype=tf.float32,shape=[None,28,28]) y=tf.placeholder(dtype=tf.int32,shape=[None]) 图像\u展平=tf.contrib.layers.flatten(x) logits=tf.contrib.layers.完全连接(图像平坦,62,tf.nn.relu) 损失= 减少平均值(tf.nn.sparse\u softmax\u cross\u entropy\u与logits( 标签=y,logits=logits)) 列车运行= tf.train.AdamOptimizer(学习率=0.001)。最小化(损失) 正确的pred=tf.argmax(logits,1) 准确度=tf.减少平均值(tf.投射(正确的预测), tf.32) 打印(“图像展开:”,图像展开) 打印(“登录:”,登录) 打印(“损失:”,损失) 打印(“预测的\u标签:”,正确的\u pred) AttributeError回溯(最近一次呼叫上次) 在里面 1 x=tf.placeholder(dtype=tf.float32,shape=[None,28,28]) 2 y=tf.placeholder(dtype=tf.int32,shape=[None]) ---->3个图像\u展平=tf.contrib.layers.flatten(x) 4个logits=tf.contrib.layers.完全连接(图像平坦,62,tf.nn.relu) 5损失=tf.减少平均值(tf.nn.稀疏\u softmax\u交叉\u熵\u与\u logits(标签=y,logits=logits)) AttributeError:模块“tensorflow_core.compat.v1”没有属性“contrib”

Python 属性错误:模块';tensorflow_core.compat.v1';没有属性';contrib&x27; x=tf.placeholder(dtype=tf.float32,shape=[None,28,28]) y=tf.placeholder(dtype=tf.int32,shape=[None]) 图像\u展平=tf.contrib.layers.flatten(x) logits=tf.contrib.layers.完全连接(图像平坦,62,tf.nn.relu) 损失= 减少平均值(tf.nn.sparse\u softmax\u cross\u entropy\u与logits( 标签=y,logits=logits)) 列车运行= tf.train.AdamOptimizer(学习率=0.001)。最小化(损失) 正确的pred=tf.argmax(logits,1) 准确度=tf.减少平均值(tf.投射(正确的预测), tf.32) 打印(“图像展开:”,图像展开) 打印(“登录:”,登录) 打印(“损失:”,损失) 打印(“预测的\u标签:”,正确的\u pred) AttributeError回溯(最近一次呼叫上次) 在里面 1 x=tf.placeholder(dtype=tf.float32,shape=[None,28,28]) 2 y=tf.placeholder(dtype=tf.int32,shape=[None]) ---->3个图像\u展平=tf.contrib.layers.flatten(x) 4个logits=tf.contrib.layers.完全连接(图像平坦,62,tf.nn.relu) 5损失=tf.减少平均值(tf.nn.稀疏\u softmax\u交叉\u熵\u与\u logits(标签=y,logits=logits)) AttributeError:模块“tensorflow_core.compat.v1”没有属性“contrib”,python,tensorflow,jupyter,tensorflow2.0,Python,Tensorflow,Jupyter,Tensorflow2.0,2.这是我在Jupyter笔记本上的代码。 我刚开始使用python,得到了标题中提到的错误。如果有人能为我提供一个代码示例来解决这个问题,我将非常感谢。tf.contrib在TensorFlow 2.0 alpha版本中从TensorFlow中删除过一次 很可能您已经在使用TensorFlow 2.0 您可以在此处找到更多详细信息: 要使用tensorflow的特定版本,请使用 x = tf.placeholder(dtype = tf.float32, shape = [None, 28,

2.这是我在Jupyter笔记本上的代码。
我刚开始使用python,得到了标题中提到的错误。如果有人能为我提供一个代码示例来解决这个问题,我将非常感谢。

tf.contrib
在TensorFlow 2.0 alpha版本中从TensorFlow中删除过一次

很可能您已经在使用TensorFlow 2.0

您可以在此处找到更多详细信息:

要使用tensorflow的特定版本,请使用

x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
y = tf.placeholder(dtype = tf.int32, shape = [None])
images_flat = tf.contrib.layers.flatten(x)
logits = tf.contrib.layers.fully_connected(images_flat, 62, tf.nn.relu)
loss = 
tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits( 
   labels = y, logits = logits))
   train_op = 
   tf.train.AdamOptimizer(learning_rate=0.001).minimize(loss)
   correct_pred = tf.argmax(logits, 1)
   accuracy = tf.reduce_mean(tf.cast(correct_pred, 
   tf.float32))

   print("images_flat: ", images_flat)
   print("logits: ", logits)
   print("loss: ", loss)
   print("predicted_labels: ", correct_pred)


AttributeError                            Traceback (most recent call last)
<ipython-input-17-183722ce66a3> in <module>
      1 x = tf.placeholder(dtype = tf.float32, shape = [None, 28, 28])
      2 y = tf.placeholder(dtype = tf.int32, shape = [None])
----> 3 images_flat = tf.contrib.layers.flatten(x)
      4 logits = tf.contrib.layers.fully_connected(images_flat, 62, tf.nn.relu)
      5 loss = tf.reduce_mean(tf.nn.sparse_softmax_cross_entropy_with_logits(labels = y, logits = logits))

AttributeError: module 'tensorflow_core.compat.v1' has no attribute 'contrib'


我认为您需要在python文件中添加以下行,您将执行它



contrib是谷歌团队最头疼的问题。我们必须逐案处理分担责任的问题。我只举两个例子如下

1.关于CNN,它有以下方法

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
2.对于RNN/LSTM,它有以下不同的方法。

import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# -initializer = tf.contrib.layers.xavier_initializer(seed=1)
initializer = tf.truncated_normal_initializer(stddev=0.1)

有没有办法使用张量流的前一个版本?谢谢你迄今为止的帮助!我更新了答案以回答您的附加问题。@TimbusCalin无法通过pip或conda安装tensorflow 1.14。如何解决此问题?我尝试了此解决方案,但不起作用,因为TensorFlow 2.0中未包含TensorFlow contrib模块。只有降级到TensorFlow 1.0,它才有效。这不起作用。
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# -initializer = tf.contrib.layers.xavier_initializer(seed=1)
initializer = tf.truncated_normal_initializer(stddev=0.1)
import tensorflow.compat.v1 as tf
tf.disable_v2_behavior()

# -outputs, states = tf.contrib.rnn.static_rnn(lstm_cells, _X, dtype=tf.float32)
outputs, states = tf.compat.v1.nn.static_rnn(lstm_cells, _X, dtype=tf.float32)