目录
1、Pytorch 中的 torch.cat() 函数:
Pytorch中的torch.cat()函数(https://www.cnblogs.com/JeasonIsCoding/p/10162356.html)
把多个 nparray 或者 tensor 在指定维度上拼接起来
示例:
C = torch.cat( (A,B),0 ) #按维数0拼接(竖着拼)
C = torch.cat( (A,B),1 ) #按维数1拼接(横着拼)
>>> import torch
>>> A=torch.ones(2,3) #2x3的张量(矩阵)
>>> A
tensor([[ 1., 1., 1.],
[ 1., 1., 1.]])
>>> B=2*torch.ones(4,3) #4x3的张量(矩阵)
>>> B
tensor([[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]])
>>> C=torch.cat((A,B),0) #按维数0(行)拼接
>>> C
tensor([[ 1., 1., 1.],
[ 1., 1., 1.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.],
[ 2., 2., 2.]])
>>> C.size()
torch.Size([6, 3])
>>> D=2*torch.ones(2,4) #2x4的张量(矩阵)
>>> C=torch.cat((A,D),1)#按维数1(列)拼接
>>> C
tensor([[ 1., 1., 1., 2., 2., 2., 2.],
[ 1., 1., 1., 2., 2., 2., 2.]])
>>> C.size()
torch.Size([2, 7])
2、pytorch 中 unsqueeze() 函数理解:
pytorch中unsqueeze()函数理解(https://blog.csdn.net/nagato_yuki_sos/article/details/109060587)
unsqueeze()函数起升维的作用,参数表示在哪个地方加一个维度。
例如
import torch
input=torch.arange(0,6)
print(input)
print(input.shape)
结果:
tensor([0, 1, 2, 3, 4, 5])
torch.Size([6])
print(input.unsqueeze(0))
print(input.unsqueeze(0).shape)
结果:
tensor([[0, 1, 2, 3, 4, 5]])
torch.Size([1, 6])
print(input.unsqueeze(1))
print(input.unsqueeze(1).shape)
结果:
tensor([[0],
[1],
[2],
[3],
[4],
[5]])
torch.Size([6, 1])
3、torch.full_like() 创造指定形状和值的矩阵 :
pytorch每日一学23(torch.full()、torch.full_like())创造全value的矩阵(https://blog.csdn.net/Fluid_ray/article/details/109855155)
torch.full_like():输入的 tensor 的形状将会决定输出的 tensor 的形状,可以指定返回 tensor 的填充值。
>>> import torch
>>>
>>> a = torch.full((3, 4), 5)
>>> a
Out[1]:
tensor([[5, 5, 5, 5],
[5, 5, 5, 5],
[5, 5, 5, 5]])
>>> a.shape
Out[2]: torch.Size([3, 4])
>>> b = torch.full_like(a, 6)
>>> b
Out[3]:
tensor([[6, 6, 6, 6],
[6, 6, 6, 6],
[6, 6, 6, 6]])
>>> b.shape
Out[4]: torch.Size([3, 4])
4、pytorch torch.where 用法:
pytorch torch.where用法(https://blog.csdn.net/Orientliu96/article/details/104827391)
根据条件,返回从x,y中选择元素所组成的张量。如果满足条件,则返回x中元素。若不满足,返回y中元素。
例子
5、Pytorch 中的转置卷积 nn.ConvTranspose2d:
谈谈Pytorch中的转置卷积 nn.ConvTranspose2d(https://blog.csdn.net/qq_36560894/article/details/115552509)
nn.ConvTranspose2d详解(https://blog.csdn.net/qq_41076797/article/details/114494990)
转置卷积,主要用于上采样。
① 首先 in_channels,out_channels 就是输入通道数以及期望的输出通道数,通常是与对应的下采样Conv相反;
② kernel, stride, padding 通常与对应的下采样 Conv 保持一致,下面讲一下如何根据这些参数计算输出特征图尺寸:
如果对转置卷积参数熟悉的话,首先我们在特征图中间插值 0,每两个元素之间插 stride-1 个 0 值;然后真实的 padding 与输入的会有所区别,真正做的 padding 为 kernel-padding-1,真实的卷积核 kernel 与输入参数一致,真实的 stride 不管输入参数是什么,永远为 1:
因此,此刻的特征图尺寸为(以 H 方向为例):
③ output_padding 参数?
通常我们将参数按 demo 的设置就已经可以得到与原图分辨率一致的上采样结果了,但有些情况下,不同分辨率的输入经过卷积能够得到同一分辨率的输出。比如,上一节给出的实例代码,我们输入 6x6 的卷积也能得到 3x3 尺寸的特征输出。
conv = nn.Conv2d(3, 8, 3, stride=2, padding=1)
Dconv = nn.ConvTranspose2d(8, 3, 3, stride=2, padding=1)
x = torch.randn(1, 3, 6, 6)
feature = conv(x)
print(feature.shape)
# out : torch.Size([1, 8, 3, 3])
y = Dconv(feature)
print(y.shape)
# out : torch.Size([1, 3, 5, 5])
但在利用转置卷积进行恢复时,只得到了 5x5 的特征,这个时候 output_padding 参数就起作用了
该参数就是用于调整输出分辨率的大小的,注意:不是在输出上插值喔!比如按照计算规则,我们只能得到 5x5 的输出,要得到 6x6 的输出就需要将 output_padding 设为 1。
conv = nn.Conv2d(3, 8, 3, stride=2, padding=1)
Dconv = nn.ConvTranspose2d(8, 3, 3, stride=2, padding=1, output_padding=1)
x = torch.randn(1, 3, 6, 6)
feature = conv(x)
y = Dconv(feature)
print(y.shape)
# out : [1,3,6,6]
6、torch. nn.Softmax(dim=1):
torch. nn.Softmax(dim=1)(https://blog.csdn.net/qq_36097393/article/details/89319643)
Softmax 函数的定义:
torch. nn.Softmax(dim=2),则对 dim=2 进行 softmax。
示例: