共享和重用Tensorflow变量

时间:2017-09-06 18:11:05

标签: python tensorflow artificial-intelligence

我几天来一直在努力解决一个问题,我无法理解发生了什么,我开发了一个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=()))

我期待两个输出都是相同的,因为我重复使用变量但是你可以看到例如在第一个变量中输出有这样的东西 的范围/ InnerScope / tile_batch_1 / Reshape_1:0

和第二个变量

scope_1 / InnerScope / tile_batch_1 / Reshape_1:0

我不知道为什么在第二次调用中将_1添加到范围,如果变量是否被共享,我有点困惑,如果不是,我应该怎么做才能返回相同的变量(共享)。

谢谢

1 个答案:

答案 0 :(得分:0)

我注意到这个问题没有得到回答,我只是在重新发布我从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为整形操作创建的节点,Tensroflow图中的每个Operation都有其唯一的名称。

这并不意味着不共享变量