In this lecture, we will build a neural net and train it to classify images, based on MNIST fashion dataset
import gzip #used to uncomress the zip files
import numpy as np
import matplotlib.pyplot as plt
from tensorflow import keras
from tensorflow.keras import layers
# function to load images
def load_images(path):
with gzip.open(path, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=16)
return data.reshape(-1, 28, 28)
def load_labels(path):
with gzip.open(path, 'rb') as f:
data = np.frombuffer(f.read(), np.uint8, offset=8)
return data
x_train = load_images('data/train-images-idx3-ubyte.gz')
y_train = load_labels('data/train-labels-idx1-ubyte.gz')
x_test = load_images('data/t10k-images-idx3-ubyte.gz')
y_test = load_labels('data/t10k-labels-idx1-ubyte.gz')
print(x_train.shape, y_train.shape)
print(x_test.shape, y_test.shape)
# see the data
'''
class_names = [
"T-shirt/top", "Trouser", "Pullover", "Dress", "Coat",
"Sandal", "Shirt", "Sneaker", "Bag", "Ankle boot"
]
# Plot a 5×5 grid of training images
plt.figure(figsize=(8, 8))
for i in range(25):
plt.subplot(5, 5, i + 1)
plt.xticks([]); plt.yticks([]) # no axis ticks
plt.grid(False)
plt.imshow(x_train[i], cmap='gray') # grayscale
plt.xlabel(class_names[y_train[i]])
plt.suptitle("Fashion-MNIST training samples", fontsize=14)
plt.show()
'''
# -------------------------
# 3. Build the model
# -------------------------
model = keras.Sequential([
layers.Flatten(input_shape=(28, 28)),
layers.Dense(128, activation='relu'),
layers.Dropout(0.2),
layers.Dense(10, activation='softmax')
])
# -------------------------
# 4. Compile the model
# -------------------------
model.compile(
optimizer='adam',
loss='sparse_categorical_crossentropy',
metrics=['accuracy']
)
# 2. Print initial weights (before training)
print("=== Weights BEFORE training ===")
for layer in model.layers:
weights = layer.get_weights()
print(f"Layer: {layer.name}")
for w in weights:
print(w)
print("-" * 40)
# -------------------------
# 5. Train the model
# -------------------------
history = model.fit(
x_train, y_train,
epochs=10,
batch_size=32,
validation_split=0.1,
verbose=1
)
# -------------------------
# 6. Evaluate
# -------------------------
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print(f"\n✅ Test accuracy: {test_acc:.3f}")
# 4. Print weights after training
print("=== Weights AFTER training ===")
for layer in model.layers:
weights = layer.get_weights()
print(f"Layer: {layer.name}")
for w in weights:
print(w)
print("-" * 40)
# -------------------------
# 7. Save model
# -------------------------
model.save("fashion_mnist_model.keras")
print('acuracy\n')
print(history.history['accuracy'])
print('val accuracy\n')
print(history.history['val_accuracy'])
# -------------------------
# 8. Plot training progress
# -------------------------
plt.figure(figsize=(10,4))
plt.subplot(1,2,1)
plt.plot(history.history['accuracy'], label='Training Accuracy')
plt.plot(history.history['val_accuracy'], label='Validation Accuracy')
plt.title('Model Accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.legend()
plt.show()
We can train a model for a number of epochs, save it. Then we can load the saved model and continue to train it for another number of epochs. The listing below illustrates how this can be done.
# --- First training session ---
history1 = model.fit(X_train, y_train, epochs=50, validation_data=(X_val, y_val))
model.save("my_model.keras")
# --- Later training continuation ---
model2 = keras.models.load_model("my_model.keras")
history2 = model2.fit(X_train, y_train,
epochs=100, # total target epochs
initial_epoch=50, # continue from where we left off
validation_data=(X_val, y_val))
Training data contain 4 files:
train-images-idx3-ubyte
train-labels-idx1-ubyte
t10k-images-idx3-ubyte
t10k-labels-idx1-ubyte