Fixing TensorFlow TypeError: ‘Tensor’ object does not support item assignment

TensorFlow TypeError: ‘Tensor’ object does not support item assignment is an error which occurs when you try to assign to a TensorFlow tensor.

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

Exploring the TensorFlow TypeError: ‘Tensor’ object does not support item assignment

This is an error which occurs when you try to assign to a tensor in tensorflow.

I will explain why this error takes place and how to fix it, while also trying to add other solutions that could solve the error.

                                                                       #
TypeError: 'Tensor' object does not support item assignment
                                                                       #

To solve the problem above, I have 2 solutions which worked for me, bellow is a detailed explanation of both.

Solution 1 : use tf.stack() and tf.unstack()

The problem occurs when you attempt to assign to a tensor, most developers do this without paying attention.

The solution is clear, stop assigning to a tensor, you can use tf.unstack to convert the tensor to a list. For example, if you have a tensor called ‘tensor1’, you can convert it to a list called ‘list1’ like this.

                                                                       #
list1 = tf.unstack(tensor1)
                                                                       #

You can then use tf.stack like this.

                                                                       #
list1[61:65] = [np.nan for x in range(5)]
tensor1 = tf.stack(list1)
                                                                       #

using tf.stack() and tf.unstack() should be more than enough to solve your problem.

If this solution does not work, please try the solution bellow.

Solution 2 : use np.zeros() or torch.zeros() or use .Variable() with .zeros() followed by .assign()

Another option is to use numpy or Pytorch like this .zeros(mytensor-dims) . like in the examples bellow.

                                                                       #
val = np.zeros(mytensor-dims)
val = torch.zeros(mytensor-dims)
                                                                       #

While mytensor-dims are the dimensions of the tensor at hand.

Another option is to use .zeros() inside .Variable() like this

                                                                       #
var=tensor2.Variable(tensor2.zeros(3, tensor2.int32))
                                                                       #

Then assign .assign() like in the line bellow ( tenso2 is our tensor in this case )

                                                                       #
var=var[2].assign(1)
                                                                       #

The solutions above should be enough to solve the problem, if you like our effort make sure to consider donating to our Kofi account, there is a red button that you can use if you are feeling generous.

Summing-up : 

That is it guys, we arrived at the end of this article dealing with the error : TensorFlow TypeError Tensor object does not support item assignment, I did my best to help you solve this issue, I wish you good luck with your Python projects and I wish you good luck with the errors to come.

If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/