gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-03 14:40:34 25 4
gpt4 key购买 nike

我有一种方法来获取输出状态并分配它,以便它在下一个 RUN() 中持续存在。我也可以用类似的方法注入(inject)这个状态的 INIT。这对于“state_is_tuple=False”非常有效,直到我尝试迁移到 TensorFlow 正在转向的“state_is_tuple=True”配置,因为我开始收到警告。

        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()”操作的不同配置,但似乎无法让它像元组一样工作。当然,如果 allocate() 支持元组那就太好了,但在此之前,我将如何完成同样的任务?

最佳答案

由于 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 已更新。

关于python - 如何使用 tf.Assign() 分配 LSTMStateTuple()?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46576194/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com