Skip to content

Instantly share code, notes, and snippets.

@KentaKudo
Last active February 5, 2018 18:18
Show Gist options
  • Save KentaKudo/a11908347280853ecc3d519cd55ff0a9 to your computer and use it in GitHub Desktop.
Save KentaKudo/a11908347280853ecc3d519cd55ff0a9 to your computer and use it in GitHub Desktop.
def simple_net(shape):
from keras.models import Model
from keras.layers import Dense, Conv2D, MaxPooling2D, Flatten, Input, BatchNormalization, Dropout
from keras.regularizers import l2
# The layers of Convolution → Convolution → Pooling
def ccp(filters, kernel_size=(3, 3), weight_decay=1e-4, dropout=0.2):
def _ccp(x):
x = Conv2D(filters, kernel_size, padding='same', kernel_regularizer=l2(weight_decay), activation='relu')(x)
x = BatchNormalization()(x)
x = Conv2D(filters, kernel_size, padding='same', kernel_regularizer=l2(weight_decay), activation='relu')(x)
x = BatchNormalization()(x)
x = MaxPooling2D(pool_size=(2, 2))(x)
x = Dropout(dropout)(x)
return x
return _ccp
inputs = Input(shape=train_X.shape[1:])
x = ccp(32)(inputs)
x = ccp(64, dropout=0.3)(x)
x = ccp(128, dropout=0.4)(x)
x = Flatten()(x)
y = Dense(10, activation='softmax')(x)
model = Model(inputs=inputs, outputs=y)
model.compile(loss='categorical_crossentropy',
optimizer='adam',
metrics=['accuracy'])
return model
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment