代码拉取完成,页面将自动刷新
from keras.models import Sequential
from keras.layers import Dense
import numpy as np
np.random.seed(1337)
import matplotlib.pyplot as plt
X = np.linspace(-1,1,200)
# np.random.shuffle(X)
Y = 0.5 * X + 2 + np.random.normal(0, 0.05, (200, ))
# plot data
# plt.scatter(X,Y)
# plt.show()
X_train, Y_train = X[:160], Y[:160]
X_test, Y_test = X[160:], Y[160:]
### build a neural network from the 1st layer to the last layer
model = Sequential()
# 定义第一层
model.add(Dense(units=1, input_dim=1))
### choose loss function and optimizing method
# mse: mean squared error 均方误差
# sgd: 随机梯度下降
model.compile(loss='mse', optimizer='sgd')
# training
print('Training --------')
for step in range(301):
cost = model.train_on_batch(X_train, Y_train)
if step % 100 == 0:
print('train cost', cost)
# testing
print('\nTesting -----------')
cost = model.evaluate(X_test, Y_test, batch_size = 40)
print('test cost: ', cost)
W,b = model.layers[0].get_weights()
print("Weights=", W, '\nbiases=',b)
# plotting the prediction
Y_pred = model.predict(X_test)
plt.scatter(X_test, Y_test)
plt.plot(X_test, Y_pred)
plt.show()
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。