I tried to use the following code to equate a tensor in tensorflow:
import tensorflow as tf
import numpy as np
a = tf.placeholder(tf.float32, shape=[2,2])
b = tf.Variable(tf.zeros(shape = [1,1]))
sess = tf.Session()
b[0,0]=a[0,0]
sess.run(tf.initialize_all_variables())
But there is an error message "'RefVariable' object does not support item assignment". How should I modify?
You have to create a tensor which performs the assignment and run it. You can make an assignment to a slice:
assg = b[0,0].assign(a[0,0])
feed_dict = {a: np.array([[3,4],[5,6]])}
sess.run(assg, feed_dict=feed_dict)
print(sess.run(b)) # [[3.]]
Since you actually want to assign new values to the whole of b, you can also just use tf.assign, but then you have to make sure the shapes match, since a[0,0] is a number, while b is a matrix of size 1x1.
assg = tf.assign(b, tf.reshape(a[0,0],shape=[1,1]))
feed_dict = {a: np.array([[3,4],[5,6]])}
sess.run(assg, feed_dict=feed_dict)
print(sess.run(b)) # [[3.]]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With