0
class Model:
    def __init__(
        self,
        learning_rate,
        num_layers,
        size,
        size_layer,
        output_size,
        forget_bias = 0.1,
    ):
        def lstm_cell(size_layer):
            return tf.compat.v1.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = False)

        rnn_cells = tf.compat.v1.nn.rnn_cell.MultiRNNCell(
            [lstm_cell(size_layer) for _ in range(num_layers)],
            state_is_tuple = False,
        )
        self.X = tf.compat.v1.placeholder(tf.float32, (None, None, size))
        self.Y = tf.compat.v1.placeholder(tf.float32, (None, output_size))
        drop = tf.compat.v1.nn.rnn_cell.DropoutWrapper(
            rnn_cells, output_keep_prob = forget_bias
        )
        self.hidden_layer = tf.compat.v1.placeholder(
            tf.float32, (None, num_layers * 2 * size_layer)
        )
        self.outputs, self.last_state = tf.compat.v1.nn.dynamic_rnn(
            drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32
        )
        self.logits = tf.compat.v1.layers.dense(self.outputs[-1], output_size)
        self.cost = tf.reduce_mean(tf.square(self.Y - self.logits))
        self.optimizer = tf.compat.v1.train.AdamOptimizer(learning_rate).minimize(
            self.cost
        )

i have this code above and i kept receive this warning :

WARNING:tensorflow:<tensorflow.python.ops.rnn_cell_impl.LSTMCell object at 0x0000021BF7EBFEF0>: Using a concatenated state is slower and will soon be deprecated.  Use state_is_tuple=True.

when i change state_is_tuple=True it shows error like this :

<ipython-input-922-91f013941f83> in __init__(self, learning_rate, num_layers, size, size_layer, output_size, forget_bias)
     25         )
     26         self.outputs, self.last_state = tf.compat.v1.nn.dynamic_rnn(
---> 27             drop, self.X, initial_state = self.hidden_layer, dtype = tf.float32
     28         )
     29         self.logits = tf.compat.v1.layers.dense(self.outputs[-1], output_size)

~\Anaconda3\lib\site-packages\tensorflow_core\python\util\deprecation.py in new_func(*args, **kwargs)
    322               'in a future version' if date is None else ('after %s' % date),
    323               instructions)
--> 324       return func(*args, **kwargs)
    325     return tf_decorator.make_decorator(
    326         func, new_func, 'deprecated',

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn.py in dynamic_rnn(cell, inputs, sequence_length, initial_state, dtype, parallel_iterations, swap_memory, time_major, scope)
    705         swap_memory=swap_memory,
    706         sequence_length=sequence_length,
--> 707         dtype=dtype)
    708 
    709     # Outputs of _dynamic_rnn_loop are always shaped [time, batch, depth].

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn.py in _dynamic_rnn_loop(cell, inputs, initial_state, parallel_iterations, swap_memory, sequence_length, dtype)
    914       parallel_iterations=parallel_iterations,
    915       maximum_iterations=time_steps,
--> 916       swap_memory=swap_memory)
    917 
    918   # Unpack final output if not using output tuples.

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\control_flow_ops.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, back_prop, swap_memory, name, maximum_iterations, return_same_structure)
   2673         name=name,
   2674         return_same_structure=return_same_structure,
-> 2675         back_prop=back_prop)
   2676 
   2677   with ops.name_scope(name, "while", loop_vars):

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\while_v2.py in while_loop(cond, body, loop_vars, shape_invariants, parallel_iterations, maximum_iterations, name, return_same_structure, back_prop)
    192         func_graph=util.WhileBodyFuncGraph(
    193             body_name, collections=ops.get_default_graph()._collections),  # pylint: disable=protected-access
--> 194         add_control_dependencies=add_control_dependencies)
    195     # Add external captures of body to the list of loop vars.
    196     # Note that external tensors will be treated as loop invariants, i.e.,

~\Anaconda3\lib\site-packages\tensorflow_core\python\framework\func_graph.py in func_graph_from_py_func(name, python_func, args, kwargs, signature, func_graph, autograph, autograph_options, add_control_dependencies, arg_names, op_return_value, collections, capture_by_value, override_flat_arg_shapes)
    976                                           converted_func)
    977 
--> 978       func_outputs = python_func(*func_args, **func_kwargs)
    979 
    980       # invariant: `func_outputs` contains only Tensors, CompositeTensors,

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\while_v2.py in wrapped_body(loop_counter, maximum_iterations_arg, *args)
    170       # `orig_loop_vars` and `args`, converts flows in `args` to TensorArrays
    171       # and packs it into the structure of `orig_loop_vars`.
--> 172       outputs = body(*_pack_sequence_as(orig_loop_vars, args))
    173       if not nest.is_sequence_or_composite(outputs):
    174         outputs = [outputs]

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn.py in _time_step(time, output_ta_t, state)
    882           skip_conditionals=True)
    883     else:
--> 884       (output, new_state) = call_cell()
    885 
    886     # Keras cells always wrap state as list, even if it's a single tensor.

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn.py in <lambda>()
    868     if is_keras_rnn_cell and not nest.is_sequence(state):
    869       state = [state]
--> 870     call_cell = lambda: cell(input_t, state)
    871 
    872     if sequence_length is not None:

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn_cell_impl.py in __call__(self, inputs, state, scope)
   1138     """
   1139     return self._call_wrapped_cell(
-> 1140         inputs, state, cell_call_fn=self.cell.__call__, scope=scope)
   1141 
   1142   def get_config(self):

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn_cell_wrapper_impl.py in _call_wrapped_cell(self, inputs, state, cell_call_fn, **kwargs)
    275       inputs = self._dropout(inputs, "input", self._recurrent_input_noise,
    276                              self._input_keep_prob)
--> 277     output, new_state = cell_call_fn(inputs, state, **kwargs)
    278     if _should_dropout(self._state_keep_prob):
    279       # Identify which subsets of the state to perform dropout on and

~\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn_cell_impl.py in __call__(self, inputs, state, scope)
    242         setattr(self, scope_attrname, scope)
    243       with scope:
--> 244         return super(RNNCell, self).__call__(inputs, state)
    245 
    246   def _rnn_get_variable(self, getter, *args, **kwargs):

~\Anaconda3\lib\site-packages\tensorflow_core\python\layers\base.py in __call__(self, inputs, *args, **kwargs)
    545 
    546       # Actually call layer
--> 547       outputs = super(Layer, self).__call__(inputs, *args, **kwargs)
    548 
    549     if not context.executing_eagerly():

~\Anaconda3\lib\site-packages\tensorflow_core\python\keras\engine\base_layer.py in __call__(self, inputs, *args, **kwargs)
    776                     outputs = base_layer_utils.mark_as_return(outputs, acd)
    777                 else:
--> 778                   outputs = call_fn(cast_inputs, *args, **kwargs)
    779 
    780             except errors.OperatorNotAllowedInGraphError as e:

~\Anaconda3\lib\site-packages\tensorflow_core\python\autograph\impl\api.py in wrapper(*args, **kwargs)
    235       except Exception as e:  # pylint:disable=broad-except
    236         if hasattr(e, 'ag_error_metadata'):
--> 237           raise e.ag_error_metadata.to_exception(e)
    238         else:
    239           raise

ValueError: in converted code:

    C:\Users\ThinkPad\Anaconda3\lib\site-packages\tensorflow_core\python\ops\rnn_cell_impl.py:1306 call
        (len(self.state_size), state))

    ValueError: Expected state to be a tuple of length 2, but received: Tensor("Placeholder_2:0", shape=(None, 256), dtype=float32)

How do I overcome this so that the state_is_tuple doesn't show any error as the TensorFlow version instruct to change it to True? because i've tried LSTMStateTuple but it doesnt work, maybe my methods are not correct, please help for it.

Zaza
  • 11
  • 1
  • Welcome to the stackoverflow community, this question has been asked check these links [prev question 1](https://stackoverflow.com/questions/39112622/how-do-i-set-tensorflow-rnn-state-when-state-is-tuple-true?rq=1) [prev question 2](https://stackoverflow.com/questions/39112622/how-do-i-set-tensorflow-rnn-state-when-state-is-tuple-true) and Please read and go through this before [asking your question] (https://stackoverflow.com/help/how-to-ask) Have Fun! – techPirate99 May 12 '20 at 11:43
  • first dear @techPirate99 sir, I don't have batch_size parameter, and secondly, I asked this question because I've tried the solution from the given link by saving the tensor for initial_state and it doesnt work, still error like : **ValueError: Expected state to be a tuple of length 2, but received: Tensor("Placeholder_2:0", shape=(None, 256), dtype=float32)** – Zaza May 12 '20 at 12:28
  • this is how the code look like @techPirate99 : class Model: def __init__( self, learning_rate, num_layers, size, size_layer, output_size, forget_bias = 0.1, ): def lstm_cell(size_layer): return tf.compat.v1.nn.rnn_cell.LSTMCell(size_layer, state_is_tuple = True) self.initial_state = np.zeros((num_layers, 2, 32, size_layer)) i add self.initial_state after lstm_cell function – Zaza May 12 '20 at 12:28
  • what is your recommendation by editing the code above so that the state_is_tuple is work, sir? @techPirate99 – Zaza May 12 '20 at 12:30
  • my recommendation would be try this on tf.compat.v1.nn.rnn_cell.LSTMCell(num_units, use_peepholes=False, cell_clip=None, initializer=None, num_proj=None, proj_clip=None, num_unit_shards=None, num_proj_shards=None, forget_bias=1.0, state_is_tuple=True, activation=None, reuse=None, name=None, dtype=None, **kwargs ) and check out this doc for tensorflow rnn cell [link for doc](https://www.tensorflow.org/api_docs/python/tf/compat/v1/nn/rnn_cell/LSTMCell) – techPirate99 May 12 '20 at 12:46

1 Answers1

0

tf.compat.v1.nn is deprecated. The newer Keras APIs have equivalent functions and are actively maintained. See https://www.tensorflow.org/guide/keras/overview for more info.

Dan Moldovan
  • 955
  • 6
  • 8