class OurModule(nn.Module):
def __init__(self, num_inputs, num_classes, dropout_prob=0.3):
super(OurModule, self).__init__()
self.pipe = nn.Sequential(
nn.Linear(num_inputs, 5),
nn.ReLU(),
nn.Linear(5, 20),
nn.ReLU(),
nn.Linear(20, num_classes),
nn.Dropout(p=dropout_prob),
nn.Softmax(dim=1)
)
def forward(self, x):
return self.pipe(x)
上面参数主要介绍下dropout_prob,模型训练时应用Dropout的流程,概况一下描述就是:
1.随机概率p随机dropout部分神经元,并前向传播
2.计算前向传播的损失,应用反向传播和梯度更新(对剩余的未被dropout的神经元)
3.恢复所有神经元的,并重复过程1
if __name__ == "__main__":
net = OurModule(num_inputs=2, num_classes=3)
print(net)
v = torch.FloatTensor([[2, 3]])
out = net(v)
print(out)
print("Cuda's availability is %s" % torch.cuda.is_available())
if torch.cuda.is_available():
print("Data from cuda: %s" % out.to('cuda'))
输入参数为Tensor[2,3],经过三层NN,最后输出softmax。
import torch as t
from torch import nn
from torch.nn import functional as F
# 假定输入的图像形状为[3,64,64]
x = t.randn(10, 3, 64, 64) # 10张 3个channel 大小为64x64的图片
x = nn.Conv2d(3, 64, kernel_size=3, stride=3, padding=0)(x)
print(x.shape)
# 之前的特征图尺寸为多少,只要设置为(1,1),那么最终特征图大小都为(1,1)
# x = F.adaptive_avg_pool2d(x, [1,1]) # [b, 64, h, w] => [b, 64, 1, 1]
# print(x.shape)
# 将四维张量转换为二维张量之后,才能作为全连接层的输入
x = x.view(x.size(0), -1) #view()的作用相当于numpy中的reshape,重新定义矩阵的形状。
print(x.shape)
# in_features由输入张量的形状决定,out_features则决定了输出张量的形状
connected_layer = nn.Linear(in_features = 64*21*21, out_features = 10)
# 调用全连接层
output = connected_layer(x)
print(output.shape)