gpt4 book ai didi

tensorflow - 属性错误: 'tensorflow.python.ops.rnn' has no attribute 'rnn'

转载 作者:行者123 更新时间:2023-12-03 00:13:18 27 4
gpt4 key购买 nike

我正在关注this tutorial关于循环神经网络。

这是导入:

import tensorflow as tf
from tensorflow.examples.tutorials.mnist import input_data
from tensorflow.python.ops import rnn
from tensorflow.contrib.rnn import core_rnn_cell

这是输入处理的代码:

x = tf.transpose(x, [1,0,2])
x = tf.reshape(x, [-1, chunk_size])
x = tf.split(x, n_chunks, 0)

lstm_cell = core_rnn_cell.BasicLSTMCell(rnn_size)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

我在输出、状态中收到以下错误:

AttributeError: module 'tensorflow.python.ops.rnn' has no attribute 'rnn'

TensorFlow 最近更新了,那么有问题的行的新代码应该是什么

最佳答案

对于使用较新版本的tensorflow的人,请将以下内容添加到代码中:

from tensorflow.contrib import rnn 


lstm_cell = rnn.BasicLSTMCell(rnn_size)
outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)

而不是

from tensorflow.python.ops import rnn, rnn_cell 
lstm_cell = rnn_cell.BasicLSTMCell(rnn_size,state_is_tuple=True)
outputs, states = rnn.rnn(lstm_cell, x, dtype=tf.float32)

PS:@BrendanA 建议使用 tf.nn.rnn_cell.LSTMCell 代替 rnn_cell.BasicLSTMCell

关于tensorflow - 属性错误: 'tensorflow.python.ops.rnn' has no attribute 'rnn' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42311007/

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