Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass list of tensors as a input to the graph in tensorflow?

Tags:

tensorflow

I am passing my input to tensors using a tf.placeholder() of shape [None, None, 10]. Now I want to iterate over the first dimension of the input, and apply some function to each slice in that dimension. However, when I try to do this using a Python for loop, I get an error saying that Tensor objects are "not iterable".

Is there any way I can pass the input as the list of tensors of shape [None, 10], and how could I assign this list to the placeholder? Or is there some other way to iterate over a dimension of a Tensor?

like image 344
akshaybetala Avatar asked Oct 30 '25 02:10

akshaybetala


1 Answers

This is possible using the new tf.map_fn(), tf.foldl() tf.foldr() or (most generally) tf.scan() higher-order operators, which were added to TensorFlow in version 0.8. The particular operator that you would use depends on the computation that you want to perform. For example, if you wanted to perform the same function on each row of the tensor and pack the elements back into a single tensor, you would use tf.map_fn():

p = tf.placeholder(tf.float32, shape=[None, None, 100])

def f(x):
    # x will be a tensor of shape [None, 100].
    return tf.reduce_sum(x)

# Compute the sum of each [None, 100]-sized row of `p`.
# N.B. You can do this directly using tf.reduce_sum(), but this is intended as 
# a simple example.
result = tf.map_fn(f, p)
like image 74
mrry Avatar answered Nov 01 '25 14:11

mrry