无法使用 tf.contrib
unable to use 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 初始化器,它与 [=23= 相同]。
按照下面的代码。
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()
)
我导入了 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 初始化器,它与 [=23= 相同]。 按照下面的代码。
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()
)