CNN - linear regression and nonlinear regression entry of Keras

introduction:

1.
I used python compiler: Jupyter.
Description :
Jupyter Ipython Notebook is an upgraded version, is a Web application, in writing directly in the browser running. Particularly lightweight, elegant interface, very convenient.
Interface is as follows:
Here Insert Picture Description
Installation and use:
PIP jupyter the install
open terminal (the browser directly in the preparation of open):
jupyter Notebook
shortcut:
Shift + Tab to move the cursor position parameters batter to view Shift + Tab two methods described
examples are as follows :
Here Insert Picture Description

Specifically refer to:
https://jupyter.org/
https://www.jianshu.com/p/f3c3dd636b8a
2.
I just started to learn keras, following just a personal study notes, code comments in great detail. Want to know more can watch the video: https://www.bilibili.com/video/av40787141?from=search&seid=14045366417315862159

A linear regression:

import keras
import numpy as np
#绘图包
import matplotlib.pyplot as plt
#Sequential按顺序构成的模型
from keras.models import Sequential
#Dense全连接层
from keras.layers import Dense


#使用numpy生成100个随机点
x_data=np.random.rand(100)
#生成与随机点相同形状的干扰点
noise=np.random.normal(0,0.01,x_data.shape)
#一次函数y=kx+b.    noise:干扰使之离散偏移
y_data=x_data*0.1+0.2+noise

#显示随机点
plt.scatter(x_data,y_data)
plt.show()

Here Insert Picture Description

#构建一个顺序模型
model=Sequential()
#在模型中添加一个全连接层   输入维度和输出维度都为1
model.add(Dense(units=1,input_dim=1))

#设置学习过程
#sgd:随机梯度下降法
#mse:均方误差
#optimizer:优化器 学习率为:0.01
#loss:计算损失
model.compile(optimizer='sgd',loss='mse')

#训练3001个批次
for step in range(3001):
    #每次训练一个批次    训练模型
    cost=model.train_on_batch(x_data,y_data)
    #每500个batch打印一次cost值
    if step % 500==0:
        print('cost:',cost)
        
#打印权值和偏置值
W,b=model.layers[0].get_weights()
print('W:',W,'b',b)

#x_data输入网络中,得到预测值y_pred
y_pred=model.predict(x_data)
#显示随机点
plt.scatter(x_data,y_data)
#显示预测的结果   用一条宽度为3,颜色为红色的线显示预测值
plt.plot(x_data,y_pred,'r-',lw=3)
plt.show()



Here Insert Picture Description

Second, the non-linear regression:

import keras
import numpy as np
#绘图包
import matplotlib.pyplot as plt
#Sequential按顺序构成的模型
from keras.models import Sequential
#Dense全连接层
from keras.layers import Dense,Activation
#导入新的SDG优化器
from keras.optimizers import SGD
#使用numpy生成-0.5~0.5区间内100个随机点
x_data=np.linspace(-0.5,0.5,200)
#从正态(高斯)分布中随机抽取样本生成与随机点相同形状的干扰点 
noise=np.random.normal(0,0.02,x_data.shape)
#二次函数:y=x~     noise:干扰使之离散偏移
y_data=np.square(x_data)+noise

#显示随机点
plt.scatter(x_data,y_data)
plt.show()

Here Insert Picture Description

#构建一个顺序模型
model=Sequential()
#在模型中添加一个全连接层   
#1-10-1    添加一个隐藏层(10个神经元)
model.add(Dense(units=10,input_dim=1))
#model.add(Dense(units=1,input_dim=10))
model.add(Dense(units=1))


#设置学习过程
#sgd:随机梯度下降法
#mse:均方误差
#optimizer:优化器 学习率为:0.01
#loss:计算损失
#model.compile(optimizer='sgd',loss='mse')

#定义优化算法    之前SGD默认的学习率为:0.01 对于这个迭代次数大概要几万次,而现在只有3000次,所以要加大学习率
sgd=SGD(lr=0.3)
model.compile(optimizer=sgd,loss='mse')

#训练3001个批次
for step in range(3001):
    #每次训练一个批次    训练模型
    cost=model.train_on_batch(x_data,y_data)
    #每500个batch打印一次cost值
    if step % 500==0:
        print('cost:',cost)
        
#打印权值和偏置值
W,b=model.layers[0].get_weights()
print('W:',W,'b:',b)

#x_data输入网络中,得到预测值y_pred
y_pred=model.predict(x_data)
#显示随机点
plt.scatter(x_data,y_data)
#显示预测的结果   用一条宽度为3,颜色为红色的线显示预测值
plt.plot(x_data,y_pred,'r-',lw=3)
plt.show()

Here Insert Picture Description

#上默认没有激活函数( activation=None,),那只是线性的    所以要添加激活函数



#构建一个顺序模型
model=Sequential()
#在模型中添加一个全连接层   
#1-10-1    添加一个隐藏层(10个神经元)
model.add(Dense(units=10,input_dim=1))
#添加激活函数tanh   tanh在大多数情况下比sigomid函数好用
model.add(Activation('tanh'))
#model.add(Dense(units=1,input_dim=10))
model.add(Dense(units=1))
model.add(Activation('tanh'))


#设置学习过程
#sgd:随机梯度下降法
#mse:均方误差
#optimizer:优化器 学习率为:0.01
#loss:计算损失
#model.compile(optimizer='sgd',loss='mse')

#定义优化算法    之前SGD默认的学习率为:0.01 对于这个迭代次数大概要几万次,而现在只有3000次,所以要加大学习率
sgd=SGD(lr=0.3)
model.compile(optimizer=sgd,loss='mse')

#训练3001个批次
for step in range(3001):
    #每次训练一个批次    训练模型
    cost=model.train_on_batch(x_data,y_data)
    #每500个batch打印一次cost值
    if step % 500==0:
        print('cost:',cost)
        
#打印权值和偏置值
W,b=model.layers[0].get_weights()
print('W:',W,'b:',b)

#x_data输入网络中,得到预测值y_pred
y_pred=model.predict(x_data)
#显示随机点
plt.scatter(x_data,y_data)
#显示预测的结果   用一条宽度为3,颜色为红色的线显示预测值
plt.plot(x_data,y_pred,'r-',lw=3)
plt.show()

Here Insert Picture Description

Released eight original articles · won praise 18 · views 723

Guess you like

Origin blog.csdn.net/qq_44222849/article/details/104532027