我已经为这个问题挣扎了几天,现在我无法理解到底发生了什么。我开发了一个seq2seq模型,在一个函数中我创建了一些Tensorflow操作和变量,然后将它们返回给调用者。我希望这个函数能够重用所有变量,无论我在作用域中做了什么,我似乎都无法正确实现。以下是该函数的代码:
def create_complete_cell(rnn_size,num_layers,encoder_outputs_tr,batch_size,encoder_state , beam_width ): with tf.variable_scope("InnerScope" , reuse=tf.AUTO_REUSE): encoder_outputs_tr =tf.contrib.seq2seq.tile_batch(encoder_outputs_tr, multiplier=beam_width) encoder_state = tf.contrib.seq2seq.tile_batch(encoder_state, multiplier=beam_width) batch_size = batch_size * beam_width dec_cell = tf.contrib.rnn.MultiRNNCell([create_cell(rnn_size) for _ in range(num_layers)]) attention_mechanism = tf.contrib.seq2seq.BahdanauAttention(num_units=rnn_size, memory=encoder_outputs_tr ) attn_cell = tf.contrib.seq2seq.AttentionWrapper(dec_cell, attention_mechanism , attention_layer_size=rnn_size , output_attention=False) attn_zero = attn_cell.zero_state(batch_size , tf.float32 ) attn_zero = attn_zero.clone(cell_state = encoder_state) return attn_zero , attn_cell
以下是调用上述函数的代码:
with tf.variable_scope('scope' ): intial_train_state , train_cell = create_complete_cell(rnn_size,num_layers,encoder_outputs_tr,batch_size,encoder_state , 1 )with tf.variable_scope('scope' ,reuse=True): intial_infer_state , infer_cell = create_complete_cell(rnn_size,num_layers,encoder_outputs_tr,batch_size,encoder_state , beam_width )print("intial_train_state" , intial_train_state)print("intial_infer_state" , intial_infer_state)
打印输出如下:
第一个打印命令输出:
('intial_train_state', AttentionWrapperState(cell_state=(LSTMStateTuple(c=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_1:0' shape=(?, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_2:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_3:0' shape=(?, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_4:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_5:0' shape=(?, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_6:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope/InnerScope/tile_batch_1/Reshape_7:0' shape=(?, 512) dtype=float32>)), attention=<tf.Tensor 'scope/InnerScope/AttentionWrapperZeroState/zeros_1:0' shape=(100, 512) dtype=float32>, time=<tf.Tensor 'scope/InnerScope/AttentionWrapperZeroState/zeros:0' shape=() dtype=int32>, alignments=<tf.Tensor 'scope/InnerScope/AttentionWrapperZeroState/zeros_2:0' shape=(100, ?) dtype=float32>, alignment_history=()))
第二个打印命令输出:
('intial_infer_state', AttentionWrapperState(cell_state=(LSTMStateTuple(c=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_1:0' shape=(?, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_2:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_3:0' shape=(?, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_4:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_5:0' shape=(?, 512) dtype=float32>), LSTMStateTuple(c=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_6:0' shape=(?, 512) dtype=float32>, h=<tf.Tensor 'scope_1/InnerScope/tile_batch_1/Reshape_7:0' shape=(?, 512) dtype=float32>)), attention=<tf.Tensor 'scope_1/InnerScope/AttentionWrapperZeroState/zeros_1:0' shape=(300, 512) dtype=float32>, time=<tf.Tensor 'scope_1/InnerScope/AttentionWrapperZeroState/zeros:0' shape=() dtype=int32>, alignments=<tf.Tensor 'scope_1/InnerScope/AttentionWrapperZeroState/zeros_2:0' shape=(300, ?) dtype=float32>, alignment_history=()))
我原本期望两个输出是相同的,因为我正在重用变量,但如你所见,例如在第一个变量的输出中,有类似这样的内容 scope/InnerScope/tile_batch_1/Reshape_1:0
而在第二个变量中
scope_1/InnerScope/tile_batch_1/Reshape_1:0
我不知道为什么在第二次调用时在 scope 后面加了 _1,我有点困惑变量是否被共享,如果没有,我应该做些什么来返回相同的(共享的)变量。
谢谢你
回答:
我注意到这个问题尚未得到回答,我只是重新发布我在Tensorflow GitHub网站上关于同一主题得到的答案。
来源: https://github.com/tensorflow/tensorflow/issues/12916
总结一下,值 scope_1/InnerScope/tile_batch_1/Reshape_1:0 和 scope/InnerScope/tile_batch_1/Reshape_1:0
并不是变量名,而是Tensorflow为重塑操作创建的节点,每个Tensorflow图中的操作都有其独特的名称。
这并不意味着变量没有被共享