Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Patch 1 #1

Merged
merged 10 commits into from
May 10, 2022
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Update mlp_kmnist.py
  • Loading branch information
anubhav2901 authored May 10, 2022
commit 93c71951158670f90a5ae1cda1c691319bd8b2a6
27 changes: 19 additions & 8 deletions models/mlp_kmnist.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,26 @@
import tensorflow as tf
from keras.layers import Dense

import tensorflow_datasets as tfds

import matplotlib.pyplot as plt

#testing flooding for Fashion-MNIST
#testing flooding for KMNIST

#import the dataset

print("Testing benefits of Flooding for MLP trained on Fashion-MNIST dataset\n")
print("Testing benefits of Flooding for MLP trained on KMNIST dataset\n")

dstrain = tfds.as_numpy(tfds.load('kmnist', split='train', batch_size=-1))
x_train = dstrain['image']
y_train = dstrain['label']


dstest = tfds.as_numpy(tfds.load('kmnist', split='test', batch_size=-1))
x_test = dstest['image']
y_test = dstest['label']

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.fashion_mnist.load_data()
(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

print("Shape of x_train: {} y_train: {}".format(x_train.shape, y_train.shape))
print("Shape of x_test: {} y_test: {}".format(x_test.shape, y_test.shape))
Expand Down Expand Up @@ -45,7 +56,7 @@


#set flood value
b = 0.09 #selecting value of b from {0.01 .. 0.1}
b = 0.01 #selecting value of b from {0.01 .. 0.1}

#for categorical crossentropy
def flood_categorical_crossentropy(y_true, y_pred):
Expand All @@ -54,11 +65,11 @@ def flood_categorical_crossentropy(y_true, y_pred):
return loss

SGD = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9,)
model.compile(loss=flood_loss, optimizer=SGD, metrics=["mse", "acc"])
model.compile(loss=flood_categorical_crossentropy, optimizer=SGD, metrics=["mse", "acc"])
history = model.fit(x_train, y_train, epochs=100, validation_data=(x_test, y_test))
model.evaluate(x_test, y_test, verbose=2)

#plot loss and accuracy
#plot loss
'''
fig = plt.figure()
plt.plot(history.history['loss'], label="Train loss")
Expand All @@ -74,11 +85,11 @@ def flood_categorical_crossentropy(y_true, y_pred):
model1.add(Dense(num_classes, activation="softmax"))

SGD = tf.keras.optimizers.SGD(learning_rate=0.1, momentum=0.9,)
model1.compile(loss="categorical_crossentropy", optimizer=SGD, metrics=["mse", "acc"]) #using categorical_crossentropy loss
model1.compile(loss="categorical_crossentropy", optimizer=SGD, metrics=["mse", "acc"]) #using categorical_crossentropy loss
history1 = model1.fit(x_train, y_train, epochs=100, validation_data=(x_test, y_test))
model1.evaluate(x_test, y_test, verbose=2)

#plot loss and accuracy
#plot loss
'''
fig = plt.figure()
plt.plot(history1.history['loss'], label="Train loss")
Expand Down