简体   繁体   English

AttributeError: 模块“tensorflow_core.compat.v1”没有属性“contrib”

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

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'

2.This is my code in Jupyter Notebook. 2.这是我在 Jupyter Notebook 中的代码。 I just started with python and get the error I mentioned in the headline.我刚开始使用 python 并得到我在标题中提到的错误。 I would be very thankful if someone could help me wizh a code example to solve the problem.如果有人可以帮助我提供代码示例来解决问题,我将不胜感激。

tf.contrib was removed from TensorFlow once with TensorFlow 2.0 alpha version. tf.contrib曾在 TensorFlow 2.0 alpha 版本中从 TensorFlow 中删除。

Most likely, you are already using TensorFlow 2.0.很可能,您已经在使用 TensorFlow 2.0。

You can find more details here: https://github.com/tensorflow/tensorflow/releases/tag/v2.0.0-alpha0您可以在此处找到更多详细信息: https : //github.com/tensorflow/tensorflow/releases/tag/v2.0.0-alpha0

For using specific versions of tensorflow, use要使用特定版本的 tensorflow,请使用

pip install tensorflow==1.14

or或者

pip install tensorflow-gpu==1.14

I think you need to add the following line in your python file which you are going to execute it.我认为您需要在要执行它的 python 文件中添加以下行。


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

contrib is a headache of Google Team. contrib 是 Google Team 头疼的问题。 We have to deal with the issue of contrib case by case.我们必须逐案处理贡献问题。 I just take two examples as follows.我只举两个例子如下。

1.With regard to CNN, it has the following method 1.关于CNN,它有如下方法

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)

2.With regard to RNN/LSTM, it has the following different method. 2.关于RNN/LSTM,有以下不同的方法。

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)

暂无
暂无

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

相关问题 LSTMCELL 的替代品,因为 tensorflow_core.compat.v1' 没有属性 'contrib' - Alternatives to LSTMCELL because tensorflow_core.compat.v1' has no attribute 'contrib' 模块“tensorflow.compat.v1”没有属性“contrib” - module 'tensorflow.compat.v1' has no attribute 'contrib' AttributeError:模块'tensorflow_core.compat.v2'没有属性'__internal__'(一周前工作?) - AttributeError: module 'tensorflow_core.compat.v2' has no attribute '__internal__' (Worked a week ago?) AttributeError:模块 'tensorflow.compat.v2' 没有属性 'logging' - AttributeError: module 'tensorflow.compat.v2' has no attribute 'logging' AttributeError:加载 tf.compat.v1.train.SessionRunHook 时模块“tensorflow”没有属性“compat” - AttributeError: module 'tensorflow' has no attribute 'compat' when loading tf.compat.v1.train.SessionRunHook Tensorflow 对象检测 AttributeError:模块“tensorflow._api.v1.compat”没有属性“v2” - Tensorflow Object Detection AttributeError: module 'tensorflow._api.v1.compat' has no attribute 'v2' AttributeError:模块“ tensorflow._api.v1.compat.v1”没有属性“ pywrap_tensorflow” - AttributeError: module 'tensorflow._api.v1.compat.v1' has no attribute 'pywrap_tensorflow' AttributeError: 模块“tensorflow._api.v1.compat”没有用于 Tensorflow 对象检测 API 的属性“v2” - AttributeError: module 'tensorflow._api.v1.compat' has no attribute 'v2' For Tensorflow Object Detection API AttributeError: 模块 &#39;tensorflow.compat&#39; 没有属性 &#39;v1&#39; Tensorflow v: 1.10.0 - AttributeError: module 'tensorflow.compat' has no attribute 'v1' Tensorflow v: 1.10.0 AttributeError:模块“tensorflow._api.v1.compat.v1.nn”没有属性“avg_pool2d” - AttributeError: module 'tensorflow._api.v1.compat.v1.nn' has no attribute 'avg_pool2d'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM