AttributeError: 'Tensor' object has no attribute 'numpy'

import numpy as np
import pandas as pd
import tensorflow as tf
import tensorflow_datasets as tfds
from tensorflow import keras
from tensorflow.keras.applications.resnet50 import ResNet50
from tensorflow.keras.models import Model    
(stl_train, stl_test, stl_unlabelled), ds_info = tfds.load('stl10',split=['train','test','unlabelled'],shuffle_files=True,as_supervised=True,with_info=True)
base_model = ResNet50(input_shape=ds_info.features['image'].shape,weights=None)
def reshapeImage(image, label):
  return tf.reshape(image, (1,96,96,3)), tf.convert_to_tensor(onehot_encoded[label.numpy()])
stl_train =,

The labels are tf.Tensor(4, shape=(), dtype=int64) ,I want the 4, in this example, which I will be using to convert into one-hot encoded labels, but I am unable to do so

Have you tried to use set_shape in your reshapeImage function?


The problem is actually in this part of the return statement. If I simply keep the labels as categorical numbers, it works, but then I will be having problems with softmax

Update: it worked with stl_train = x, y: (x, tf.one_hot(integer_encoded,depth=10)[y]))
Can you explain why it did ? y is literally a tensor!

It was not clear in the original code gist what is onehot_encoded

onehot_encoded is a numpy array generated from sklearn.preprocessing.OneHotEncoder

I’ve ran in to similar problems before when part of the input is not a Tensor. I think builds a graph and might have problems with NumPy arrays. What happens if you wrap your function in tf.numpy_function: tf.numpy_function  |  TensorFlow Core v2.8.0


Yes you cannot use .numpy as you are in graph mode there.

1 Like