当前位置:网站首页>tf. Implementation of linear regression by keras
tf. Implementation of linear regression by keras
2022-07-19 21:41:00 【booze-J】
article
One 、 Univariate linear regression
Univariate linear regression algorithm ( such as ,x Represents Education ,f(x) Representative income )
Can be listed :f(x) = ax + b
This formula reflects the relationship between education and income .
We use f(x) This function maps the input characteristics to the output values .
Two 、 Problem analysis
Predict the goal : Make the prediction function f(x) The overall error between and the true value is the smallest .
How to define the minimum error ?
At this time, we need to use the loss function .
For example, use the mean square deviation as the loss function . That is, the square of the difference between the predicted value and the real value is taken as the mean .y Represents actual income , Find the right a and b, bring (f(x)-y)^2 Value , The smaller the better. , The smaller it is, the closer the predicted income is to the actual income .
Be careful Now the solution is the parameter a and b, The problem turns into How to solve the appropriate parameters a and b?
At this time, the optimization algorithm of model training will be involved .
For example, an optimization algorithm called gradient descent method is often used in linear regression .
3、 ... and 、 Gradient descent method
Gradient descent method is an algorithm dedicated to finding the extreme point of a function .
So-called “ Study ” That is to improve the model parameters , In order to minimize the loss through a large number of training steps . With this concept , Applying the gradient descent method to find the extreme point of the loss function constitutes the model learning based on the input data .
The output of the gradient is a vector composed of several partial derivatives , Each component of it corresponds to the partial derivative of the function to the corresponding component of the input vector :
The output vector of the gradient indicates the direction in which the loss function grows fastest at each position , It can be regarded as indicating which direction to move in each position of the function , Function value can be increased .
Four 、 Learning rate
Learning rate It is a kind of super parameter or a kind of model Manually configurable The setting of needs to specify the correct value for it . If The learning rate is too small , It may take many rounds to find the minimum point of the loss function ; If it is too big , Then the algorithm may “ skip ” Minimum point and due to periodic “ jumping ” And the minimum point can never be found .
5、 ... and 、 Linear regression example code
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 matplotlib.pyplot as plt
import tensorflow as tf
import pandas as pd
2. Reading data
data = pd.read_csv("/content/drive/MyDrive/learing_data/Income.csv")
data
3. Draw a scatter chart of education and income
%matplotlib inline
plt.scatter(data.Education,data.Income)
You can see that education and income seem to be in a linear relationship , A straight line can be fitted .
4. Build a linear network model
model = tf.keras.models.Sequential()
# Build a full connection layer , Input is 1 Dimensional ( Because only 1 Column input data ), The output is also 1 dimension .
model.add(tf.keras.layers.Dense(1,input_shape=(1,))) # Equivalent to building equations f(x) = ax + b
# View the network model structure
model.summary()
You can see from above Param
yes 2, in other words Fully connected layer (Dense layer ) There are two parameters , One parameter is The weight (a, Equivalent to f(x)=ax+b Medium a), Another parameter is bias (b, Equivalent to f(x)=ax+b Medium b).
5. model training
x = data.Education
y = data.Income
# Use adam Gradient descent method as optimizer , Use mean square error mse As a loss function
model.compile(optimizer="adam",loss="mse")
model.fit(x,y,epochs=50)
6. Model to predict
model.predict(x)# Forecast existing value .
model.predict(pd.Series([20]))
边栏推荐
- Exclusive interview with ringcentral he Bicang: empowering future mixed office with innovative MVP
- 大国数据跨境规则博弈,谁能抢占“数据经济制高地”?
- Gorm cannot update the field to null value by using raw method
- JDBC's own MySQL connection pool practice
- ASML二季度卖出58台光刻机,营收27.4亿欧元!中国大陆贡献两成!
- 经济日报:莫把数字藏品当“生财”产品
- 同星科技IPO过会:年营收7.8亿 张良灿家族是实控人
- Management of jupyter kernel environment
- 备份mysql数据,直接物理复制data_dir指向的目录可以吗?
- 路维光电通过科创板注册:年营收5亿 杜武兵为实控人
猜你喜欢
SAP FICO S_ ALR_ 87012172 and fs10n f.01 do not correspond to some subjects
Matlab digital image processing experiment 3: spatial frequency domain image denoising and sharpening
I still envy that other platforms have cross store full reduction. In fact, you can also!
10GBASE-xx相关知识点
MySQL character set
Information theory collation
LeetCode_40_组合总和Ⅱ
Ban commercial open source to provoke "public anger"? The latest response from Microsoft is coming!
Cluster of Grain Mall
Prometheus getting started (I)
随机推荐
过拟合及过拟合处理
Prometheus入门使用(一)
es6 promise 源码解析
Hengshuo semiconductor has passed the registration: the annual revenue is 580million, and the actual controller is Chinese Americans
mysql用户权限设置
路维光电通过科创板注册:年营收5亿 杜武兵为实控人
Advanced audio and video development | Lecture 4: audio automatic gain control AGC
10GBASE XX related knowledge points
Never pass a request to an asynchronous thread! There are pits!
同星科技IPO过会:年营收7.8亿 张良灿家族是实控人
Kingbasees database administrator's Guide -- 10 manage tablespaces
UNI-APP 中 子组件和父组件之间的传参, a组件修改b组件中的数据,组件之间的通讯方式
美国刚刚解禁中兴,澳大利亚又要封杀华为?
Transfer parameters between sub components and parent components of uni-app, a component modifies the data in B component, and the communication mode between components
jdbc自带MySQL连接池实践
音视频开发进阶|第四讲:音频自动增益控制 AGC
mysql数据库密码
经济日报:莫把数字藏品当“生财”产品
[1152]Mysql报错:message from server: * is blocked because of many
Nearly 30 wafer factories are under construction in China, and half of the key components of conductors are out of stock