gpt4 book ai didi

python-3.x - 如何修复 : AttributeError: module 'tensorflow' has no attribute 'contrib'

转载 作者:行者123 更新时间:2023-12-02 16:50:25 25 4
gpt4 key购买 nike

我正在训练 LSTM 并定义参数和回归层。我用这段代码在标题中得到错误:

 lstm_cells = [
tf.contrib.rnn.LSTMCell(num_units=num_nodes[li],
state_is_tuple=True,
initializer= tf.contrib.layers.xavier_initializer()
)
for li in range(n_layers)]

drop_lstm_cells = [tf.contrib.rnn.DropoutWrapper(
lstm, input_keep_prob=1.0,output_keep_prob=1.0-dropout, state_keep_prob=1.0-dropout
) for lstm in lstm_cells]
drop_multi_cell = tf.contrib.rnn.MultiRNNCell(drop_lstm_cells)
multi_cell = tf.contrib.rnn.MultiRNNCell(lstm_cells)

w = tf.get_variable('w',shape=[num_nodes[-1], 1], initializer=tf.contrib.layers.xavier_initializer())
b = tf.get_variable('b',initializer=tf.random_uniform([1],-0.1,0.1))

我正在使用 tensorflow2,我已经阅读了 https://www.tensorflow.org/guide/migrate指南,我认为网上几乎所有内容。但我无法解决它。我该怎么做?

最佳答案

出现此错误是因为 contrib 模块已从 tensorflow 版本 2 中删除。这个问题有两种解决方案:

  1. 您可以删除当前包并安装其中一个系列 1 版本。

  2. 您可以使用此命令,它也与版本二的软件包兼容:使用 tf.compat.v1.nn.rnn_cell.LSTMCell 而不是 tf.contrib。 rnn.LSTMCell 并在其他包含 rnn 的命令中使用 tf.initializers.GlorotUniform () 而不是 tf.contrib.layers.xavier_initializer () tf.compat.v1.nn.rnn_cell

关于python-3.x - 如何修复 : AttributeError: module 'tensorflow' has no attribute 'contrib' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58976313/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com