PyTorch学习笔记3—PyTorch深度学习入门(一)—基本方法
本篇是pytorch学习笔记系列第三篇,这一系列博客应该大多会来自于开源教程书:pytorch中文手册(pytorch handbook),其github网址为:https://github.com/zergtant/pytorch-handbook 本篇博客内容来自于这一手册以及pytorch官方的教程 ,感兴趣的观众门可以去自行下载
1. 基本方法
Tensors与Numpy中的 ndarrays类似,但是在PyTorch中 Tensors 可以使用GPU进行计算.
1.1 创建未初始化的矩阵
创建一个 a 5x3 矩阵, 但是未初始化:
from __future__ import print_function
import torch
x = torch.empty(5, 3)
print(x)
结果:
tensor([[9.2755e-39, 8.9082e-39, 9.9184e-39],
[8.4490e-39, 9.6429e-39, 1.0653e-38],
[1.0469e-38, 4.2246e-39, 1.0378e-38],
[9.6429e-39, 9.2755e-39, 9.7346e-39],
[1.0745e-38, 1.0102e-38, 9.9184e-39]])
1.2 创建一个随机初始化的矩阵
x = torch.rand(5, 3)
print(x)
结果
tensor([[0.6642, 0.0362, 0.0539],
[0.4637, 0.7058, 0.7993],
[0.7591, 0.8579, 0.5384],
[0.5536, 0.4386, 0.0536],
[0.8988, 0.5459, 0.6196]])
1.3 创建一个0填充的矩阵,数据类型为long
x = torch.zeros(5, 3, dtype=torch.long)
print(x)
结果:
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
1.4 创建tensor并使用现有数据初始化
x = torch.tensor([5.5, 3])
print(x)
结果:
tensor([5.5000, 3.0000])
根据现有的张量创建张量。 这些方法将重用输入张量的属性,例如, dtype,除非设置新的值进行覆盖
x = x.new_ones(5, 3, dtype=torch.double) # new_* 方法来创建对象
print(x)
x = torch.randn_like(x, dtype=torch.float) # 覆盖 dtype!
print(x) # 对象的size 是相同的,只是值和类型发生了变化
结果为:
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
tensor([[ 0.5691, -2.0126, -0.4064],
[-0.0863, 0.4692, -1.1209],
[-1.1177, -0.5764, -0.5363],
[-0.4390, 0.6688, 0.0889],
[ 1.3334, -1.1600, 1.8457]])
1.5 获取 size
注:使用size方法与Numpy的shape属性返回的相同,张量也支持shape属性,后面会详细介绍
print(x.size())
torch.Size([5, 3])
Note:torch.Size
返回值是 tuple类型, 所以他支持tuple类型的所有操作.
1.6 加法
加法1:
y = torch.rand(5, 3)
print(x + y)
tensor([[ 0.7808, -1.4388, 0.3151],
[-0.0076, 1.0716, -0.8465],
[-0.8175, 0.3625, -0.2005],
[ 0.2435, 0.8512, 0.7142],
[ 1.4737, -0.8545, 2.4833]])
加法2
print(torch.add(x, y))
tensor([[ 0.7808, -1.4388, 0.3151],
[-0.0076, 1.0716, -0.8465],
[-0.8175, 0.3625, -0.2005],
[ 0.2435, 0.8512, 0.7142],
[ 1.4737, -0.8545, 2.4833]])
提供输出tensor作为参数
result = torch.empty(5, 3)
torch.add(x, y, out=result)
print(result)
tensor([[ 0.7808, -1.4388, 0.3151],
[-0.0076, 1.0716, -0.8465],
[-0.8175, 0.3625, -0.2005],
[ 0.2435, 0.8512, 0.7142],
[ 1.4737, -0.8545, 2.4833]])
将 x 加给 y
y.add_(x)
print(y)
tensor([[ 0.7808, -1.4388, 0.3151],
[-0.0076, 1.0716, -0.8465],
[-0.8175, 0.3625, -0.2005],
[ 0.2435, 0.8512, 0.7142],
[ 1.4737, -0.8545, 2.4833]])
Note:任何 以
_
结尾的操作都会用结果替换原变量. 例如:x.copy_(y)
,x.t_()
, 都会改变
x
.
你可以使用与NumPy索引方式方式的操作来进行对张量的操作
print(x[:, 1])
tensor([-2.0126, 0.4692, -0.5764, 0.6688, -1.1600])
1.7 torch.view改变张量的维度和大小
torch.view 与Numpy的resharp类似
x = torch.randn(4, 4)
y = x.view(16)
z = x.view(-1, 8) # size -1 从其他维度推断
print(x.size(), y.size(), z.size())
torch.Size([4, 4]) torch.Size([16]) torch.Size([2, 8])
1.8 .item()获取标量的数值
如果只有一个元素,使用.item()来得到标量的数值
x = torch.randn(1)
print(x)
print(x.item())
结果:
tensor([-0.2368])
-0.23680149018764496
更多的tensor操作,可以查看pytorch官网说明
1.9 Tensor基本数据类型
Tensor的基本数据类型有五种:
32位浮点型:torch.FloatTensor。 (默认)
64位整型:torch.LongTensor。
32位整型:torch.IntTensor。
16位整型:torch.ShortTensor。
64位浮点型:torch.DoubleTensor。
除以上数字类型外,还有 byte和chart型
1.10 NumPy 转换
可以将tensor张量转换成numpy数组,转换后双方将共享存储单元,当改变一者,另一者也将改变,且看下例
1.10.1 tensor转numpy数组
a = torch.ones(5)
print(a)
b = a.numpy()
print(b)
结果:
tensor([1., 1., 1., 1., 1.])
[1. 1. 1. 1. 1.]
之后对a这个tensor张量加1,分别输出
a.add_(1)
print(a)
print(b)
结果发现b也会相应的改变
tensor([2., 2., 2., 2., 2.])
[2. 2. 2. 2. 2.]
1.10.2 numpy数组转tensor
使用from_numpy()函数
import numpy as np
a = np.ones(5)
b = torch.from_numpy(a)
np.add(a, 1, out=a)
print(a)
print(b)
可以发现当a这个numpy数组加1后,b这个张量也会相应改变
[2. 2. 2. 2. 2.]
tensor([2., 2., 2., 2., 2.], dtype=torch.float64)
1.11 CUDA 张量
使用.to 方法 可以将Tensor移动到任何设备中
创建时指定device可以直接在指定的device中创建
# is_available 函数判断是否有cuda可以使用
# torch.device将张量移动到指定的设备中
if torch.cuda.is_available():
device = torch.device("cuda:0") # 获得CUDA设备对象
y = torch.ones_like(x, device=device) # 通过指定device直接从GPU创建张量
x = x.to(device) # 或者直接使用.to("cuda")将张量移动到cuda中
z = x + y
print(z)
print(z.to("cpu", torch.double)) # .to也可以对变量的类型做更改
结果为:
tensor([0.7632], device='cuda:0')
tensor([0.7632], dtype=torch.float64)
本篇完…
本系列已更新的学习笔记:
PyTorch学习笔记1—PyTorch简介
PyTorch学习笔记2—win10下pytorch-gpu安装以及CUDA安装记录
PyTorch学习笔记3—PyTorch深度学习入门(一)—基本方法
PyTorch学习笔记4—PyTorch深度学习入门(二)—自动求导
PyTorch学习笔记5—PyTorch深度学习入门(三)—神经网络
PyTorch学习笔记6—PyTorch深度学习入门(四)—入门实例