如何为 state_is_tuple = True 设置 RNN?
Posted
技术标签:
【中文标题】如何为 state_is_tuple = True 设置 RNN?【英文标题】:How to set RNN for the state_is_tuple = True? 【发布时间】:2020-08-28 05:24:39 【问题描述】: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)
~\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)
我该如何克服这个问题,以便 state_is_tuple 不会显示任何错误,因为 TensorFlow 版本指示将其更改为 True?因为我尝试过 LSTMStateTuple 但它不起作用,可能是我的方法不正确,请帮忙。
【问题讨论】:
欢迎来到 *** 社区,这个问题已经被问到检查这些链接 prev question 1 prev question 2 并且请在 [提出你的问题] (***.com/help/how-to-ask) 之前阅读并阅读此内容! /跨度> 首先亲爱的@techPirate99先生,我没有batch_size参数,其次,我问了这个问题,因为我已经通过保存初始状态的张量尝试了给定链接中的解决方案,但它不起作用,仍然出现错误,例如:ValueError: Expected state to be a tuple of length 2, but received: Tensor("Placeholder_2:0", shape=(None, 256), dtype=float32) 这就是代码的样子 @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)) 我在 lstm_cell 函数之后添加 self.initial_state 您有什么建议可以通过编辑上面的代码使 state_is_tuple 正常工作,先生? @techPirate99 我的建议是在 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 ) 并查看此文档以了解 tensorflow rnn 单元 link for doc 【参考方案1】:tf.compat.v1.nn
已弃用。较新的 Keras API 具有等效的功能并得到积极维护。请参阅https://www.tensorflow.org/guide/keras/overview 了解更多信息。
【讨论】:
以上是关于如何为 state_is_tuple = True 设置 RNN?的主要内容,如果未能解决你的问题,请参考以下文章
deep_learning_Function_rnn_cell.BasicLSTMCell
如何为 contenteditable="true" 内容添加内联工具栏?