TensorBoard
TensorBoard
After Training the model, run
tensorboard --logdir=path/to/log-directory
Tensorboard provides a good visualization tool for all the variables you like and works on a browser.
from keras.models import Sequential
from keras.layers import Dense, Dropout
from keras.optimizers import RMSprop
from keras.datasets import mnist
from keras import initializations
from keras.utils import np_utils
from keras.callbacks import TensorBoard
Using TensorFlow backend.
# Hyper parameters
batch_size = 128
nb_epoch = 100
# Parameters for MNIST dataset
nb_classes = 10
# Parameters for MLP
prob_drop_input = 0.2 # drop probability for dropout @ input layer
prob_drop_hidden = 0.5 # drop probability for dropout @ fc layer
def init_weights(shape, name=None):
return initializations.normal(shape, scale=0.01, name=name)
# Load MNIST dataset
(X_train, y_train), (X_test, y_test) = mnist.load_data()
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
X_train /= 255
X_test /= 255
Y_Train = np_utils.to_categorical(y_train, nb_classes)
Y_Test = np_utils.to_categorical(y_test, nb_classes)
# Multilayer Perceptron model
model = Sequential()
model.add(Dense(output_dim=625, input_dim=784, init=init_weights, activation='sigmoid', name='dense1'))
model.add(Dropout(prob_drop_input, name='dropout1'))
model.add(Dense(output_dim=625, input_dim=625, init=init_weights, activation='sigmoid', name='dense2'))
model.add(Dropout(prob_drop_hidden, name='dropout2'))
model.add(Dense(output_dim=10, input_dim=625, init=init_weights, activation='softmax', name='dense3'))
model.compile(optimizer=RMSprop(lr=0.001, rho=0.9), loss='categorical_crossentropy', metrics=['accuracy'])
model.summary()
____________________________________________________________________________________________________
Layer (type) Output Shape Param # Connected to
====================================================================================================
dense1 (Dense) (None, 625) 490625 dense_input_1[0][0]
____________________________________________________________________________________________________
dropout1 (Dropout) (None, 625) 0 dense1[0][0]
____________________________________________________________________________________________________
dense2 (Dense) (None, 625) 391250 dropout1[0][0]
____________________________________________________________________________________________________
dropout2 (Dropout) (None, 625) 0 dense2[0][0]
____________________________________________________________________________________________________
dense3 (Dense) (None, 10) 6260 dropout2[0][0]
====================================================================================================
Total params: 888135
____________________________________________________________________________________________________
# Train
history = model.fit(X_train, Y_Train, nb_epoch=nb_epoch, batch_size=batch_size, verbose=1,
callbacks=[TensorBoard(log_dir='./logs/09_tensorboard', histogram_freq=1)])
Epoch 1/100
60000/60000 [==============================] - 3s - loss: 0.6212 - acc: 0.8026
Epoch 2/100
60000/60000 [==============================] - 2s - loss: 0.2564 - acc: 0.9227
Epoch 3/100
60000/60000 [==============================] - 2s - loss: 0.1883 - acc: 0.9433
Epoch 4/100
60000/60000 [==============================] - 2s - loss: 0.1509 - acc: 0.9547
Epoch 5/100
60000/60000 [==============================] - 2s - loss: 0.1239 - acc: 0.9622
Epoch 6/100
60000/60000 [==============================] - 2s - loss: 0.1063 - acc: 0.9680
Epoch 7/100
60000/60000 [==============================] - 2s - loss: 0.0953 - acc: 0.9716
Epoch 8/100
60000/60000 [==============================] - 2s - loss: 0.0832 - acc: 0.9747
Epoch 9/100
60000/60000 [==============================] - 2s - loss: 0.0749 - acc: 0.9776
Epoch 10/100
60000/60000 [==============================] - 2s - loss: 0.0677 - acc: 0.9792
Epoch 11/100
60000/60000 [==============================] - 2s - loss: 0.0632 - acc: 0.9805
Epoch 12/100
60000/60000 [==============================] - 2s - loss: 0.0578 - acc: 0.9822
Epoch 13/100
60000/60000 [==============================] - 2s - loss: 0.0535 - acc: 0.9835
Epoch 14/100
60000/60000 [==============================] - 2s - loss: 0.0510 - acc: 0.9845
Epoch 15/100
60000/60000 [==============================] - 2s - loss: 0.0472 - acc: 0.9853
Epoch 16/100
60000/60000 [==============================] - 2s - loss: 0.0430 - acc: 0.9871
Epoch 17/100
60000/60000 [==============================] - 2s - loss: 0.0393 - acc: 0.9879
Epoch 18/100
60000/60000 [==============================] - 2s - loss: 0.0381 - acc: 0.9877
Epoch 19/100
60000/60000 [==============================] - 2s - loss: 0.0366 - acc: 0.9888
Epoch 20/100
60000/60000 [==============================] - 2s - loss: 0.0328 - acc: 0.9896
Epoch 21/100
60000/60000 [==============================] - 2s - loss: 0.0337 - acc: 0.9900
Epoch 22/100
60000/60000 [==============================] - 2s - loss: 0.0294 - acc: 0.9905
Epoch 23/100
60000/60000 [==============================] - 2s - loss: 0.0288 - acc: 0.9913
Epoch 24/100
60000/60000 [==============================] - 2s - loss: 0.0284 - acc: 0.9912
Epoch 25/100
60000/60000 [==============================] - 2s - loss: 0.0261 - acc: 0.9919
Epoch 26/100
60000/60000 [==============================] - 2s - loss: 0.0258 - acc: 0.9922
Epoch 27/100
60000/60000 [==============================] - 2s - loss: 0.0248 - acc: 0.9925
Epoch 28/100
60000/60000 [==============================] - 2s - loss: 0.0220 - acc: 0.9930
Epoch 29/100
60000/60000 [==============================] - 2s - loss: 0.0212 - acc: 0.9936
Epoch 30/100
60000/60000 [==============================] - 2s - loss: 0.0201 - acc: 0.9938
Epoch 31/100
60000/60000 [==============================] - 2s - loss: 0.0199 - acc: 0.9940
Epoch 32/100
60000/60000 [==============================] - 2s - loss: 0.0190 - acc: 0.9943
Epoch 33/100
60000/60000 [==============================] - 2s - loss: 0.0179 - acc: 0.9943
Epoch 34/100
60000/60000 [==============================] - 2s - loss: 0.0177 - acc: 0.9947
Epoch 35/100
60000/60000 [==============================] - 2s - loss: 0.0168 - acc: 0.9947
Epoch 36/100
60000/60000 [==============================] - 2s - loss: 0.0166 - acc: 0.9947
Epoch 37/100
60000/60000 [==============================] - 2s - loss: 0.0157 - acc: 0.9953
Epoch 38/100
60000/60000 [==============================] - 2s - loss: 0.0132 - acc: 0.9959
Epoch 39/100
60000/60000 [==============================] - 2s - loss: 0.0142 - acc: 0.9952
Epoch 40/100
60000/60000 [==============================] - 2s - loss: 0.0136 - acc: 0.9955
Epoch 41/100
60000/60000 [==============================] - 2s - loss: 0.0133 - acc: 0.9959
Epoch 42/100
60000/60000 [==============================] - 2s - loss: 0.0138 - acc: 0.9960
Epoch 43/100
60000/60000 [==============================] - 2s - loss: 0.0131 - acc: 0.9960
Epoch 44/100
60000/60000 [==============================] - 2s - loss: 0.0132 - acc: 0.9955
Epoch 45/100
60000/60000 [==============================] - 2s - loss: 0.0121 - acc: 0.9963
Epoch 46/100
60000/60000 [==============================] - 2s - loss: 0.0135 - acc: 0.9959
Epoch 47/100
60000/60000 [==============================] - 2s - loss: 0.0124 - acc: 0.9963
Epoch 48/100
60000/60000 [==============================] - 2s - loss: 0.0103 - acc: 0.9965
Epoch 49/100
60000/60000 [==============================] - 2s - loss: 0.0105 - acc: 0.9968
Epoch 50/100
60000/60000 [==============================] - 2s - loss: 0.0104 - acc: 0.9968
Epoch 51/100
60000/60000 [==============================] - 2s - loss: 0.0093 - acc: 0.9971
Epoch 52/100
60000/60000 [==============================] - 2s - loss: 0.0105 - acc: 0.9967
Epoch 53/100
60000/60000 [==============================] - 2s - loss: 0.0109 - acc: 0.9969
Epoch 54/100
60000/60000 [==============================] - 2s - loss: 0.0109 - acc: 0.9966
Epoch 55/100
60000/60000 [==============================] - 2s - loss: 0.0092 - acc: 0.9970
Epoch 56/100
60000/60000 [==============================] - 2s - loss: 0.0098 - acc: 0.9966
Epoch 57/100
60000/60000 [==============================] - 2s - loss: 0.0103 - acc: 0.9969
Epoch 58/100
60000/60000 [==============================] - 2s - loss: 0.0087 - acc: 0.9971
Epoch 59/100
60000/60000 [==============================] - 2s - loss: 0.0096 - acc: 0.9972
Epoch 60/100
60000/60000 [==============================] - 2s - loss: 0.0084 - acc: 0.9974
Epoch 61/100
60000/60000 [==============================] - 2s - loss: 0.0101 - acc: 0.9971
Epoch 62/100
60000/60000 [==============================] - 2s - loss: 0.0081 - acc: 0.9972
Epoch 63/100
60000/60000 [==============================] - 2s - loss: 0.0089 - acc: 0.9974
Epoch 64/100
60000/60000 [==============================] - 2s - loss: 0.0072 - acc: 0.9979
Epoch 65/100
60000/60000 [==============================] - 2s - loss: 0.0080 - acc: 0.9978
Epoch 66/100
60000/60000 [==============================] - 2s - loss: 0.0065 - acc: 0.9981
Epoch 67/100
60000/60000 [==============================] - 2s - loss: 0.0070 - acc: 0.9978
Epoch 68/100
60000/60000 [==============================] - 2s - loss: 0.0077 - acc: 0.9975
Epoch 69/100
60000/60000 [==============================] - 2s - loss: 0.0066 - acc: 0.9980
Epoch 70/100
60000/60000 [==============================] - 2s - loss: 0.0072 - acc: 0.9978
Epoch 71/100
60000/60000 [==============================] - 2s - loss: 0.0075 - acc: 0.9978
Epoch 72/100
60000/60000 [==============================] - 2s - loss: 0.0065 - acc: 0.9983
Epoch 73/100
60000/60000 [==============================] - 2s - loss: 0.0062 - acc: 0.9980
Epoch 74/100
60000/60000 [==============================] - 2s - loss: 0.0069 - acc: 0.9980
Epoch 75/100
60000/60000 [==============================] - 2s - loss: 0.0072 - acc: 0.9979
Epoch 76/100
60000/60000 [==============================] - 2s - loss: 0.0062 - acc: 0.9984
Epoch 77/100
60000/60000 [==============================] - 2s - loss: 0.0069 - acc: 0.9981
Epoch 78/100
60000/60000 [==============================] - 2s - loss: 0.0062 - acc: 0.9982
Epoch 79/100
60000/60000 [==============================] - 2s - loss: 0.0065 - acc: 0.9980
Epoch 80/100
60000/60000 [==============================] - 2s - loss: 0.0060 - acc: 0.9983
Epoch 81/100
60000/60000 [==============================] - 2s - loss: 0.0051 - acc: 0.9984
Epoch 82/100
60000/60000 [==============================] - 2s - loss: 0.0056 - acc: 0.9983
Epoch 83/100
60000/60000 [==============================] - 2s - loss: 0.0062 - acc: 0.9982
Epoch 84/100
60000/60000 [==============================] - 2s - loss: 0.0056 - acc: 0.9984
Epoch 85/100
60000/60000 [==============================] - 2s - loss: 0.0059 - acc: 0.9983
Epoch 86/100
60000/60000 [==============================] - 2s - loss: 0.0059 - acc: 0.9982
Epoch 87/100
60000/60000 [==============================] - 2s - loss: 0.0050 - acc: 0.9986
Epoch 88/100
60000/60000 [==============================] - 2s - loss: 0.0050 - acc: 0.9985
Epoch 89/100
60000/60000 [==============================] - 2s - loss: 0.0057 - acc: 0.9985
Epoch 90/100
60000/60000 [==============================] - 2s - loss: 0.0049 - acc: 0.9984
Epoch 91/100
60000/60000 [==============================] - 2s - loss: 0.0055 - acc: 0.9984
Epoch 92/100
60000/60000 [==============================] - 2s - loss: 0.0055 - acc: 0.9985
Epoch 93/100
60000/60000 [==============================] - 2s - loss: 0.0056 - acc: 0.9984
Epoch 94/100
60000/60000 [==============================] - 2s - loss: 0.0062 - acc: 0.9981
Epoch 95/100
60000/60000 [==============================] - 2s - loss: 0.0052 - acc: 0.9983
Epoch 96/100
60000/60000 [==============================] - 2s - loss: 0.0044 - acc: 0.9986
Epoch 97/100
60000/60000 [==============================] - 2s - loss: 0.0054 - acc: 0.9984
Epoch 98/100
60000/60000 [==============================] - 2s - loss: 0.0046 - acc: 0.9986
Epoch 99/100
60000/60000 [==============================] - 2s - loss: 0.0053 - acc: 0.9984
Epoch 100/100
60000/60000 [==============================] - 2s - loss: 0.0047 - acc: 0.9987
# Evaluate
evaluation = model.evaluate(X_test, Y_Test, verbose=1)
print('\nSummary: Loss over the test dataset: %.2f, Accuracy: %.2f' % (evaluation[0], evaluation[1]))
9920/10000 [============================>.] - ETA: 0s
Summary: Loss over the test dataset: 0.12, Accuracy: 0.98