1. 各层介绍

    1.1 Flatten(扁平操作)

torch.nn.Flatten(start_dim=1,end_dim=-1) 

import torch
from torch import nn
input1 = torch.randn(2,3,4,5)
m = nn.Flatten()   #合并第二位到最后一位
output1 = m(input1)
print(output1.size())

input2 = torch.randn(3,4,5)
output2 = m(input2)
print(output2.size())

m2 = nn.Flatten(0,3)   #first dim to flatten and last dim to flatten
output3 = m2(input1)
print(output3.size())

m3 = nn.Flatten(0,1)
output4 = m3(input2)
print(output4.size())

运行结果:

     1.2 Linear(全连接操作)

torch.nn.Linear(in_features, out_features, bias=True, device=None, dtype=None)

  • in_features (int) – size of each input sample

  • out_features (int) – size of each output sample

  • bias (bool) – If set to False, the layer will not learn an additive bias. Default: True

    import torch
    from torch import nn
    input = torch.randn(128,20)
    m = nn.Linear(20,30)
    output = m(input)
    print(output.size())
    
    input2 = torch.randn(4)
    m2 = nn.Linear(4,2)
    output2 = m2(input2)
    print(output2)

    运行结果:

       1.3 Softmax(归一化操作)

torch.nn.Softmax(dim=None)

dim (int) – 计算 Softmax 的维度(因此每个切片 沿 dim 将总和为 1)。

import torch
from torch import nn
input = torch.randn(2,3)
print(input)
m = nn.Softmax(dim=0)  #沿列归一化
output1 = m(input)
print(f"列归一化结果:{output1}")
m2 = nn.Softmax(dim=1)  #沿行归一化
output2 = m2(input)
output3  = nn.Softmax(dim=1)(input)
print(f"行归一化结果:{output2}")

运行结果:

2.Sequential(顺序容器)

model = nn.Sequential(
          nn.Conv2d(1,20,5),
          nn.ReLU(),
          nn.Conv2d(20,64,5),
          nn.ReLU()
        )
model = nn.Sequential(OrderedDict([
          ('conv1', nn.Conv2d(1,20,5)),
          ('relu1', nn.ReLU()),
          ('conv2', nn.Conv2d(20,64,5)),
          ('relu2', nn.ReLU())
        ]))

3.输出模型结构与参数大小

from torch import nn
model = nn.Sequential(
    nn.Flatten(0,-1),
    nn.Linear(20,10),
    nn.ReLU(),
    nn.Linear(10,5),
    nn.ReLU(),
    nn.Linear(5,1)
        )
print("model structure:",{model})
for name,param in model.named_parameters():
    print(f"Layer:{name},Size:{param.size()}")    #类似于参数的shape
    # print(f"Values:{param.values()}")     #输出每层的参数值

运行结果:

4. 通过子类化定义神经网络

from torch import nn
class NeuralNetwork(nn.Module):
    def __init__(self):
        super().__init__()
        self.flatten = nn.Flatten()
        self.linear_relu_stack = nn.Sequential(
            nn.Linear(28*28, 512),
            nn.ReLU(),
            nn.Linear(512, 512),
            nn.ReLU(),
            nn.Linear(512, 10),
        )

    def forward(self, x):
        x = self.flatten(x)
        logits = self.linear_relu_stack(x)
        return logits
model = NeuralNetwork()
print(model)
for name ,param in model.named_parameters():
    print(f"Layer:{name},size:{param.size}")

运行结果:

更多推荐