TF v2 AttributeError: ‘Tensor’ object has no attribute ‘numpy’ inside loss function

  eager-execution, numpy, python, tensorflow

I’m getting the error AttributeError: ‘Tensor’ object has no attribute ‘numpy’ when trying to change a tf tensor to a numpy array and then back to a tensor. The code giving me the error is as follow.

network = models.Sequential()
network.add(layers.Dense(512,activation='relu'))
network.add(layers.Dense(10,activation='linear'))

def root_mean_squared_error(y_true, y_pred):
        y_pred = y_pred.numpy()
        y_pred = tf.convert_to_tensor(y_pred)
        return K.sqrt(K.mean(K.square(y_pred - y_true)))

network.compile(optimizer='rmsprop',
                loss=root_mean_squared_error,
                metrics=['accuracy'])

network.fit(train_images,cat_train_labels,epochs=5,batch_size=28)

Here, i’m just trying to get this basic implementation to work as i’ve been getting issues while using .numpy()

It seems as though eager execution isn’t enabled but from what i tried I couldn’t enable it.

Any help is appreciated.

Source: Python Questions

LEAVE A COMMENT