I want to do for loop's with dynamic inputs in tensorflow.
I know for the code below reduce_sum is sufficient, but I want to be able to formulate code in the manner described below. So given that I have an array with different sizes, I want to traverse that array.
How can I traverse dynamic length arrays?
Problem:
TypeError: range() integer end argument expected, got Tensor
How can I evaluate get_sum that is outside the session?
sum = get_sum(x, xshape) shouldn't be in the for loop inside session
For example:
import tensorflow as tf
import numpy as np
x = tf.placeholder(tf.float32, shape=[None])
xshape = tf.placeholder(tf.float32, shape=[])
def get_sum(x, xshape):
sum = 0
for i in range(xshape):
sum += x[i]
init = tf.initialize_all_variables()
sum = get_sum(x, xshape)
with tf.Session() as sess:
sess.run(init)
for i in range(100):
length = np.random.randint(0,10)
a = np.random.randint(0, 10, length)
print sess.run(sum,feed_dict={x:a, xshape:length})
whileloop.