【LLM学习之路】9月16日 第六天
损失函数
L1Loss
可以取平均也可以求和
参数解析
input (N,*) N是batchsize,星号代表可以是任意维度 不是输入的参数,只是描述数据
target 形状要同上
MSELoss平方差
CrossEntropyLoss交叉熵
inputs的形状要是
(N, C) N是批次大小
x = torch.tensor([0.1,0.2,0.3]) #形状为 (3,) 的 1D 张量
y = torch.tensor([1])
x = torch.reshape(x,(1,3)) #inputs 的形状要是 (N, C)
loss_cross = nn.CrossEntropyLoss()
result_cross = loss_cross(x,y)
反向传播
result_loss.backward()
优化器
套路是这样的
optim = torch.optim.SGD(tudui.parameters(),loss=0.01)
optim.zero_grad() 进行梯度清零
result_loss.backward() 反向传播计算梯度
optim.step() 对模型参数进行调优
后面自己添加了如何使用显卡
import torch
import torchvision.datasets
from torch import nn
from torch.nn import Conv2d, MaxPool2d, Flatten, Linear, Sequential
from torch.utils.data import DataLoader
# 检查是否有 GPU 可用
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
print(f"Using device: {device}")
dataset = torchvision.datasets.CIFAR10("./data",train = False,download=True,transform=torchvision.transforms.ToTensor())
dataloader = DataLoader(dataset,batch_size=1)
class Tudui(nn.Module):
def __init__(self):
super(Tudui,self).__init__()
self.conv1 = Conv2d(3,32,5,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)
self.flatten = Flatten()
self.linear1 = Linear(1024,64)
self.linear2 = Linear(64,10)
self.model1 = Sequential(
Conv2d(3, 32, 5, 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.model1(x)
return x
loss = nn.CrossEntropyLoss()
tudui = Tudui().to(device)
optim = torch.optim.SGD(tudui.parameters(),lr=0.01)
for epoch in range(20):
running_loss = 0.0
for data in dataloader:
imgs,targets = data
imgs,targets = imgs.to(device), targets.to(device)
outputs = tudui(imgs)
# print(outputs)
# print(targets)
result_loss = loss(outputs,targets)
optim.zero_grad()
result_loss.backward()
optim.step()
# print("ok")
running_loss = result_loss + running_loss
print(running_loss)
完整的模型验证套路
利用已经训练好的模型,然后给它提供输入