2016-06-02 4 views
2

Ich versuche, einen wiederkehrenden Zustandstensor unter Verwendung tf.scan zu implementieren. Der Code, den ich im Moment haben, ist dies:Versuch, rekursives Netzwerk mit tf.scan() zu implementieren

import tensorflow as tf 
import math 
import numpy as np 

INPUTS = 10 
HIDDEN_1 = 20 
BATCH_SIZE = 3 


def iterate_state(prev_state_tuple, input): 
    with tf.name_scope('h1'): 
     weights = tf.get_variable('W', shape=[INPUTS, HIDDEN_1], initializer=tf.truncated_normal_initializer(stddev=1.0/math.sqrt(float(INPUTS)))) 
     biases = tf.get_variable('bias', shape=[HIDDEN_1], initializer=tf.constant_initializer(0.0)) 
     matmuladd = tf.matmul(inputs, weights) + biases 
     unpacked_state, unpacked_out = tf.split(0,2,prev_state_tuple) 
     prev_state = unpacked_state 
     state = 0.9* prev_state + 0.1*matmuladd 
     output = tf.nn.relu(state) 
     return tf.concat(0,[state, output]) 

def data_iter(): 
    while True: 
     idxs = np.random.rand(BATCH_SIZE, INPUTS) 
     yield idxs 

with tf.Graph().as_default(): 
    inputs = tf.placeholder(tf.float32, shape=(BATCH_SIZE, INPUTS)) 
    with tf.variable_scope('states'): 
     initial_state = tf.zeros([HIDDEN_1], 
           name='initial_state') 
     initial_out = tf.zeros([HIDDEN_1], 
           name='initial_out') 
     concat_tensor = tf.concat(0,[initial_state, initial_out]) 
     states, output = tf.scan(iterate_state, inputs, 
            initializer=concat_tensor, name='states') 

    sess = tf.Session() 
    # Run the Op to initialize the variables. 
    sess.run(tf.initialize_all_variables()) 
    iter_ = data_iter() 
    for i in xrange(0, 2): 
     print ("iteration: ",i) 
     input_data = iter_.next() 
     out,st = sess.run([output,states], feed_dict={ inputs: input_data}) 

Allerdings habe ich diese Fehlermeldung erhalten, wenn dieser ausgeführt wird:

Traceback (most recent call last): 
    File "cycles_in_graphs_with_scan.py", line 37, in <module> 
    initializer=concat_tensor, name='states') 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 442, in __iter__ 
    raise TypeError("'Tensor' object is not iterable.") 
TypeError: 'Tensor' object is not iterable. 
(tensorflow)[email protected] ~/projects/stuff $ python cycles_in_graphs_with_scan.py 
Traceback (most recent call last): 
    File "cycles_in_graphs_with_scan.py", line 37, in <module> 
    initializer=concat_tensor, name='states') 
    File "/usr/local/lib/python2.7/dist-packages/tensorflow/python/framework/ops.py", line 442, in __iter__ 
    raise TypeError("'Tensor' object is not iterable.") 
TypeError: 'Tensor' object is not iterable. 

ich schon versucht, mit pack/unpack und concat/split aber ich habe diesen gleichen Fehler.

Irgendwelche Ideen, wie man dieses Problem löst?

Antwort

3

Sie bekommen einen Fehler, da tf.scan() gibt einen einzigentf.Tensor, so dass die Zeile:

states, output = tf.scan(...) 

... kann nicht destrukturiert (auspacken) der Tensor aus tf.scan() in zwei Werte zurück (states und outputs). Effektiv versucht der Code, das Ergebnis tf.scan() als eine Liste der Länge 2 zu behandeln und das erste Element states und das zweite Element zuzuweisen, aber — im Gegensatz zu einer Python-Liste oder Tuple — tf.Tensor unterstützt dies nicht.

Stattdessen müssen Sie die Werte manuell aus dem Ergebnis tf.scan() extrahieren. Zum Beispiel mit tf.split():

scan_result = tf.scan(...) 
# Assumes values are packed together along `split_dim`. 
states, output = tf.split(split_dim, 2, scan_result) 

Alternativ Sie tf.slice() oder tf.unpack() zu extrahieren die entsprechenden states und output Werte nutzen könnten.

Verwandte Themen