## nn.module

module是所有神经网络最基本的类

import torch.nn as nn
import torch.nn.functional as F
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5)
self.conv2 = nn.Conv2d(20, 20, 5)
def forward(self, x):
x = F.relu(self.conv1(x))
return F.relu(self.conv2(x))

F.relu 常用的激活函数

nn.conv2d 卷积函数

import torch
from torch import nn
import torch.nn.functional as F
class simple_nn(nn.Module):
def __init__(self):
super().__init__()
def forward(self, input):
output = F.relu(input)
return output
example_nn = simple_nn()
x = torch.tensor(0.1)
print(example_nn(x))
x = torch.tensor(-0.1)
print(example_nn(x))
'''
tensor(0.1000)
tensor(0.)
'''

In the simplest case, the output value of the layer with input size ( N , C in , H , W ) (N, C_{\text{in}}, H, W) ( N , C in ​ , H , W ) and output ( N , C out , H out , W out ) (N, C_{\text{out}}, H_{\text{out}}, W_{\text{out}}) ( N , C out ​ , H out ​ , W out ​ ) can be precisely described as:

out ⁡ ( N i , C out  j ) = bias ⁡ ( C out  j ) + ∑ k = 0 C in  − 1  weight  ( C out  j , k ) ⋆ input ⁡ ( N i , k ) \operatorname{out}\left(N_{i}, C_{\text {out }_{j}}\right)=\operatorname{bias}\left(C_{\text {out }_{j}}\right)+\sum_{k=0}^{C_{\text {in }}-1} \text { weight }\left(C_{\text {out }_{j}}, k\right) \star \operatorname{input}\left(N_{i}, k\right) o u t ( N i ​ , C out  j ​ ​ ) = b i a s ( C out  j ​ ​ ) + k = 0 ∑ C in  ​ − 1 ​  weight  ( C out  j ​ ​ , k ) ⋆ i n p u t ( N i ​ , k )

C o u t j C_{out_j} C o u t j ​ ​

, 权重与输入的互相关的累加(不知道描述准不准确, 没学过cross-correlation…)再加上通道的偏差bias

import torch
import torch.nn.functional as F
input = torch.tensor([[ 1,  2,  3,  4,  5],
[10,  9,  8,  7,  6],
[11, 12, 13, 14, 15],
[20, 19, 18, 17, 16],
[21, 22, 23, 24, 25]])
kernel = torch.tensor([[1, 0, 1],
[0, 1, 0],
[1, 0, 1]])
input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))
print(input.shape)
print(kernel.shape)
output = F.conv2d(input, kernel, stride=1)
print(output)
output2 = F.conv2d(input, kernel, stride=2)
print(output2)
output3 = F.conv2d(input, kernel, stride=1, padding=1)
print(output3)
'''
torch.Size([1, 1, 5, 5])
torch.Size([1, 1, 3, 3])
tensor([[[[37, 40, 43],
[68, 65, 62],
[87, 90, 93]]]])
tensor([[[[37, 43],
[87, 93]]]])
tensor([[[[10, 20, 19, 18, 12],
[24, 37, 40, 43, 24],
[39, 68, 65, 62, 39],
[54, 87, 90, 93, 54],
[40, 60, 59, 58, 42]]]])
'''

input = torch.reshape(input, (1, 1, 5, 5))
kernel = torch.reshape(kernel, (1, 1, 3, 3))

import torch
import torchvision
from torch.nn import Conv2d
from torch import nn
from torch.utils.tensorboard import SummaryWriter
class simple_convnn(nn.Module):
def __init__(self):
super(simple_convnn, self).__init__()
self.conv = Conv2d(in_channels=3, out_channels=6, kernel_size=3, stride=1, padding=0)
def forward(self, x):
x = self.conv(x)
return x
example_convnn = simple_convnn()
writer = SummaryWriter("logs")
step = 0
imgs, targets = data
output = example_convnn(imgs)
shape = output.shape
# print(shape)
output_reshape = torch.reshape(output, (-1, 3, 30, 30))
step += 1
writer.close()

class torch.nn.MaxPool2d(kernel_size, stride=None, padding=0,
dilation=1, return_indices=False, ceil_mode=False)

import torch
import torchvision
from torch.nn import MaxPool2d
from torch import nn
from torch.utils.tensorboard import SummaryWriter
class pooling_nn(nn.Module):
def __init__(self):
super(pooling_nn, self).__init__()
self.maxpool = MaxPool2d(kernel_size=3, ceil_mode=False)
def forward(self, input):
output = self.maxpool(input)
return output
example_pooling = pooling_nn()
writer = SummaryWriter("logs")
step = 0
imgs, targets = data
output = example_pooling(imgs)
step += 1
writer.close()

## sequential

import torch
from torch import nn
class simple_sequential(nn.Module):
def __init__(self):
super(simple_sequential, self).__init__()
self.model = nn.Sequential(
nn.Conv2d(3, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 32, 5, 1, 2),
nn.MaxPool2d(2),
nn.Conv2d(32, 64, 5, 1, 2),
nn.MaxPool2d(2),
nn.Flatten(),
nn.Linear(64*4*4, 64),
nn.Linear(64, 10)
)
def forward(self, x):
x = self.model(x)
return x
if __name__ == '__main__':
example_sequential = simple_sequential()
input = torch.ones((64, 3, 32, 32))
output = example_sequential(input)
print(output.shape)
'''
torch.Size([64, 10])
'''

## 损失函数

ML里的重点, 一切都要归结到损失函数上…

L1loss 平均绝对误差
MSEloss 均方误差
CrossEntropyLoss 交叉熵loss

inputs = torch.tensor([1, 2, 3], dtype=torch.float32)
targets = torch.tensor([1, 2, 5], dtype=torch.float32)
inputs = torch.reshape(inputs, (1, 1, 1, 3))
targets = torch.reshape(targets, (1, 1, 1, 3))
loss = L1Loss(reduction='sum')
result = loss(inputs, targets)
loss_mse = nn.MSELoss()
result_mse = loss_mse(inputs, targets)
print(result)
print(result_mse)
x = torch.tensor([0.1, 0.2, 0.3])
y = torch.tensor([1])
x = torch.reshape(x, (1, 3))
loss_cross = nn.CrossEntropyLoss()
result_cross = loss_cross(x, y)
print(result_cross)
'''
tensor(2.)
tensor(1.3333)
tensor(1.1019)
'''

import torchvision
from torch import nn
from torch.nn import Sequential, Conv2d, MaxPool2d, Linear, Flatten
dataset = torchvision.datasets.CIFAR10("./dataset", train=False,
class simple_nn(nn.Module):
def __init__(self):
super(simple_nn, self).__init__()
self.model1 = Sequential(
MaxPool2d(2),
MaxPool2d(2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
loss = nn.CrossEntropyLoss()
example_nn = simple_nn()
imgs, targets = data
outputs = example_nn(imgs)
result_loss = loss(outputs, targets)
print(result_loss)

import torch
import torchvision
from torch import nn
from torch.nn import Conv2d, Flatten, Linear, MaxPool2d, Sequential
dataset = torchvision.datasets.CIFAR10("dataset", train=False,
class nn_optim(nn.Module):
def __init__(self):
super(nn_optim, self).__init__()
self.model1 = Sequential(
MaxPool2d(2),
MaxPool2d(2),
MaxPool2d(2),
Flatten(),
Linear(1024, 64),
Linear(64, 10)
)
def forward(self, x):
x = self.model1(x)
return x
loss = nn.CrossEntropyLoss()
example_optim = nn_optim()
optim = torch.optim.SGD(example_optim.parameters(), lr=0.01)
for epoch in range(10):
running_loss = 0.0
imgs, targets = data
outputs = example_optim(imgs)
result_loss = loss(outputs, targets)
result_loss.backward()
optim.step()
running_loss = running_loss + result_loss
print(running_loss)
'''
'''

(可以看到第3轮训练时loss最小, 后边就是反向训练了

## 网络模型修改

import torch
import torchvision
from torch import nn
from torch.utils.tensorboard import SummaryWriter
vgg16_false = torchvision.models.vgg16(pretrained=False)
vgg16_true = torchvision.models.vgg16(pretrained=True)
dataset = torchvision.datasets.CIFAR10('dataset', train=True,
print(vgg16_true)
print(vgg16_true)
print(vgg16_false)
vgg16_false.classifier[6] = nn.Linear(4096, 10)
print(vgg16_false)
writer = SummaryWriter("logs")
step = 0
imgs, targets = data
output = vgg16_true(imgs)
output = torch.reshape(output, (-1,3,10,8))
step += 1
writer.close()

print(vgg16_true)
print(vgg16_true)

print(vgg16_false)
vgg16_false.classifier[6] = nn.Linear(4096, 10)
print(vgg16_false)

## 模型保存和读取

vgg16 = torchvision.models.vgg16(pretrained=False)
# 保存方式1,模型结构 + 模型参数
torch.save(vgg16, "vgg16_method1.pth")
# 保存方式2，模型参数（官方推荐）
torch.save(vgg16.state_dict(), "vgg16_method2.pth")

# 方式1, 直接加载模型
# 方式2，先加载无预训练模型, 然后加载模型参数
vgg16 = torchvision.models.vgg16(pretrained=False)
vgg16.load_state_dict(torch.load("vgg16_method2.pth"))