Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active July 22, 2019 07:57
Show Gist options
  • Save michelkana/f521327b115a5dd97a53c9b53c08af5a to your computer and use it in GitHub Desktop.
Save michelkana/f521327b115a5dd97a53c9b53c08af5a to your computer and use it in GitHub Desktop.
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from sklearn.metrics import r2_score
# network with two layers
# first layer has 2 neurons
# second layer has 1 neuron
model = Sequential()
model.add(Dense(2, activation = 'sigmoid', input_dim = 1))
model.add(Dense(1, activation = 'linear'))
model.summary()
# training
model.compile(optimizer=SGD(), loss='mse')
history = model.fit(one_hump_df.x, one_hump_df.y, batch_size=1,
epochs=5000, shuffle=True, verbose=0)
# prediction
y_pred = model.predict(one_hump_df.x)
print('R2 score with ANN: {0:0.2%}'.format(r2_score(one_hump_df.y, y_pred)))
# plot prediction
fig, ax = plt.subplots(1,1)
plot_data(one_hump_df.x, one_hump_df.y, ax=ax, title='', label='Original Data')
plot_data(one_hump_df.x, one_hump_yout, ax=ax, title='', label='ANN manual')
plot_data(one_hump_df.x, y_pred, ax=ax, title='', label='ANN prediction')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment