当前位置:网站首页>1.线性回归
1.线性回归
2022-07-07 23:11:00 【booze-J】
代码运行平台为jupyter-notebook,文章中的代码块,也是按照jupyter-notebook中的划分顺序进行书写的,运行文章代码,直接分单元粘入到jupyter-notebook即可。
1.导入第三方库
import keras
import numpy as np
import matplotlib.pyplot as plt
# Sequential 按顺序构成的模型
from keras.models import Sequential
# Dense 全连接层
from keras.layers import Dense
2.随机生成数据集
# 使用numpy生成100个随机点
x_data = np.random.rand(100)
# 噪音的形状和x_data的形状是一样的
noise = np.random.normal(0,0.01,x_data.shape)
# 设置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
# 显示随机点
plt.scatter(x_data,y_data)
# plt.scatter(x_data,y_data_no_noisy)
运行效果:
这是添加噪声的情况下y_data = x_data*0.1+0.2+noise:
不添加噪声的情况下y_data_no_noisy = x_data*0.1+0.2(w=0.1,b=0.2):

线性回归就是要根据添加噪声的散点图,拟合出一条与不添加噪声的散点图近似的直线。
3.线性回归
# 构建一个顺序模型
model = Sequential()
# 在模型中添加一个全连接层 在jupyter-notebook中,按shift+tab可以显示参数
model.add(Dense(units=1,input_dim=1))
# sgd:Stochastic gradient descent , 随机梯度下降法
# mse:Mean Squared Error , 均方误差
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)
print("b:",b)
# x_data输入网络中得到预测值
y_pred = model.predict(x_data)
# 显示随机点
plt.scatter(x_data,y_data)
# 显示预测结果
plt.plot(x_data,y_pred,"r-",lw=3)
plt.show()
运行效果:
可以看到预测出来的w和b都十分接近我们设置的w和b。
注意
- 在jupyter-notebook中,按shift+tab可以显示参数
- train_on_batch的使用
- compile的使用
边栏推荐
- New library online | information data of Chinese journalists
- 《因果性Causality》教程,哥本哈根大学Jonas Peters讲授
- Service mesh introduction, istio overview
- Fofa attack and defense challenge record
- Malware detection method based on convolutional neural network
- QT establish signal slots between different classes and transfer parameters
- Course of causality, taught by Jonas Peters, University of Copenhagen
- [Yugong series] go teaching course 006 in July 2022 - automatic derivation of types and input and output
- ABAP ALV LVC模板
- 【obs】官方是配置USE_GPU_PRIORITY 效果为TRUE的
猜你喜欢

Kubernetes static pod (static POD)

C # generics and performance comparison

13.模型的保存和载入

基于卷积神经网络的恶意软件检测方法
![[note] common combined filter circuit](/img/2f/a8c2ef0d76dd7a45b50a64a928a9c8.png)
[note] common combined filter circuit

Binder core API

Semantic segmentation model base segmentation_ models_ Detailed introduction to pytorch

Codeforces Round #804 (Div. 2)(A~D)

Analysis of 8 classic C language pointer written test questions

Service mesh introduction, istio overview
随机推荐
Vscode software
What has happened from server to cloud hosting?
Cascade-LSTM: A Tree-Structured Neural Classifier for Detecting Misinformation Cascades(KDD20)
ReentrantLock 公平锁源码 第0篇
SDNU_ACM_ICPC_2022_Summer_Practice(1~2)
基于人脸识别实现课堂抬头率检测
Basic principle and usage of dynamic library, -fpic option context
Binder core API
Is it safe to speculate in stocks on mobile phones?
图像数据预处理
Introduction to paddle - using lenet to realize image classification method I in MNIST
SDNU_ ACM_ ICPC_ 2022_ Summer_ Practice(1~2)
They gathered at the 2022 ecug con just for "China's technological power"
fabulous! How does idea open multiple projects in a single window?
Where is the big data open source project, one-stop fully automated full life cycle operation and maintenance steward Chengying (background)?
What does interface testing test?
【愚公系列】2022年7月 Go教学课程 006-自动推导类型和输入输出
The standby database has been delayed. Check that the MRP is wait_ for_ Log, apply after restarting MRP_ Log but wait again later_ for_ log
语义分割模型库segmentation_models_pytorch的详细使用介绍
Implementation of adjacency table of SQLite database storage directory structure 2-construction of directory tree