Sahil Singh
A benchmark network in keras.
Dataset: MNIST
No. of hidden layers: 2
No. of hidden units: 10
from keras.datasets import mnist
from keras.models import Sequential
from keras.layers import Dense, Flatten, Activation
from keras.optimizers import SGD
from keras.utils import to_categorical
from matplotlib import pyplot as plt
import seaborn as sns
(x_train, y_train), (x_test, y_test) = mnist.load_data()
num_classes = 10
# Apply one-hot encoding to the labels
y_train = to_categorical(y_train, num_classes)
y_test = to_categorical(y_test, num_classes)
# Break training data into training and validation sets
(x_train, x_valid) = x_train[:50000], x_train[50000:]
(y_train, y_valid) = y_train[:50000], y_train[50000:]
# Model definition
model = Sequential()
model.add(Flatten(input_shape=(28,28)))
# Model hyperparameter
num_hidden_layers = 2
num_hidden_units = 10
for _ in range(num_hidden_layers):
model.add(Dense(num_hidden_units))
model.add(Activation('relu'))
# Output Layer
model.add(Dense(num_classes))
model.add(Activation('softmax'))
# Set optimization hyperparameters
batch_size = 256
epochs = 10
model.compile(
optimizer='adam',
loss='categorical_crossentropy',
metrics=['accuracy'])
history = model.fit(
x_train, y_train,
epochs=epochs,
batch_size=batch_size,
validation_data=(x_valid, y_valid),
verbose=2)
accuracy_score = model.evaluate(
x_test, y_test, batch_size=128)
print(f'\nTest accuracy: {accuracy_score[1]:>5.3f}')
Epoch 1/10
1s - loss: 2.8814 - acc: 0.2122 - val_loss: 1.9508 - val_acc: 0.2677
Epoch 2/10
0s - loss: 1.9028 - acc: 0.2714 - val_loss: 1.8804 - val_acc: 0.2859
...
...
...
Epoch 10/10
0s - loss: 0.9046 - acc: 0.6434 - val_loss: 0.8798 - val_acc: 0.6495
5632/10000 [===============>..............] - ETA: 0s
Test accuracy: 0.648
Model Evaluation
A is the overall range of values. [C, D] is the interval that we want to map to.
For the range [0, 1], The formula becomes:
tf.truncated_normal(shape=(INPUT, OUTPUT), mean=0, stddev=1)
tf.truncated_normal(shape=(INPUT, OUTPUT), mean=0, stddev=0.1)
W = tf.get_variable("W", shape=[INPUT, OUTPUT], initializer=tf.contrib.layers.xavier_initializer())
W = tf.get_variable("W", shape=[INPUT, OUTPUT], initializer=tf.contrib.layers.variance_scaling_initializer())
Image Source: http://http://cs231n.github.io