Tensor - 简介、创建、操作与运算

发布时间 2023-08-08 23:36:52作者: 珠峰上吹泡泡

一、简介

1.1 Variable和Tensor的关系

  • Variabletorch.autograd中的数据类型,用于封装Tensor,使其可以进行自动求导

  • 0.4.0后Variable并入了Tensor

#Variable含有的属性
data
grad
grad_fn
requires_grad
is_leaf
#Tensor新增的三个属性
dtype   #9种,包含float、int、boolean,CPU与GPU有不同的宏表示
shape
device

二、 创建

2.1 依据数值创建

# 1 torch.tensor()
data	#可以是list,numpy
dtype
device
requires_grad
pin_memory	#是否用于锁页内存

# 2 torch.from_numpy(ndarray)--->创建的tensor与原ndarray共享内存

2.2 直接创建

# 1 torch.zeros()
size
out
layout	#内存中的布局形势,有strided,sparse_coo
device
requires_grad

# 2 torch.zeros_like() --->依据input形状创建全0张量
input
dtype
layout

# 3 torch.ones() --->参数与1一样

# 4 torch.ones_like() --->参数与2一样

# 5 torch.full() --->依据size的形状填充fill_value
size
fill_value
out
dtype
layout
device
requires_grad

# 6 torch.arange() --->创建等差的1维张量,左开右闭
start
end
step	#数列公差
out
dtype
layout
device
requires_grad

# 7 torch.linspace() --->创建均分的1维张量,左闭右闭
start
end
steps	#需要注意,这个不是步长,是数列的长度,实际步长为(end - start) / (steps - 1)
out
dtype
layout
device
requires_grad

# 8 torch.logspace() --->创建对数均分的1维张量,左闭右闭,只是在steps后增加一个base参数
base	#对数函数的底

# 9 torch.eye() --->创建2维单位对角矩阵
n	#行数
m	#列数
out
dtype
layout
device
requires_grad

2.3 依据概率创建

# 1 torch.normal() --->生成正态分布
mean
std
(size)
out
'''
上述mean、std有四种模式:
mean为标量,std为标量:需指定size参数
mean为标量,std为张量:没有size参数
mean为张量,std为标量:没有size参数
mean为张量,std为张量:没有size参数,对应位取mean和std生成正态分布数值
'''

# 2 torch.randn() --->生成标准正态分布
size
out
dtype
layout
device
requires_grad

# 3 torch.randn_like()

# 4 torch.rand() --->在[0,1)上生成均匀分布
size
out
dtype
layout
device
requires_grad

# 5 torch.rand_like()

# 6 torch.randint() --->在[low,high)上生成均匀分布
low
high
size
out
dtype
layout
device
requires_grad

# 7 torch.randperm() -->生成0~n-1的随机排列
n	#张量的长度
out
dtype
layout
device
requires_grad

# 8 torch.bernoulli() -->以input为概率,生成伯努利分布(0-1分布)
input	#概率值
*
generator
out

三、 操作

3.1 拼接

# 1 torch.cat() --->将张量按维度dim进行拼接
tensors	#张量序列
dim
out

# 2 torch.stack() --->在新创建的维度dim进行拼接,如果dim存在则后移
tensors	#张量序列
dim
out

3.2 切分

# 1 torch.chunk() --->将张量按照维度dim进行平均切分,返回张量列表,需要注意的是:若不能整除时,最后一份张量小于其他张量
input
chunks: 要切分的份数
dim

# 2 torch.split() --->将张量按照维度dim进行切分,返回张量列表
tensor
split_size_or_sections:为int时,表示每一份的长度;为list时,按照list元素切分
dim

3.3 索引

# 1 torch.index_select() --->在维度dim上,按照index索引数据,返回索引数据拼接的张量
input
dim
index

# 2 torch.masked_select() --->按照mask中的True进行索引,返回一维张量 
input
mask:与input相同形状的bool值张量

3.4 变换

# 1 torch.reshape() -->按照shape变换张量的形状,若张量在内存中是连续的,则新张量与input共享数据内存
input
shape

# 2 torch.transpose() --->交换张量的两个维度
input
dim0
dim1

# 3 torch.t() --->2维张量转置,相当于torch.transpose(input, 0, 1)

# 4 torch.squeeze() --->压缩长度为1的维度(轴)
input
dim	#若为None,移除所有长度为1的轴;若指定维度,仅当该维度的数值为1时会被移除
out

# 5 torch.unsqueeze() --->依据dim扩展维度(1)
input
dim
out

四、 数学运算

4.1 加减乘除

torch.add() --->input + alpha * other
input
alpha
other
out
torch.addcdiv() ---> input + value * tensor1 / tensor2

input
value
tensor1
tensor2
out
torch.addcmul()
torch.sub()
torch.div()
torch.mul()

4.2 对数、指数、幂函数

torch.log(input, out=None)
torch.log10(input, out=None)
torch.log2(input, out=None)
torch.exp(input, out=None)
torch.pow()

4.3 三角函数

torch.abs(input, out=None)
torch.acos(input, out=None)
torch.cosh(input, out=None)
torch.cos(input, out=None)
torch.asin(input, out=None)
torch.atan(input, out=None)
torch.atan2(input, other, out=None)