Tensor使用
序列
a = torch.arange(0, 11,3) # tensor([0,3,6,9])
等间隔数字
a = torch.linspace(0, 11,3)
# tensor([2.00, 4.66, 7.33, 10.00])
序列打乱
a = torch.randperm( 10 ) # tensor([6,2,7,0,8,9,5,3,1,4]
Tensor属性
每一个Tensor有torch.dtype、torch.device、torch.layout三种属性。
torch.device标识了torch.Tensor对象在创建之后所存储在的设备名称。(CPU/CUDA:0/1)
torch.layout表示torch.Tensor内存布局的对象。
#定义稠密张量 torch.tensor([ 1 , 2 , 3 ], dtype = torch.float32, device = torch.device( 'cpu' )) #定义稀疏张量,coo类型表示了非零元素的坐标形式 i = torch.tensor([[ 0 , 1 , 1 ],[ 2 , 0 , 2 ]]) #三个非零坐标( 0 , 2 )( 1 , 0 )( 1 , 2 ) v = torch.tensor([ 3 , 4 , 5 ],dtype = tensor.float32) #三个非零元素的值 a = torch.sparse_coo_tensor(i, v, [ 2 , 4 ]) #稀疏张量的大小 #稀疏转稠密 a.to_dense()
Tensor算术运算
a = torch.rand(2, 3) # 2*3形状的随机值 b = torch.rand(2, 3) # 2*3形状的随机值 # add——对应加,不改变a b值 a + b a.add(b) torch.add(a,b) # add——将b的值加在a上,改变a a.add_(b) # sub——对应减,不改变a b值 a - b a.sub(b) torch.sub(a,b) # sub——改变a a.sub_(b) # mul——乘 a * b a.mul(b) torch.mul(a,b) # mul——改变a a.mul_(b) # div——除 a/b a.div(b) torch.div(a,b) # div——改变a a.div_(b)
a = torch.ones(2, 1)
b = torch.ones(1, 2)
# @——矩阵乘
a @ b
a.matmul(b)
torch.mm(a,b)
torch.matmul(a,b)
a.mm(b)
高维tensor
a = torch.ones(1, 2, 3, 4) b = torch.ones(1, 2, 4, 3)
a.matmul(b).shape # (1,2,3,3),最后两纬进行矩阵运算
逐元素的指数运算
a = torch.tensor([1, 2])
# pow
torch.pow(a, 3)
a.pow(3)
a**3
# pow——修改a
a.pow_(3)
# out >> tensor([1, 8])
自然常数指数运算
a = torch.tensor([1, 2],
dtype = torch.float32) # exp torch.exp(a) torch.exp_(a) a.exp() a.exp_()
# out >> tensor([e^1, e^2])
自然对数运算
# log
a = torch.tensor([10, 2], dtype = torch.float32) torch.log(a) torch.log_(a) a.log() a.log_()
开根号
# sqrt torch.sqrt(a) torch.sqrt_(a) a.sqrt() a.sqrt_()
Tensor的取整/取余运算
| .floor() | 向下取整数 |
| .ceil() | 向上取整数 |
| .round() |
四舍五入 >=0.5向上取整,<0.5向下取整 |
| .trunc() | 裁剪,只取整数部分 |
| .frac() | 只取小数部分 |
| % | 取余 |
a = torch.rand(2, 2)
torch.floor(a)
Tensor的比较运算
| torch.eq(input, other, out=None) |
返回类型为Tensor 逐个元素比较,相同返回True. |
| torch.equal(tensor1, tensor2) |
返回单个T/F 如果tensor1和tensor2有相同的size和elements,则为true |
| torch.ge(input, other, out=None) | 返回类型为Tensor# input> = other |
| torch.gt(input, other, out=None) | 返回类型为Tensor# input>other |
| torch.le(input, other, out=None) | 返回类型为Tensor# input= <other |
| torch.lt(input, other, out=None) | 返回类型为Tensor# input<other |
| torch.ne(input, other, out=None) | 返回类型为Tensor# input != other不等于 |
取前k大/前k小/第k小的数值及其索引
| torch.sort(input, dim=None, descending=False, out=None) | #对目标input进行排序 |
| torch.topk(input, k,dim=None, largest=True, sorted=True,out=None) | #沿着指定维度返回最大k个数值及其索引值 |
| torch.kthvalue(input, k, dim=None, out=None) | #沿着指定维度返回第k个最小值及其索引值 |
a = torch.tensor([[2, 4, 3, 1, 5], [2, 3, 5, 1, 4]]) a.shape #torch.Size([2, 5]) torch.sort(a, dim=0, descending=False) #tensor([[2, 3, 3, 3, 4], # [2, 4, 5, 3, 5]])
torch.topk(a, k=2, dim=1)
#values = tensor([[5, 4],
[5, 4]]) #indices = tensor([[4, 1]
[2, 4]])
判定是否为finite/inf/nan
| torch.isfinite(tensor) / torch.isinf(tensor) / torch.isnan(tensor) |
| 返回类型为Tensor返回一个标记元素是否为finite/ inf/ np.nan 的mask张量。F/T |
Python中的in-place操作
in-place:不允许使用临时变量
x = x + y
# add_ sub_ mul
Python中的广播机制
张量参数自动扩展为相同大小
广播机制需要满足两个条件:
- 每个张量至少有一个维度
- 满足右对齐
import torch a = torch.rand(2, 1, 1, 3) b = torch.rand(4, 2, 3) c = a + b # a 2*1*1*3 # b 4*2*3 右对齐,补齐左 # c 2*4*2*3
a = torch.rand(2, 3, 1, 3) b = torch.rand(4, 2, 3) c = a + b #报错
Tensor中的统计学相关函数
| torch.mean() | #返回平均值 |
| torch.sum() | #返回总和 |
| torch.prod() | #计算所有元素的积 |
| torch.max() | #返回最大值 |
| torch.min() | #返回最小值 |
| torch.argmax() | #返回最大值排序的索引值 |
| torch.argmin() | #返回最小值排序的索引值 |
| torch.std() | #返回标准差 |
| torch.var() | #返回方差 |
| torch.median() | #返回中间值 |
| torch.mode() | #返回众数值 |
| torch.histc(input) | #计算input的直方图 |
| torch.bincount() |
#返回每个值的频数。 解决计算机视觉相关问题 LBP gabor sift 纹理 HOG... |
Tensor中的随机抽样
- 定义随机种子,保证随机抽样方法相同
torch.manual_seed(int)
- 定义随机数满足的分布
torch.normal(mean, std)
Tensor中的范数运算
范数是用来度量某个向量(或矩阵)长度/大小的一个概念,需要满足以下条件:
- 非负性
- 齐次性
- 三角不等式
p范数:元素p次方的和,开p次根。
0范数:非零元素的和。
torch.dist(input, other, p=2) # 计算p范数 torch.norm() # 计算2范数,即欧氏距离
核范数:求解低秩问题。
图像通常是低秩的
torch.norm(a, p='fro')
低秩——>稀疏矩阵——>图像噪声
浙公网安备 33010602011771号