简体   繁体   English

PyTorch:在训练中添加验证错误

[英]PyTorch: Add validation error in training

I am using PyTorch to train a cnn model. 我正在使用PyTorch来训练一个cnn模型。 Here is my Network architecture: 这是我的网络架构:

import torch
from torch.autograd import Variable
import torch.nn as nn
import torch.nn.functional as F
import torch.nn.init as I


    class Net(nn.Module):

        def __init__(self):
            super(Net, self).__init__()

            self.conv1 = nn.Conv2d(1, 32, 5)
            self.pool = nn.MaxPool2d(2,2)
            self.conv1_bn = nn.BatchNorm2d(32)
            self.conv2 = nn.Conv2d(32, 64, 5)
            self.conv2_drop = nn.Dropout2d()
            self.conv2_bn = nn.BatchNorm2d(64)
            self.fc1 = torch.nn.Linear(53*53*64, 256)
            self.fc2 = nn.Linear(256, 136)


        def forward(self, x):

            x = F.relu(self.conv1_bn(self.pool(self.conv1(x))))
            x = F.relu(self.conv2_bn(self.pool(self.conv2_drop(self.conv2(x)))))
            x = x.view(-1, 53*53*64)
            x = F.relu(self.fc1(x))
            x = F.dropout(x, training=self.training)
            x = self.fc2(x)

            return x

Then I train the model like below: 然后我训练模型如下:

# prepare the net for training
    net.train()

    for epoch in range(n_epochs):  # loop over the dataset multiple times

        running_loss = 0.0

        # train on batches of data, assumes you already have train_loader
        for batch_i, data in enumerate(train_loader):
            # get the input images and their corresponding labels
            images = data['image']
            key_pts = data['keypoints']

            # flatten pts
            key_pts = key_pts.view(key_pts.size(0), -1)

            # wrap them in a torch Variable
            images, key_pts = Variable(images), Variable(key_pts)

            # convert variables to floats for regression loss
            key_pts = key_pts.type(torch.FloatTensor)
            images = images.type(torch.FloatTensor)

            # forward pass to get outputs
            output_pts = net(images)

            # calculate the loss between predicted and target keypoints
            loss = criterion(output_pts, key_pts)

            # zero the parameter (weight) gradients
            optimizer.zero_grad()

            # backward pass to calculate the weight gradients
            loss.backward()

            # update the weights
            optimizer.step()

            # print loss statistics
            running_loss += loss.data[0]

I am wondering if it is possible to add the validation error in the training? 我想知道是否有可能在培训中添加验证错误? I mean something like this (validation split) in Keras : 我的意思是在Keras这样的(验证分裂):

myModel.fit(trainX, trainY, epochs=50, batch_size=1, verbose=2, validation_split = 0.1)

Here is an example how to split your dataset for training and validation, then switch between the two phases every epoch: 下面是一个如何拆分数据集进行培训和验证,然后在每个时期切换两个阶段的示例:

import numpy as np
import torch
from torchvision import datasets
from torch.autograd import Variable
from torch.utils.data.sampler import SubsetRandomSampler

# Examples:
my_dataset = datasets.MNIST(root="/home/benjamin/datasets/mnist", train=True, download=True)
validation_split = 0.1

dataset_len = len(my_dataset)
indices = list(range(dataset_len))

# Randomly splitting indices:
val_len = int(np.floor(validation_split * dataset_len))
validation_idx = np.random.choice(indices, size=val_len, replace=False)
train_idx = list(set(indices) - set(validation_idx))

# Contiguous split
# train_idx, validation_idx = indices[split:], indices[:split]

## Defining the samplers for each phase based on the random indices:
train_sampler = SubsetRandomSampler(train_idx)
validation_sampler = SubsetRandomSampler(validation_idx)

train_loader = torch.utils.data.DataLoader(my_dataset, sampler=train_sampler)
validation_loader = torch.utils.data.DataLoader(my_dataset, sampler=validation_sampler)
data_loaders = {"train": train_loader, "val": validation_loader}
data_lengths = {"train": len(train_idx), "val": val_len}

# Training with Validation (your code + code from Pytorch tutorial: https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html)
n_epochs = 40
net = ...

for epoch in range(n_epochs):
    print('Epoch {}/{}'.format(epoch, n_epochs - 1))
    print('-' * 10)

    # Each epoch has a training and validation phase
    for phase in ['train', 'val']:
        if phase == 'train':
            optimizer = scheduler(optimizer, epoch)
            net.train(True)  # Set model to training mode
        else:
            net.train(False)  # Set model to evaluate mode

        running_loss = 0.0

        # Iterate over data.
        for data in data_loaders[phase]:

            # get the input images and their corresponding labels
            images = data['image']
            key_pts = data['keypoints']

            # flatten pts
            key_pts = key_pts.view(key_pts.size(0), -1)

            # wrap them in a torch Variable
            images, key_pts = Variable(images), Variable(key_pts)

            # convert variables to floats for regression loss
            key_pts = key_pts.type(torch.FloatTensor)
            images = images.type(torch.FloatTensor)

            # forward pass to get outputs
            output_pts = net(images)

            # calculate the loss between predicted and target keypoints
            loss = criterion(output_pts, key_pts)

            # zero the parameter (weight) gradients
            optimizer.zero_grad()

            # backward + optimize only if in training phase
            if phase == 'train':
                loss.backward()
                # update the weights
                optimizer.step()

            # print loss statistics
            running_loss += loss.data[0]

        epoch_loss = running_loss / data_lengths[phase]
        print('{} Loss: {:.4f}'.format(phase, epoch_loss))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 Pytorch 模型中添加验证和测试集? - How to add a Validation and Test Set in Pytorch Model? 在 PyTorch 中为简单神经网络添加训练和测试精度 - Add Training and Testing Accuracy to a Simple Neural Network in PyTorch Pytorch - 在 GPU 上训练时,在设备 1 上的副本 1 中捕获 StopIteration 错误 - Pytorch - Caught StopIteration in replica 1 on device 1 error while Training on GPU 师资力量训练PyTorch - Teacher force training PyTorch Pytorch:在训练时可视化模型 - Pytorch: Visualize model while training 在 pytorch 中训练用于回归的 cnn 时出现太多值无法解包(预期为 2)错误 - Too many values to unpack (expected 2) error occurs when training a cnn for regression in pytorch pytorch_lightning.utilities.exceptions.MisconfigurationException 在 pytorch 闪电训练时 - pytorch_lightning.utilities.exceptions.MisconfigurationException when training in pytorch lightning 为 pytorch 中的每次运行修复训练和测试数据集 - Fix training and testing dataset for every run in pytorch fastai - plot 验证和训练准确度 - fastai - plot validation and training accuracy KeyError:使用 pytorch 训练拥抱脸 model 时出现 337 - KeyError: 337 when training a hugging face model using pytorch
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM