3.8. MNISTΒΆ

In [1]:
from conx import Network, Layer, Conv2DLayer, MaxPool2DLayer, FlattenLayer
conx, version 3.3.4
Using Theano backend.
In [2]:
net = Network("MNIST")
net.add(Layer("input", shape=784, vshape=(28, 28), colormap="hot", minmax=(0,1)))
net.add(Layer("hidden1", shape=512, vshape=(16,32), activation='relu', dropout=0.2))
net.add(Layer("hidden2", shape=512, vshape=(16,32), activation='relu', dropout=0.2))
net.add(Layer("output", shape=10, activation='softmax'))

net.connect('input', 'hidden1')
net.connect('hidden1', 'hidden2')
net.connect('hidden2', 'output')
In [3]:
net.compile(error='mean_squared_error', optimizer='sgd')
In [4]:
dataset = net.dataset
dataset.get('mnist')
#dataset.slice(0, 100)
dataset.inputs.reshape(0, 784)
dataset.shuffle()
dataset.summary()
Input Summary:
   count  : 70000 (70000 for training, 0 for testing)
   shape  : (784,)
   range  : (0.0, 1.0)
Target Summary:
   count  : 70000 (70000 for training, 0 for testing)
   shape  : (10,)
   range  : (0.0, 1.0)
In [5]:
widget = net.dashboard()
In [6]:
widget
In [9]:
net
Out[9]:
MNISTLayer: output (output) shape = (10,) Keras class = Dense activation = softmaxoutputWeights from hidden2 to output output/kernel has shape (512, 10) output/bias has shape (10,)Layer: hidden2 (hidden) shape = (512,) dropout = 0.2 Keras class = Dense activation = reluhidden2Weights from hidden1 to hidden2 hidden2/kernel has shape (512, 512) hidden2/bias has shape (512,)Layer: hidden1 (hidden) shape = (512,) dropout = 0.2 Keras class = Dense activation = reluhidden1Weights from input to hidden1 hidden1/kernel has shape (784, 512) hidden1/bias has shape (512,)Layer: input (input) shape = (784,) Keras class = Inputinput
In [13]:
net.snapshot(height="500px")
Out[13]:
MNISTLayer: output (output) shape = (10,) Keras class = Dense activation = softmaxtargetsLayer: output (output) shape = (10,) Keras class = Dense activation = softmaxoutputWeights from hidden2 to output output/kernel has shape (512, 10) output/bias has shape (10,)Layer: hidden2 (hidden) shape = (512,) dropout = 0.2 Keras class = Dense activation = reluhidden2Weights from hidden1 to hidden2 hidden2/kernel has shape (512, 512) hidden2/bias has shape (512,)Layer: hidden1 (hidden) shape = (512,) dropout = 0.2 Keras class = Dense activation = reluhidden1Weights from input to hidden1 hidden1/kernel has shape (784, 512) hidden1/bias has shape (512,)Layer: input (input) shape = (784,) Keras class = Inputinput
In [11]:
net.config["show_targets"] = True
In [8]:
net.train(1, verbose=1)
net.test()
Training...
       |  Training |  Training
Epochs |     Error |  Accuracy
------ | --------- | ---------
#    0 |   0.08656 |   0.00000
========================================================================
#    1 |   0.08656 |   0.00000
========================================================
Testing train dataset with tolerance 0.1000...
Total count: 70000
      correct: 0
      incorrect: 70000
Total percentage correct: 0.0
In [9]:
%%time
for pattern in net.dataset.inputs[:100]:
    net.propagate(pattern)
CPU times: user 1.43 s, sys: 140 ms, total: 1.57 s
Wall time: 1.46 s
In [14]:
from conx import Network, Layer, Conv2DLayer, MaxPool2DLayer, FlattenLayer
In [15]:
net = Network("MNIST-CNN")
net.add(Layer("input", (28,28,1), colormap="hot", minmax=(0,1)))
net.add(Conv2DLayer("Conv2D-1", 16, (5,5), activation="relu"))
net.add(MaxPool2DLayer("maxpool1", (2,2)))
net.add(Conv2DLayer("Conv2D-2", 132, (5,5), activation="relu"))
net.add(MaxPool2DLayer("maxpool2", (2,2)))
net.add(FlattenLayer("flatten", visible=True))
net.add(Layer("hidden", 1000, activation='relu', vshape=(8,125), image_maxdim=500))
net.add(Layer("output", 10, activation='softmax'))

net.connect()
In [16]:
net.compile(error="binary_crossentropy", optimizer="rmsprop")
In [17]:
ds = net.dataset
ds.get('mnist')
In [14]:
ds.slice(100)
In [15]:
net.train()
Training...
Epoch 1/1
100/100 [==============================] - 0s - loss: 0.3262 - acc: 0.9000
========================================================================
Epoch #    1 | train error 0.32620 | train accuracy 0.90000 | validate% 7.03000
In [23]:
net.dashboard()
In [21]:
net.snapshot(height="500px")
Out[21]:
MNIST-CNNLayer: output (output) shape = (10,) Keras class = Dense activation = softmaxoutputWeights from hidden to output output/kernel has shape (1000, 10) output/bias has shape (10,)Layer: hidden (hidden) shape = (1000,) Keras class = Dense activation = reluhiddenWeights from flatten to hidden hidden/kernel has shape (2112, 1000) hidden/bias has shape (1000,)Layer: flatten (hidden) Keras class = FlattenflattenWeights from maxpool2 to flattenLayer: maxpool2 (hidden) Keras class = MaxPooling2Dmaxpool21320Weights from Conv2D-2 to maxpool2Layer: Conv2D-2 (hidden) Keras class = Conv2D activation = reluConv2D-21320Weights from maxpool1 to Conv2D-2 Conv2D-2/kernel has shape (5, 5, 16, 132) Conv2D-2/bias has shape (132,)Layer: maxpool1 (hidden) Keras class = MaxPooling2Dmaxpool1160Weights from Conv2D-1 to maxpool1Layer: Conv2D-1 (hidden) Keras class = Conv2D activation = reluConv2D-1160Weights from input to Conv2D-1 Conv2D-1/kernel has shape (5, 5, 1, 16) Conv2D-1/bias has shape (16,)Layer: input (input) shape = (28, 28, 1) Keras class = Inputinput10
In [19]:
net
Out[19]:
MNIST-CNNLayer: output (output) shape = (10,) Keras class = Dense activation = softmaxoutputWeights from hidden to output output/kernel has shape (1000, 10) output/bias has shape (10,)Layer: hidden (hidden) shape = (1000,) Keras class = Dense activation = reluhiddenWeights from flatten to hidden hidden/kernel has shape (2112, 1000) hidden/bias has shape (1000,)Layer: flatten (hidden) Keras class = FlattenflattenWeights from maxpool2 to flattenLayer: maxpool2 (hidden) Keras class = MaxPooling2Dmaxpool21320Weights from Conv2D-2 to maxpool2Layer: Conv2D-2 (hidden) Keras class = Conv2D activation = reluConv2D-21320Weights from maxpool1 to Conv2D-2 Conv2D-2/kernel has shape (5, 5, 16, 132) Conv2D-2/bias has shape (132,)Layer: maxpool1 (hidden) Keras class = MaxPooling2Dmaxpool1160Weights from Conv2D-1 to maxpool1Layer: Conv2D-1 (hidden) Keras class = Conv2D activation = reluConv2D-1160Weights from input to Conv2D-1 Conv2D-1/kernel has shape (5, 5, 1, 16) Conv2D-1/bias has shape (16,)Layer: input (input) shape = (28, 28, 1) Keras class = Inputinput10
In [22]:
for pattern in net.dataset.inputs[:10]:
    net.propagate(pattern)
In [21]:
net.train(epochs=100, accuracy=.7)
Training...
Epoch #    2 | train error 0.30195 | train accuracy 0.91000 | validate% 0.00000
Epoch #    3 | train error 0.29779 | train accuracy 0.90000 | validate% 0.00000
Epoch #    4 | train error 0.22481 | train accuracy 0.90000 | validate% 0.15000
Epoch #    5 | train error 0.14948 | train accuracy 0.95200 | validate% 0.06000
Epoch #    6 | train error 0.32091 | train accuracy 0.90300 | validate% 0.00000
Epoch #    7 | train error 0.18543 | train accuracy 0.93000 | validate% 0.20000
Epoch #    8 | train error 0.10438 | train accuracy 0.96200 | validate% 0.35000
Epoch #    9 | train error 0.06161 | train accuracy 0.98600 | validate% 0.54000
Epoch #   10 | train error 0.04596 | train accuracy 0.98900 | validate% 0.47000
Epoch #   11 | train error 0.08075 | train accuracy 0.96700 | validate% 0.49000
Epoch #   12 | train error 0.12112 | train accuracy 0.95800 | validate% 0.51000
Epoch #   13 | train error 0.08936 | train accuracy 0.96900 | validate% 0.50000
Epoch #   14 | train error 0.04674 | train accuracy 0.98900 | validate% 0.69000
Epoch #   15 | train error 0.02372 | train accuracy 0.99600 | validate% 0.77000
========================================================================
Epoch #   15 | train error 0.02372 | train accuracy 0.99600 | validate% 0.77000