pytorch——tensor.expand_as()函数和AdaptiveAvgPool2d函数

1.tensor.expand.as()函数

import torch
x = torch.tensor([[1], [2], [3]])  # 生成tensor x,3行1列
print('xsize:',x.size())
print('x:',x)

xsize: torch.Size([3, 1])
x: tensor([[1],
           [2],
           [3]])


x_expand=x.expand(3,4)  # 将x扩展为3行4列
print('x_expand:',x_expand)

x_expand: tensor([[1, 1, 1, 1],
                  [2, 2, 2, 2],
                  [3, 3, 3, 3]]) 


x_expand=x.expand(-1,4)  # -1 means not changing the size of that dimension
print('x_expand:',x_expand)

x_expand: tensor([[1, 1, 1, 1],
                  [2, 2, 2, 2],
                  [3, 3, 3, 3]])
 
 
x_expand_as=x.expand_as(x_expand) # 将x的形状变为和x_expand一样
print('x_expand_as:',x_expand_as)

x_expand_as: tensor([[1, 1, 1, 1],
                     [2, 2, 2, 2],
                     [3, 3, 3, 3]])
————————————————
版权声明:本文为CSDN博主「机器不学习我学习」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/AugustMe/article/details/121473996

就是会将维度更小的tensor在同一行用相同的数来填充,用于se模块中,求出各个通道之间的权重之后与原数据相乘进行加权操作。

2.AdaptiveAvgPool2d函数

nn.AdaptiveAvgPool2d((1,1)),首先这句话的含义是使得池化后的每个通道上的大小是一个1x1的,也就是每个通道上只有一个像素点。(1,1)表示的outputsize。

原型如下:

如题:只需要给定输出特征图的大小就好,其中通道数前后不发生变化。具体如下:

AdaptiveAvgPool2d

CLASStorch.nn.AdaptiveAvgPool2d(output_size)[SOURCE]

Applies a 2D adaptive average pooling over an input signal composed of several input planes.

The output is of size H x W, for any input size. The number of output features is equal to the number of input planes.

Parameters

output_size – the target output size of the image of the form H x W. Can be a tuple (H, W) or a single H for a square image H x H. H and W can be either a int, or None which means the size will be the same as that of the input.

你可能感兴趣的:(pytorch,人工智能,python)