Artificial neural networks are one of the main tools utilized in machine learning. As the “neural” part of their name suggests, they are brain-inspired systems which are expected to replicate the way that we humans learn.
Neural networks comprise of input and output layers, as well as (in most cases) a hidden layer comprising of units that change the input into something that the output layer can use.
They are superb tools for finding patterns which are very complicated or numerous for a human programmer to extract and teach the machine to perceive.
This Machine Learning Project categorize Clothes from the Fashion MNIST Data set using Artificial Neural Networks and Python.
Let’s begin by importing the libraries we want for this task.
import tensorflow as tf from tensorflow import keras import numpy as np import matplotlib.pyplot as plt
To load the data set
fashion = keras.datasets.fashion_mnist (trainImages, trainLabels), (testImages, testLabels) = fashion.load_data() imgIndex = 0 img = trainImages[imgIndex] print("Image Label :",trainLabels[imgIndex]) plt.imshow(img) #Output Image Label : 9 <matplotlib.image.AxesImage at 0x7f1111a06d68>
To print the shape of the training and testing data
print(trainImages.shape) print(testImages.shape) #Output (60000, 28, 28) (10000, 28, 28)
Now let’s create a Neural Network
model = keras.Sequential([ keras.layers.Flatten(input_shape=(28,28)), keras.layers.Dense(128, activation=tf.nn.relu), keras.layers.Dense(10, activation=tf.nn.softmax) ])
To Compile the Model
model.compile(optimizer = 'adam', loss = 'sparse_categorical_crossentropy', metrics=['accuracy']) model.fit(trainImages, trainLabels, epochs=5, batch_size=32)
To Evaluate the Model
model.evaluate(testImages, testLabels) #Output 313/313 [==============================] - 0s 1ms/step - loss: 0.5916 - accuracy: 0.7981 [0.5915989279747009, 0.7980999946594238]
To Make a Prediction
predictions = model.predict(testImages[0:5]) # Print the predicted labels print(predictions)
To print the maximum labels
print(np.argmax(predictions, axis=1)) # Print the actual label values print(testLabels[0:5]) #Output [9 2 1 1 6] [9 2 1 1 6]
To Print the first 5 images
for i in range(0,5): plt.imshow(testImages[i], cmap='gray') plt.show()