0% found this document useful (0 votes)
0 views

Perceptron_TensorFlow_Keras

This document outlines the steps to implement a Perceptron model using TensorFlow/Keras on the MNIST dataset. It includes importing libraries, loading and preprocessing the dataset, defining the model architecture, compiling, training, and evaluating the model. The expected output shows the training progress and final test accuracy.

Uploaded by

sfaritha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Perceptron_TensorFlow_Keras

This document outlines the steps to implement a Perceptron model using TensorFlow/Keras on the MNIST dataset. It includes importing libraries, loading and preprocessing the dataset, defining the model architecture, compiling, training, and evaluating the model. The expected output shows the training progress and final test accuracy.

Uploaded by

sfaritha
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

PERCEPTRON IMPLEMENTATION IN TENSORFLOW/KERAS

Step 1: Import the necessary libraries

# Importing modules
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt

from tensorflow.keras.models import Sequential


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

Step 2: Download the dataset

# TensorFlow allows us to read the MNIST dataset and we can load it


directly in the program as a train and test dataset
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

Step 3: Convert pixels into floating-point values

# Cast the records into float values


x_train = x_train.astype('float32')
x_test = x_test.astype('float32')

Step 4: Normalize the pixel values

# Normalize data to range [0, 1]


x_train /= 255.0
x_test /= 255.0

Step 5: Define the Perceptron model

# Define a simple perceptron model


model = Sequential()
model.add(Flatten(input_shape=(28, 28))) # Input layer
model.add(Dense(128, activation='relu')) # Hidden layer
model.add(Dense(10, activation='softmax')) # Output layer
Step 6: Compile the model

model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])

Step 7: Train the model

model.fit(x_train, y_train, epochs=5, validation_data=(x_test, y_test))

Step 8: Evaluate the model

test_loss, test_acc = model.evaluate(x_test, y_test)


print('Test accuracy:', test_acc)

Expected Output:
Epoch 1/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.2633 -
accuracy: 0.9243 - val_loss: 0.1380 - val_accuracy: 0.9585
...
Epoch 5/5
1875/1875 [==============================] - 4s 2ms/step - loss: 0.0891 -
accuracy: 0.9729 - val_loss: 0.0863 - val_accuracy: 0.9737

Test accuracy: 0.9737

You might also like