1. strong>数据集介绍：

1. strong>第一步：加载数据集

1 . 导入必要的第三方库

import torchfrom torch import nnimport torch.optim as optimimport torch.nn.functional as Ffrom torch.autograd import Variableimport matplotlib.pyplot as pltfrom torchvision import transforms,datasetsfrom torch.utils.data import DataLoader
1. 载数据集 （pytorch或者tensorflow都是预留了下载数据集的接口的，所以不需要我们再另外去下载）
def plot_curve(data):       fig = plt.figure()    plt.plot(range(len(data)), data, color='blue')      plt.legend(['value'], loc='upper right')    plt.xlabel('step')    plt.ylabel('value')    plt.show()transTrain=transforms.Compose([transforms.RandomHorizontalFlip(),                               transforms.RandomGrayscale(),                             transforms.ToTensor(),                             transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])transTest=transforms.Compose([transforms.ToTensor(),                             transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])

plot_curve函数是对后面的loss和acc进行简单的可视化处理

# 这行代码是对数据进行加强transforms.RandomHorizontalFlip()transforms.RandomGrayscale()

3.定义网络结构

class Lenet5(nn.Module):    def __init__(self):        super(Lenet5, self).__init__()        self.conv_unit = nn.Sequential(            nn.Conv2d(3, 16, kernel_size=5, stride=1, padding=0),            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),            nn.Conv2d(16, 32, kernel_size=5, stride=1, padding=0),            nn.MaxPool2d(kernel_size=2, stride=2, padding=0),        )        self.fc_unit = nn.Sequential(            nn.Linear(32*5*5, 32),            nn.ReLU(),            nn.Linear(32, 10)        )        tmp = torch.randn(2, 3, 32, 32)        out = self.conv_unit(tmp)        print('conv out:', out.shape)    def forward(self, x):        batchsz = x.size(0)        x = self.conv_unit(x)        x = x.view(batchsz, 32*5*5)        logits = self.fc_unit(x)        return logits

1. strong>定义 Loss 函数和优化器

Loss使用CrossEntropyLoss （交叉熵损失函数）

loss = nn.CrossEntropyLoss()#optimizer = optim.SGD(self.parameters(),lr=0.01)optimizer = optim.Adam(self.parameters(), lr=0.0001)
1. strong>训练
for epoch in range(100):    for i, (x, label) in enumerate(train_data_load):        x, label = x.to(device), label.to(device)        logits = net(x)        loss = name(logits, label)        optimizer.zero_grad()        loss.backward()        optimizer.step()        trans_loss.append(loss.item())    net.eval()    with torch.no_grad():        # test        total_correct = 0        total_num = 0        for x, label in test_data_load:            # [b, 3, 32, 32]            # [b]            x, label = x.to(device), label.to(device)            # [b, 10]            logits = net(x)            # [b]            pred = logits.argmax(dim=1)            # [b] vs [b] => scalar tensor            correct = torch.eq(pred, label).float().sum().item()            total_correct += correct            total_num += x.size(0)            # print(correct)        acc = total_correct / total_num        test_acc.append(acc)    print(epoch+1,'loss:',loss.item(),'test acc:',acc)plot_curve(trans_loss)plot_curve(test_acc)

1. strong>调整方案一

class CNN(nn.Module):    def __init__(self):        super(CNN,self).__init__()        self.conv1=nn.Sequential(            nn.Conv2d(3,16,kernel_size=3,stride=1,padding=1),            nn.ReLU(True),        )        self.conv2=nn.Sequential(            nn.Conv2d(16,32,kernel_size=5,stride=1,padding=2),            nn.ReLU(True),        )        self.conv3=nn.Sequential(            nn.Conv2d(32,64,kernel_size=5,stride=1,padding=2),            nn.ReLU(True),            nn.MaxPool2d(kernel_size=2,stride=2),            nn.BatchNorm2d(64)        )        self.function=nn.Linear(15*15*64,10)    def forward(self, x):        out = self.conv1(x)        out = self.conv2(out)        out = self.conv3(out)        out = out.view(out.size(0), -1)        out = self.function(out)        return out

1. strong>方案二

class CNN(nn.Module):    def __init__(self):        super(CNN,self).__init__()        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)        self.pool1 = nn.MaxPool2d(2, 2)        self.bn1 = nn.BatchNorm2d(64)        self.relu1 = nn.ReLU()        self.conv3 = nn.Conv2d(64, 128, 3, padding=1)        self.conv4 = nn.Conv2d(128, 128, 3, padding=1)        self.pool2 = nn.MaxPool2d(2, 2, padding=1)        self.bn2 = nn.BatchNorm2d(128)        self.relu2 = nn.ReLU()        self.conv5 = nn.Conv2d(128, 128, 3, padding=1)        self.conv6 = nn.Conv2d(128, 128, 3, padding=1)        self.conv7 = nn.Conv2d(128, 128, 1, padding=1)        self.pool3 = nn.MaxPool2d(2, 2, padding=1)        self.bn3 = nn.BatchNorm2d(128)        self.relu3 = nn.ReLU()        self.conv8 = nn.Conv2d(128, 256, 3, padding=1)        self.conv9 = nn.Conv2d(256, 256, 3, padding=1)        self.conv10 = nn.Conv2d(256, 256, 1, padding=1)        self.pool4 = nn.MaxPool2d(2, 2, padding=1)        self.bn4 = nn.BatchNorm2d(256)        self.relu4 = nn.ReLU()        self.conv11 = nn.Conv2d(256, 512, 3, padding=1)        self.conv12 = nn.Conv2d(512, 512, 3, padding=1)        self.conv13 = nn.Conv2d(512, 512, 1, padding=1)        self.pool5 = nn.MaxPool2d(2, 2, padding=1)        self.bn5 = nn.BatchNorm2d(512)        self.relu5 = nn.ReLU()        self.fc14 = nn.Linear(512 * 4 * 4, 1024)        self.drop1 = nn.Dropout2d()        self.fc15 = nn.Linear(1024, 1024)        self.drop2 = nn.Dropout2d()        self.fc16 = nn.Linear(1024, 10)    def forward(self, x):        x = self.conv1(x)        x = self.conv2(x)        x = self.pool1(x)        x = self.bn1(x)        x = self.relu1(x)        x = self.conv3(x)        x = self.conv4(x)        x = self.pool2(x)        x = self.bn2(x)        x = self.relu2(x)        x = self.conv5(x)        x = self.conv6(x)        x = self.conv7(x)        x = self.pool3(x)        x = self.bn3(x)        x = self.relu3(x)        x = self.conv8(x)        x = self.conv9(x)        x = self.conv10(x)        x = self.pool4(x)        x = self.bn4(x)        x = self.relu4(x)        x = self.conv11(x)        x = self.conv12(x)        x = self.conv13(x)        x = self.pool5(x)        x = self.bn5(x)        x = self.relu5(x)        x = x.view(-1, 512 * 4 * 4)        x = F.relu(self.fc14(x))        x = self.drop1(x)        x = F.relu(self.fc15(x))        x = self.drop2(x)        x = self.fc16(x)        return x
1. strong>方案二的训练结果

1. strong>最终代码如下
import torchfrom torch import nnimport torch.optim as optimimport torch.nn.functional as Ffrom torch.autograd import Variableimport matplotlib.pyplot as pltfrom torchvision import transforms,datasetsfrom torch.utils.data import DataLoaderdef plot_curve(data):    fig = plt.figure()    plt.plot(range(len(data)), data, color='blue')    plt.legend(['value'], loc='upper right')    plt.xlabel('step')    plt.ylabel('value')    plt.show()transTrain=transforms.Compose([transforms.RandomHorizontalFlip(),                              transforms.RandomGrayscale(),                              transforms.ToTensor(),                              transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])transTest=transforms.Compose([transforms.ToTensor(),                              transforms.Normalize((0.5,0.5,0.5),(0.5,0.5,0.5))])# download datatrain_data=datasets.CIFAR10(root='./CIFAR',train=True,transform=transTrain,download=True)test_data=datasets.CIFAR10(root='./CIFAR',train=False,transform=transTest,download=True)train_data_load=DataLoader(train_data,batch_size=100,shuffle=True,num_workers=2)test_data_load=DataLoader(test_data,batch_size=100,shuffle=False,num_workers=2)# definde CNNclass CNN(nn.Module):    def __init__(self):        super(CNN,self).__init__()        self.conv1 = nn.Conv2d(3, 64, 3, padding=1)        self.conv2 = nn.Conv2d(64, 64, 3, padding=1)        self.pool1 = nn.MaxPool2d(2, 2)        self.bn1 = nn.BatchNorm2d(64)        self.relu1 = nn.ReLU()        self.conv3 = nn.Conv2d(64, 128, 3, padding=1)        self.conv4 = nn.Conv2d(128, 128, 3, padding=1)        self.pool2 = nn.MaxPool2d(2, 2, padding=1)        self.bn2 = nn.BatchNorm2d(128)        self.relu2 = nn.ReLU()        self.conv5 = nn.Conv2d(128, 128, 3, padding=1)        self.conv6 = nn.Conv2d(128, 128, 3, padding=1)        self.conv7 = nn.Conv2d(128, 128, 1, padding=1)        self.pool3 = nn.MaxPool2d(2, 2, padding=1)        self.bn3 = nn.BatchNorm2d(128)        self.relu3 = nn.ReLU()        self.conv8 = nn.Conv2d(128, 256, 3, padding=1)        self.conv9 = nn.Conv2d(256, 256, 3, padding=1)        self.conv10 = nn.Conv2d(256, 256, 1, padding=1)        self.pool4 = nn.MaxPool2d(2, 2, padding=1)        self.bn4 = nn.BatchNorm2d(256)        self.relu4 = nn.ReLU()        self.conv11 = nn.Conv2d(256, 512, 3, padding=1)        self.conv12 = nn.Conv2d(512, 512, 3, padding=1)        self.conv13 = nn.Conv2d(512, 512, 1, padding=1)        self.pool5 = nn.MaxPool2d(2, 2, padding=1)        self.bn5 = nn.BatchNorm2d(512)        self.relu5 = nn.ReLU()        self.fc14 = nn.Linear(512 * 4 * 4, 1024)        self.drop1 = nn.Dropout2d()        self.fc15 = nn.Linear(1024, 1024)        self.drop2 = nn.Dropout2d()        self.fc16 = nn.Linear(1024, 10)    def forward(self, x):        x = self.conv1(x)        x = self.conv2(x)        x = self.pool1(x)        x = self.bn1(x)        x = self.relu1(x)        x = self.conv3(x)        x = self.conv4(x)        x = self.pool2(x)        x = self.bn2(x)        x = self.relu2(x)        x = self.conv5(x)        x = self.conv6(x)        x = self.conv7(x)        x = self.pool3(x)        x = self.bn3(x)        x = self.relu3(x)        x = self.conv8(x)        x = self.conv9(x)        x = self.conv10(x)        x = self.pool4(x)        x = self.bn4(x)        x = self.relu4(x)        x = self.conv11(x)        x = self.conv12(x)        x = self.conv13(x)        x = self.pool5(x)        x = self.bn5(x)        x = self.relu5(x)        x = x.view(-1, 512 * 4 * 4)        x = F.relu(self.fc14(x))        x = self.drop1(x)        x = F.relu(self.fc15(x))        x = self.drop2(x)        x = self.fc16(x)        return xdevice = torch.device('cuda')net=CNN().to(device)name = nn.CrossEntropyLoss().to(device)optimizer = optim.Adam(net.parameters(), lr=0.001)loss_num=0.0trans_loss=[]test_acc=[]for epoch in range(25):    for i, (x, label) in enumerate(train_data_load):        x, label = x.to(device), label.to(device)        logits = net(x)        loss = name(logits, label)        optimizer.zero_grad()        loss.backward()        optimizer.step()        trans_loss.append(loss.item())    net.eval()    with torch.no_grad():        total_correct = 0        total_num = 0        for x, label in test_data_load:            x, label = x.to(device), label.to(device)            logits = net(x)            pred = logits.argmax(dim=1)            # [b] vs [b] => scalar tensor            correct = torch.eq(pred, label).float().sum().item()            total_correct += correct            total_num += x.size(0)        acc = total_correct / total_num        test_acc.append(acc)    print(epoch+1,'loss:',loss.item(),'test acc:',acc)plot_curve(trans_loss)plot_curve(test_acc)
1. strong>总结