非线性回归结果
Show me the code
导包,由于是非线性拟合,所以Keras默认的激活函数
y
=
x
y = x
y=x不再合适,所以需要导入Activation
模块,这里用到的是双曲正切激活函数,即tanh
。
# 顺序模型
from keras import Sequential
# 神经网络,及激活函数
from keras.layers import Dense, Activation
# 随机梯度下降法
from keras.optimizers import SGD
import numpy as np
import matplotlib.pyplot as plt
生成数据
# 生成数据
a = 0.72
b = 0
c = -0.26
n_samples = 100
x_true = np.linspace(-0.5, 0.5, n_samples)
noise = np.random.normal(0, 0.01, x_true.shape)
y_true = a * np.square(x_true) + b * x_true + c + noise
# 绘制原始数据点
plt.scatter(x_true, y_true)
plt.show()
创建神经网络进行训练
# 建立神经网络
model = Sequential()
model.add(Dense(units= 10, input_dim= 1))
model.add(Activation("tanh"))
model.add(Dense(units= 1)) # 该层的输入维度会默认成上一层的输出维度
model.add(Activation("tanh"))
# 整合神经网络
sgd = SGD(lr= 0.3) # 调大学习率
model.compile(optimizer= sgd, loss= "mse")
#定义训练次数并开始训练
MAX_LOOP = 3001
for step in range(MAX_LOOP):
step_loss = model.train_on_batch(x_true, y_true)
if step % 500 == 0:
print("loss= ", step_loss)
y_predict = model.predict(x_true)
# 绘制拟合结果
plt.scatter(x_true, y_true, c= "b")
plt.scatter(x_true, y_predict, c= "r")
plt.show()