当前位置:网站首页>2. Nonlinear regression
2. Nonlinear regression
2022-07-08 01:01:00 【booze-J】
The code running platform is jupyter-notebook, Code blocks in the article , According to jupyter-notebook Written in the order of division in , Run article code , Glue directly into jupyter-notebook that will do .
1. Import third-party library
import keras
import numpy as np
import matplotlib.pyplot as plt
# Sequential Sequential model
from keras.models import Sequential
# Dense Fully connected layer
from keras.layers import Dense,Activation
from tensorflow.keras.optimizers import SGD
2. Randomly generate data sets
# Use numpy Generate 200 A random point
# stay -0.5~0.5 Generate 200 A little bit
x_data = np.linspace(-0.5,0.5,200)
noise = np.random.normal(0,0.02,x_data.shape)
# y = x^2 + noise
y_data = np.square(x_data) + noise
# Show random points
plt.scatter(x_data,y_data)
plt.show()
Running results :
3. Nonlinear regression
# Build a sequential model
model = Sequential()
# Press shift+tab Parameters can be displayed
# 1-10-1 Input a neural layer ,10 Hidden layers , Output a neural layer
model.add(Dense(units=10,input_dim=1))
# Add activation function The activation function is linear by default , But we are nonlinear regression , Therefore, the activation function should be modified
# Add activation function Mode one : Add... Directly activation Parameters
# model.add(Dense(units=10,input_dim=1,activation="relu"))
# Add activation function Mode two : Add... Directly Activation Activation layer
model.add(Activation("tanh"))
model.add(Dense(units=1,input_dim=10))
# model.add(Dense(units=1,input_dim=10,activation="relu"))
model.add(Activation("tanh"))
# Define optimization algorithms Improving the learning rate can reduce the number of iterations
sgd = SGD(lr=0.3)
# sgd:Stochastic gradient descent , Random gradient descent method default sgd The learning rate table is smaller Therefore, the number of iterations required is relatively large It takes more time
# mse:Mean Squared Error , Mean square error
model.compile(optimizer=sgd,loss='mse')
# Training 3001 Lots
for step in range(3001):
# One batch at a time
cost = model.train_on_batch(x_data,y_data)
# Every time 500 individual batch Print once cost
if step%500==0:
print("cost:",cost)
# Print weights and batch values
W,b = model.layers[0].get_weights()
print("W:",W)
print("b:",b)
# x_data Input the predicted value in the network
y_pred = model.predict(x_data)
# Show random points
plt.scatter(x_data,y_data)
# Show forecast results
plt.plot(x_data,y_pred,"r-",lw=3)
plt.show()
Running results :
Be careful
- 1. For nonlinear regression, we should pay attention to modifying the activation function , Because the activation function is linear by default .
- 2. default sgd The learning rate table is smaller Therefore, the number of iterations required is relatively large It takes more time , So you can customize sgd Learning rate to learn .
- 3. On the whole, the code is very similar to the code of linear regression , Just modify the data set , A network model , Activation function , And optimizer .
边栏推荐
- Serial port receives a packet of data
- 基础篇——整合第三方技术
- [OBS] the official configuration is use_ GPU_ Priority effect is true
- Y59. Chapter III kubernetes from entry to proficiency - continuous integration and deployment (III, II)
- 跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
- C#中string用法
- Thinkphp内核工单系统源码商业开源版 多用户+多客服+短信+邮件通知
- The weight of the product page of the second level classification is low. What if it is not included?
- 牛客基础语法必刷100题之基本类型
- Codeforces Round #804 (Div. 2)(A~D)
猜你喜欢
Cve-2022-28346: Django SQL injection vulnerability
What does interface testing test?
fabulous! How does idea open multiple projects in a single window?
y59.第三章 Kubernetes从入门到精通 -- 持续集成与部署(三二)
C # generics and performance comparison
Analysis of 8 classic C language pointer written test questions
Malware detection method based on convolutional neural network
【GO记录】从零开始GO语言——用GO语言做一个示波器(一)GO语言基础
How does starfish OS enable the value of SFO in the fourth phase of SFO destruction?
新库上线 | CnOpenData中国星级酒店数据
随机推荐
The whole life cycle of commodity design can be included in the scope of industrial Internet
Lecture 1: the entry node of the link in the linked list
[note] common combined filter circuit
New library online | information data of Chinese journalists
牛客基础语法必刷100题之基本类型
Is it safe to open an account on the official website of Huatai Securities?
【obs】Impossible to find entrance point CreateDirect3D11DeviceFromDXGIDevice
跨模态语义关联对齐检索-图像文本匹配(Image-Text Matching)
基于微信小程序开发的我最在行的小游戏
Four stages of sand table deduction in attack and defense drill
Deep dive kotlin collaboration (the end of 23): sharedflow and stateflow
v-for遍历元素样式失效
Leetcode brush questions
Langchao Yunxi distributed database tracing (II) -- source code analysis
1.线性回归
Stock account opening is free of charge. Is it safe to open an account on your mobile phone
基于卷积神经网络的恶意软件检测方法
Reentrantlock fair lock source code Chapter 0
完整的模型训练套路
130. 被圍繞的區域