2017-04-26 5 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)