Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

type error 'tensor' object is not iterable when I using tf.contrib.rnn.LayerNormBasicLSTMCell

Tags:

tensorflow

My version of tensorflow is 1.0.0. When I run with tf.contrib.rnn.GRUCell(n_hidden_units) normal,but with tf.contrib.rnn.LayerNormBasicLSTMCell(n_hidden_units) running error:"type error 'tensor' object is not iterable"

`with tf.variable_scope('init_name',initializer=tf.orthogonal_initializer()):   

        cell = tf.contrib.rnn.LayerNormBasicLSTMCell(n_hidden_units)
        init_state = tf.get_variable('init_state', [1, n_hidden_units],initializer=tf.constant_initializer(0.0))  #tf.constant_initializer(0.0)
        init_state = tf.tile(init_state, [train_batch_size, 1])

        outputs, states = tf.nn.dynamic_rnn(
        cell,X,dtype=tf.float32,sequence_length=true_lenth,initial_state=init_state)`

And the error is :

/usr/anaconda3/lib/python3.5/site-packages/tensorflow/python/ops/rnn.py in <lambda>()
681 
682     input_t = nest.pack_sequence_as(structure=inputs, flat_sequence=input_t)--> 683     call_cell = lambda: cell(input_t, state)    684     685     if sequence_length is not None:/usr/anaconda3/lib/python3.5/site-packages/tensorflow/contrib/rnn/python/ops/rnn_cell.py in __call__(self, inputs, state, scope)1228 1229     with vs.variable_scope(scope or 
"layer_norm_basic_lstm_cell"):
-> 1230       c, h = state

1231 args = array_ops.concat([inputs, h], 1) 1232 concat = self._linear(args)

/usr/anaconda3/lib/python3.5/site-packages/tensorflow/python/framework/ops.py in iter(self)

514       TypeError: when invoked.
515     """
--> 516     raise TypeError("'Tensor' object is not iterable.")
517 
518   def __bool__(self):

TypeError: 'Tensor' object is not iterable.

Can someone help me? Thank you so much.

like image 968
mosquito Avatar asked Jun 13 '26 22:06

mosquito


1 Answers

The LayerNormBasicLSTMCell requires initial state to be a tuple of (num_units, num_units).

You can make your code work by doing

    cell = tf.contrib.rnn.LayerNormBasicLSTMCell(n_hidden_units)
    init_state = (tf.zeros([train_batch_size, n_hidden_units]), 
                  tf.zeros([train_batch_size, n_hidden_units]))

    outputs, states = tf.nn.dynamic_rnn(
        cell, X, dtype=tf.float32, 
        sequence_length=true_lenth,initial_state=init_state)
like image 88
ilblackdragon Avatar answered Jun 16 '26 03:06

ilblackdragon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!