首页 > 其他分享 >pytorch train demo

pytorch train demo

时间:2022-12-22 15:36:24浏览次数:46  
标签:loss nn demo self pytorch train test data

一、用pytorch实现lenet类似网络的训练

  1.网络结构

    

  2.代码

mport torch                                                                                                          
import torch.nn as nn
import torch.nn.functional as F
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader
import torch.optim as optim
import pdb

class Lenet(nn.Module):
    def __init__(self, num_classes):
        super(Lenet, self).__init__()
        self.conv1 = nn.Conv2d(3, 16, 5)
        self.pool1 = nn.MaxPool2d(2, 2)
        self.conv2 = nn.Conv2d(16, 32, 5)
        self.pool2 = nn.MaxPool2d(2, 2)
        self.fc1 = nn.Linear(32 * 5 * 5, 120)
        self.fc2 = nn.Linear(120, 84)
        self.fc3 = nn.Linear(84, 10)

    def forward(self, x):
        x = F.relu(self.conv1(x))  # input(3, 32, 32) output(16, 28, 28)
        x = self.pool1(x)  # output(16, 14, 14)
        x = F.relu(self.conv2(x))  # output(32, 10, 10)
        x = self.pool2(x)  # output(32, 5, 5)
        x = x.view(-1, 32 * 5 * 5)  # output(32*5*5)
        x = F.relu(self.fc1(x))  # output(120)
        x = F.relu(self.fc2(x))  # output(84)
        x = self.fc3(x)  # output(10)
        return x

def get_data():
    transform = transforms.Compose(
        [transforms.ToTensor(),
         transforms.Normalize((0.5, 0.5, 0.5), (0.5, 0.5, 0.5))]
    )
    train_data = torchvision.datasets.CIFAR10(root='./data', train=True, download=True, transform=transform)
    train_loader = DataLoader(train_data, batch_size=50, shuffle=False, num_workers=0)
    test_data = torchvision.datasets.CIFAR10(root='./data', train=False, download=False, transform=transform)
    test_loader = DataLoader(test_data, batch_size=1000, shuffle=False, num_workers=0)
    test_data_iter = iter(test_loader)
    test_image, test_label = test_data_iter.next()
    return train_loader, test_image, test_label

def train(train_loader, test_image, test_label):
    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    net = Lenet(10)
    net.to(device)
    loss_function = nn.CrossEntropyLoss()
    optimizer = optim.Adam(net.parameters(), lr=0.001)
    for epoch in range(100):
        running_loss = 0.0
        for step, data in enumerate(train_loader, start=0):
            inputs, labels = data
            optimizer.zero_grad()
            outputs = net(inputs.to(device))
            loss = loss_function(outputs, labels.to(device))
            loss.backward()
            optimizer.step()
            running_loss += loss.item()
            if step % 1000 == 999:
                with torch.no_grad():
                    outputs = net(test_image.to(device)) # 将test_image分配到指定的device中
                    predict_y = torch.max(outputs, dim=1)[1]
                    accuracy = (predict_y == test_label.to(device)).sum().item() / test_label.size(0)
                    print('[%d, %5d] train_loss: %.3f  test_accuracy: %.3f' %
                          (epoch + 1, step + 1, running_loss / 1000, accuracy))
                    running_loss = 0.0

if __name__ == '__main__':
    train_loader, test_image, test_label = get_data()
    train(train_loader, test_image, test_label)

二、用pytorch实现一个基础函数


 

标签:loss,nn,demo,self,pytorch,train,test,data
From: https://www.cnblogs.com/EstherLjy/p/16998817.html

相关文章