import tensorflow as tf
import matplotlib.pyplot as plt
import pandas as pd
import time

# MNIST loading
train = pd.read_csv("mnist_train(1).csv")
test = pd.read_csv("mnist_test(1).csv")

# Fashion-MNIST loading: Replace the above filenames with:
# train = pd.read_csv("fashion-mnist_train.csv")
# test = pd.read_csv("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)

experiments = [
    ('relu',128,64),
    ('tanh',128,64),
    ('relu',64,32)
]

histories = []
times = []

for act,a,b in experiments:
    model = tf.keras.Sequential([
        tf.keras.layers.Flatten(input_shape=(28,28)),
        tf.keras.layers.Dense(a,activation=act),
        tf.keras.layers.Dense(b,activation=act),
        tf.keras.layers.Dense(10,activation='softmax')
    ])

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

    start = time.time()
    h = model.fit(x_train,y_train,epochs=2,batch_size=32,validation_split=0.1,verbose=1)
    elapsed = time.time()-start

    histories.append(h)
    times.append(elapsed)

    loss,acc = model.evaluate(x_test,y_test,verbose=0)
    print(act,a,b,"Test Accuracy:",acc)
    print("Training Time:",round(elapsed,2),"seconds")

labels = ['ReLU 128-64','Tanh 128-64','ReLU 64-32']

# Training and Validation Accuracy
for h,label in zip(histories,labels):
    plt.plot(h.history['accuracy'],label='Train '+label)
    plt.plot(h.history['val_accuracy'],label='Validation '+label)
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='Train '+label)
    plt.plot(h.history['val_loss'],label='Validation '+label)
plt.title("Training and Validation Loss")
plt.xlabel("Epochs")
plt.ylabel("Loss")
plt.legend()
plt.grid()
plt.show()

# Training Time Comparison
plt.bar(labels,times)
plt.title("Training Time Comparison")
plt.xlabel("Model")
plt.ylabel("Time (seconds)")
plt.show()