0

Tensorflow While loop with Variable Creation Code here :

x = tf.Variable(100)    
c = tf.Constant(2)    
n = 100    
loops = 50

l1 = tf.Variable(np.random.random(n))    
c1 = tf.Variable(np.random.random(n))     
x = tf.multiply(c1,tf.exp(-(x-l1)/c))

l2 = tf.Variable(np.random.random(n))
c2 = tf.Variable(np.random.random(n)) 
x = tf.multiply(c2,tf.exp(-(x-l2)/c))

l3 = tf.Variable(np.random.random(n))
c3 = tf.Variable(np.random.random(n))
x = tf.multiply(c3,tf.exp(-(x-l3)/c))

.....

.....

l50 = tf.Variable(np.random.random(n))
c50 = tf.Variable(np.random.random(n))
x = tf.multiply(c50,tf.exp(-(x-l50)/c))

So, I want to do that in while loop in tensorflow as:

while loop(i from 1 to 50):
   l[i] = tf.Variable(np.random.random(n))
   c[i] = tf.Variable(np.random.random(n))
   x = tf.multiply(c[i],tf.exp(-(x-l[i])/c))


How can I achieve this in tensorflow. Thanking you

3
  • 1
    You can format blocks of code by indenting it all by four spaces, or by highlighting it all and pressing the {} (format code) button. You don't need to individually backtick every line. Commented Jul 16, 2018 at 10:31
  • see tf.while_loop for details Commented Jul 16, 2018 at 11:10
  • Can you tell the actual formula, might help to come up with a vectorized implementation. Commented Jul 16, 2018 at 12:14

1 Answer 1

1

Can you feed it like this ?

l = tf.placeholder(tf.float32, shape=[None, ])
c = tf.placeholder(tf.float32, shape=[None, ])

sess = tf.Session()
sess.run(tf.global_variables_initializer())

x = tf.multiply(l,c)   #Assume a formula

for i in range(50) :
    arr = np.random.random_sample((i,))
    print (arr)
    sess.run(x,feed_dict={l :arr,c :arr})

Based on your comment I've only attempted. This isn't the exact answer.

results = tf.TensorArray(dtype=tf.float32, size=100)

lvalues = tf.TensorArray(dtype=tf.float32, size=50)
cvalues = tf.TensorArray(dtype=tf.float32, size=50)

sess = tf.Session()
sess.run(tf.global_variables_initializer())

def my_func(x):
    a = np.random.random_sample((x,))
    return a

input = tf.placeholder(tf.int32)
y = tf.py_func(my_func, [input], tf.float64)
for i in range(50) :
    r = sess.run(y,feed_dict={input : i})
    lvalues.write(i,y)
    cvalues.write(i,y)
    print(y)
    print(r)
Sign up to request clarification or add additional context in comments.

2 Comments

No SIr, Actually, I want to use computation graph for these variable.
Please update your question with some more details so that others can attempt.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.