Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in AI and Deep Learning by (50.2k points)

I'm creating a very basic AI with Tensorflow, and am using the code from the official docs/tutorial. Here's my full code:

from __future__ import absolute_import, division, print_function
import tensorflow as tf from tensorflow import keras import matplotlib.pyplot as plt fashion_mnist = keras.datasets.fashion_mnist (train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'] train_images = train_images / 255.0 train_labels = train_labels / 255.0 plt.figure(figsize=(10,10)) for i in range(25): plt.subplot(5,5,i+1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[i], plt.xlabel(class_names[train_labels[i]])
The issue is on this line:
plt.xlabel(class_names[train_labels[i]]) TypeError: list indices must be integers or slices, not numpy.float64
No problem, change the numpy.float64 to int using .item()
plt.xlabel(class_names[train_labels[i.item()]]) AttributeError: 'int' object has no attribute 'item'

Was it an int in the first place?

This is running on Python 3.7, with Tensorflow 1.13.1.


1 Answer

0 votes
by (119k points)

The error is because of the following python line:

train_labels = train_labels / 255.0

Here you have divided by 255.0 to scale the values so that it reduces the computational time. This is called Normalization. You need to do normalization only for train_images but not for train_labels.  So, remove that mentioned line above and it will work fine.

You can register for this AI Course by Intellipaat that teaches Artificial Intelligence in TensorFlow with Instructor-led training, good projects, and case studies.

Browse Categories