python - 使用 MultiRNNcell 张量流堆叠不同大小的 LSTM 层
问题描述
这是我用来从列表中堆叠多个大小为 64 和 32 的 LSTM 单元的代码num_hidden=[64,32]
stacked_cells=tf.nn.rnn_cell.MultiRNNCell([get_cell(num_hidden[_],keep_prob,z_prob_cells,z_prob_states,use_dropout,use_zoneout) for _ in range(num_hidden_layers)],state_is_tuple=True)
default_state=stacked_cells.zero_state(128,tf.float32)
print(default_state)
#init_state=tf.placeholder_with_default(default_state,shape=[num_hidden_layers,2,None,num_hidden])
init_state=tf.Variable(default_state,trainable=False)
state_per_layer=tf.unstack(init_state,axis=0)
#print(type(state_per_layer))
rnn_tuple_state=tuple([tf.nn.rnn_cell.LSTMStateTuple(state_per_layer[idx][0],state_per_layer[idx][1]) for idx in range(num_hidden_layers)])
outputs, new_states = tf.nn.dynamic_rnn(stacked_cells, x, dtype=tf.float32,
sequence_length=seq,parallel_iterations=64,initial_state=rnn_tuple_state)
#update_op=get_state_update_op(states,new_states)
new_states=tf.stack(new_states)
我使用操作更新状态init_state.assign(new_states)
,一切正常,直到我在隐藏状态中使用相同数量的功能,但是当我更改它时,我收到以下错误:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1567, in _create_c_op
c_op = c_api.TF_FinishOperation(op_desc)
tensorflow.python.framework.errors_impl.InvalidArgumentError: Dimension 2 in both shapes must be equal, but are 64 and 32. Shapes are [2,128,64] and [2,128,32].
From merging shape 0 with other shapes. for 'Variable_1/initial_value' (op: 'Pack') with input shapes: [2,128,64], [2,128,32].
>During handling of the above exception, another exception occurred:
>Traceback (most recent call last):
File "model_4_f.py", line 289, in <module>
score,state_update_op= seqRNN(x_batch,seq_batch,weights_out,biases)
File "model_4_f.py", line 253, in seqRNN
init_state=tf.Variable(default_state,trainable=False)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 235, in __init__
constraint=constraint)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/variables.py", line 355, in _init_from_args
initial_value, name="initial_value", dtype=dtype)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1014, in convert_to_tensor
as_ref=False)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1104, in internal_convert_to_tensor
ret = conversion_func(value, dtype=dtype, name=name, as_ref=as_ref)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 1034, in _autopacking_conversion_function
return _autopacking_helper(v, inferred_dtype, name or "packed")
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 997, in _autopacking_helper
return gen_array_ops.pack(elems_as_tensors, name=scope)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 4517, in pack
"Pack", values=values, axis=axis, name=name)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3392, in create_op
op_def=op_def)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1734, in __init__
control_input_ops)
File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1570, in _create_c_op
raise ValueError(str(e))
ValueError: Dimension 2 in both shapes must be equal, but are 64 and 32. Shapes are [2,128,64] and [2,128,32].
From merging shape 0 with other shapes. for 'Variable_1/initial_value' (op: 'Pack') with input shapes: [2,128,64], [2,128,32].
我做错了什么,是否有解决方法?
解决方案
推荐阅读
- apache-spark - 有没有办法定义仍然利用所有 Pyspark 优化的纯 SQL UDF?
- python - 从函数检查类外的输入?
- asp.net-mvc - 如何使用 LINQ 过滤 ViewData[ " " ] = new SelectList() 中的项目?
- jmeter - 是否可以在 inPrivate 模式下使用 Microsoft Edge 或在 JMeter 中使用 JSR223 Sampler 使用访客帐户?
- python - 如何访问列表中的列表项?
- r - 安装包 IDSpatialStats 具有非零退出状态
- javascript - 使用“$Keys”对联合类型和类型转换进行流程细化
- python - 如何将汇总数据合并到非汇总数据?
- jquery - JS Accordion 只关闭自己的部分而不是其他部分
- c# - 使用 WinUI3/Project Reunion 0.5 时,我有哪些选择/打开文件和文件夹的选项?