Skip to content Skip to sidebar Skip to footer

How To Assign The Element Of Tensor From Other Tensor In Tensorflow

I want to assign tensor from other tensor. i will give simple demo as followed: import tensorflow as tf input = tf.constant([1.,2.,3.],dtype=tf.float32) test = tf.zeros(shape=(3

Solution 1:

Only Variables support sliced assignment, while tf.zeros creates a constant Value tensor; You need to declare test as a variable:

sess = tf.Session()
test = tf.Variable(tf.zeros(shape=(3,),dtype = tf.float32))

init_op = tf.global_variables_initializer()
sess.run(init_op)
sess.run(test)
# array([ 0.,  0.,  0.], dtype=float32)

assign_op = tf.assign(test[0], input[0])
sess.run(assign_op)
# array([ 1.,  0.,  0.], dtype=float32)
sess.run(test)
# array([ 1.,  0.,  0.], dtype=float32)

Post a Comment for "How To Assign The Element Of Tensor From Other Tensor In Tensorflow"