Keras 成长之路(二)非线性回归

非线性回归结果

在这里插入图片描述

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()
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值