pytorch的基本概念
一、 pytorch介绍
1.1 什么是pytorch
这是一个基于Python的科学计算软件包,针对两组受众:
- NumPy的替代品,可以使用GPU的强大功能
- 深度学习研究平台,提供最大的灵活性和速度
1.2 为什么选择pytorch
- 易用性:容易学,易上手,可以快速进行学习与运用
- 流行性:当前的流行框架之一,另外还有tensorflow等
- 简洁:API接口简洁,容易理解及记忆
二、pytorch安装
2.1 anaconda下载与安装
我实在anaconda环境下进行安装的,
首先下载anaconda,点击download
然后选择相应平台及python版本进行下载
我选择的是Python3.7
2.2 环境配置
下载完之后,进行创建虚拟环境。
首先找到anaconda prompt
然后输入命令
创建环境
conda create -n env_name [python=3.x]
以上env_name为你自己想取得名字,[]里面可以指定python或者其他包版本,若不知指定python版本 则默认安装最新的python版本。
激活环境
conda activate env_name
安装pytorch
https://pytorch.org/get-started/locally/
根据自己的硬件环境和软件版本以及cuda版本安装pytorch【注意cuda一般需要提前安装,可百度如何安装cuda】,若需要安装cpu版本,则CUDA选择None
笔者环境为:win10+cuda9+conda+python3.7
故命令为
conda install pytorch torchvision cudatoolkit=9.0 -c pytorch
2.3 环境测试
安装测试
第一行为:测试是否安装pytorch成功
第二行为:测试pytoch的gpu版本是否成功
三、pytorch基础知识
3.1 A 60 MINUTE BLITZ(60分钟快速了解)
1 What is pytorch(什么是pytorch)
1.1 Tensors(张量)
张量与NumPy的ndarray类似,另外还有Tensors也可用于GPU以加速计算。
导入torch
from __future__ import print_function
import torch
构造5*3矩阵吗,未初始化
x = torch.empty(5, 3)
print(x)
out
tensor([[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.],
[0., 0., 0.]])
构建随机初始化矩阵
x = torch.rand(5, 3)
print(x)
Out:
tensor([[0.5728, 0.5375, 0.0494],
[0.2820, 0.1853, 0.8619],
[0.0856, 0.8380, 0.8117],
[0.7959, 0.8802, 0.3610],
[0.4440, 0.4028, 0.2289]])
构建一个矩阵都是0,类型为long
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
out
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
直接从数据中构建张量
x = torch.tensor([5.5, 3])
print(x)
out
tensor([5.5000, 3.0000])
或者根据现有的张量创建张量。 这些方法将重用输入张量的属性,例如, dtype,除非用户提供新值
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!
print(x)
Out:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.3928, -0.4377, -0.6426],
[ 0.6000, 0.1942, -0.9790],
[-3.0629, 0.2410, -1.5378],
[ 0.0219, 0.5899, 0.8386],
[-0.1540, 0.2724, 0.3881]])
获得其大小
print(x.size())
out:
torch.Size([5, 3])
注意:torch.Size实际上是一个元组,因此它支持所有元组操作。
1.2 基本操作(加、减等)
操作有多种语法。 在下面的示例中,我们将查看添加操作。
加:第一种:
y = torch.rand(5, 3)
print(x + y)
out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
加:第二种
print(torch.add(x, y))
Out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
加法:提供输出张量作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
Out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
加法,in-place(改变原有的)
# adds x to y
y.add_(x)
print(y)
Out:
tensor([[ 1.0653, 0.1367, -0.1789],
[ 1.2751, 0.3061, -0.2860],
[-2.9511, 0.3313, -1.5280],
[ 0.5614, 1.4668, 1.7641],
[ 0.1087, 0.8200, 0.5692]])
任何用_后固定的操作。 例如:x.copy_(y),x.t_(),将改变x。
Resizing(改变张量大小):如果你想调整/重塑张量,你可以使用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())
out:
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
如果你有一个元素张量,使用.item()将值作为Python数字
x = torch.randn(1)
print(x)
print(x.item())
Out:
tensor([-1.7816])
-1.7815848588943481
1.3 Numpy 和Tensor转换
将Torch Tensor转换为NumPy阵列(反之亦然)是一件轻而易举的事。
Converting a Torch Tensor to a NumPy Array(将tensor转换为Numpy)
a = torch.ones(5)
print(a)
Out:
tensor([1., 1., 1., 1., 1.])
b = a.numpy()
print(b)
out:
[1. 1. 1. 1. 1.]
a.add_(1)
print(a)
print(b)
out:
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
注意:Torch Tensor和NumPy阵列将共享其底层内存位置,而更改一个将改变其他位置
1.4 Numpy 转换为Tensor
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
out:
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
除了CharTensor之外,CPU上的所有Tensors都支持转换为NumPy并返回。
1.5 CUDA Tensors
可以使用.to方法将张量移动到任何设备上。
# 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!
Out:
tensor([-0.7816], device='cuda:0')
tensor([-0.7816], dtype=torch.float64)