1

I would like to modify certain indexes of a Variable inside a while loop. Basically convert the python code below to Tensorflow:

import numpy
tf_variable=numpy.zeros(10,numpy.int32)
for i in range (10):
    tf_variable[i]=i
tf_variable

Tensorflow code would look like following: except it gives error

import tensorflow as tf
var=tf.get_variable('var',initializer=tf.zeros([10],tf.int32),trainable=False)
itr=tf.constant(0)
sess=tf.Session()
sess.run(tf.global_variables_initializer()) #initializing variables


print('itr=',sess.run(itr))
def w_c(itr,var):
    return(tf.less(itr,10))
def w_b(itr,var):
    var=tf.assign(var[1],9) #lets say i want to modify index 1 of variable var
    itr=tf.add(itr,1)
    return [itr,var] #these tensors when returning actually get called


OP=tf.while_loop(w_c,w_b,[itr,var],parallel_iterations=1,back_prop=False)
print(sess.run(OP))

Thanks

Aseem
  • 5,848
  • 7
  • 45
  • 69

2 Answers2

0

Its quite a unique thing to do I am sure if you express your problem in further detail, I can help you better but if you intend to change a variable in a tf.variable, this is what I will suggest

tf_Variable=tf.random_normal([1,10])
array=tf.Session().run(tf_Variable)
print(array)

array([[ 1.8884579 , -1.4278126 , -1.5084593 , 2.2028043 , 0.10910247, -1.6836789 , 0.41359457, 2.0960712 , 0.5169063 , -0.66555417]], dtype=float32)

array[0][3]=2
print(array)

array([[ 1.8884579 , -1.4278126 , -1.5084593 , 2. , 0.10910247, -1.6836789 , 0.41359457, 2.0960712 , 0.5169063 , -0.66555417]], dtype=float32)

you can again feed this into a tf variable if you like As is explained here

Inder
  • 3,711
  • 9
  • 27
  • 42
  • 3
    how to do it inside a tensorflow while_loop? – Aseem Jul 19 '18 at 18:57
  • How will the loop look like do share that, I will be able to write the exact code – Inder Jul 19 '18 at 19:04
  • I have added tensorflow code to the question which generates error – Aseem Jul 19 '18 at 19:19
  • The issue is that the built in while loop for tf converts the variable to `Tensor("while/Identity_1:0", shape=(10,), dtype=int32` and that is precisely why you are not able to assign with slices as its only supported for a variable. I will suggest that either you change your code or build a custom while loop to perform these operations – Inder Jul 20 '18 at 07:43
0

Making a "detour" over the CPU is not always feasible (you lose the gradients). Here is a possibility how to implement your numpy example in TensorFlow (inspired by this post and an answer I gave on this other post)

import tensorflow as tf

tf_variable = tf.Variable(tf.ones([10]))

def body(i, v):
    index = i
    new_value = tf.to_float(i)
    delta_value = new_value - v[index:index+1]
    delta = tf.SparseTensor([[index]], delta_value, (10,))
    v_updated = v + tf.sparse_tensor_to_dense(delta)
    return tf.add(i, 1), v_updated


_, updated = tf.while_loop(lambda i, _: tf.less(i, 10), body, [0, tf_variable])

with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    print(sess.run(tf_variable))
    print(sess.run(updated))

This prints

[1. 1. 1. 1. 1. 1. 1. 1. 1. 1.]
[0. 1. 2. 3. 4. 5. 6. 7. 8. 9.]
kafman
  • 2,862
  • 1
  • 29
  • 51