AttributeError: 'tensorflow.python.ops.rnn' does not have the attribute 'rnn'

I am following this tutorial on recurrent neural networks.

This is import:

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

This is the code for handling input:

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)

I get the following error for outputs, states:

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

TensorFlow has recently been updated, so what should be the new code for the offensive line

+5
source share
2 answers

For people using the newer version of tenorflow, add this to the code:

from tensorflow.contrib import rnn 


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

instead

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 suggested using tf.nn.rnn_cell.LSTMCellinsteadrnn_cell.BasicLSTMCell

+26
source

Thanks @suku

I get the following error: ImportError: No module named 'tensorflow.contrib.rnn.python.ops.core_rnn'

Solve:

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

replaced by:

from tensorflow.python.ops import rnn, rnn_cell

and in my code I used core_rnn.static_rnn:

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

I got this error:

NameError: name 'core_rnn' is not defined

This is solved by replacing the string with:

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

: 3,6 64 rensorflow: 1.10.0

0

Source: https://habr.com/ru/post/1670137/


All Articles