MNISTΒΆ

In [1]:
from conx import Network, Layer, Conv2DLayer, MaxPool2DLayer, FlattenLayer
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 [4]:
net.compile(error='mean_squared_error', optimizer='sgd')
In [5]:
from conx import Dataset
In [6]:
dataset = Dataset.get('mnist')
dataset.slice(0, 100)
dataset.reshape_inputs(784)
dataset.shuffle()
dataset.summary()
Input Summary:
   count  : 100 (100 for training, 0 for testing)
   shape  : (784,)
   range  : (0.0, 1.0)
Target Summary:
   count  : 100 (100 for training, 0 for testing)
   shape  : (10,)
   range  : (0.0, 1.0)
In [7]:
net.set_dataset(dataset)
In [8]:
widget = net.dashboard()
In [9]:
widget
In [10]:
net.train(100, verbose=0)
#net.test()
Out[10]:
(100, 0.090015381574630737, 0.12999999523162842, 0.0)
In [11]:
%%time
for pattern in net.dataset.inputs[:100]:
    net.propagate(pattern)
CPU times: user 5.79 s, sys: 658 ms, total: 6.45 s
Wall time: 6 s
In [12]:
from conx import Network, Layer, Conv2DLayer, MaxPool2DLayer, FlattenLayer
In [13]:
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=False))
net.add(Layer("hidden", 1000, activation='relu', vshape=(8,125), image_maxdim=500))
net.add(Layer("output", 10, activation='softmax'))

net.connect()
In [14]:
net.compile(error="binary_crossentropy", optimizer="rmsprop")
In [15]:
ds = Dataset.get('mnist')
In [16]:
ds.slice(100)
In [17]:
net.set_dataset(ds)
In [18]:
net.train()
Training...
Epoch 1/1
100/100 [==============================] - 0s - loss: 0.3260 - acc: 0.9000
========================================================================
Epoch #    1 | train error 0.32605 | train accuracy 0.90000 | validate% 0.00000
In [18]:
net.dashboard()
In [20]:
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