Skip to content

Instantly share code, notes, and snippets.

Created Oct 13, 2020
What would you like to do?
import keras
from keras.layers import Dense, Activation
from keras.models import Sequential
import matplotlib.pyplot as plt
import numpy as np
xx = ipdata = np.linspace(1,2,100)
yy = xx*4 + np.random.randn(*xx.shape) * 0.3
modeldef = Sequential()
modeldef.add(Dense(1, input_dim=1, activation='linear'))
modeldef.compile(optimizer='sgd', loss='mse', metrics=['mse'])
weight = modeldef.layers[0].get_weights()
wInit = weight[0][0][0]
bInit = weight[1][0]
print('Linear regression model is initialized with weights w: %.2f, b: %.2f' % (wInit, bInit)),yy, batch_size=1, epochs=30, shuffle=False)
weight = modeldef.layers[0].get_weights()
wFinal = weight[0][0][0]
bFinal = weight[1][0]
print('Linear regression model is trained to have weight w: %.2f, b: %.2f' % (wFinal, bFinal))
pred = modeldef.predict(ipdata)
plt.plot(data, pred, 'b', ipdata , yy, 'k.')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment