Skip to main content

Fixed: TypeError: 'EagerTensor' object does not support item assignment

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

Changing one value of a tensor with scores[1] = 10 stopped with TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment. A TensorFlow tensor can't be changed once it's made. Building a new tensor with the change fixed it, and a tf.Variable is the way to hold values that do change.

What I Was Doing​

I had a tensor of three scores, [7, 9, 8], and wanted to change the second one to 10, the way I'd change an item in a Python list:

update_score.py
import tensorflow as tf

scores = tf.constant([7, 9, 8])
scores[1] = 10
print(scores)

I ran it in a uv project with TensorFlow 2.22.0:

Terminal
uv run python update_score.py

What I Expected​

The scores [7 10 8].

What I Got​

Output
Traceback (most recent call last):
File "/Users/you/source/fx-item-assignment/update_score.py", line 4, in <module>
scores[1] = 10
~~~~~~^^^
TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment

The terminal after uv run python update_score.py: a traceback pointing at scores[1] = 10 on line 4, ending in TypeError: 'tensorflow.python.framework.ops.EagerTensor' object does not support item assignment.

The script stopped at line 4 and printed nothing.

What I Checked​

1. What "item assignment" is​

scores[1] = 10 is item assignment: Python's way of replacing the item at a position in place. A list supports it; the message says this object doesn't.

2. What an EagerTensor is​

type(scores) printed <class 'tensorflow.python.framework.ops.EagerTensor'>. That's the class of the tensor tf.constant returned: an ordinary TensorFlow tensor, holding its values straight away.

3. What TensorFlow's tensor guide says​

The tensor guide is clear: "All tensors are immutable like Python numbers and strings: you can never update the contents of a tensor, only create a new one."

Why It Happens​

A tensor can't be changed after it's created, so it has no way to replace one of its values. Changing a value means making a new tensor that has it.

The Fix​

Build a new tensor with the change, using tf.tensor_scatter_nd_update:

update_score.py
import tensorflow as tf

scores = tf.constant([7, 9, 8])
updated = tf.tensor_scatter_nd_update(scores, indices=[[1]], updates=[10])
print(updated)
print(scores)

indices is a list of positions, each written as a list: [[1]] is the one position 1. updates holds the new value for each position. The tf.tensor_scatter_nd_update API page says it "creates a new tensor by applying sparse updates to the input tensor. This is similar to an index assignment." The script prints the new tensor, then the original.

Another way: use a tf.Variable for values that change​

When the values are meant to change over time, keep them in a tf.Variable instead of a tensor. The variable guide says: "A tf.Variable represents a tensor whose value can be changed by running ops on it." Change a value with .assign():

update_variable.py
import tensorflow as tf

scores = tf.Variable([7, 9, 8])
scores[1].assign(10)
print(scores)
Terminal
uv run python update_variable.py
Output
<tf.Variable 'Variable:0' shape=(3,) dtype=int32, numpy=array([ 7, 10, 8], dtype=int32)>

The terminal after uv run python update_variable.py: the tf.Variable now holds the values 7, 10 and 8.

scores[1].assign(10) changed the variable itself: it now holds [ 7, 10, 8].

After the Fix​

Terminal
uv run python update_score.py
Output
tf.Tensor([ 7 10 8], shape=(3,), dtype=int32)
tf.Tensor([7 9 8], shape=(3,), dtype=int32)

The terminal after the fix: the new tensor 7, 10, 8 from tf.tensor_scatter_nd_update, then the original tensor, still 7, 9, 8.

updated has the new score, [ 7 10 8], and scores still has [7 9 8]: the function made a new tensor and left the old one as it was. The commands on this page work unchanged in PowerShell.

Common Mistakes​

MistakeWhat happensDo this instead
Expecting tf.tensor_scatter_nd_update to change scoresscores still printed [7 9 8]: the function returns a new tensorUse what it returns: scores = tf.tensor_scatter_nd_update(...) gives the name the new tensor
Writing the positions without the inner list: indices=[1]InvalidArgumentError: … Inner dimensions of output shape must match inner dimensions of updates shape. Output: [3] updates: [1] [Op:TensorScatterUpdate]Wrap each position in a list: indices=[[1]], or [[0], [2]] for two positions
Using = on a tf.Variable: scores[1] = 10TypeError: 'ResourceVariable' object does not support item assignmentUse scores[1].assign(10)

Troubleshooting​

SymptomLikely causeFix
'EagerTensor' object does not support item assignmenttensor[i] = value on a tensortf.tensor_scatter_nd_update, or keep the values in a tf.Variable
'ResourceVariable' object does not support item assignmentvariable[i] = value on a tf.Variablevariable[i].assign(value)
Inner dimensions of output shape must match inner dimensions of updates shapeindices written as [1] instead of [[1]]One inner list per position

FAQ​

Can I change several values at once?​

Yes: give one position and one value for each. tf.tensor_scatter_nd_update(scores, indices=[[0], [2]], updates=[1, 2]) returned [1 9 2].

Can I change the tensor as a NumPy array instead?​

You can change a copy: a = scores.numpy() gives a NumPy array, a[1] = 10 works on it, and tf.constant(a) made the tensor [ 7 10 8]. The original tensor stayed [7 9 8].

Conclusion​

'EagerTensor' object does not support item assignment meant scores[1] = 10 tried to change a tensor, and tensors can't be changed. tf.tensor_scatter_nd_update(scores, indices=[[1]], updates=[10]) built a new tensor, [ 7 10 8], and a tf.Variable with .assign() holds values that are meant to change.

Additional Resources​