当前位置:网站首页>1. Linear regression
1. Linear regression
2022-07-08 01:02: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
2. Randomly generate data sets
# Use numpy Generate 100 A random point
x_data = np.random.rand(100)
# Noise shape and x_data The shape of is the same
noise = np.random.normal(0,0.01,x_data.shape)
# Set up w=0.1 b=0.2
y_data = x_data*0.1+0.2+noise
# y_data_no_noisy = x_data*0.1+0.2
# Show random points
plt.scatter(x_data,y_data)
# plt.scatter(x_data,y_data_no_noisy)
Running effect :
This is the case of adding noise y_data = x_data*0.1+0.2+noise
:
Without adding noise y_data_no_noisy = x_data*0.1+0.2
(w=0.1,b=0.2):
Linear regression is based on the scatter plot with added noise , Fit a straight line that is similar to the scatter diagram without adding noise .
3. Linear regression
# Build a sequential model
model = Sequential()
# Add a full connection layer to the model stay jupyter-notebook in , Press shift+tab Parameters can be displayed
model.add(Dense(units=1,input_dim=1))
# sgd:Stochastic gradient descent , Random gradient descent method
# 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 The loss of
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 effect :
You can see the prediction w and b Are very close to what we set w and b.
Be careful
- stay jupyter-notebook in , Press shift+tab Parameters can be displayed
- train_on_batch Use
- compile Use
边栏推荐
- Su embedded training - Day5
- SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
- 图像数据预处理
- 完整的模型验证(测试,demo)套路
- 英雄联盟胜负预测--简易肯德基上校
- Basic mode of service mesh
- 2.非线性回归
- Reentrantlock fair lock source code Chapter 0
- Application practice | the efficiency of the data warehouse system has been comprehensively improved! Data warehouse construction based on Apache Doris in Tongcheng digital Department
- Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch
猜你喜欢
Deep dive kotlin synergy (XXII): flow treatment
fabulous! How does idea open multiple projects in a single window?
AI遮天传 ML-回归分析入门
NVIDIA Jetson测试安装yolox过程记录
C # generics and performance comparison
letcode43:字符串相乘
Analysis of 8 classic C language pointer written test questions
Malware detection method based on convolutional neural network
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
13. Enregistrement et chargement des modèles
随机推荐
Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree
12. RNN is applied to handwritten digit recognition
QT adds resource files, adds icons for qaction, establishes signal slot functions, and implements
Codeforces Round #804 (Div. 2)(A~D)
Kubernetes static pod (static POD)
51 communicates with the Bluetooth module, and 51 drives the Bluetooth app to light up
国内首次,3位清华姚班本科生斩获STOC最佳学生论文奖
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
大二级分类产品页权重低,不收录怎么办?
【笔记】常见组合滤波电路
Langchao Yunxi distributed database tracing (II) -- source code analysis
1293_ Implementation analysis of xtask resumeall() interface in FreeRTOS
Invalid V-for traversal element style
基于微信小程序开发的我最在行的小游戏
DNS series (I): why does the updated DNS record not take effect?
My best game based on wechat applet development
新库上线 | CnOpenData中国星级酒店数据
AI遮天传 ML-回归分析入门
ThinkPHP kernel work order system source code commercial open source version multi user + multi customer service + SMS + email notification
股票开户免费办理佣金最低的券商,手机上开户安全吗