改版公司网站时,百度提交需要注意哪些细节?
摘要:网站改版百度提交,做公司网站注意事项,国外设计师个人网站,山海关区建设局网站PyTorch 模型进阶训练技巧 自定义损失函数动态调整学习率 典型案例:loss上下震荡 [外链图片转存失败,源站可能有防盗链机制,
网站改版百度提交,做公司网站注意事项,国外设计师个人网站,山海关区建设局网站PyTorch 模型进阶训练技巧
自定义损失函数动态调整学习率
典型案例#xff1a;loss上下震荡 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BndMyRX0-1692613806232)(attachment:image-2.png)]
1、自定义损失函数
1、PyTorch已经提供了很多常用…PyTorch 模型进阶训练技巧
自定义损失函数动态调整学习率
典型案例loss上下震荡 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-BndMyRX0-1692613806232)(attachment:image-2.png)]
1、自定义损失函数
1、PyTorch已经提供了很多常用的损失函数但是有些非通用的损失函数并未提供比如DiceLoss、HuberLoss…等2、模型如果出现loss震荡在经过调整数据集或超参后现象依然存在非通用损失函数或自定义损失函数针对特定模型会有更好的效果
比如DiceLoss是医学影像分割常用的损失函数定义如下 [外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-Fsl0SyZ6-1692613806233)(attachment:image-2.png)]
Dice系数, 是一种集合相似度度量函数通常用于计算两个样本的相似度(值范围为 [0, 1])∣X∩Y∣表示X和Y之间的交集∣ X ∣ 和∣ Y ∣ 分别表示X和Y的元素个数其中分子中的系数 2是因为分母存在重复计算 X 和 Y 之间的共同元素的原因.
import torch
import torch.nn.functional as F
import torch.nn as nn
from torch.optim.lr_scheduler import LambdaLR
from torch.optim.lr_scheduler import StepLR
import torchvision
from torch.utils.data import Dataset, DataLoader
from torchvision.transforms import transforms
import matplotlib.pyplot as plt
from torch.utils.tensorboard import SummaryWriter
import time
import numpy as np#DiceLoss 实现 Vnet 医学影像分割模型的损失函数
class DiceLoss(nn.Module):def __init__(self, weightNone, size_averageTrue):super(DiceLoss, self).__init__()def forward(self, inputs, targets, smooth1):inputs F.sigmoid(inputs) inputs inputs.view(-1)targets targets.view(-1)intersection (inputs * targets).sum() dice_loss 1 - (2.*intersection smooth)/(inputs.sum() targets.sum() smooth)return dice_loss#自定义实现多分类损失函数 处理多分类
# cross_entropy L2正则化
class MyLoss(torch.nn.Module):def __init__(self, weight_decay0.01):super(MyLoss, self).__init__()self.weight_decay weight_decaydef forward(self, inputs, targets):ce_loss F.cross_entropy(inputs, targets)l2_loss torch.tensor(0., requires_gradTrue).to(inputs.device)for name, param in self.named_parameters():if weight in name:l2_loss torch.norm(param)loss ce_loss self.weight_decay * l2_lossreturn loss
注
在自定义损失函数时涉及到数学运算时我们最好全程
