【问题标题】:sharing and Reusing Tensorflow variables共享和重用 TensorFlow 变量
【发布时间】:2017-09-06 18:11:04
【问题描述】:

我这几天一直在为一个问题苦苦挣扎,我无法理解发生了什么,我开发了一个 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 添加到 scope 中,我有点困惑该变量是否被共享,如果不是我应该怎么做才能返回相同的变量(共享)。

谢谢你

【问题讨论】:

    标签: python tensorflow artificial-intelligence


    【解决方案1】:

    我注意到这个问题没有得到解答,我只是重新发布了我从 Tensorflow GitHub 站点获得的关于同一主题的答案。

    来源:https://github.com/tensorflow/tensorflow/issues/12916

    总结一下,价值观 scope_1/InnerScope/tile_batch_1/Reshape_1:0 范围/InnerScope/tile_batch_1/Reshape_1:0

    不是变量名,而是 tensorflow 为 reshape 操作创建的节点,Tensroflow 图中的每个操作都有其唯一的名称。

    这并不意味着变量不共享

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-08-07
      • 1970-01-01
      • 2016-08-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多