Skip to content

Commit

Permalink
Create mnist
Browse files Browse the repository at this point in the history
  • Loading branch information
Mattral authored May 4, 2024
1 parent e744de5 commit 5a8f63e
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions mnist
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import tensorflow as tf
from tensorflow.keras.losses import SparseCategoricalCrossentropy
from tensorflow.keras.metrics import SparseCategoricalAccuracy
from tensorflow.keras.optimizers import Adam
from tensorflow.keras.datasets import mnist
from KANtf import KAN

# Load and preprocess data
(x_train, y_train), (x_val, y_val) = mnist.load_data()
x_train, x_val = (x_train / 255.0 - 0.5).astype('float32'), (x_val / 255.0 - 0.5).astype('float32') # Normalize
x_train, x_val = x_train.reshape(-1, 784), x_val.reshape(-1, 784) # Flatten

# Create TensorFlow datasets
train_ds = tf.data.Dataset.from_tensor_slices((x_train, y_train)).shuffle(10000).batch(64)
val_ds = tf.data.Dataset.from_tensor_slices((x_val, y_val)).batch(64)

# Define the KAN model
model = KAN([
{'in_features': 784, 'out_features': 64, 'grid_size': 5, 'spline_order': 3, 'activation': 'silu'},
{'in_features': 64, 'out_features': 10, 'grid_size': 5, 'spline_order': 3, 'activation': 'silu'}
])

# Compile the model with optimizer and loss function
model.compile(optimizer=Adam(learning_rate=1e-3), loss=SparseCategoricalCrossentropy(from_logits=True))

# Metrics
train_loss = tf.keras.metrics.Mean(name='train_loss')
val_loss = tf.keras.metrics.Mean(name='val_loss')
val_accuracy = SparseCategoricalAccuracy(name='val_accuracy')

# Lists to store metrics for plotting
epoch_train_loss = []
epoch_val_loss = []
epoch_val_accuracy = []

for epoch in range(epochs):
train_loss.reset_states()
val_loss.reset_states()
val_accuracy.reset_states()

# Existing training and validation loop here ...

# Append metrics after each epoch
epoch_train_loss.append(train_loss.result().numpy())
epoch_val_loss.append(val_loss.result().numpy())
epoch_val_accuracy.append(val_accuracy.result().numpy())

print(f'Epoch {epoch + 1}, Train Loss: {train_loss.result():.4f}, Validation Loss: {val_loss.result():.4f}, Validation Accuracy: {val_accuracy.result():.4f}')

# After training, plot the metrics
plt.figure(figsize=(8, 4))
plt.subplot(1, 2, 1)
plt.plot(epoch_train_loss, label='Train Loss')
plt.plot(epoch_val_loss, label='Validation Loss')
plt.title('Training and Validation Loss')
plt.xlabel('Epoch')
plt.ylabel('Loss')
plt.legend()

plt.subplot(1, 2, 2)
plt.plot(epoch_val_accuracy, label='Validation Accuracy')
plt.title('Validation Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()

plt.show()

0 comments on commit 5a8f63e

Please sign in to comment.