Skip to content Skip to sidebar Skip to footer

Error In Equating Subtensor In Tensorflow

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(

Solution 1:

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.]]

Post a Comment for "Error In Equating Subtensor In Tensorflow"