在张量流中改变循环中张量的指数

时间:2017-04-26 12:07:21

标签: python tensorflow

我有以下功能:

def forward_propagation(self, x):
                # The total number of time steps
                T = len(x)
                # During forward propagation we save all hidden states in s because need them later.
                # We add one additional element for the initial hidden, which we set to 0
                s = tf.Variable(np.zeros((T + 1, self.hidden_dim)))
                # The outputs at each time step. Again, we save them for later.
                o = tf.Variable(np.zeros((T, self.word_dim)))

                init_op = tf.initialize_all_variables()
                # For each time step...
                with tf.Session() as sess:
                        sess.run(init_op)
                        for t in range(T):
                                # Note that we are indexing U by x[t]. This is the same as multiplying U with a one-hot vector.
                                s[t].assign(tf.nn.tanh(self.U[:,x[t]]) + tf.reduce_sum(tf.multiply(self.W, s[t-1])))
                                o[t].assign(tf.nn.softmax(tf.reduce_sum(self.V * s[t], axis=1)))
                        s = s.eval()
                        o = o.eval()
                return [o, s]

s [t]和o [t]值不会在循环中发生变化。如何在循环期间更新s [t]和o [t]值?

1 个答案:

答案 0 :(得分:1)

分配变量是不够的。您必须再次运行变量。这样的事情应该有效:

for t in range(T):
    s[t].assign(tf.nn.tanh(self.U[:,x[t]]) + tf.reduce_sum(tf.multiply(self.W, s[t-1])))
    o[t].assign(tf.nn.softmax(tf.reduce_sum(self.V * s[t], axis=1)))
    sess.run(s)
    sess.run(o)