一、tensor基础函数
1. 基本函数
函数 | 功能 |
---|---|
Tensor(*size) | 创建tensor |
Tensor(data) | 将数据转化为tensor |
ones(*size) | 全1 |
zeros(*size) | 全0 |
zeros_like(tensor)/ones_like(tensor) | 创建一个与参数相同shape且元素全为0/全为1的tensor |
eye(*size) | 单位tensor |
torch.empty(*size) | 创建一个未被初始化数值的tensor(值不一定是0),tensor的大小是由size确定 |
arange(s, e, step) | 返回一个1维张量,取包含在[s, e)在内的间隔为steps的点 |
linspace(s, e, step=100) | 返回一个1维张量,取包含在[s, e]在内的均匀间隔的step个点 |
rand/randn(*size) | 均匀分布:从区间[0, 1)的均匀分布中抽取的一组随机数 / 标准正态分布:从标准正态分布(均值为0,方差为1,即高斯白噪声)中抽取的一组随机数 |
normal(mean, std)/uniform_(from, to) | 正态分布:从指定均值和标准差的正态分布中抽取的一组随机数 / 均匀分布:从from到to的均匀分布中随机抽样数值进行填充 |
randperm(n) | 将[0, n)随机打乱后获得的数字序列 |
import torch
print(torch.Tensor([[1, 2], [3, 4]])) # 创建
print(torch.Tensor(3, 2))
print(torch.zeros(2, 2))
print(torch.ones(2, 2))
print(torch.eye(2, 2))
print(torch.empty([2, 2]))
a = torch.Tensor([[1, 2], [3, 4]])
print(torch.zeros_like(a))
print(torch.ones_like(a))
print(torch.rand(2, 2))
print(torch.randn(2, 2))
print(torch.normal(torch.randn(1, 3), torch.rand(1, 3)))
print(torch.linspace(1, 5, 5))
print(torch.arange(1, 5, 1))
print(torch.Tensor(2, 2).uniform_(-1, 1))
print(torch.randperm(10))
# 输出
tensor([[1., 2.], # 创建
[3., 4.]])
tensor([[ 7.3908e+22, 1.4764e-41],
[-6.2287e-23, 4.5916e-41],
[ 0.0000e+00, 0.0000e+00]])
tensor([[0., 0.], # zeros
[0., 0.]])
tensor([[1., 1.], # ones
[1., 1.]])
tensor([[1., 0.], # eye
[0., 1.]])
tensor([[1.4013e-45, 0.0000e+00], # empty
[1.6393e+19, 4.5916e-41]])
tensor([[0., 0.], # zeros_like
[0., 0.]])
tensor([[1., 1.], # ones_like
[1., 1.]])
tensor([[0.4374, 0.2534], # rand
[0.1902, 0.1513]])
tensor([[ 1.8772, 0.9770], # randn
[ 0.7970, -0.5733]])
tensor([[-0.2111, 0.9958, 0.2982]]) # normal
tensor([1., 2., 3., 4., 5.]) # linspace
tensor([1, 2, 3, 4]) # arange
tensor([[-0.3002, -0.6143], # uniform_
[ 0.7185, -0.2010]])
tensor([3, 5, 8, 2, 6, 9, 1, 4, 0, 7]) # randperm
2. 其他函数
- torch.abs():绝对值函数
- torch.sign():符号函数
- torch.sigmoid():sigmoid函数
二、torch运算函数
1. tensor的算数运算
①加减乘除
加为add函数,减为sub函数,乘(element wise:对应元素相乘)为mul函数,除为div函数
不过对于add_(),sub_(),mul_(),div_() 而言会对原来的操作数a进行修改
要注意的是
- 矩阵的element-wise乘法是可以使用广播机制的,而矩阵乘法不能使用广播(对执行矩阵乘法的那一部分而言,而对于batchsize那一部分是可以广播),例如:matrix A(3*4)和matrix B(1*4),若A与B相乘则前者可以执行,后者报错;
- 另外,pytorch中的一维数组是以列向量为数学计算约定,却以行向量为表示形式的向量,例如:matrix A(10*3*4)和matrix B(4),若A与B相乘则前者,后者都可以执行,但前者算出的shape为10*3*4,后者算出的shape为10*3。
a, b = torch.tensor(5), torch.tensor(3)
print(a + b) # 加减乘除只需要修改对应函数与符号
print(torch.add(a, b))
print(a.add(b))
print(a.add_(b))
print(a)
# 输出
tensor(8)
tensor(8)
tensor(8)
tensor(8)
tensor(8)
②幂运算
print(a ** b) # 普通幂运算同加减乘除
print(torch.pow(a, b))
print(a.pow(b))
print(a.pow_(b))
print(torch.exp(a)) # 以e为底的指数函数
print(a.exp())
print(a.exp_())
print(torch.sqrt(a)) # 开根号
print(a.sqrt())
print(a.sqrt_()) # 同add_()函数
③矩阵运算
对二维来说,矩阵乘法包括torch.mm()、torch.matmul()、@,且这里 没有mm()_、matmul()_的方法
print(torch.mm(a, b)) # 开根号
print(torch.matmul(a, b)) # 开根号
print(a @ b) # 开根号
print(a.matmul(b)) # 开根号
print(a.mm(b)) # 开根号
对于高维的Tensor (dim>2),定义其矩阵乘法仅在最后的两个维度上进行二维运算,并要求 前面的维度必须保持一致 ,且操作函数只有 torch.matmul()
a, b = torch.rand(1, 2, 3), torch.rand(1, 3, 7)
print(torch.matmul(a, b)) # 开根号
print(a.matmul(b)) # 开根号
④对数运算
log2和log10的函数同log函数的用法
print(torch.log(a)) # e为底的对数函数
print(a.log())
print(a.log_())
⑤取整/余
~可以为torch或操作数
函数 | 作用 |
---|---|
% | 取余 |
~.floor() | 下取整 |
~.ceil() | 上取整 |
~.round() | 四舍五入 |
~.trunc() | 只取整数部分 |
~.frac() | 只取小数部分 |
print(a)
print(torch.floor(a))
print(a.floor())
print(torch.ceil(a))
print(a.ceil())
print(torch.round(a))
print(a.round())
print(torch.trunc(a))
print(a.trunc())
print(torch.frac(a))
print(a.frac())
2. 比较运算
torch.equal()返回只返回一个布尔值,而torch.eq()和其他函数是返回一个内容为true或false的tensor
ge为great than or equal;lt为Less than,nt为not equal
本文只用于个人学习与记录,侵权立删