0% found this document useful (0 votes)
4 views4 pages

Neural Network

The document outlines the process of building a neural network using TensorFlow to classify handwritten digits from the MNIST dataset. It includes steps for data preparation, model architecture definition, training, evaluation, and visualization of predictions. The model achieves a test accuracy of approximately 99% after training for 10 epochs.

Uploaded by

Latheesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views4 pages

Neural Network

The document outlines the process of building a neural network using TensorFlow to classify handwritten digits from the MNIST dataset. It includes steps for data preparation, model architecture definition, training, evaluation, and visualization of predictions. The model achieves a test accuracy of approximately 99% after training for 10 epochs.

Uploaded by

Latheesh Kumar
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

9. Build a neural network that will read the image of a digit and correctly identify the number.

# Import necessary libraries


import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, Flatten, Conv2D, MaxPooling2D
from tensorflow.keras.utils import to_categorical
import matplotlib.pyplot as plt
import numpy as np
# Load MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
# Reshape and normalize input data
X_train = X_train.reshape(60000, 28, 28, 1).astype('float32') / 255.0
X_test = X_test.reshape(10000, 28, 28, 1).astype('float32') / 255.0
# Convert class labels to categorical labels
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)
# Define neural network architecture
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(10, activation='softmax'))
# Compile model
model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy'])
# Train model
model.fit(X_train, y_train, batch_size=128, epochs=10, verbose=1, validation_data=(X_test,
y_test))

# Evaluate model
test_loss, test_acc = model.evaluate(X_test, y_test)
print(f'Test accuracy: {test_acc:.2f}')

# Use model for predictions


predictions = model.predict(X_test)

# Plot sample images with predictions


fig, axs = plt.subplots(3, 3, figsize=(10, 10))
for i in range(9):
axs[i//3, i%3].imshow(X_test[i].reshape(28, 28), cmap='gray')
axs[i//3, i%3].set_title(f'Predicted: {np.argmax(predictions[i])}')
plt.show()
Output:
Epoch 1/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 32s 66ms/step - accuracy: 0.8252 -


loss: 0.5836 - val_accuracy: 0.9734 - val_loss: 0.0865
Epoch 2/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 31s 65ms/step - accuracy: 0.9609 -


loss: 0.1333 - val_accuracy: 0.9799 - val_loss: 0.0608
Epoch 3/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 32s 68ms/step - accuracy: 0.9717 -


loss: 0.0970 - val_accuracy: 0.9820 - val_loss: 0.0490
Epoch 4/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 41s 67ms/step - accuracy: 0.9753 -


loss: 0.0803 - val_accuracy: 0.9834 - val_loss: 0.0469
Epoch 5/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 30s 64ms/step - accuracy: 0.9793 -


loss: 0.0699 - val_accuracy: 0.9842 - val_loss: 0.0439
Epoch 6/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 41s 65ms/step - accuracy: 0.9809 -


loss: 0.0616 - val_accuracy: 0.9858 - val_loss: 0.0408
Epoch 7/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 30s 64ms/step - accuracy: 0.9822 -


loss: 0.0575 - val_accuracy: 0.9865 - val_loss: 0.0397
Epoch 8/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 31s 67ms/step - accuracy: 0.9840 -


loss: 0.0497 - val_accuracy: 0.9882 - val_loss: 0.0363
Epoch 9/10

469/469 ━━━━━━━━━━━━━━━━━━━━ 42s 69ms/step - accuracy: 0.9857 -


loss: 0.0484 - val_accuracy: 0.9888 - val_loss: 0.0342
Epoch 10/10
469/469 ━━━━━━━━━━━━━━━━━━━━ 30s 64ms/step - accuracy: 0.9870 -
loss: 0.0430 - val_accuracy: 0.9873 - val_loss: 0.0373

313/313 ━━━━━━━━━━━━━━━━━━━━ 1s 5ms/step - accuracy: 0.9844 - loss:


0.0462
Test accuracy: 0.99

313/313 ━━━━━━━━━━━━━━━━━━━━ 2s 5ms/step

You might also like