def convnet(input, output, dropout_rate=0., input_shape=(1, 28, 28), batch_size=100,
l2_rate=0.001, nb_epoch=12, img_rows=28, img_cols=28, nb_filters=64,
pool_size=(2, 2), kernel_size=(3, 3), activations='relu', constrain_norm=False):
'''
Helper function for building a Keras convolutional network.
input: Keras Input object appropriate for the data. e.g. input=Input(shape=(20,))
output: Function representing final layer for the network that maps from the last
hidden layer to output.
e.g. if output = Dense(10, activation='softmax') if we're doing 10 class
classification or output = Dense(1, activation='linear') if we're doing
regression.
'''
const = maxnorm(2) if constrain_norm else None
state = Convolution2D(nb_filters, kernel_size, padding='valid',
input_shape=input_shape, activation=activations,
kernel_regularizer=l2(l2_rate), kernel_constraint=const)(input)
state = Convolution2D(nb_filters, kernel_size,
activation=activations, kernel_regularizer=l2(l2_rate),
kernel_constraint=const)(state)
state = MaxPooling2D(pool_size=pool_size)(state)
state = Flatten()(state)
if dropout_rate > 0.:
state = Dropout(dropout_rate)(state)
state = Dense(128, activation=activations, kernel_regularizer=l2(l2_rate), kernel_constraint=const)(state)
if dropout_rate > 0.:
state = Dropout(dropout_rate)(state)
return output(state)
评论列表
文章目录