Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active January 30, 2021 19:35
Show Gist options
  • Save michelkana/b915c39afaff56d976f48207832c1cff to your computer and use it in GitHub Desktop.
Save michelkana/b915c39afaff56d976f48207832c1cff to your computer and use it in GitHub Desktop.
import keras
from keras.models import Sequential
from keras.layers import Dense
from keras.optimizers import SGD
from sklearn.metrics import r2_score
# neural network with 1 neuron and a linear activation
model = Sequential()
model.add(Dense(1, activation = 'linear', input_dim = 1))
# training the network
model.compile(optimizer=SGD(), loss='mse')
model.fit(x, y, batch_size=1, epochs=40, shuffle=True, verbose=0)
# prediction
y_pred = model.predict(x)
# plotting
plt.scatter(x,y, label='data')
plt.plot(x, beta_0 + beta_1*x, label='regression line')
plt.plot(x, y_pred, label='predicted ANN')
plt.xlabel('x')
plt.ylabel('y')
plt.legend()
plt.title('sample line with noise');
# compare estimated parameters
W = model.get_weights()
print('slope and intercept - formula: {0:.3f}, {1:.3f}'.format(beta_0, beta_1))
print('slope and intercept - artifical neuron: {0:.3f}, {1:.3f}'.format(W[1][0], W[0][0][0]))
# evaluate performance
print('R2 score - regression line: {0:0.2%}'.format(r2_score(y, beta_0+beta_1*x)))
print('R2 score - ANN : {0:0.2%}'.format(r2_score(y, y_pred)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment