目录

5、导入数据

6、定义卷积神经网络

7、定义优化器和损失函数

8、训练过程

9、测试过程

三、训练结果


码字不易,如果各位看官感觉该文章对你有所帮助,麻烦点个关注,如果有任何问题,请留言交流。如需转载,请注明出处,谢谢。

前情提要:

【深度学习】纯干货之如何使用pytorch训练自己的数据(一)

【深度学习】纯干货之如何使用pytorch训练自己的数据(一)_莫克_Cheney的博客-CSDN博客_mvcnn 怎么用pytorch跑数据

书接上回:

5、导入数据

我们将读取好的数据,dataset_train与dataset_test分别导入,其中将之前定义的超参数BATCH_SIZE传入,另外shuffle在训练的时候置为真,即打乱顺序,而测试的时候并不需要打乱。

train_loader = torch.utils.data.DataLoader(dataset_train, batch_size=BATCH_SIZE, shuffle=True)
test_loader = torch.utils.data.DataLoader(dataset_test, batch_size=BATCH_SIZE, shuffle=False)

6、定义卷积神经网络

下面的代码就是我们定义的卷积神经网络,可以看到该网络比较简单,当然我们可以使用现成的成熟的网络进行测试。pytorch只需要定义前向的过程即可,例如:forward函数,而后向传播的事情我们并不需要管他,完全傻瓜式操作,一切尽在不言中。

class Net(torch.nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = torch.nn.Sequential(
            torch.nn.Conv2d(3, 32, 3, 1, 1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2))
        self.conv2 = torch.nn.Sequential(
            torch.nn.Conv2d(32, 64, 3, 1, 1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2)
        )
        self.conv3 = torch.nn.Sequential(
            torch.nn.Conv2d(64, 64, 3, 1, 1),
            torch.nn.ReLU(),
            torch.nn.MaxPool2d(2)
        )
        self.dense = torch.nn.Sequential(
            torch.nn.Linear(64 * 3 * 3, 128),
            torch.nn.ReLU(),
            torch.nn.Linear(128, 10)
        )

    def forward(self, x):
        conv1_out = self.conv1(x)
        conv2_out = self.conv2(conv1_out)
        conv3_out = self.conv3(conv2_out)
        res = conv3_out.view(conv3_out.size(0), -1)
        out = self.dense(res)
        return out


model = Net().to(DEVICE)
print(model)

7、定义优化器和损失函数

这里我们采用Adam方式进行优化,使用交叉熵损失作为损失函数,当然根据我们的任务不同,大家可以选择适合自己的函数进行测试。

optimizer = torch.optim.Adam(model.parameters())
loss_func = torch.nn.CrossEntropyLoss()

8、训练过程

将batch_x和batch_y分批传入进行训练,最后通过前向传播与后向传播,得到损失,最后统计训练的精度和误差。

这里可以看到,使用to(DEVICE)将变量或者网络传入CPU/GPU。

for epoch in range(10):
    print('epoch {}'.format(epoch + 1))
    # training-----------------------------
    train_loss = 0.
    train_acc = 0.
    for batch_x, batch_y in train_loader:
        batch_x, batch_y = Variable(batch_x).to(DEVICE), Variable(batch_y).to(DEVICE)
        out = model(batch_x)
        loss = loss_func(out, batch_y)        
        train_loss += loss.item()        
        pred = torch.max(out, 1)[1]
        train_correct = (pred == batch_y).sum()
        train_acc += train_correct.item()
        optimizer.zero_grad()
        loss.backward()
        optimizer.step()
    print(len(dataset_train))
    print('Train Loss: {:.6f}, Acc: {:.6f}'.format(train_loss / (len(
        dataset_train)), train_acc / (len(dataset_train))))
    # evaluation--------------------------------
    model.eval()
    eval_loss = 0.
    eval_acc = 0.

9、测试过程

测试的过程基本上也是一样的,只不过是对加载的验证集的数据进行循环,最终统计精度信息。

    for batch_x, batch_y in test_loader:
        batch_x, batch_y = Variable(batch_x, volatile=True).to(DEVICE), Variable(batch_y, volatile=True).to(DEVICE)
        out = model(batch_x)
        loss = loss_func(out, batch_y)
        eval_loss += loss.item()
        pred = torch.max(out, 1)[1]
        num_correct = (pred == batch_y).sum()
        eval_acc += num_correct.item()
    print('Test Loss: {:.6f}, Acc: {:.6f}'.format(eval_loss / (len(
        dataset_test)), eval_acc / (len(dataset_test))))

三、训练结果

下面的代码是我的运行结果,可以看到,10个训练周期后,训练集与测试集的损失与精度的情况。

epoch 1
8599
Train Loss: 0.004398, Acc: 0.894174
d:\PROJECTS\pythonProject\TorchFrame.py:99: UserWarning: volatile was removed and now has no effect. Use `with torch.no_grad():` instead.
  batch_x, batch_y = Variable(batch_x, volatile=True).to(DEVICE), Variable(batch_y, volatile=True).to(DEVICE)
Test Loss: 0.000387, Acc: 0.992500
epoch 2
8599
Train Loss: 0.000095, Acc: 0.998372
Test Loss: 0.000015, Acc: 1.000000
epoch 3
8599
Train Loss: 0.000047, Acc: 0.999302
Test Loss: 0.000005, Acc: 1.000000
epoch 4
8599
Train Loss: 0.000041, Acc: 0.999535
Test Loss: 0.000017, Acc: 1.000000
epoch 5
8599
Train Loss: 0.000039, Acc: 0.999535
Test Loss: 0.000007, Acc: 1.000000
epoch 6
8599
Train Loss: 0.000028, Acc: 0.999651
Test Loss: 0.000003, Acc: 1.000000
epoch 7
8599
Train Loss: 0.000300, Acc: 0.993720
Test Loss: 0.000138, Acc: 0.995000
epoch 8
8599
Train Loss: 0.000096, Acc: 0.997907
Test Loss: 0.000005, Acc: 1.000000
epoch 9
8599
Train Loss: 0.000024, Acc: 0.999767
Test Loss: 0.000002, Acc: 1.000000
epoch 10
8599
Train Loss: 0.000015, Acc: 0.999767
Test Loss: 0.000003, Acc: 1.000000

版权声明:本文为herocheney原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
原文链接:https://blog.csdn.net/herocheney/article/details/118698483