Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
第一个时代的错误极高(Pytorch-图像分割)_Pytorch_Loss Function - Fatal编程技术网

第一个时代的错误极高(Pytorch-图像分割)

第一个时代的错误极高(Pytorch-图像分割),pytorch,loss-function,Pytorch,Loss Function,为什么在第一个时期,当我将网络设置为eval时,我的模型对验证数据的误差函数非常高 如果我使用model.eval(),在最初的4-5个时期内,误差大于40-50k,然后迅速下降到3-4,但如果我将网络留在model.train()上,误差仅为5-6 def eval_model(DataLoader, model, criterion, device, withStat, withImage): model.eval() eval_epochen_loss = 0 im

为什么在第一个时期,当我将网络设置为eval时,我的模型对验证数据的误差函数非常高

如果我使用model.eval(),在最初的4-5个时期内,误差大于40-50k,然后迅速下降到3-4,但如果我将网络留在model.train()上,误差仅为5-6

def eval_model(DataLoader, model, criterion, device, withStat, withImage):
    model.eval()
    eval_epochen_loss = 0
    img = None
    n_eval = 0
    TP, TN, FP, FN = 0, 0, 0, 0
    stat = None

    for i, data in enumerate(DataLoader):
        dicoms, targets = data
        dicoms, targets = Variable(dicoms.to(device)), Variable(targets.to(device))

        assert targets.shape[1] - 1 == model.n_classes, \
            f'Network has been defined with {model.n_classes} output classes, ' \
            f'but loaded target have {targets.shape[1] - 1} channels. Please check the labeled data or adjust ' \
            f'the network classes. '

        preds = model(dicoms)
        loss = criterion(preds, targets[:, -1, :, :].long())

        eval_epochen_loss += loss.item()
        n_eval += dicoms.shape[0]

        if withStat:
            res = TP_TN_FP_FN_in_batch(targets[:, -1, :, :].cpu().detach().long(), preds.detach().cpu())
            TP += res[0]
            TN += res[1]
            FP += res[2]
            FN += res[3]

    if withStat:
        stat = Statistic(TP, TN, FP, FN)

    if withImage:
        img_np = np.array(draw_images((dicoms, targets[:, -1], preds), outline_bool=True)).transpose(0, 3, 1, 2)
        img = torch.from_numpy(img_np)

    return eval_epochen_loss / n_eval, stat, img

在不查看模型的情况下很难诊断问题,但批处理规范在评估模式和训练模式下的行为不同。这可能就是您出现此问题的原因。Batchnorm在培训期间使用整个培训批次获取其参数,但在验证/测试期间使用存储的运行平均值。这个运行的平均值似乎需要几个时间才能很好地收敛到您的验证集。你的数据正常化了吗