2017-04-26 63 views
0

我有以下功能:改變張量的索引在環中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

分配變量是不夠的。你必須再次運行該變量。像這樣的東西應該工作:

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)