1、Tensor概念
张量是一个多维数组,它是标量、向量、矩阵的高维拓展
dtype:张量的数据类型,如 torch.FloatTensor, torch.cuda.FloatTensor
shape: 张量的形状,如(64,3,224,224)
device:张量所在设备,GPU、CPU
| 数据类型 | CPU Tensor | GPU Tensor |
|---|---|---|
| 32位浮点 | torch.FloatTensor | torch.cuda.FloatTensor |
| 64位浮点 | torch.DoubleTensor | torch.cuda.DoubleTensor |
| 16位半精度浮点 | N/A | torch.cuda.HalfTensor |
| 8位无符号整型 | torch.ByteTensor | torch.cuda.ByteTensor |
| 8位有符号整型 | torch.CharTensor | torch.cuda.CharTensor |
| 16位有符号整型 | torch.ShortTensor | torch.cuda.ShortTensor |
| 32位有符号整型 | torch.IntTensor | torch.cuda.IntTensor |
| 64位有符号整型 | torch.LongTensor | torch.cuda.LongTensor |
tensor之间类型转换可以通过type(new_type)、type_as()、int()等多种方式
# 创建新Tensor, 默认类型为torch.FloatTensor
a = torch.Tensor(2, 2)
print(a)
# 使用 int()、float()、double()等直接进行数据类型转换
b = a.double()
print(b)
# 使用type()函数
c = a.type(torch.DoubleTensor)
print(c)
# 使用type_as()函数
d = a.type_as(b)
print(d)
tensor([[-1.3968e+18, 4.5666e-41],
[-1.5290e-27, 8.1363e+07]])
tensor([[-1.3968e+18, 4.5666e-41],
[-1.5290e-27, 8.1363e+07]], dtype=torch.float64)
tensor([[-1.3968e+18, 4.5666e-41],
[-1.5290e-27, 8.1363e+07]], dtype=torch.float64)
tensor([[-1.3968e+18, 4.5666e-41],
[-1.5290e-27, 8.1363e+07]], dtype=torch.float64)
2、Tensor创建一 直接创建
通过torch.tensor()创建张量
torch.tensor(data,
dtype=None,
device=None,
requires_grad=False,
pin_memory=False)
功能:从data创建tensor
- data:数据,可以是list,numpy
- dtype:数据类型,默认与data的一致
- device:所在设备,cuda/cpu
- requires_grad:是否需要梯度
- pin_memory:是否存于锁页内存
import torch
import numpy as np
x = np.ones((3, 3))
print(x)
t = torch.tensor(x)
print(t)
[[1. 1. 1.]
[1. 1. 1.]
[1. 1. 1.]]
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], dtype=torch.float64)
t = torch.tensor(x, device='cuda')
print(t)
tensor([[1., 1., 1.],
[1., 1., 1.],
[1., 1., 1.]], device='cuda:0', dtype=torch.float64)
通过torch.from_numpy创建张量
# arr与t 共享内存
arr = np.array([[1, 2, 3], [4, 5, 6]])
t = torch.from_numpy(arr)
print(arr)
print(t)
[[1 2 3]
[4 5 6]]
tensor([[1, 2, 3],
[4, 5, 6]])
3、Tensor创建二 依据数值创建
通过torch.zeros创建张量
torch.zeros(*size,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:依size创建全0张量
- size:张量的形状,如(3,3)、(3,224,224)
- out:输出的张量
- layout:内存中布局形式,有strided,sparse_coo等
- device:所在设备,gpu/cpu
- requires_grad: 是否需要梯度
out_t = torch.tensor([1])
t = torch.zeros((3, 3), out=out_t)
print(t)
print(out_t)
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
tensor([[0, 0, 0],
[0, 0, 0],
[0, 0, 0]])
通过torch.zeros_like()创建张量
torch.zeros_like(input,
dtype=None,
layout=None,
device=None,
requires_grad=False)
功能:依input形状创建全0张量
- input:创建于input同形状的全0张量
- dtype:数据类型
- layout:内存中布局形式
通过torch.ones()和torch.ones_like()创建张量
torch.ones(*size,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
torch.ones_like(input,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:依input形状创建全1张量
- size:张量的形状,如(3,3)、(3,224,224)
- dtype:数据类型
- layout:内存中布局形式
- device:所在设备,gpu/cpu
- requires_grad:是否需要梯度
通过torch.full()和torch.full_like()创建张量
torch.full(size,
fill_value,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
torch.full_like(input,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:依input形状创建张量
- size:张量的形状,如(3,3)
- fill_value:张量的值
t = torch.full((3,3),10)
print(t)
tensor([[10., 10., 10.],
[10., 10., 10.],
[10., 10., 10.]])
通过torch.arange()创建张量
torch.arange(start=0,
end,
step=1,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:创建等差的1维张量
注意:数值区间维[start, end)
- start:数列起始值
- end:数列 “结束值”
- step: 数列公差,默认为1
t = torch.arange(2,10,2)
print(t)
tensor([2, 4, 6, 8])
通过torch.linspace()创建张量
torch.linspace(start,
end,
steps=100,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:创建均分的1维张量 注意事项:数值区间为[start, end]
- start:数列起始值
- end:数列结束值
- steps:数列长度
t = torch.linspace(2,10,5)
print(t)
t = torch.linspace(2,10,6)
print(t)
tensor([ 2., 4., 6., 8., 10.])
tensor([ 2.0000, 3.6000, 5.2000, 6.8000, 8.4000, 10.0000])
通过torch.logspace()创建张量
torch.logspace(start,
end,
steps=100,
base=10.0,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:创建对数均分的1维张量 注意事项:长度为steps,底为base
- start:数列起始值
- end:数列结束值
- steps:数列长度
- base:对数函数的底,默认为10
通过torch.eye()创建张量
torch.eye(n,
m=None,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
功能:创建单位对角矩阵(2维张量)
注意事项:默认为方阵
- n:矩阵行数
- m:矩阵列数
4、Tensor创建三 依据概率创建
通过torch.normal()创建张量
torch.normal(mean,
std,
out=None)
torch.normal(mean,
std,
size,
out=None)
四种模式:
mean为标量,std为标量
mean为标量,std为张量
mean为张量,std为标量
mean为张量,std为张量
# mean:张量 std:张量
mean = torch.arange(1, 5, dtype=torch.float)
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print(t_normal)
# mean:标量 std:标量
t_normal = torch.normal(0., 1., size=(4,))
print(t_normal)
# mean:张量 std:标量
mean = torch.arange(1, 5, dtype=torch.float)
std = 1
t_normal = torch.normal(mean, std)
print(t_normal)
# mean:标量 std:张量
mean = 0
std = torch.arange(1, 5, dtype=torch.float)
t_normal = torch.normal(mean, std)
print(t_normal)
tensor([ 2.7012, -0.3600, 0.8785, 3.8647])
tensor([-0.0676, -0.6523, -0.6152, -0.3463])
tensor([0.5923, 1.7479, 2.4897, 3.6702])
tensor([ 0.3142, -2.2728, -1.3982, -1.6841])
通过torch.randn()和torch.randn_like()创建张量
torch.randn(*size,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
torch.randn_like()
功能:生成标准正态分布
- size:张量的形状
通过torch.rand()和torch.rand_like()创建张量
torch.rand(*size,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
torch.rand_like()
功能:在区间[0, 1)上,生成均匀分布
通过torch.randint()和torch.randint_like()创建张量
torch.randint(low=0,
high,
size,
out=None,
dtype=None,
layout=torch.strided,
device=None,
requires_grad=False)
torch.randint_like()
功能:区间[low, hight)生成整数均匀分布
- size:张量的形状
通过torch.randperm()创建张量
torch.randperm(n,
out=None,
dtype=torch.int64,
layout=torch.strided,
device=None,
requires_grad=False)
功能:生成从0到n-1的随机排列
- n:张量的长度
通过torch.bernoulli()创建张量
torch.bernoulli(input,
*,
generator=None,
out=None)
功能:以input为概率,生成伯努利分布(0-1分布,两点分布)
- input:概率值