Single and Multi layer
# -*- coding: utf-8 -*-
"""Single and Multi Layer Perceptron.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1sUwF-aPoUh-5eiqZRHDTrFQqz01k8xZs
"""
# importing modules
import tensorflow as tf
import numpy as np
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten
from tensorflow.keras.layers import Dense
from tensorflow.keras.layers import Activation
import matplotlib.pyplot as plt
import plotly.express as px
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()
# Cast the records into float values
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# normalize image pixel values by dividing
# by 255
gray_scale = 255
x_train /= gray_scale
x_test /= gray_scale
print("Feature matrix:", x_train.shape)
print("Target matrix:", x_test.shape)
print("Feature matrix:", y_train.shape)
print("Target matrix:", y_test.shape)
fig, ax = plt.subplots(10, 10)
k = 0
for i in range(10):
for j in range(10):
ax[i][j].imshow(x_train[k].reshape(28, 28),
aspect='auto')
k += 1
plt.show()
single_layer_model= Sequential([
# reshape 28 row * 28 column data to 28*28 rows
Flatten(input_shape=(28, 28)),
# dense layer 1
Dense(256, activation='sigmoid'),
])
multi_layer_model= Sequential([
# reshape 28 row * 28 column data to 28*28 rows
Flatten(input_shape=(28, 28)),
# dense layer 1
Dense(256, activation='sigmoid'),
# dense layer 2
Dense(128, activation='sigmoid'),
# output layer
Dense(10, activation='sigmoid'),
])
single_layer_model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
multi_layer_model.compile(optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy'])
singleresults = single_layer_model.evaluate(x_test, y_test, verbose = 0)
multiresults = multi_layer_model.evaluate(x_test, y_test, verbose = 0)
#print('test loss, test acc:', results)
print("Single layer Accuracy:",singleresults[1]*100)
print("Multi layer Accuracy:",multiresults[1]*100)
sm= [singleresults[1],multiresults[1]]
plt.bar(['Single Layer', 'Multi Layer'],sm, width=0.4 ,color=['r','y'])
plt.plot(singleresults[1],linestyle = 'dashed')
plt.show()
Comments
Post a Comment