如何为 state_is_tuple = True 设置 RNN?


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
)

我在上面有这个代码,我一直收到这个警告:

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.

当我更改state_is_tuple=True时,它显示如下错误:

<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)
~Anaconda3libsite-packagestensorflow_corepythonutildeprecation.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',
~Anaconda3libsite-packagestensorflow_corepythonopsrnn.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].
~Anaconda3libsite-packagestensorflow_corepythonopsrnn.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.
~Anaconda3libsite-packagestensorflow_corepythonopscontrol_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):
~Anaconda3libsite-packagestensorflow_corepythonopswhile_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.,
~Anaconda3libsite-packagestensorflow_corepythonframeworkfunc_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,
~Anaconda3libsite-packagestensorflow_corepythonopswhile_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]
~Anaconda3libsite-packagestensorflow_corepythonopsrnn.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.
~Anaconda3libsite-packagestensorflow_corepythonopsrnn.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:
~Anaconda3libsite-packagestensorflow_corepythonopsrnn_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):
~Anaconda3libsite-packagestensorflow_corepythonopsrnn_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
~Anaconda3libsite-packagestensorflow_corepythonopsrnn_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):
~Anaconda3libsite-packagestensorflow_corepythonlayersbase.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():
~Anaconda3libsite-packagestensorflow_corepythonkerasenginebase_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:
~Anaconda3libsite-packagestensorflow_corepythonautographimplapi.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:UsersThinkPadAnaconda3libsite-packagestensorflow_corepythonopsrnn_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)

如何克服这个问题,以便state_is_tuple不会显示任何错误,因为 TensorFlow 版本指示将其更改为 True? 因为我尝试过 LSTMStateTuple 但它不起作用,也许我的方法不正确,请帮忙。

tf.compat.v1.nn已被弃用。较新的 Keras API 具有等效的功能,并得到积极维护。有关详细信息,请参阅 https://www.tensorflow.org/guide/keras/overview。

相关内容

最新更新