TSimron
TSimron

Reputation: 73

how to assign a value to the 'Tensor' object in Keras?

I want to assign value to a tensor variable in the following manner. However, I get an error saying: "'Tensor' object does not support item assignment".

I am trying to convert these python codes to tensorflow in Keras. However, the second line gives the error

s1 = tf.zeros([5:256:256:3], tf.float64) 
s1[:,:,:,2] = -1

#depth is in shape [5:256,256,1]
lamda = -(depth/s2[:,:,:,2])

x_c = np.around(lamda * s1[:,:,:,0]/step,decimals=2) 
y_c = np.around(lamda * s1[:,:,:,1]/step,decimals=2) 

Please let me know how to fix this issue? Thank you in advance.

Upvotes: 1

Views: 2333

Answers (1)

Sihyeon Kim
Sihyeon Kim

Reputation: 161

A TensorFlow tensor object is not assignable.
This question and this might be helpful.

import tensorflow as tf

s1 = tf.Variable(tf.zeros([5,256,256,3], tf.float64))
s2 = tf.Variable(-tf.ones([5,256,256,3], tf.float64))  

assign_op = tf.assign(s1[:,:,:,2], s2[:,:,:,2])

init = tf.global_variables_initializer()
sess = tf.Session()
sess.run(init)
result = sess.run(assign_op)

print(result)
[[[[ 0.  0. -1.]
   [ 0.  0. -1.]
   [ 0.  0. -1.]
   ...
   [ 0.  0. -1.]
   [ 0.  0. -1.]
   [ 0.  0. -1.]]

  [[ 0.  0. -1.]
   [ 0.  0. -1.]
   [ 0.  0. -1.]
   ...

Upvotes: 1

Related Questions