AttributeError:'tensorflow.python.ops.rnn'没有属性'rnn'

时间:2017-02-18 04:20:31

标签: tensorflow

我在追踪神经网络上关注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)

我收到outputs, states的以下错误:

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

TensorFlow最近更新了,那么违规行的新代码应该是什么

3 个答案:

答案 0 :(得分:25)

对于使用较新版本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)

答案 1 :(得分:0)

感谢@suku

我收到以下错误:ImportError: No module named 'tensorflow.contrib.rnn.python.ops.core_rnn'

解决:

from tensorflow.contrib.rnn.python.ops import core_rnn

替换为:

from tensorflow.python.ops import rnn, rnn_cell

在我的代码中,我使用了core_rnn.static_rnn

 outputs,_ = core_rnn.static_rnn(cell, input_list, dtype=tf.float32)

我收到此错误:

NameError: name 'core_rnn' is not defined

这可以通过以下行代替:

outputs,_ = rnn.static_rnn(cell, input_list, dtype=tf.float32)

python:3.6 64位 rensorflow:1.10.0

答案 2 :(得分:0)

使用 static_rnn 方法代替 rnn。

WebView

代替:

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