accuracy value per class tensorflow

Viewed 31

Hi im using binary alpha digits dataset from tensorflow and I need to find the accuracy value per class(labels). However, evaluate function only gives me the overall accuracy.

from matplotlib import pyplot as plt
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)
result = model.evaluate(test_ds)

Appreciate if anyone can help with this thank you.

0 Answers
Related