pytorch深度学习60分钟闪电战(代码片段)

sdu20112013 sdu20112013     2022-12-03     342

关键词:

https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html
官方推荐的一篇教程

Tensors

#Construct a 5x3 matrix, uninitialized:  
x = torch.empty(5, 3)

#Construct a randomly initialized matrix:  
x = torch.rand(5, 3)

# Construct a matrix filled zeros and of dtype long:
x = torch.zeros(5, 3, dtype=torch.long)

# Construct a tensor directly from data:
x = torch.tensor([5.5, 3])

# create a tensor based on an existing tensor. These methods will reuse properties of the input tensor, e.g. dtype, unless new values are provided by user
x = x.new_ones(5, 3, dtype=torch.double)      # new_* methods take in sizes
print(x)
x = torch.randn_like(x, dtype=torch.float)    # override dtype!    #沿用了x已有的属性,只是修改dtype
print(x)                                      # result has the same size

tensor操作的语法有很多写法,以加法为例

#1
x = x.new_ones(5, 3, dtype=torch.double) 
y = torch.rand(5, 3)
print(x + y)

#2
print(torch.add(x, y))

#3
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)

##注意以_做后缀的方法,都会改变原始的变量
#4 Any operation that mutates a tensor in-place is post-fixed with an _. For example: x.copy_(y), x.t_(), will change x.
# adds x to y
y.add_(x)
print(y)

改变tensor的size,使用torch.view:

x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8)  # the size -1 is inferred from other dimensions
print(x.size(), y.size(), z.size())

输出如下:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])

numpy array和torch tensor的相互转换

  • torch tensor转换为numpy array
a = torch.ones(5)
print(a)
输出tensor([1., 1., 1., 1., 1.])

#torch tensor--->numpy array
b = a.numpy()
print(b)
输出[1. 1. 1. 1. 1.]


#注意!:a,b同时发生了变化
a.add_(1)
print(a)
print(b)
输出tensor([2., 2., 2., 2., 2.])
   [2. 2. 2. 2. 2.]
  • numpy array转换为torch tensor
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)

所有的cpu上的tensor,除了chartensor,都支持和numpy之间的互相转换.

All the Tensors on the CPU except a CharTensor support converting to NumPy and back.

CUDA Tensors

Tensors can be moved onto any device using the .to method.

#let us run this cell only if CUDA is available  
#We will use ``torch.device`` objects to move tensors in and out of GPU  
if torch.cuda.is_available():  
    device = torch.device("cuda")          # a CUDA device object  
    y = torch.ones_like(x, device=device)  # directly create a tensor on GPU  
    x = x.to(device)                       # or just use strings ``.to("cuda")``  
    z = x + y  
    print(z)  
    print(z.to("cpu", torch.double))       # ``.to`` can also change dtype together!  
    
--->
tensor([0.6635], device='cuda:0')
tensor([0.6635], dtype=torch.float64)

AUTOGRAD

The autograd package provides automatic differentiation for all operations on Tensors. It is a define-by-run framework, which means that your backprop is defined by how your code is run, and that every single iteration can be different.

Generally speaking, torch.autograd is an engine for computing vector-Jacobian product

.requires_grad属性设为true,则可以追踪tensor上的所有操作(比如加减乘除)

torch.Tensor is the central class of the package. If you set its attribute .requires_grad as True, it starts to track all operations on it. When you finish your computation you can call .backward() and have all the gradients computed automatically. The gradient for this tensor will be accumulated into .grad attribute.

autograd包实现自动的求解梯度.

神经网络

torch.nn包可以用来构建神经网络.
nn依赖autogard来不断地更新model中各层的参数. nn.Module包含layers,forward方法.
技术图片

典型的神经网络的训练过程如下:

  • 定义一个神经网络,含learnable parameters或者叫weights
  • 对数据集的所有数据作为Input输入网络
  • 计算loss
  • 反向传播计算weights的梯度
  • 更新weights,一个典型的简单规则:weight = weight - learning_rate * gradient

定义网络

import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):

    def __init__(self):
        super(Net, self).__init__()
        # 1 input image channel, 6 output channels, 5x5 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 5)    #输入是1个矩阵,输出6个矩阵,filter是5*5矩阵.即卷积层1使用6个filter.
        self.conv2 = nn.Conv2d(6, 16, 5)   #输入是6个矩阵,输出16个矩阵,filter是5*5矩阵.即卷积层2使用16个filter.
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 5 * 5, 120)   #全连接层,fc=fullconnect  作用是分类
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        # Max pooling over a (2, 2) window
        x = F.max_pool2d(F.relu(self.conv1(x)), (2, 2))
        # If the size is a square you can only specify a single number
        x = F.max_pool2d(F.relu(self.conv2(x)), 2)
        x = x.view(-1, self.num_flat_features(x))
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def num_flat_features(self, x):
        size = x.size()[1:]  # all dimensions except the batch dimension
        num_features = 1
        for s in size:
            num_features *= s
        return num_features


net = Net()
print(net)

输出如下:
Net(
  (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))
  (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))
  (fc1): Linear(in_features=400, out_features=120, bias=True)
  (fc2): Linear(in_features=120, out_features=84, bias=True)
  (fc3): Linear(in_features=84, out_features=10, bias=True)
)

你只需要定义forward函数,backward函数(即计算梯度的函数)是autograd包自动定义的.你可以在forward函数里使用任何tensor操作.

model的参数获取.

params = list(net.parameters())
print(len(params))
print(params[0].size())  # conv1's .weight

输出如下:
10
torch.Size([6, 1, 5, 5])

以MNIST识别为例,输入图像为3232.我们用一个随机的3232输入演示一下.

input = torch.randn(1, 1, 32, 32)
out = net(input)
print(out)

输出如下:
tensor([[ 0.0659, -0.0456,  0.1248, -0.1571, -0.0991, -0.0494,  0.0046, -0.0767,
         -0.0345,  0.1010]], grad_fn=<AddmmBackward>)
         

#清空所有的parameter的gradient buffer.用随机的梯度反向传播。
#Zero the gradient buffers of all parameters and backprops with random gradients:
net.zero_grad()
out.backward(torch.randn(1, 10))

回忆一下部分概念

  • torch.Tensor - A multi-dimensional array with support for autograd operations like backward(). Also holds the gradient w.r.t. the tensor.
  • nn.Module - Neural network module. Convenient way of encapsulating parameters, with helpers for moving them to GPU, exporting, loading, etc.
  • nn.Parameter - A kind of Tensor, that is automatically registered as a parameter when assigned as an attribute to a Module.
  • autograd.Function - Implements forward and backward definitions of an autograd operation. Every Tensor operation creates at least a single Function node that connects to functions that created a Tensor and encodes its history.

损失函数

nn package有好几种损失函数.以nn.MSELoss为例

output = net(input)
target = torch.randn(10)  # a dummy target, for example
target = target.view(1, -1)  # make it the same shape as output
criterion = nn.MSELoss()

loss = criterion(output, target)
print(loss)

输出
tensor(0.6918, grad_fn=<MseLossBackward>)

Now, if you follow loss in the backward direction, using its .grad_fn attribute, you will see a graph of computations that looks like this:
input -> conv2d -> relu -> maxpool2d -> conv2d -> relu -> maxpool2d
-> view -> linear -> relu -> linear -> relu -> linear
-> MSELoss
-> loss

print(loss.grad_fn)  # MSELoss
print(loss.grad_fn.next_functions[0][0])  # Linear
print(loss.grad_fn.next_functions[0][0].next_functions[0][0])  # ReLU
输出如下:
<MseLossBackward object at 0x7ff3406e1be0>
<AddmmBackward object at 0x7ff3406e1da0>
<AccumulateGrad object at 0x7ff3406e1da0>

反向传播

调用loss.backward()重新计算梯度

#首先清空现有的gradient buffer
net.zero_grad()     # zeroes the gradient buffers of all parameters

print('conv1.bias.grad before backward')
print(net.conv1.bias.grad)

loss.backward()

print('conv1.bias.grad after backward')
print(net.conv1.bias.grad)

输出如下:
conv1.bias.grad before backward
tensor([0., 0., 0., 0., 0., 0.])
conv1.bias.grad after backward
tensor([-0.0080,  0.0043, -0.0006,  0.0142, -0.0017, -0.0082])

更新权重

最常见的是使用随机梯度下降法更新权重:
weight = weight - learning_rate * gradient
简单实现如下

learning_rate = 0.01
for f in net.parameters():
    f.data.sub_(f.grad.data * learning_rate)

torch.optim包封装了各种各样的优化方法, SGD, Nesterov-SGD, Adam, RMSProp等等.

import torch.optim as optim

# create your optimizer
optimizer = optim.SGD(net.parameters(), lr=0.01)

# in your training loop:
optimizer.zero_grad()   # zero the gradient buffers
output = net(input)
loss = criterion(output, target)
loss.backward()
optimizer.step()    # Does the update

训练一个分类器

并行计算

pytorch60分钟入门教程:pytorch深度学习官方入门中文教程(代码片段)

什么是 PyTorch?PyTorch是一个基于Python的科学计算包,主要定位两类人群:NumPy的替代品,可以利用GPU的性能进行计算。深度学习研究平台拥有足够的灵活性和速度开始学习Tensors(张量)Tensors类似于NumPy的ndarrays,同时 Tensors可... 查看详情

pytorch深度学习:60分钟入门(translation)

这是https://zhuanlan.zhihu.com/p/25572330的学习笔记。 TensorsTensors和numpy中的ndarrays较为相似,因此Tensor也能够使用GPU来加速运算。from__future__importprint_functionimporttorchx=torch.Tensor(5,3)#构造一个未初始化的5*3的矩阵x=torch.rand 查看详情

pytorch深度学习60分钟快速入门part1:pytorch是什么?

 0x00PyTorch是什么?PyTorch是一个基于Python的科学计算工具包,它主要面向两种场景:用于替代NumPy,可以使用GPU的计算力一种深度学习研究平台,可以提供最大的灵活性和速度0x01开始学习1、TensorsTensors(张量)类似于numpy的ndar... 查看详情

pytorch深度学习60分钟快速入门part0:系列介绍

 说明:本系列教程翻译自PyTorch官方教程《DeepLearningwithPyTorch:A60MinuteBlitz》 教程目标在高层次上理解PyTorch的Tensor库和神经网络训练一个小型的神经网络来分类图像前提条件假设读者熟悉基础的NumPy库确保已经安装了torch和t... 查看详情

pytorch深度学习60分钟快速入门part2:autograd自动化微分

 在PyTorch中,集中于所有神经网络的是autograd包。首先,我们简要地看一下此工具包,然后我们将训练第一个神经网络。autograd包为张量的所有操作提供了自动微分。它是一个运行式定义的框架,这意味着你的后向传播是由你... 查看详情

pytorch深度学习60分钟快速入门part3:神经网络

 神经网络可以通过使用torch.nn包来构建。既然你已经了解了autograd,而nn依赖于autograd来定义模型并对其求微分。一个nn.Module包含多个网络层,以及一个返回输出的方法forward(input)。例如,查看下图中的对数字图片分类的网络... 查看详情

pytorch60分钟入门教程:数据并行处理(代码片段)

...个教程中,我们将学习如何用DataParallel来使用多GPU。通过PyTorch使用多个GPU非常简单。你可以将模型放在一个GPU:device=torch.device("cuda:0")model.to(device)然后,你可以复制所有的张量到GPU:mytensor=my_te 查看详情

[pytorch学习]2.官网60分钟教程摘要(代码片段)

 https://pytorch.org/tutorials/beginner/deep_learning_60min_blitz.html1.Pytorch的基本单元,tensor,本质上和numpy的操作类似;不同的主要在与可以自动计算微分和梯度(Autograd);2.每个tensor的requires_grad设置为True时,就能够自动计算梯度;操... 查看详情

深度学习之30分钟快速入门pytorch(附学习资源推荐)(代码片段)

目录1、Pytorch简介1.0如何使用本教程1.1PyTorch由来1.2Torch简介1.3重新认识PyTorch1.4PyTorch和Tensorflow的对比1.5总结2、Pytorch环境搭建2.1安装Pytorch2.2配置JupyterNotebook2.3测试3、张量4、自动求导5、神经网络6、用cifar10训练一个分类器7、数据... 查看详情

pytorch学习笔记基础知识

PyTorch深度学习:60分钟入门(Translation) 查看详情

深度学习——pytorch基础(代码片段)

深度学习(1)——Pytorch基础作者:夏风喃喃参考:《动手学深度学习第二版》李沐文章目录深度学习(1)——Pytorch基础一.数据操作二.数据预处理三.绘图四.自动求导五.概率论导入相关包:importtorch ... 查看详情

深度学习——pytorch基础(代码片段)

深度学习(1)——Pytorch基础作者:夏风喃喃参考:《动手学深度学习第二版》李沐文章目录深度学习(1)——Pytorch基础一.数据操作二.数据预处理三.绘图四.自动求导五.概率论导入相关包:importtorch ... 查看详情

深度学习pytorch大坑处理(代码片段)

深度学习pytorch大坑处理importtoralprint(torch.cuda.is_available())//false解决方法.https://download.pytorch.org/whl/torch_stable.html用迅雷下载进入AnacondaPrompt,输入condaactivatepytorch下载好torchvision-0.10.1+cu1 查看详情

pytorch学习笔记3.深度学习基础(代码片段)

...多分类22.全连接层23.激活函数与GPU加速24.测试根据龙良曲Pytorch学习视频整理,视频链接:【计算机-AI】PyTorch学这个就够了!(好课推荐)深度学习与PyTorch入门实战——主讲人龙 查看详情

[九]深度学习pytorch-transforms图像增强(剪裁翻转旋转)(代码片段)

0.往期内容[一]深度学习Pytorch-张量定义与张量创建[二]深度学习Pytorch-张量的操作:拼接、切分、索引和变换[三]深度学习Pytorch-张量数学运算[四]深度学习Pytorch-线性回归[五]深度学习Pytorch-计算图与动态图机制[六]深度学习Pyto... 查看详情

深度学习pytorch——数据加载和处理(代码片段)

深度学习Pytorch(五)——数据加载和处理文章目录深度学习Pytorch(五)——数据加载和处理一、下载安装包二、下载数据集三、读取数据集四、编写一个函数看看图像和landmark五、数据集类六、数据可视化七、数... 查看详情

《动手学深度学习》(pytorch版)(代码片段)

《动手学深度学习》PyTorch版前言简介面向人群食用方法方法一方法二方法三目录原书地址引用阅读指南前言读书啦!!!本项目将《动手学深度学习》原书中MXNet代码实现改为PyTorch实现。原书作者:阿斯顿·张、... 查看详情

《深度学习笔记》——pytorch调整学习率(代码片段)

1定义调整学习率函数defadjust_learning_rate(optimizer,epoch,lr):"""SetsthelearningratetotheinitialLRdecayedby10every2epochs"""#optimizer表示优化器对象lr*=(0.1**(epoch//2))forparam_groupinop 查看详情