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?
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With