# -*- coding: UTF-8 -*-
import torch
import torch.nn as nn
# 先定义x
x = torch.rand(1, 16, 28, 28)
# 括号内第一个参数是:窗口的大小,第二个是移动的步长距离
layer = nn.MaxPool2d(2, stride=2)
out1 = layer(x)
print(out1.size())
"""
输出结果:
torch.Size([1, 16, 14, 14])
"""
# -*- coding: UTF-8 -*-
import torch
import torch.nn as nn
# 先定义x
x = torch.rand(1, 16, 28, 28)
# 括号内第一个参数是:窗口的大小,第二个是移动的步长距离
layer = nn.AvgPool2d(2, stride=2)
# 进行Avg pooling的计算
out2 = layer(x)
print(out2.size())
"""
输出结果:
torch.Size([1, 16, 14, 14])
"""
# -*- coding: UTF-8 -*-
import torch
import torch.nn.functional as F
# 先定义x
x = torch.rand(1, 16, 28, 28)
# 上采样的API为: .interpolate
# 括号内参数为输入的tensor、放大的倍率、模式为紧邻差值法
out = F.interpolate(x, scale_factor=2, mode='nearest')
print(out.size())
"""
输出结果:
torch.Size([1, 16, 56, 56])
"""
# -*- coding: UTF-8 -*-
import torch
import torch.nn as nn
x = torch.rand(1, 16, 28, 28)
layer = nn.ReLU(inplace=True)
out = layer(x)
print(out.size())
"""
在进行ReLU操作,进行inplace操作后,较小值会变为零,但数据的size不会发生改变。
通过这种数据会节省一部分的数据存储量。
输出结果:
torch.Size([1, 16, 28, 28])
"""