Advertisement

导入PyTorch库并定义数据加载器、模型、损失函数和优化器;然后进行训练循环并应用label smoothing技术;最后返回训练好的模型参数。(PyTorch官方文档)

阅读量:

pytorch标签平滑实现代码

复制代码
    import torch
    import torch.nn as nn
    from torch.autograd import Variable
    import matplotlib.pyplot as plt
    import numpy as np
     
     
    class LabelSmoothing(nn.Module):
    # "Implement label smoothing."
     
    def __init__(self, size, smoothing=0.0):
     
        super(LabelSmoothing, self).__init__()
     
        self.criterion = nn.KLDivLoss(size_average=False)
     
        #self.padding_idx = padding_idx
     
        self.confidence = 1.0 - smoothing
     
        self.smoothing = smoothing
     
        self.size = size
     
        self.

全部评论 (0)

还没有任何评论哟~