如何使用tf.Assign()分配LSTMStateTuple()?

时间:2017-10-05 00:39:21

标签: python tensorflow lstm

我有一种方法可以获取输出状态并分配它,以便它在下一个RUN()上持续存在。我也可以用类似的方法注入这种状态的INIT。这适用于" state_is_tuple = False"直到我尝试迁移到" state_is_tuple = True"当我开始收到警告时,TensorFlow正朝着这个配置发展。

        self.initiate_state = self.cell_L1.zero_state(batch_size, tf.float32)

        self.state = tf.Variable(self.initiate_state, trainable=False)

        with tf.control_dependencies([self.state.assign(self.initiate_state)]):
            self.initiate_state_op = tf.no_op(name="initiate_state")

        output, self.new_state = tf.nn.dynamic_rnn(self.cell_L1,hidden_input,time_major=True,\
                initial_state=self.state,  dtype=tf.float32, swap_memory=True) 

        with tf.control_dependencies([self.state.assign(self.new_state)]):
            outputs = tf.identity(output)

        outputs          = tf.reshape(outputs, [-1,self.hidden_state_size])  

我尝试了几种不同的配置" assign()" OP但似乎无法让它像Tuple一样工作。当然,如果assign()支持Tuples会很棒,但在此之前,我将如何完成同样的任务?

1 个答案:

答案 0 :(得分:1)

由于LSTMStateTuple包含两个部分,即c和h,因此应单独指定它。例如:

In [108]: c = tf.Variable([[1, 1, 1, 2, 2, 2], [3, 3, 3, 4, 4, 4]])                                                                                                                                                                                                                                                                                                

In [110]: h = tf.Variable([[5, 5, 5, 6, 6, 6], [7, 7, 7, 8, 8, 8]])                                                                                                                                                  

In [111]: c_new = tf.Variable([[11, 11, 11, 22, 22, 22], [33, 33, 33, 44, 44, 44]])                                                                                                                                  

In [112]: h_new = tf.Variable([[55, 55, 55, 66, 66, 66], [77, 77, 77, 88, 88, 88]])                                                                                                                                  

In [113]: init = tf.initialize_all_variables()                                                                                                                                                                       
WARNING:tensorflow:From /usr/local/lib/python2.7/dist-packages/tensorflow/python/util/tf_should_use.py:175: initialize_all_variables (from tensorflow.python.ops.variables) is deprecated and will be removed after 2017-03-02.
Instructions for updating:
Use `tf.global_variables_initializer` instead.

In [114]: ch = tf.contrib.rnn.LSTMStateTuple(c, h)                                                                                                                                                                   

In [115]: sess.run(init)

In [116]: sess.run(ch)
Out[116]: 
LSTMStateTuple(c=array([[1, 1, 1, 2, 2, 2],
       [3, 3, 3, 4, 4, 4]], dtype=int32), h=array([[5, 5, 5, 6, 6, 6],
       [7, 7, 7, 8, 8, 8]], dtype=int32))

In [117]: ass = tf.assign(ch.c, c_new)                                                                                                                                                                               

In [118]: sess.run(ass)
Out[118]: 
array([[11, 11, 11, 22, 22, 22],
       [33, 33, 33, 44, 44, 44]], dtype=int32)

In [119]: sess.run(ch)                                                                                                                                                                                               
Out[119]: 
LSTMStateTuple(c=array([[11, 11, 11, 22, 22, 22],
       [33, 33, 33, 44, 44, 44]], dtype=int32), h=array([[5, 5, 5, 6, 6, 6],
       [7, 7, 7, 8, 8, 8]], dtype=int32))

您可以看到ch.c已更新。