1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
| from matplotlib import pyplot as plt import numpy as np import collections
import torch import torch.nn as nn import torch.nn.functional as F import torch.optim as optim
torch.set_printoptions(edgeitems=2) torch.manual_seed(123) class_names = ['airplane','automobile','bird','cat','deer','dog','frog','horse','ship','truck']
from torchvision import datasets, transforms data_path = 'E:/CIFAR-10/' cifar10 = datasets.CIFAR10( data_path, train=True, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4915, 0.4823, 0.4468), (0.2470, 0.2435, 0.2616)) ])) cifar10_val = datasets.CIFAR10( data_path, train=False, download=True, transform=transforms.Compose([ transforms.ToTensor(), transforms.Normalize((0.4915, 0.4823, 0.4468), (0.2470, 0.2435, 0.2616)) ]))
label_map = {0: 0, 2: 1} class_names = ['airplane', 'bird'] cifar2 = [(img, label_map[label]) for img, label in cifar10 if label in [0, 2]] cifar2_val = [(img, label_map[label]) for img, label in cifar10_val if label in [0, 2]]
class Net(nn.Module): def __init__(self): super().__init__() self.conv1 = nn.Conv2d(3, 16, kernel_size=3, padding=1) self.act1 = nn.Tanh() self.pool1 = nn.MaxPool2d(2) self.conv2 = nn.Conv2d(16, 8, kernel_size=3, padding=1) self.act2 = nn.Tanh() self.pool2 = nn.MaxPool2d(2) self.fc1 = nn.Linear(8 * 8 * 8, 32) self.act3 = nn.Tanh() self.fc2 = nn.Linear(32, 2)
def forward(self, x): out = self.pool1(self.act1(self.conv1(x))) out = self.pool2(self.act2(self.conv2(out))) out = out.view(-1, 8 * 8 * 8) out = self.act3(self.fc1(out)) out = self.fc2(out) return out
import datetime
def training_loop(n_epochs, optimizer, model, loss_fn, train_loader): for epoch in range(1, n_epochs + 1): loss_train = 0.0 for imgs, labels in train_loader: imgs = imgs.to("cuda:0") labels = labels.to("cuda:0") outputs = model(imgs) loss = loss_fn(outputs, labels) optimizer.zero_grad() loss.backward() optimizer.step() loss_train += loss.item() if epoch == 1 or epoch % 10 == 0: print('{} Epoch {}, 训练损失{}'.format(datetime.datetime.now(), epoch, loss_train / len(train_loader)))
|