Jovian
⭐️
Sign In
In [1]:
import numpy as np
import keras
from keras.datasets import mnist
(train_images, train_labels), (test_images, test_labels) = mnist.load_data()
Using TensorFlow backend.
In [2]:
train_images[0].shape, train_labels[0]
Out[2]:
((28, 28), 5)
In [3]:
%matplotlib inline
import matplotlib.pyplot as plt

grid_size = 6
f, axarr = plt.subplots(grid_size, grid_size)
for i in range(grid_size):
    for j in range(grid_size):
        ax = axarr[i, j]
        ax.get_xaxis().set_visible(False)
        ax.get_yaxis().set_visible(False)
        ax.imshow(train_images[i * grid_size + j], cmap='gray')
Notebook Image
In [4]:
train_images = train_images.reshape((60000, 28, 28, 1))
train_images = train_images.astype('float32') / 255
test_images = test_images.reshape((10000, 28, 28, 1))
test_images = test_images.astype('float32') / 255

from keras.utils import to_categorical

partial_train_images = train_images[:45000]
partial_train_labels = train_labels[:45000]

validation_images = train_images[45000:]
validation_labels = train_labels[45000:]

partial_train_labels = to_categorical(partial_train_labels)
validation_labels = to_categorical(validation_labels)
test_labels = to_categorical(test_labels)
In [5]:
input_shape = (28,28,1)
num_classes = 10
In [6]:
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout

model = Sequential()
model.add(Conv2D(16, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1)))
model.add(Conv2D(16, kernel_size=(3, 3), activation='relu', input_shape=(28,28,1)))
model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Flatten())
model.add(Dense(32, activation='relu'))
model.add(Dense(num_classes, activation='softmax'))

model.summary()
_________________________________________________________________ Layer (type) Output Shape Param # ================================================================= conv2d_1 (Conv2D) (None, 26, 26, 16) 160 _________________________________________________________________ conv2d_2 (Conv2D) (None, 24, 24, 16) 2320 _________________________________________________________________ max_pooling2d_1 (MaxPooling2 (None, 12, 12, 16) 0 _________________________________________________________________ flatten_1 (Flatten) (None, 2304) 0 _________________________________________________________________ dense_1 (Dense) (None, 32) 73760 _________________________________________________________________ dense_2 (Dense) (None, 10) 330 ================================================================= Total params: 76,570 Trainable params: 76,570 Non-trainable params: 0 _________________________________________________________________
In [7]:
from keras import optimizers

model.compile(optimizers.Adam(lr=0.005), loss='categorical_crossentropy', metrics=['accuracy'])
In [18]:
import jovian 

jovian.log_hyperparams({
    'arch': 'Conv(16+16)+Dense(32)',
    'epochs': 2,
    'optimizer': 'Adam',
    'lr': 0.005
})
[jovian] Hypermaters logged.
In [8]:
history = model.fit(
    partial_train_images, 
    partial_train_labels, 
    epochs=2, 
    batch_size=128, 
    validation_data=(validation_images, validation_labels))
Train on 45000 samples, validate on 15000 samples Epoch 1/2 45000/45000 [==============================] - 157s 3ms/step - loss: 0.1940 - acc: 0.9389 - val_loss: 0.0841 - val_acc: 0.9751 Epoch 2/2 45000/45000 [==============================] - 148s 3ms/step - loss: 0.0596 - acc: 0.9815 - val_loss: 0.0720 - val_acc: 0.9776
In [19]:
jovian.log_metrics({
    'loss': 0.0558,
    'acc': 0.9826,
    'val_loss': 0.0683,
    'val_acc': 0.9788
})
[jovian] Metrics logged.
In [17]:
import matplotlib.pyplot as plt
acc=history.history['acc']
loss=history.history['loss']
val_acc=history.history['val_acc']
val_loss=history.history['val_loss']
epochs=range(1,len(acc)+1)
plt.plot(epochs,acc,'bo',label='Training acc')
plt.plot(epochs,val_acc,'b',label='Validation acc')
plt.title('Training and Validation accuracy')
plt.xlabel('Epochs')
plt.ylabel('Accuracy')
plt.margins(0.05)
plt.figure()
plt.plot(epochs,loss,'bo',label='Training loss')
plt.plot(epochs,val_loss,'b',label='Validation loss')
plt.title('Training and Validation loss')
plt.xlabel('Epochs')
plt.ylabel('loss')
plt.margins(0.05)
plt.show()
Notebook Image
Notebook Image
In [10]:
import jovian
In [ ]:
jovian.commit()
[jovian] Saving notebook..
In [ ]: