问题
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
回答1:
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
回答2:
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.]
来源:https://stackoverflow.com/questions/51419333/modify-a-tensorflow-variable-inside-a-loop