【python】一些常见函数

1.pad

torch.nn.functional.pad(input, pad, mode='constant', value=0)
input
需要扩充的tensor,可以是图像数据,抑或是特征矩阵数据
​​pad
扩充维度,用于预先定义出某维度上的扩充参数
mode
扩充方法,’constant‘, ‘reflect’ or ‘replicate’三种模式,分别表示常量,反射,复制
value
扩充时指定补充值,但是value只在mode='constant’有效,即使用value填充在扩充出的新维度位置,而在’reflect’和’replicate’模式下,value不可赋值

代码举例:

x = f.pad(x, (1, 0), "constant", 0)

x是要填充的tensor

(1,0)是扩充维度,(1,0)的意思就是在tensor左边填充。若为(0,1)则是右边填充

constant代表扩充方法

0代表扩充的值是0

数据举例:

假设原始数据如表:

2.56 6.35544 1.2995
6.259 5.9584 7.94136
3.652 2.486 5.19572

使用x = f.pad(x, (1, 0), "constant", 0)填充

0 2.56 6.35544 1.2995
0 6.259 5.9584 7.94136
0 3.652 2.486 5.19572

使用x = f.pad(x, (0,1), "constant", 0)填充 

2.56 6.35544 1.2995 0
6.259 5.9584 7.94136 0
3.652 2.486 5.19572 0

使用x = f.pad(x, (0,1), "constant", 1)填充 

2.56 6.35544 1.2995 1
6.259 5.9584 7.94136 1
3.652 2.486 5.19572 1

猜你喜欢

转载自blog.csdn.net/qq_46073783/article/details/128138425
今日推荐