Skip to content

Instantly share code, notes, and snippets.

@sriharshaj
Last active June 26, 2019 17:11
Show Gist options
  • Save sriharshaj/bbf4887a790c33fffbbb4e8af3d913f0 to your computer and use it in GitHub Desktop.
Save sriharshaj/bbf4887a790c33fffbbb4e8af3d913f0 to your computer and use it in GitHub Desktop.
Keras code snippets
def model(input_shape):
# Define the input placeholder as a tensor with shape input_shape. Think of this as your input image!
X_input = Input(input_shape)
# Zero-Padding: pads the border of X_input with zeroes
X = ZeroPadding2D((3, 3))(X_input)
# CONV -> BN -> RELU Block applied to X
X = Conv2D(32, (7, 7), strides = (1, 1), name = 'conv0')(X)
X = BatchNormalization(axis = 3, name = 'bn0')(X)
X = Activation('relu')(X)
# MAXPOOL
X = MaxPooling2D((2, 2), name='max_pool')(X)
# FLATTEN X (means convert it to a vector) + FULLYCONNECTED
X = Flatten()(X)
X = Dense(1, activation='sigmoid', name='fc')(X)
# Create model. This creates your Keras model instance, you'll use this instance to train/test the model.
model = Model(inputs = X_input, outputs = X, name='HappyModel')
return model
happyModel = HappyModel(X_train[0].shape)
happyModel.compile(optimizer='Adam', loss='binary_crossentropy', metrics=["accuracy"])
happyModel.fit(x= X_train, y=Y_train, epochs=100, batch_size=50)
preds = happyModel.evaluate(x=X_test, y= Y_test)
# Summarize the model
happyModel.summary()
# Graphical representation of model
plot_model(happyModel, to_file='HappyModel.png')
SVG(model_to_dot(happyModel).create(prog='dot', format='svg'))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment