Skip to content

Instantly share code, notes, and snippets.

@L-Lewis
Last active May 16, 2019 20:13
Show Gist options
  • Save L-Lewis/2a44cad9d44922a1c6953208409afd8d to your computer and use it in GitHub Desktop.
Save L-Lewis/2a44cad9d44922a1c6953208409afd8d to your computer and use it in GitHub Desktop.
Building, compiling and visualising a three-layer neural network
from keras import models, layers, optimizers, regularizers
from keras.utils.vis_utils import model_to_dot
from IPython.display import SVG
# Building the model
nn2 = models.Sequential()
nn2.add(layers.Dense(128, input_shape=(X_train.shape[1],), activation='relu'))
nn2.add(layers.Dense(256, activation='relu'))
nn2.add(layers.Dense(256, activation='relu'))
nn2.add(layers.Dense(1, activation='linear'))
# Compiling the model
nn2.compile(loss='mean_squared_error',
optimizer='adam',
metrics=['mean_squared_error'])
# Printing the model summary
print(nn2.summary())
# Visualising the neural network
SVG(model_to_dot(nn2, show_layer_names=False, show_shapes=True).create(prog='dot', format='svg'))
# Training the model
nn2_history = nn2.fit(X_train,
y_train,
epochs=100,
batch_size=256,
validation_split = 0.1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment