pytorch学习笔记一:认识pytorch

8 篇文章 5 订阅
1 篇文章 0 订阅

一、什么是pytorch

PyTorch是一个深度学习框架,它是基于Python的科学计算软件包,可实现两个广泛的目的:

1、替代NumPy,以使用GPU和其他加速器的功能。
2、一个自动微分库,对实现神经网络很有用

所谓的框架就是别人把底层做好了,应用者只需要在框架上搭建自己的城堡就行

二、什么是torch

Torch是一个与Numpy类似的张量(Tensor)操作库,与Numpy不同的是Torch对GPU支持的很好

三、使用pytorch需要理解的几个概念

3.1 张量

张量是一种特殊的数据结构,与数组和矩阵非常相似。在PyTorch中,我们使用张量对模型的输入和输出以及模型的参数进行编码。

张量与NumPy的ndarray相似,除了张量可以在GPU或其他专用硬件上运行以加速计算。如果您熟悉ndarrays,则可以轻松使用Tensor API。

import numpy as np
import torch

data = [[1, 2],[3, 4]]
print(data)
print(type(data))
x_data = torch.tensor(data)
print(x_data)
print(x_data.shape)
print(type(x_data))

结果如下:

[[1, 2], [3, 4]]
<class 'list'>
tensor([[1, 2],
        [3, 4]])
torch.Size([2, 2])
<class 'torch.Tensor'>
import numpy as np
import torch

np_array = np.array(data)
print(np_array)
print(np_array.shape)
print(type(np_array))
x_np = torch.from_numpy(np_array)
print(x_np)
print(x_np.shape)
print(type(x_np))

结果如下:

[[1 2]
 [3 4]]
(2, 2)
<class 'numpy.ndarray'>
tensor([[1, 2],
        [3, 4]], dtype=torch.int32)
torch.Size([2, 2])
<class 'torch.Tensor'>

总结:张量和数组矩阵本质上其实是一个东西,并且用法几乎没用差别。PyTorch张量在概念上与numpy数组相同:张量是n维数组,PyTorch提供了许多在这些张量上进行操作的功能。在后台,张量可以跟踪计算图和渐变,但它们也可用作科学计算的通用工具。

既然tensor和torch没有差别,那为什么还弄出来torch这东西呢?

原因:**Numpy虽然是一个很棒的框架,但是它不能利用GPU来加速其数值计算。**对于现代深度神经网络,GPU通常会提供50倍或更高的加速比,仅凭numpy不足以实现现代深度学习。

PyTorch张量可以利用GPU加速其数字计算。要在GPU上运行PyTorch Tensor,您只需要指定正确的设备即可。

3.2 torch.autograd

torch.autograd是PyTorch的自动差分引擎(即梯度),可为神经网络训练提供支持。
torch.autograd主要涉及两大块内容,前向传播和后向传播

3.3神经网络

pytorch中使用 torch.nn软件包构建神经网络。可以这么理解,在pytorch中构建网络层的时候,只需要在前加上torch.nn即可。如下示例,构建了一个简单的网络

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, 3x3 square convolution
        # kernel
        self.conv1 = nn.Conv2d(1, 6, 3)
        self.conv2 = nn.Conv2d(6, 16, 3)
        # an affine operation: y = Wx + b
        self.fc1 = nn.Linear(16 * 6 * 6, 120)  # 6*6 from image dimension
        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=(3, 3), stride=(1, 1))
  (conv2): Conv2d(6, 16, kernel_size=(3, 3), stride=(1, 1))
  (fc1): Linear(in_features=576, 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)
)

torch.nn仅支持小批量。整个torch.nn 程序包仅支持作为小批量样本的输入,而不支持单个样本。

例如,nn.Conv2d将采用的4D张量 。nSamples x nChannels x Height x Width

如果您只有一个样本,则只需使用input.unsqueeze(0)即可添加伪造的批次尺寸。

四、总结

1、torch.Tensor-多维数组,支持autograd操作(如)backward()。还保持张量的梯度。
2、nn.Module-神经网络模块。方便的封装参数的方式,以及将其移动到GPU,导出,加载等的帮助器。
3、nn.Parameter-一种Tensor,将其作为属性分配给时 会自动注册为参数Module。
4、autograd.Function-实现autograd操作的前向和后向定义。每个Tensor操作都至少创建一个Function节点,该节点连接到创建aTensor并对其历史进行编码的函数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值