I have a variable that contains the 4x4 identitiy matrix. I wish to assign some values to this matrix (these values are learned by the model).
When I use tf.assign()
I get an error saying that strided slices do not have gradients.
My question is how can I do this without using tf.assign()
Here is a sample code of the desired behaviour(without the error, since the values are not learned here) :
params = [[1.0, 2.0, 3.0]]
M = tf.Variable(tf.eye(4, batch_shape=[1]), dtype=tf.float32)
M = tf.assign(M[:, 0:3, 3], params)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
output_val = sess.run(M)
Note - the variable is created solely for the purpose of housing these parameters.
UPDATE: I am adding a minimal working example that creates the error. (obviously training like this won't result in anything good. Its just to illustrate the error since my code is far too long to copy here)
params = [[1.0, 2.0, 3.0]]
M_gt = np.eye(4)
M_gt[0:3, 3] = [4.0, 5.0, 6.0]
M = tf.Variable(tf.eye(4, batch_shape=[1]), dtype=tf.float32)
M = tf.assign(M[:, 0:3, 3], params)
loss = tf.nn.l2_loss(M - M_gt)
optimizer = tf.train.AdamOptimizer(0.001)
train_op = optimizer.minimize(loss)
sess = tf.Session()
init = tf.global_variables_initializer()
sess.run(init)
sess.run(train_op)