close

[Solved] TypeError: ‘Tensor’ object does not support item assignment in TensorFlow

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error TypeError: ‘Tensor’ object does not support item assignment in TensorFlow in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How TypeError: ‘Tensor’ object does not support item assignment in TensorFlow Error Occurs?

Today I get the following error TypeError: ‘Tensor’ object does not support item assignment in TensorFlow in python.

How To Solve TypeError: ‘Tensor’ object does not support item assignment in TensorFlow Error ?

  1. How To Solve TypeError: 'Tensor' object does not support item assignment in TensorFlow Error ?

    To Solve TypeError: 'Tensor' object does not support item assignment in TensorFlow Error
    In general, a TensorFlow tensor object is not assignable*, so you cannot use it on the left-hand side of an assignment.

  2. TypeError: 'Tensor' object does not support item assignment in TensorFlow

    To Solve TypeError: 'Tensor' object does not support item assignment in TensorFlow Error
    In general, a TensorFlow tensor object is not assignable*, so you cannot use it on the left-hand side of an assignment.

Solution 1

In general, a TensorFlow tensor object is not assignable*, so you cannot use it on the left-hand side of an assignment.

The easiest way to do what you’re trying to do is to build a Python list of tensors, and tf.stack() them together at the end of the loop:

outputs, states = rnn.rnn(lstm_cell, x, initial_state=initial_state,
                          sequence_length=real_length)

output_list = []

tensor_shape = outputs.get_shape()
for step_index in range(tensor_shape[0]):
    word_index = self.x[:, step_index]
    word_index = tf.reshape(word_index, [-1,1])
    index_weight = tf.gather(word_weight, word_index)
    output_list.append(tf.mul(outputs[step_index, :, :] , index_weight))

outputs = tf.stack(output_list)

 * With the exception of tf.Variable objects, using the Variable.assign() etc. methods. However, rnn.rnn() likely returns a tf.Tensor object that does not support this method.

Solution 2

Another way you can do it like this.

aa=tf.Variable(tf.zeros(3, tf.int32))
aa=aa[2].assign(1)

then the output is:

array([0, 0, 1], dtype=int32)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read