当前位置:网站首页>Pytorch随记(2)
Pytorch随记(2)
2022-07-17 05:24:00 【心灵在路上】
在刘老师第五节课和第六节课中讲解了线性回归和逻辑斯蒂回归,有兴趣的道友可以去B站搜刘二大人。
线性回归

- 其中 y ^ \hat{y} y^,可以理解为观测值,线性方程的作用就是构建函数求出观测值,之后根据所选择的损失函数,对观测值和实际值求出误差。
四步走
- 1.准备数据集
- 2.设计模型
- 3.构造损失函数和优化器
- 4.循环训练(forward backward update)
# SGD AND LinearModel
import torch
# 忽略警告
import warnings
warnings.filterwarnings("ignore")
#准备数据集
x_data = torch.Tensor([[1.0], [2.0],[3.0]])
y_data = torch.Tensor([[2.0], [4.0],[6.0]])
#设计模型
class LinearModel(torch.nn.Module):#这里继承torch的module,只有forward没有backword的原因是,module会自动的进行反向传播
def __init__(self):
super(LinearModel,self).__init__() # 调用父类 初始化的过程
self.linear = torch.nn.Linear(1,1) #就是调用了线性函数的类,y= Ax +b 并给定初始值1,1
def forward(self, x):
y_pred = self.linear(x)
return y_pred
model = LinearModel()
#构造损失函数和优化器
def SGD_LIST_Loss():
#SDG_LIST_new = [] #这里创建列表的原因是,后续做可视化展示
criterion = torch.nn.MSELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
#
# optim中很多优化器,Adadelta、Adagrad、Adam等 后续想展示不同的曲线,直接把SGD修改为相应的名字就可
#有兴趣的道友可以去看看文档 https://pytorch.org/docs/stable/optim.html
for epoch in range(100):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
print(epoch, loss.item())
#SDG_LIST_new.append(loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
print("w = ", model.linear.weight.item())
print("b = ", model.linear.bias.item())
x_test = torch.Tensor([4.0])
y_test = model(x_test)
print("y_pred = ", y_test.data)
#这里提供一种简单的可视化代码 供道友阅读
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['Simhei'] #显示中文
x = range(len(Adam_list))
plt.figure(figsize=(30,10))
plt.title("损失函数趋势图")
plt.xlabel("迭代次数")
plt.ylabel("LOSS值")
plt.plot(x,SDG_LIST_new,'-',color='c',label="SDG_LIST")
plt.legend()
逻辑回归
介绍

- 逻辑回归不是用来解决回归问题,而是用来解决分类问题。
- 逻辑回归和线性回归的区别就是,逻辑回归加入了SIGMOD函数,因为是用来解决分类问题的,所以最后需要的是一个概率值,也就是我们所说的P(X=x)。
- 为什么用sigmod函数 因为sigmod函数有很好的的先天条件,比如取值区间[0,1],是增函数,是饱和函数(当X取到一定值的之后,y趋近于极值)。
- sigmod函数其实有很多种但是目前用的最多,也是最有代表性的
s i g m o d = 1 1 + e − x sigmod = \frac{1}{1+e^{-x}} sigmod=1+e−x1
所以大家约定俗称,这个函数就是sigmod函数。
以下是其他的sigmod函数
损失函数

- 逻辑回归中我们用到的损失函数称为BCE。
- 当我们之后对小批量数据进行计算的时候,我们会用到Mini-Batch,也就是对Loss求和之后求平均,为什么加负号呢?跟梯度下降和梯度上升同理,本来是求出的值越大越好,加上负号就是越小越好。
# 逻辑回归
import torchvision
#这里root参数是你数据保存的位置,如果你直接用这行代码有可能会报错,
# 你可以直接放到根目录,也可以新建一个文件夹放进去,注意要修改路径
train_set = torchvision.datasets.MNIST(root="./datasets/mnist", train = True, download= True)
test_set = torchvision.datasets.MNIST(root="./datasets/mnist", train = False, download= True)
#还有别的库,里面是一些不同种类的图片,这里下载很慢很慢,有差不多1700W个数据
train_set_new = torchvision.datasets.CIFAR10(root="./datasets/mnist", train = True, download= True)
test_set_new = torchvision.datasets.CIFAR10(root="./datasets/mnist", train = True, download= True)
这里的代码和线性回归基本一致,只需要修改一些函数名就可以了。
# 1、准备数据
# 2、确定模型
# 3、构建损失和优化器
# 4、训练
#你要是直接跑的话,可能会报错,原因是anconda中的libiomp5md.dll和库函数冲突,可以加上下面这两行代码,就不会报错了。
import os
os.environ['KMP_DUPLICATE_LIB_OK'] = 'True'
import torch.nn.functional as F
import torch
# 忽略警告
import warnings
warnings.filterwarnings("ignore")
x_data = torch.Tensor([[1.0], [2.0],[3.0]])
y_data = torch.Tensor([[0], [0],[1]])
class LogisticRegressionModel(torch.nn.Module):#这里继承torch的module,只有forward没有backword的原因是,module会自动的进行反向传播
def __init__(self):
super(LogisticRegressionModel,self).__init__() # 调用父类 初始化的过程
self.linear = torch.nn.Linear(1,1) #就是调用了线性函数的类,y= Ax +b 并给定初始值1,1
def forward(self, x):
y_pred = F.sigmoid(self.linear(x))
return y_pred
model = LogisticRegressionModel()
criterion = torch.nn.BCELoss(size_average=False)
optimizer = torch.optim.SGD(model.parameters(), lr = 0.01)
for epoch in range(1000):
y_pred = model(x_data)
loss = criterion(y_pred, y_data)
# print(epoch, loss.item())
optimizer.zero_grad()
loss.backward()
optimizer.step()
import numpy as np
import matplotlib.pyplot as plt
x=np.linspace(0,10,200)
x_t = torch.Tensor(x).view((200,1))
y_t = model(x_t)
y = y_t.data.numpy()
plt.plot(x,y)
plt.plot([0,10],[0.5,0.5], c='r')
plt.xlabel ('Hours')
plt.ylabel('Probability of Pass')
plt.grid()
plt.show()

边栏推荐
猜你喜欢

3D可视化入门基础:看渲染管线如何在GPU运作

express

Introduction to arm development environment

Ivew shuttle box transfer component highlights the operation value

day01(Flume)

This article introduces you to SOA interface testing

How to output a digital diamond with a for loop

【操作细则】如何实现TSN系统级测试?

Hypothesis testing

Administrator blocked this app from running
随机推荐
Application of A2B audio bus in intelligent cockpit
Ivew shuttle box transfer component highlights the operation value
Arrayslist method
MySql
Redis details
express
Understanding of v2x test series v2x phase II application scenario
V2X测试系列之认识V2X第二阶段应用场景
Spark入门到精通-番外篇(Standaone集群的运维和简单操作)
How to output a digital diamond with a for loop
Jenkins如何设置邮箱自动发送邮件?
Extortion virus of enterprise data governance
Review of Linear Algebra
Where is the future of HMI conceptual design?
网络知识-04 网络层-ICMP协议
Tail -f pause method
Network knowledge-03 data link layer PPPoE
What is the difference between SD NAND and nandflash?
Edit close automatically generate configuration file when saving
Product Case Interviews