Created
October 13, 2020 13:48
-
-
Save megha444/b32e0d72c3810063009e9da98897e184 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | |
modeldef.fit(xx,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.') | |
plt.show() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 31 should be plt.plot(ipdata, pred, 'b', ipdata , yy, 'k.') not plt.plot(data, pred, 'b', ipdata , yy, 'k.')