getting prediction accuracy by labels and getting value from tf.Tensor

Viewed 36

I am trying to loop through the predicted value with the test label to compare the accuracy of each label but i am unable to obtain 'label' values

import tensorflow as tf
from tensorflow.keras import layers
import tensorflow_datasets as tfds

train_ds, test_ds = tfds.load('BinaryAlphaDigits', 
                              split=['train[:60%]', 'train[60%:]'])
def preprocess(data):
    image = data['image']
    image = tf.image.resize(image, (28, 28))
    label = data['label']
    return image, label

train_ds = train_ds.map(preprocess)
train_ds = train_ds.shuffle(1024)
train_ds = train_ds.batch(batch_size = 32)

test_ds = test_ds.map(preprocess)
test_ds = test_ds.batch(batch_size = 32)

model = tf.keras.Sequential()
model.add(layers.Flatten(input_shape=(28, 28,1)))      
model.add(layers.Dense(10, activation=tf.nn.relu))    
model.add(layers.Dense(10, activation=tf.nn.relu))    
model.add(layers.Dense(39, activation=tf.nn.softmax))   

model.compile(optimizer= tf.optimizers.Adam(), 
              loss='sparse_categorical_crossentropy',
              metrics=['accuracy'])
epochs = 20
history = model.fit(train_ds, epochs=epochs)
class_name = [0,1,2,3,4,5,6,7,8,9,'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
prediction = model.predict(test_ds)

im trying to loop through this prediction and test_ds together to compare the labels from test_ds but not sure how to go from here as i cant loop through test_ds

this is what I have so far: (I have not written code to track the accuracy for different labels yet as I want to resolve this issue first)

track = 0
total = 0
for x in test_ds:
  label_list = x[1]
  for y in x[1]:
    if np.argmax(prediction[track]) == y:
      total += 1
      track+=1

I know that my y looks like this but i cant find solution online that would help me get the value of y

tf.Tensor(20, shape=(), dtype=int64)
0 Answers
Related