Moving from Keras

In [1]:
batch_size = 128
num_classes = 10
epochs = 12
img_rows, img_cols = (28, 28)
In [2]:
import keras.backend as K
if K.image_data_format() == 'channels_first':
    input_shape = (1, img_rows, img_cols)
else:
    input_shape = (img_rows, img_cols, 1)
Using Theano backend.

Keras Imperative Interface

In [3]:
import keras
from keras.models import Sequential
from keras.layers import Conv2D, MaxPool2D, Dropout, Flatten, Dense
In [4]:
model = Sequential()
model.add(Conv2D(32, kernel_size=(3, 3),
                 activation='relu',
                 input_shape=input_shape))
model.add(Conv2D(64, (3, 3), activation='relu'))
model.add(MaxPool2D(pool_size=(2, 2)))
model.add(Dropout(0.25))
model.add(Flatten())
model.add(Dense(128, activation='relu'))
model.add(Dropout(0.5))
model.add(Dense(num_classes, activation='softmax'))

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])

model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
conv2d_1 (Conv2D)            (None, 26, 26, 32)        320
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 24, 24, 64)        18496
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 12, 12, 64)        0
_________________________________________________________________
dropout_1 (Dropout)          (None, 12, 12, 64)        0
_________________________________________________________________
flatten_1 (Flatten)          (None, 9216)              0
_________________________________________________________________
dense_1 (Dense)              (None, 128)               1179776
_________________________________________________________________
dropout_2 (Dropout)          (None, 128)               0
_________________________________________________________________
dense_2 (Dense)              (None, 10)                1290
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0
_________________________________________________________________

Keras Functional Interface

In [5]:
import keras
from keras.models import Model
from keras.layers import Input, Conv2D, MaxPool2D, Dropout, Flatten, Dense, Dropout
In [6]:
inputs = k = Input(input_shape, name="input")
k = Conv2D(32, kernel_size=(3, 3), activation='relu')(k)
k = Conv2D(64, (3, 3), activation='relu')(k)
k = MaxPool2D(pool_size=(2, 2))(k)
k = Dropout(0.25)(k)
k = Flatten()(k)
k = Dense(128, activation='relu')(k)
k = Dropout(0.5)(k)
k = Dense(num_classes, activation='softmax')(k)
model = Model(inputs=inputs, outputs=k)

model.compile(loss=keras.losses.categorical_crossentropy,
              optimizer=keras.optimizers.Adadelta(),
              metrics=['accuracy'])
model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input (InputLayer)           (None, 28, 28, 1)         0
_________________________________________________________________
conv2d_3 (Conv2D)            (None, 26, 26, 32)        320
_________________________________________________________________
conv2d_4 (Conv2D)            (None, 24, 24, 64)        18496
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 12, 12, 64)        0
_________________________________________________________________
dropout_3 (Dropout)          (None, 12, 12, 64)        0
_________________________________________________________________
flatten_2 (Flatten)          (None, 9216)              0
_________________________________________________________________
dense_3 (Dense)              (None, 128)               1179776
_________________________________________________________________
dropout_4 (Dropout)          (None, 128)               0
_________________________________________________________________
dense_4 (Dense)              (None, 10)                1290
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0
_________________________________________________________________

Conx Interface

In [7]:
from conx import Layer, Network, Conv2DLayer, MaxPool2DLayer, FlattenLayer
In [8]:
network = Network("MNIST-CNN")
network.add(Layer("input", input_shape))
network.add(Conv2DLayer("conv1", 32, (3, 3), activation='relu'))
network.add(Conv2DLayer("conv2", 64, (3, 3), activation='relu'))
network.add(MaxPool2DLayer("maxpool", (2, 2), dropout=0.25))
network.add(FlattenLayer("flatten"))
network.add(Layer("hidden", 128, activation='relu', dropout=0.5))
network.add(Layer("output", num_classes, activation='softmax'))

network.connect("input", "conv1")
network.connect("conv1", "conv2")
network.connect("conv2", "maxpool")
network.connect("maxpool", "flatten")
network.connect("flatten", "hidden")
network.connect("hidden", "output")

# or, because this is sequetial, and layers added in order:
# network.connect()

network.compile(loss="categorical_crossentropy",
                optimizer="adadelta")
network.model.summary()
_________________________________________________________________
Layer (type)                 Output Shape              Param #
=================================================================
input (InputLayer)           (None, 28, 28, 1)         0
_________________________________________________________________
conv1 (Conv2D)               (None, 26, 26, 32)        320
_________________________________________________________________
conv2 (Conv2D)               (None, 24, 24, 64)        18496
_________________________________________________________________
maxpool (MaxPooling2D)       (None, 12, 12, 64)        0
_________________________________________________________________
dropout_5 (Dropout)          (None, 12, 12, 64)        0
_________________________________________________________________
flatten (Flatten)            (None, 9216)              0
_________________________________________________________________
hidden (Dense)               (None, 128)               1179776
_________________________________________________________________
dropout_6 (Dropout)          (None, 128)               0
_________________________________________________________________
output (Dense)               (None, 10)                1290
=================================================================
Total params: 1,199,882
Trainable params: 1,199,882
Non-trainable params: 0
_________________________________________________________________