无法使用tf.contrib

我已经导入了tensorflow模块,但无法使用tf.contrib。我不知道问题出在哪里。我尝试在不同的版本中运行,但得到的输出始终相同。

已导入的模块:

import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior()
import tensorflow as tf2

代码:

tf2.contrib.rnn.LSTMCell(num_units=num_nodes[li],
                            state_is_tuple=True,
                            initializer= tf.contrib.layers.xavier_initializer()
                           )

输出:

AttributeError: module 'tensorflow' has no attribute 'contrib'

回答:

我认为问题出在版本上,我在1.15.2版本中尝试过,它对我来说是有效的。安装上述版本后,尝试以下代码,应该可以工作。

import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior()
import tensorflow as tf2 #Tensorflow 1.15.2
from tensorflow.contrib.rnn import LSTMCell
LSTMCell(num_units=num_nodes[li],
                            state_is_tuple=True,
                            initializer= tf.contrib.layers.xavier_initializer()
                           )

但是,如果您使用的是TensorFlow 2.x版本,contrib已被弃用,您可以使用以下代码。由于xavier_initializer也使用contrib,您可以使用GlorotUniform初始化器,它与xavier_initializer相同。请按照以下代码操作。

import tensorflow.compat.v1 as tf1
tf1.disable_v2_behavior()
import tensorflow as tf2 #Tensorflow 2.x
tf2.compat.v1.nn.rnn_cell.LSTMCell(num_units=10,
                            state_is_tuple=True,
                            initializer= tf2.initializers.GlorotUniform()
                           ) 

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注