Tutorial Example

Fix TensorFlow AttributeError: object has no attribute ‘_lazy_read’ – TensorFlow Tutorial

In this tutorial, we will introduce you how to fix tensorflow AttributeError: object has no attribute ‘_lazy_read’ error.

Look at this example:

import numpy as np
import tensorflow as tf
x = tf.convert_to_tensor(np.random.random((5, 10)))
y = tf.convert_to_tensor(np.random.random((5, 10)))
diff = x - y

xy = tf.convert_to_tensor(np.array([0,1,2,3,4]))
z = tf.scatter_sub(x, xy, diff)

Run this code, you will see this errro:

AttributeError: ‘Tensor’ object has no attribute ‘_lazy_read’

How to fix this no attribute error?

As to tf.scatter_sub(), it is defined as:

tf.scatter_sub(
    ref, indices, updates, use_locking=False, name=None
)

Here ref must be a variable, not a constant.

In this example, we can convert x to a tensorflow variable, this error will be fixed.

For example:

import numpy as np
import tensorflow as tf
y = tf.convert_to_tensor(np.random.random((5, 10)), dtype=tf.float32)
x = tf.get_variable('x', [5, 10], dtype=tf.float32, initializer=tf.constant_initializer(0), trainable=False)
diff = x - y

xy = tf.convert_to_tensor(np.array([0,1,2,3,4]))
z = tf.scatter_sub(x, xy, diff)

Then you will find this no attribute error is fixed.