文章目录
- Tensor
- 一、Tensor维度等变换操作
- 不同Tensor之间的合并与分离
- 高级操作:将两个batch size>1的Tensor中的每一层对应组合在一起
- 单个Tensor的维度变换
- Tensor内存优化
- 二、Tensor计算
- Tensor乘法
- Tensor 的归并运算
- 三、Tensor条件索引等操作
- Tensor排序
- Tensor反转顺序
- Tensor取最大值和最小值并返回索引
- 取最大值最小值
- 取最大值最小值的索引
- 对矩阵指定idx内容进行操作
- idx索引矩阵
- 条件对比,返回等大小的Bool矩阵
- 统计tensor中满足条件元素的个数
- 高级操作:将矩阵中大于某个阈值的元素index返回并取出对应元素组成新的矩阵(适用于深度学习检测类任务后处理)
- 四、按照规则生成新的Tensor
- 五、Tensor类型转换(与numpy、list、转换float、四舍五入等)
- 六、Tensor平移和旋转
- 七、Tensor训练
- Module
- 一、nn子模块
- 计算坐标点距离(欧式距离)
- 二、nn.functional
- 线性插值
- Pytorch数据相关操作
- 生成随机数
- 话外
- Numpy中的ndarrays和Torch中的tensor有什么区别?
Tensor
一、Tensor维度等变换操作
不同Tensor之间的合并与分离
PyTorch常用张量切割和拼接方法
- Tensor.cat()(指定维度进行合并)
- Tensor.stack()(指定并创建一个新维度进行合并)
- Tensor.split()(指定并创建一个新维度进行合并)
高级操作:将两个batch size>1的Tensor中的每一层对应组合在一起
应用示例:
B>1
Tensor A: [B, C, H, W]
,a为每个Tensor的每个batch:[1, C, H, W]
Tensor B: [B, C, H, W]
,b为每个Tensor的每个batch:[1, C, H, W]
目标 Tensor C: [2*B, C, H, W]
我们需要让Tensor A和B组成新的Tensor C,但是Tensor C中每两个Batch中,第一个必须是Tensor A,第二个是Tensor B,即Tensor C的内容应为:[[a_1,b_1], [a_2, b_2], ... [a_n, b_n]]
单个Tensor的维度变换
- Tensor.view()(维度合并)
其他小用法:
-
Tensor.reshape()
(交换 / 合并 / 扩展 维度)
- Tensor.permute()(维度交换)
-
Tensor.unsqueeze()
(扩展维度)
-
Tensor.squeeze()
(删除一个空维度)
-
Tensor.repeat()
(在某一维进行重复扩充)
-
Tensor.range()
和Tensor.arange()
(产生一维数组)
-
torch.range(1,10)
和 torch.arange(10)
都产生了一个1维的数组,类型是 <class ‘torch.Tensor’>
- 二者不同的是:
-
range
产生的长度是10-1+1=10
是由1到10组成的1维张量,类型float
- 而
arange
产生的是10-1=9
由1-9组成的1维度张量 ,类型int
Tensor内存优化
- Tensor.contiguous()
- 功能:
Tensor.contiguous()
函数不会对原始数据进行任何修改,而仅仅对其进行复制,并在内存空间上进行对齐,即在内存空间上,tensor元素的内存地址保持连续。 - 意义: 这么做的目的是,在对tensor元素进行转换和维度变换等操作之后,元素地址在内存空间中保证连续性,在后续利用指针对tensor元素进行读取时,能够减少读取便利,提高内存空间优化。
二、Tensor计算
Tensor乘法
-
torch.matmul()
Tensor 的归并运算
Tensor 的归并运算(torch.mean、sum、median、mode、norm、dist、std、var、cumsum、cumprod)
- Tensor.max()(指定某一维取最大值并合并)
-
torch.sum()
-
torch.mean()
-
torch.median()
-
torch.mode()
-
torch.norm()
-
torch.dist()
-
torch.std()
-
torch.var()
-
torch.cumsum()
-
torch.cumprod()
三、Tensor条件索引等操作
Tensor排序
-
torch.topk()
(可以指定获取前k个最大值的value
和index
)
-
input
:tensor数据; -
k
:指明是得到前k个数据以及其index; -
dim
: 指定在哪个维度上排序, 默认是最后一个维度; -
largest
:如果为True,按照大到小排序; 如果为False,按照小到大排序;
-
torch.sort()
Tensor反转顺序
-
torch.flip()
Tensor取最大值和最小值并返回索引
取最大值最小值
-
torch.max()
和torch.min()
-
input
:表示输入的张量 -
dim
:表示的是索引的维度,0和1分别表示列和行 - 返回两个tensor,第一个tensor表示对应维度的最大/小值;第二个tensor表示最大/小值的索引
取最大值最小值的索引
-
tensor.argmax()
和tensor.argmin()
-
dim
:表示的是索引的维度
对矩阵指定idx内容进行操作
-
torch.tensor.index_put()
idx索引矩阵
-
torch.index_select()
条件对比,返回等大小的Bool矩阵
-
torch.gt()
-
torch.lt()
-
torch.eq()
参考文章:pytorch中torch.gt(),torch.lt(),torch.eq()
-
torch.where(condition, a, b)
- 作用:按照一定的规则合并两个tensor类型
- 输入参数:
-
condition
:条件限制;如果满足条件,则选择a
对应的值,否则选择b
对应的值作为输出。
参考文章:Pytorch:Tensor的高阶操作【where(按条件取元素)、gather(查表取元素)、scatter_(查表取元素)】【可并行计算,提高速度】
统计tensor中满足条件元素的个数
-
torch.nonzero()
+torch.numel()
- 需要注意的是,统计时需要将tensor转换为一维向量,否则给出的结果是所有元素的个数,并不是满足条件的元素!
高级操作:将矩阵中大于某个阈值的元素index返回并取出对应元素组成新的矩阵(适用于深度学习检测类任务后处理)
一般在检测任务中,模型预测的输出都为(N, 4+1),N为预测box数,4为x,y,w,h,还有一个为类别。
本次我的点集预测任务中,我的预测结果为(N, 40+1),N为预测instance数,40为20组x,y点,还有一个为类别。
我需要通过confidence滤除掉小于阈值threshold的instance,然后再将其他符合要求的元素组成新的矩阵。
四、按照规则生成新的Tensor
-
Tensor.new_tensor()
(生成一个指定大小,全部填充给定值的Tensor)
-
Tensor.new_ones()
(生成一个指定大小,全部填充1的Tensor) -
Tensor.new_zeros()
(生成一个指定大小,全部填充0的Tensor)
-
Tensor.new_full()
(生成一个指定大小,全部填充给定值的Tensor)
-
torch.linspace()
(生成线性等间距一维数组)
-
torch.arange(start, end, step)
(生成一维数组,不包含尾部数据)
-
torch.range(start, end, step)
(生成一维数组,包含尾部数据)
五、Tensor类型转换(与numpy、list、转换float、四舍五入等)
-
torch.tensor
转Python
- Tensor变量且只能为包含单个数据
-
torch.tensor
转list
-
numpy
转torch.tensor
-
torch.tensor
转numpy
-
list
转torch.tensor
-
torch.tensor
转换类型
-
torch.tensor
四舍五入
六、Tensor平移和旋转
Pytorch中的仿射变换(affine_grid)
-
torch.nn.functional.affine_grid()
-
torch.nn.functional.grid_sample()
七、Tensor训练
-
torch.tensor.detach()
(截断某个变量的梯度反传,到此为止;只反传此变量相关的前边部分梯度,之后涉及到该变量的梯度均不反传)
- pytorch函数tensor.detach()
Module
一、nn子模块
-
nn.Embedding
- 定义Embedding并初始化指定的参数:
理解:pytorch nn.Embedding的用法和理解
计算坐标点距离(欧式距离)
-
torch.nn.PairwiseDistance()
(计算两个tensor中每个点的欧式距离)
- 只需要保证输入的两个Tensor’维度一致,且最后一维为2即可,前边的维度可以无限扩展,例如
[bs, num_pts, 2]
或者[num_pts, 2]
。
Pytorch计算距离(例如欧式距离)torch.nn.PairwiseDistance
二、nn.functional
线性插值
-
torch.nn.functional.interpolate()
(各种线性插值函数)
- 其他参考文章:F.interpolate——数组采样操作
- 插值使用场景:
- 对于x,y,z等坐标插值:用单线性插值;
- 对图像等2D Tensor插如内容值:用双线性插值;
- 单线性插值示例:
Pytorch数据相关操作
生成随机数
-
torch.randperm
:将0~n-1(包括0和n-1)随机打乱后获得的数字序列,函数名是random permutation缩写。
-
torch.Generator
- 通常不用手动实例化
torch.Generator
, 当需要随机数时, PyTorch 会自动创建一个默认的 torch.Generator
实例; - 也可以手动指定使用
torch.Generator
;
话外
Numpy中的ndarrays和Torch中的tensor有什么区别?
- 浅谈torch的tensor和Numpy的ndarrays