2017-04-26 4 views
0

Ich habe die folgende Funktion:die Indizes eines Tensors in Schleife in tensorflow Wechsel

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] und o [t] Werte ändern sich nicht in der Schleife. Wie kann ich s [t] und o [t] Werte während der Schleife aktualisieren?

Antwort

1

Die Zuweisung der Variablen ist nicht ausreichend. Sie müssen die Variable erneut ausführen. So etwas sollte funktionieren:

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) 
Verwandte Themen