这是我用来从列表num_hidden=[64,32]
堆叠多个大小为64和32的LSTM单元的代码。
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].
我做错了什么,对此有解决方法吗?