AI in Hc -5
AI in Hc -5
Theory:-
Neural networks consist of input and output layers, as well as (in
most cases) a hidden layer consisting of units that transform the
input into something that the output layer can use.
They are excellent tools for finding patterns that are far too
complex or numerous for a human programmer to extract and
teach the machine to recognize.
This Machine Learning Project Classifies Clothes from the
Fashion MNIST Data set using Artificial Neural Networks and
Python.
Let’s start by importing the libraries we need for this task
import tensorflow as tf
from tensorflow import keras
import numpy as np
import matplotlib.pyplot as plt
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
print(trainImages.shape)
print(testImages.shape)
#Output
model =
keras.Sequential([ keras.layers.Flatten(input_sh
ape=(28,28)),
keras.layers.Dense(128, activation=tf.nn.relu)
keras.layers.Dense(10,activation=tf.nn.softmax)
model.compile(optimizer = 'adam',loss =
'sparse_categorical_crossentropy',metrics=['accuracy'])
To Train the model
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss:
3.6150 - accuracy: 0.6802
Epoch 2/5
1875/1875 [==============================] - 4s 2ms/step - loss:
0.7296 - accuracy: 0.7488
Epoch 3/5
1875/1875 [==============================] - 4s 2ms/step - loss:
0.6374 - accuracy: 0.7725
Epoch 4/5
1875/1875 [==============================] - 4s 2ms/step - loss:
0.5873 - accuracy: 0.7906
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss:
0.5579 - accuracy: 0.7993
<tensorflow.python.keras.callbacks.History at 0x7f1108dc3588>
To Make a Prediction
predictions = model.predict(testImages[0:5])
# Print the predicted labels
print(predictions)
#Output
2
[[1.74235439e-07 2.69071290e-08 6.66509115e-20 3.09463957e-07
3
1.11526007e-20 1.34603798e-01 8.10060641e-08 7.74199590e-02
4
3.87958280e-05 7.87936807e-01]
5
[2.89689321e-02 1.06601091e-02 6.28736615e-01 2.77338717e-02
6
1.61624148e-01 1.49910515e-02 8.56256112e-02 1.23378839e-02
7
2.35275514e-02 5.79410419e-03]
8
[6.75366528e-06 9.99993205e-01 4.27281517e-12 2.68350314e-10
9
8.65088672e-16 1.05001736e-14 1.33745196e-12 0.00000000e+00
10
1.84386378e-11 0.00000000e+00]
11
[6.56618613e-06 9.99993443e-01 1.46741508e-11 1.80866895e-08
12
7.95811239e-14 1.56570215e-16 5.96713607e-12 0.00000000e+00
13
3.94146077e-10 0.00000000e+00]
14
[2.19924763e-01 1.00887669e-02 1.99720263e-01 6.23517819e-02
15
4.97664846e-02 3.40277069e-07 4.30076748e-01 7.25772731e-09
16
2.80708820e-02 2.27675168e-09]]
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]
for i in range(0,5):
plt.imshow(testImages[i], cmap='gray')
plt.show()