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,),dtype = tf.float32)
test[0] = input[0]
#tf.assign(test[0],input[0])
with tf.Session() as sess:
sess.run(test)
I want the result of the first element of test can be equal to inputs'
The error is Tensor object does not support item assignment
.
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)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.