<>DAY 3-4 end

1. Some simple loss function calls
loss function loss Role of
1. Calculate the gap between the actual output and the target
2. It provides a basis for us to update the output （ Back propagation ）, grad
import torch from torch.nn import L1Loss from torch import nn # loss function loss Role of #
1. Calculate the gap between the actual output and the target # 2. It provides a basis for us to update the output （ Back propagation ）, grad inputs = torch.tensor([1,2,3] ,
dtype=torch.float32) targets = torch.tensor([1,2,5] , dtype=torch.float32) #
reshape become batchsize：1 , channels：1 , Number of rows ：1 , Number of columns ： 1 inputs = torch.reshape(inputs ,
(1, 1,1,3) ) targets = torch.reshape(targets , (1,1,1,3)) #
L1Loss() Is the corresponding bit subtraction , Then the results are averaged loss = L1Loss() result = loss(inputs , targets)
print(result) # MSE Square difference loss function # MSE = （0 + 0 +2^2）/3 = 1.333 loss_mse =
nn.MSELoss() result_mse = loss_mse(inputs , targets) print(result_mse) #
Cross entropy CrossEntropyLoss , Used in classification problems 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)
2. Adding loss function to network
import torch import torchvision.datasets from torch import nn from torch.nn
import Conv2d, MaxPool2d, Linear, Flatten, Sequential from torch.utils.data
import DataLoader from torch.utils.tensorboard import SummaryWriter dataset =
torchvision.datasets.CIFAR10("dataseset_CIFAR10" , train=False ,
DataLoader(dataset , batch_size=1) class Tudui(nn.Module): def __init__(self):
super(Tudui, self).__init__() # self.conv1 = Conv2d(3 , 32 , 5, stride=1 ,
padding=2) # self.maxpool1 = MaxPool2d(2) # self.conv2 = Conv2d( 32 , 32 , 5
,padding=2) # self.maxpool2 = MaxPool2d(2) # self.conv3 = Conv2d(32, 64 , 5
,padding=2) # self.maxpool3 = MaxPool2d(2) # # Input layer to hidden layer # self.linear1 =
Linear(1024 , 64) # # Hide layer to output layer # self.linear2 = Linear(64 , 10) # self.flatten =
Flatten() # Introduce a Sequential, Package your actions into model1, For use below # The following code is the same as the code commented above
self.model1 = Sequential(Conv2d(3 , 32 , 5, stride=1 , padding=2),
MaxPool2d(2), Conv2d( 32 , 32 , 5 ,padding=2), MaxPool2d(2), Conv2d(32, 64 , 5
,padding=2), MaxPool2d(2), Flatten(), Linear(1024 , 64), Linear(64 , 10)) def
forward(self , x): # x = self.conv1(x) # x = self.maxpool1(x) # x =
self.conv2(x) # x = self.maxpool2(x) # x = self.conv3(x) # x = self.maxpool3(x)
# x = self.flatten(x) # x = self.linear1(x) # x = self.linear2(x) #
The following code works the same as above x = self.model1(x) return x # Adding loss function to network loss =
nn.CrossEntropyLoss() tudui = Tudui() for data in dataloader: imgs , targets =
data outputs = tudui(imgs) result_loss = loss(outputs , targets)
result_loss.backward() print("ok")
result_loss = loss(outputs , targets)
The parameters are the actual results and the target results
result_loss.backward()

3. optimizer
# Define an optimizer , Learning rate lr Set as 0.01 optim = torch.optim.SGD(tudui.parameters() ,
lr=0.01) Then pay attention to clearing the gradient of each cycle , Because the previous gradient is useless now ```
import torch
import torchvision.datasets
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Linear, Flatten, Sequential
from torch.utils.tensorboard import SummaryWriter

dataset = torchvision.datasets.CIFAR10(“dataseset_CIFAR10” , train=False ,
transform=torchvision.transforms.ToTensor(),

class Tudui(nn.Module):
def init(self):
super(Tudui, self).init()
# self.conv1 = Conv2d(3 , 32 , 5, stride=1 , padding=2)
# self.maxpool1 = MaxPool2d(2)
# self.conv2 = Conv2d( 32 , 32 , 5 ,padding=2)
# self.maxpool2 = MaxPool2d(2)
# self.conv3 = Conv2d(32, 64 , 5 ,padding=2)
# self.maxpool3 = MaxPool2d(2)
# # Input layer to hidden layer
# self.linear1 = Linear(1024 , 64)
# # Hide layer to output layer
# self.linear2 = Linear(64 , 10)
# self.flatten = Flatten()
# Introduce a Sequential, Package your actions into model1, For use below # The following code is the same as the code commented above self.model1 =
Sequential(Conv2d(3 , 32 , 5, stride=1 , padding=2), MaxPool2d(2), Conv2d( 32 ,
Flatten(), Linear(1024 , 64), Linear(64 , 10)) def forward(self , x): # x =
self.conv1(x) # x = self.maxpool1(x) # x = self.conv2(x) # x = self.maxpool2(x)
# x = self.conv3(x) # x = self.maxpool3(x) # x = self.flatten(x) # x =
self.linear1(x) # x = self.linear2(x) # The following code works the same as above x = self.model1(x) return x
loss = nn.CrossEntropyLoss()
tudui = Tudui()

<> Define an optimizer , Learning rate lr Set as 0.01

optim = torch.optim.SGD(tudui.parameters() , lr=0.01)
for epoch in range(20):
running_loss = 0.0
imgs , targets = data
outputs = tudui(imgs)
result_loss = loss(outputs , targets)
result_loss.backward()
optim.step()
running_loss = running_loss + result_loss
print(running_loss)
import torchvision

<># False It's an untrained network model

from torch import nn

<> Use and modification of existing models

vgg16_false = torchvision.models.vgg16(pretrained=False , progress=True)

<># True It is a trained network model

vgg16_true = torchvision.models.vgg16(pretrained=True , progress=True)
print(vgg16_true)

train_data = torchvision.datasets.CIFAR10(“dataseset_CIFAR10” , train=True ,
transform=torchvision.transforms.ToTensor(),

<> In the network model classifier Add a linear layer to the

<> because CIFAR10 Output features yes 10 , and vgg16 Last output features yes 1000 , So it needs to be changed

print(vgg16_true)
Change in existing model
***5. Two ways to save models ***
import torch
import torchvision
from torch import nn

vgg16 = torchvision.models.vgg16(pretrained=False)

<> Save mode 1– model structure + model parameter

torch.save(vgg16 , “vgg16_method1.pth”)

<> Save mode 2– model parameter （ Official recommendation ）

<> Save parameters as a dictionary

torch.save(vgg16.state_dict() , “vgg16_method2.pth”)

<> trap

class Tudui(nn.Module):
def init(self):
super(Tudui , self).init()
self.conv1 = nn.Conv2d(3 , 64 ,kernel_size=3)
def _slow_forward(self, x): x = self.conv1(x) return x
tudui = Tudui()
torch.save(tudui , “tudui_method1.pth”)
import torch
from torch import nn

import torchvision.models

print(model)

vgg16 =torchvision.models.vgg16(pretrained=False)

<> trap 1： It doesn't work need # Comment Section Only omitted ## part

<>class Tudui(nn.Module):

<>def init(self):

<>super(Tudui , self).init()

<>self.conv1 = nn.Conv2d(3 , 64 ,kernel_size=3)

<>

<>def _slow_forward(self, x):

<>x = self.conv1(x)

<>return x

<>#tudui = Tudui()

print(model)
***8. A complete with cpu Training model ***
import torch
from torch import nn

import torchvision.models

print(model)

vgg16 =torchvision.models.vgg16(pretrained=False)

<> trap 1： It doesn't work need # Comment Section Only omitted ## part

<>class Tudui(nn.Module):

<>def init(self):

<>super(Tudui , self).init()

<>self.conv1 = nn.Conv2d(3 , 64 ,kernel_size=3)

<>

<>def _slow_forward(self, x):

<>x = self.conv1(x)

<>return x

<>#tudui = Tudui()

print(model)
**9. A complete with gpu Training model **
import torch
import torchvision
from torch import nn
from torch.utils.tensorboard import SummaryWriter
import time

# Can use cuda Training stuff

<> network model

<> data （ input , tagging ）

<> loss function

<>.cuda()

<> Prepare dataset

train_data = torchvision.datasets.CIFAR10(“dataseset_CIFAR10”,train=True
,transform=torchvision.transforms.ToTensor(),
test_data = torchvision.datasets.CIFAR10(“dataseset_CIFAR10”,train=False
,transform=torchvision.transforms.ToTensor(),

<>length length

train_data_size = len(train_data)
test_data_size = len(test_data)
print(“ The length of training data set is ：{}”.format(train_data_size))
print(“ Test data set length is ：{}”.format(test_data_size))

<> Build neural network

class Tudui(nn.Module):
def init(self):
super(Tudui, 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(1024,64),
nn.Linear(64,10)
)
def forward(self,x): x = self.model(x) return x
<> Create network model

tudui = Tudui()
tudui = tudui.cuda()

<> loss function

loss_fn = nn.CrossEntropyLoss()
loss_fn = loss_fn.cuda()

<> optimizer

learning_rate = 0.01
optimizer = torch.optim.SGD(tudui.parameters(),lr=learning_rate,)

<> Set some parameters of the training network

<> Record the number of workouts

total_train_step = 0

<> Record the number of tests

total_test_step = 0

<> Number of rounds of training

epoch = 10

writer = SummaryWriter(“logs”)
start_time = time.time()

for i in range(epoch):
print("------ The first {} The first round of training began ----".format(i+1))
# Start of training steps tudui.train() # This line of code is useful for some specific network layers , as dropout for data in train_data: imgs
, targets = data imgs = imgs.cuda() targets = targets.cuda() outputs =
tudui(imgs) loss = loss_fn(outputs , targets) # Optimizer optimization model optimizer.zero_grad()
loss.backward() optimizer.step() total_train_step += 1 if total_train_step %
100 == 0: end_time = time.time() print(" The first {} What is the training time ：{}".format(total_train_step,
end_time - start_time )) print(" Training times ：{},Loss：{}".format(total_train_step ,
loss)) writer.add_scalar("train_loss" , loss.item(),total_train_step) # Test step start
tudui.eval() # Similarly , Useful for some specific layers total_test_loss = 0 with torch.no_grad():
<> That is, no tuning is required
for data in test_data: imgs , targets = data imgs = imgs.cuda() targets =
targets.cuda() outputs = tudui(imgs) loss = loss_fn(outputs , targets)
total_test_loss += loss.item() print(" On the overall test set Loss：{}".format(total_test_loss))
+= 1
<> Model saving
torch.save(tudui,"tudui_{}_gpu.pth".format(i)) print(" Model saved ")
total_end_time = time.time()
print(“ The total training time is ：{}”.format( total_end_time - start_time))

writer.close()
***10. Model testing ***
import torch
import torchvision.transforms
from PIL import Image
from torch import nn

image_path = “imgs/dog.png”
image = Image.open(image_path)
print(image)
image = image.convert(‘RGB’)

transform =
torchvision.transforms.Compose([torchvision.transforms.Resize((32,32)),
torchvision.transforms.ToTensor()])
image = transform(image)
print(image.shape)

class Tudui(nn.Module):
def init(self):
super(Tudui, 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(1024,64),
nn.Linear(64,10)
)
def forward(self,x): x = self.model(x) return x