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

Ex 5

The document outlines a Python implementation of a Pattern Recognition System using an autoencoder model on the MNIST dataset. It details the steps for data preparation, model building, training, and visualization of original and reconstructed images. The code successfully executes the pattern recognition task, demonstrating the effectiveness of the algorithm.

Uploaded by

amirthamm2083
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 views3 pages

Ex 5

The document outlines a Python implementation of a Pattern Recognition System using an autoencoder model on the MNIST dataset. It details the steps for data preparation, model building, training, and visualization of original and reconstructed images. The code successfully executes the pattern recognition task, demonstrating the effectiveness of the algorithm.

Uploaded by

amirthamm2083
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/ 3

EX.

NO: PATTERN RECOGNITION SYSTEM


DATE:

AIM:
To write the python code for Pattern Recognition System by utilizing training algorithm for
pattern association in images.

ALGORITHM:

STEP 1: Import the necessary libraries (numpy, matplotlib.pyplot, and tensorflow.keras).

STEP 2: Load the MNIST dataset using mnist.load_data() and split it into training and testing sets.

STEP 3: Normalize the image data by converting pixel values to the range [0, 1] using astype('float32') /
255.0.

STEP 4: Flatten the 28x28 images into 1D vectors of size 784 using reshape().

STEP 5: Build an autoencoder model using Sequential:

5.1 Add a Dense layer with 64 neurons and ReLU activation (Encoder).
5.2 Add a Dense layer with 784 neurons and Sigmoid activation (Decoder).

STEP 6: Compile the model using the Adam optimizer and Binary Crossentropy loss function.

STEP 7: Train the model using fit() with:

7.1 10 epochs
7.2 Batch size of 256
7.3 Validation data from the test set

STEP 8: Predict the reconstructed images using autoencoder.predict() on the test data.

STEP 9: Visualize the original and reconstructed images using matplotlib.pyplot with a 2-row layout.

STEP 10: Display the images using plt.show().

PROGRAM:
import numpy as np

import matplotlib.pyplot as plt

from tensorflow.keras.datasets import mnist

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Dense, Flatten, Reshape

# Load the MNIST dataset

CSE/NM/NN&DL / 513322104003/DHIVYA G
(x_train, _), (x_test, _) = mnist.load_data()

x_train = x_train.astype('float32') / 255.0 # Normalize to [0, 1]

x_test = x_test.astype('float32') / 255.0

# Flatten the images

x_train = x_train.reshape((len(x_train), 28 * 28))

x_test = x_test.reshape((len(x_test), 28 * 28))

# Build the autoencoder model

autoencoder = Sequential()

autoencoder.add(Dense(64, activation='relu', input_shape=(28 * 28,))) # Encoder

autoencoder.add(Dense(28 * 28, activation='sigmoid')) # Decoder

# Compile the model

autoencoder.compile(optimizer='adam', loss='binary_crossentropy')

# Train the model

autoencoder.fit(x_train, x_train, epochs=10, batch_size=256, shuffle=True, validation_data=(x_test,


x_test))

# Use the autoencoder to predict the test set

decoded_imgs = autoencoder.predict(x_test)

# Visualize the original and reconstructed images

n = 5 # Number of images to display

plt.figure(figsize=(20, 4))

for i in range(n):

# Display original

ax = plt.subplot(2, n, i + 1)

plt.imshow(x_test[i].reshape(28, 28), cmap='gray')

plt.axis('off')

# Display reconstruction

ax = plt.subplot(2, n, i + 1 + n)

CSE/NM/NN&DL / 513322104003/DHIVYA G
plt.imshow(decoded_imgs[i].reshape(28, 28), cmap='gray')

plt.axis('off')

plt.show()

OUTPUT:

RESULT:
Thus, the python code for Pattern Recognition System is executed successfully.

CSE/NM/NN&DL / 513322104003/DHIVYA G

You might also like