import sys
sys.path.append("..")
import grading
# use preloaded keras datasets and models
! mkdir -p ~/.keras/datasets
! mkdir -p ~/.keras/models
! ln -s $(realpath ../readonly/keras/datasets/*) ~/.keras/datasets/
! ln -s $(realpath ../readonly/keras/models/*) ~/.keras/models/
import numpy as np
from preprocessed_mnist import load_dataset
import keras
X_train, y_train, X_val, y_val, X_test, y_test = load_dataset()
y_train,y_val,y_test = map(keras.utils.np_utils.to_categorical,[y_train,y_val,y_test])
import matplotlib.pyplot as plt
%matplotlib inline
plt.imshow(X_train[0]);
import tensorflow as tf
s = tf.InteractiveSession()
import keras
from keras.models import Sequential
import keras.layers as ll
model = Sequential(name="mlp")
model.add(ll.InputLayer([28, 28]))
model.add(ll.Flatten())
# network body
model.add(ll.Dense(128))
model.add(ll.Activation('relu'))
model.add(ll.Dense(128))
model.add(ll.Activation('relu'))
# output layer: 10 neurons for each class with softmax
model.add(ll.Dense(10, activation='softmax'))
# categorical_crossentropy is your good old crossentropy
# but applied for one-hot-encoded vectors
model.compile("adam", "categorical_crossentropy", metrics=["accuracy"])
model.summary()
Keras models follow Scikit-learn's interface of fit/predict with some notable extensions. Let's take a tour.
# fit(X,y) ships with a neat automatic logging.
# Highly customizable under the hood.
model.fit(X_train, y_train,
validation_data=(X_val, y_val), epochs=5);
# estimate probabilities P(y|x)
model.predict_proba(X_val[:2])
# Save trained weights
model.save("weights.h5")
print("\nLoss, Accuracy = ", model.evaluate(X_test, y_test))
So far our model is staggeringly inefficient. There is something wring with it. Guess, what?
# Test score...
test_predictions = model.predict_proba(X_test).argmax(axis=-1)
test_answers = y_test.argmax(axis=-1)
test_accuracy = np.mean(test_predictions==test_answers)
print("\nTest accuracy: {} %".format(test_accuracy*100))
assert test_accuracy>=0.92,"Logistic regression can do better!"
assert test_accuracy>=0.975,"Your network can do better!"
print("Great job!")
answer_submitter = grading.Grader("0ybD9ZxxEeea8A6GzH-6CA")
answer_submitter.set_answer("N56DR", test_accuracy)
answer_submitter.submit(<your-email>, <your-assignment-token>)
Remember the interactive graphs from Tensorboard one notebook ago?
Thing is, Keras can use tensorboard to show you a lot of useful information about the learning progress. Just take a look!
! rm -r /tmp/tboard/**
from keras.callbacks import TensorBoard
model.fit(X_train, y_train, validation_data=(X_val, y_val),
epochs=10,
callbacks=[TensorBoard("/tmp/tboard")])
Here are some tips on what you could do. Don't worry, to reach the passing threshold you don't need to try all the ideas listed here, feel free to stop once you reach the 0.975 accuracy mark.
Network size
More layers, (docs)
Nonlinearities in the hidden layers
from scipy.misc import imrotate,imresize