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

suk*_*uku 5 tensorflow

我正在学习关于递归神经网络的教程.

这是进口:

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
Run Code Online (Sandbox Code Playgroud)

这是输入处理的代码:

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)
Run Code Online (Sandbox Code Playgroud)

我收到以下错误outputs, states:

AttributeError: module 'tensorflow.python.ops.rnn' has no attribute 'rnn'
Run Code Online (Sandbox Code Playgroud)

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

suk*_*uku 26

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

from tensorflow.contrib import rnn 


lstm_cell = rnn.BasicLSTMCell(rnn_size) 
outputs, states = rnn.static_rnn(lstm_cell, x, dtype=tf.float32)
Run Code Online (Sandbox Code Playgroud)

代替

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)
Run Code Online (Sandbox Code Playgroud)