import tensorflow as tf
import matplotlib.pyplot as plt
import pandas as pd
from tensorflow.keras import regularizers
# Load MNIST dataset
train = pd.read_csv("mnist_train(1).csv") # Fashion: "fashion-mnist_train.csv"
test = pd.read_csv("mnist_test(1).csv") # Fashion: "fashion-mnist_test.csv"
x_train = train.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
y_train = train.iloc[:,0].values
x_test = test.iloc[:,1:].values.astype('float32').reshape(-1,28,28)/255.0
y_test = test.iloc[:,0].values
print("Training:",x_train.shape)
print("Testing:",x_test.shape)
# Model 1: No Regularization
m1 = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(128,activation='relu'),
    tf.keras.layers.Dense(10,activation='softmax')
])
# Model 2: L2 Regularization
m2 = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(128,activation='relu',kernel_regularizer=regularizers.l2(0.001)),
    tf.keras.layers.Dense(10,activation='softmax')
])
# Model 3: Dropout Regularization
m3 = tf.keras.Sequential([
    tf.keras.layers.Flatten(input_shape=(28,28)),
    tf.keras.layers.Dense(128,activation='relu'),
    tf.keras.layers.Dropout(0.5),
    tf.keras.layers.Dense(10,activation='softmax')
])
histories = []
for model in [m1,m2,m3]:
    model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])
    h = model.fit(x_train,y_train,epochs=5,validation_split=0.2,batch_size=32,verbose=0)
    histories.append(h)
labels = ['No Reg','L2','Dropout']
# Validation Accuracy Comparison
for h,label in zip(histories,labels):
    plt.plot(h.history['val_accuracy'],label=label)
plt.title("Validation Accuracy Comparison")
plt.xlabel("Epochs")
plt.ylabel("Validation Accuracy")
plt.legend()
plt.grid()
plt.savefig("practical3_output.png")
plt.show()
# Training and Validation Accuracy
for h,label in zip(histories,labels):
    plt.plot(h.history['accuracy'],label=label+' Train')
    plt.plot(h.history['val_accuracy'],label=label+' Val')
plt.title("Training and Validation Accuracy")
plt.xlabel("Epochs")
plt.ylabel("Accuracy")
plt.legend()
plt.grid()
plt.show()
# Training and Validation Loss
for h,label in zip(histories,labels):
    plt.plot(h.history['loss'],label=label+' Train')
    plt.plot(h.history['val_loss'],label=label+' Val')
plt.title("Training and Validation Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.grid()
plt.savefig("practical3_loss.png")
plt.show()
# Evaluate all models on test data
for model,label in zip([m1,m2,m3],labels):
    loss,acc = model.evaluate(x_test,y_test,verbose=0)
    print(label,"Test Accuracy:",acc*100,"%")
