网站常用代码,网站主题咋做,博客系统 wordpress,济南网站制作多少钱一个文章目录 二分类交叉熵损失函数交叉熵损失函数L1损失函数MSE损失函数平滑L1(Smooth L1)损失函数目标泊松分布的负对数似然损失KL散度MarginRankingLoss多标签边界损失函数二分类损失函数多分类的折页损失三元组损失HingEmbeddingLoss余弦相似度CTC损失函数参考资料 学习目标 二分类交叉熵损失函数
函数 torch.nn.BCELoss(weightNone, size_averageNone, reduceNone, reductionmean) input为sigmoid函数的输出或softmax函数的输出 label为{0, 1} 功能计算二分类任务时的交叉熵Cross Entropy 参数说明 weight给每个类别的loss设置权值 size_averagebool类型值为True时返回各样本loss平均值值为False时返回各样本loss之和 reducebool类型值为True时返回loss为标量 代码说明
m nn.Sigmoid()
loss nn.BCELoss()
input torch.randn(3, requires_gradTrue)
target torch.empty(3).random_(2)
output loss(m(input), target)
output.backward()
print(BCELoss损失函数的计算结果为,output)交叉熵损失函数
函数 torch.nn.CrossEntropyLoss(weightNone, size_averageNone, ignore_index-100, reduceNone, reductionmean) 功能计算交叉熵 计算公式 l o s s ( x , c l a s s ) − l o g ( e x p ( x [ c l a s s ] ) ∑ j e x p ( x [ j ] ) ) − x [ c l a s s ] l o g ( ∑ j e x p ( x [ j ] ) ) loss(x, class) -log(\frac{exp(x[class])}{\sum_jexp(x[j])}) -x[class] log(\sum_jexp(x[j])) loss(x,class)−log(∑jexp(x[j])exp(x[class]))−x[class]log(j∑exp(x[j]))
loss nn.CrossEntropyLoss()
input torch.randn(3, 5, requires_gradTrue)
target torch.empty(3, dtypetorch.long).random_(5)
output loss(input, target)
output.backward()L1损失函数
函数 torch.nn.L1Loss(size_aveageNone, reduceNone, reductionmean) 功能output和真实标签之间差值的绝对值 参数说明 reduction参数——决定计算模式 none——逐个元素计算output和输入元素同尺寸 sum——所有元素求和返回标量 mean——加权平均返回标量 计算公式 L n ∣ x n − y n ∣ L_n |x_n-y_n| Ln∣xn−yn∣ 代码示例
loss nn.L1Loss()
input torch.randn(3, 5, requires_gradTrue)
target torch.randn(3, 5)
output loss(input, target)
output.backward()MSE损失函数
函数 torch.nn.MSELoss(size_averageNone, reduceNone, reductionmean) 功能计算output和真实标签target之差的平方 代码示例
loss nn.MSELoss()
input torch.randn(3, 5, requires_gradTrue)
target torch.randn(3, 5)
output loss(input, target)
output.backward()平滑L1(Smooth L1)损失函数
torch.nn.SmoothLoss(size_averageNone, reduceNone, reductionmean, beta1.0) 功能L1损失函数的平滑输出能够减轻离群点带来的影响 计算公式 l o s s ( x , y ) 1 n ∑ i 1 n z i loss(x,y)\frac{1}{n}\sum_{i1}^nz_i loss(x,y)n1i1∑nzi 其中 z i z_i zi的计算公式如下 代码示例
loss nn.SmoothL1Loss()
input torch.randn(3, 5, requires_gradTrue)
target torch.randn(3, 5)
output loss(input, target)
output.backward()对比平滑L1和L1 可视化两种损失函数曲线
inputs torch.linspace(-10, 10, steps5000)
target torch.zeros_like(inputs)loss_f_smooth nn.SmoothL1Loss(reductionnone)
loss_smooth loss_f_smooth(inputs, target)
loss_f_l1 nn.L1Loss(reductionnone)
loss_l1 loss_f_l1(inputs,target)plt.plot(inputs.numpy(), loss_smooth.numpy(), labelSmooth L1 Loss)
plt.plot(inputs.numpy(), loss_l1, labelL1 loss)
plt.xlabel(x_i - y_i)
plt.ylabel(loss value)
plt.legend()
plt.grid()
plt.show()目标泊松分布的负对数似然损失 适用于回归任务目标值ground truth服从泊松分布的回归问题典型场景包括计数数据、连续型回归问题如一天内发生的事件次数、网页访问次数 泊松分布 P ( Y k ) λ k k ! e − λ P(Yk) \frac{\lambda^k}{k!}e^{-\lambda} P(Yk)k!λke−λ torch.nn.PoissonNLLLoss(log_inputTrue, fullfalse, size_averageNone, eps1e-08, reduceNone, reductionmean) 参数解释 log_inputbool类型参数用于说明输入是否为对数形式 fullbool类型是否计算所有loss eps修正项用于避免input为0时log(input)为Nan的情况 计算公式 当参数log_inputTrue l o s s ( x n , y n ) e x n − x n ⋅ y n loss(x_n, y_n)e^{x_n}-x_n\cdot y_n loss(xn,yn)exn−xn⋅yn 当参数log_inputFalse l o s s ( x n , y n ) x n − y n ⋅ l o g ( x n e p s ) loss(x_n, y_n) x_n-y_n\cdot log(x_neps) loss(xn,yn)xn−yn⋅log(xneps) 代码示例
loss nn.PoissonNLLLoss()
log_input torch.randn(5, 2, requires_gradTrue)
target torch.randn(5, 2)
output loss(log_input, target)
output.backward()KL散度
torch.nn.KLDivLoss(size_averageNone, reduceNone, reductionmean, log_targetFalse) 功能计算相对熵度量连续分布的距离对离散采用的连续输出空间分布进行回归 参数解释 reduction 取值有none/sum/mean/batchmean none逐个元素计算 sum求和 mean加权求和 batchmeanbatchsize维度求平均值 计算公式 代码示例
inputs torch.tensor([[0.5, 0.3, 0.2], [0.2, 0.3, 0.5]])
target torch.tensor([[0.9, 0.05, 0.05], [0.1, 0.7, 0.2]], dtypetorch.float)
loss nn.KLDivLoss()
output loss(inputs,target)print(KLDivLoss损失函数的计算结果为,output)MarginRankingLoss
torch.nn.MarginRankingLoss(margin0.0, size_averageNone, reduceNone, reductionmean) 功能计算两个向量之间的相似度用于排序任务又称为排序损失函数 参数解释 margin边界值两个向量之间的差异值 计算公式 l o s s ( x 1 , x 2 , y ) m a x ( 0 , − y ∗ ( x 1 − x 2 ) m a r g i n ) loss(x_1, x_2, y) max(0, -y*(x_1-x_2)margin) loss(x1,x2,y)max(0,−y∗(x1−x2)margin) 代码示例
loss nn.MarginRankingLoss()
input1 torch.randn(3, requires_gradTrue)
input2 torch.randn(3, requires_gradTrue)
target torch.randn(3).sign()
output loss(input1, input2, target)
output.backward()print(MarginRankingLoss损失函数的计算结果为,output)多标签边界损失函数
torch.nn.MultiLabelMarginLoss(size_averageNone, reduceNone, reductionmean) 关注的是样本和决策边界之间的间隔使每个标签的得分都高于一个给定的阈值并最小化样本与这些决策边界之间的间隔 计算公式 L ( y , f ( x ) ) 1 N ∑ i 1 N ∑ j 1 C L i j L(y, f(x)) \frac{1}{N}\sum^N_{i1}\sum^C_{j1}L_{ij} L(y,f(x))N1∑i1N∑j1CLij 其中N为样本数量C为标签数量 L i j L_{ij} Lij为样本i对标签j的边界损失 L i j m a x ( 0 , 1 − y i j ⋅ f ( x ) i j ) L_{ij}max(0, 1-y_{ij}\cdot f(x)_{ij}) Lijmax(0,1−yij⋅f(x)ij) y i j y_{ij} yij取值为 -1 或 1表示标签 j 是否属于样本 i 的真实标签 若 y i j 1 y_{ij}1 yij1则要求 f ( x ) i j ≥ m a r g i n f(x)_{ij} \geq margin f(x)ij≥margin 若 y i j − 1 y_{ij}-1 yij−1则要求 f ( x ) i j ≤ m a r g i n f(x)_{ij} \leq margin f(x)ij≤margin
代码示例
loss nn.MultiLabelMarginLoss()
x torch.FloatTensor([[0.9, 0.2, 0.4, 0.8]])
# for target y, only consider labels 3 and 0, not after label -1
y torch.LongTensor([[3, 0, -1, -1]])# 真实的分类是第3类和第0类标签中的-1表示该标签不适用于该样本
output loss(x, y)print(MultiLabelMarginLoss损失函数的计算结果为,output)二分类损失函数
torch.nn.SoftMarginLoss(size_averageNone, reduceNone, reductionmean) 功能计算二分类的logistic损失在训练SVM被经常使用使得正确分类的样本得分与正确类别的边界之差大于一个预定义的边界值 计算公式 L ( y , f ( x ) ) 1 N ∑ i 1 N m a x ( 0 , 1 − y i ⋅ f ( x ) i ) L(y, f(x))\frac{1}{N}\sum^N_{i1}max(0, 1-y_{i}\cdot f(x)_{i}) L(y,f(x))N1∑i1Nmax(0,1−yi⋅f(x)i) 样本的真实标签和预测得分的乘积与1之差的最大值 若 y i ⋅ f ( x ) i ≥ 1 y_{i}\cdot f(x)_{i} \geq 1 yi⋅f(x)i≥1样本被正确分类损失为0 反之样本离正确分类还有距离加入损失 代码示例
inputs torch.tensor([[0.3, 0.7], [0.5, 0.5]]) # 两个样本两个神经元
target torch.tensor([[-1, 1], [1, -1]], dtypetorch.float) # 该 loss 为逐个神经元计算需要为每个神经元单独设置标签loss_f nn.SoftMarginLoss()
output loss_f(inputs, target)print(SoftMarginLoss损失函数的计算结果为,output)多分类的折页损失
torch.nn.MultiMarginLoss(p1, margin1.0, weightNone, size_averageNone, reduceNone, reductionmean) 参数解释 p可选 1 或 2 weight各类别loss的权值 计算公式 代码示例
inputs torch.tensor([[0.3, 0.7], [0.5, 0.5]])
target torch.tensor([0, 1], dtypetorch.long) loss_f nn.MultiMarginLoss()
output loss_f(inputs, target)print(MultiMarginLoss损失函数的计算结果为,output)三元组损失
torch.nn.TripletMarginLoss(margin1.0, p2.0, eps1e-06, swapFalse, size_averageNone, reduceNone, reductionmean) 说明 三元组特殊的数据存储格式有很多具体的引用如nlp的关系抽取任务中实体1 关系 实体2又如项目中的anchor, positive examples, negative examples 损失函数倾向于使anchor与positive examples的距离更近而与negative examples的距离更远 计算公式 代码示例
triplet_loss nn.TripletMarginLoss(margin1.0, p2)
anchor torch.randn(100, 128, requires_gradTrue)
positive torch.randn(100, 128, requires_gradTrue)
negative torch.randn(100, 128, requires_gradTrue)
output triplet_loss(anchor, positive, negative)
output.backward()
print(TripletMarginLoss损失函数的计算结果为,output)HingEmbeddingLoss
torch.nn.HingeEmbeddingLoss(margin1.0, size_averageNone, reduceNone, reductionmean) 计算公式 代码示例
loss_f nn.HingeEmbeddingLoss()
inputs torch.tensor([[1., 0.8, 0.5]])
target torch.tensor([[1, 1, -1]])
output loss_f(inputs,target)print(HingEmbeddingLoss损失函数的计算结果为,output)余弦相似度
torch.nn.CosineEmbeddingLoss(margin0.0, size_averageNone, reduceNone, reductionmean) 计算公式 代码示例
loss_f nn.CosineEmbeddingLoss()
inputs_1 torch.tensor([[0.3, 0.5, 0.7], [0.3, 0.5, 0.7]])
inputs_2 torch.tensor([[0.1, 0.3, 0.5], [0.1, 0.3, 0.5]])
target torch.tensor([1, -1], dtypetorch.float)
output loss_f(inputs_1,inputs_2,target)print(CosineEmbeddingLoss损失函数的计算结果为,output)CTC损失函数 CTC(Connectionist Temporal Classification)处理seq2seq任务如语音识别、手写体识别解决序列对齐问题即在输入序列和目标序列长度不同的情况下计算它们之间的对应关系 计算路径概率对于给定的输入序列和目标标签CTC损失考虑所有可能的对齐路径在这些对齐路径对应的对齐方式中重复字符被合并空白标签被插入。CTC损失计算这些路径的概率之和 在反向传播中CTC损失也会考虑所有可能的对齐方式 L ( y , y ^ ) − l o g ( P ( y ^ ∣ y ) ) L(y, \hat y) -log(P(\hat y|y)) L(y,y^)−log(P(y^∣y)) torch.nn.CTCLoss(blank0, reductionmean, zero_infinityFalse) 功能用于时序类数据的分类 计算连续时间序列和目标序列之间的损失 参数解释 blankblank label zero_infinity无穷大的值 代码示例
# Target are to be padded
T 50 # Input sequence length
C 20 # Number of classes (including blank)
N 16 # Batch size
S 30 # Target sequence length of longest target in batch (padding length)
S_min 10 # Minimum target length, for demonstration purposes# Initialize random batch of input vectors, for *size (T,N,C)
# 生成一批随机输入序列
# 这些input序列通过softmax函数沿着最后一个维度(detach函数)传递对数化处理
input torch.randn(T, N, C).log_softmax(2).detach().requires_grad_()# Initialize random batch of targets (0 blank, 1:C classes) 生成随机目标标签
target torch.randint(low1, highC, size(N, S), dtypetorch.long)# 初始化input序列长度和目标序列长度input中每个序列长度都为样本最大序列长度Ttarget中每个序列长度为S_min到S之间的随机值
input_lengths torch.full(size(N,), fill_valueT, dtypetorch.long)
target_lengths torch.randint(lowS_min, highS, size(N,), dtypetorch.long)
ctc_loss nn.CTCLoss()
loss ctc_loss(input, target, input_lengths, target_lengths)
loss.backward()# Target are to be un-padded
T 50 # Input sequence length
C 20 # Number of classes (including blank)
N 16 # Batch size# Initialize random batch of input vectors, for *size (T,N,C)
input torch.randn(T, N, C).log_softmax(2).detach().requires_grad_()
input_lengths torch.full(size(N,), fill_valueT, dtypetorch.long)# Initialize random batch of targets (0 blank, 1:C classes)
target_lengths torch.randint(low1, highT, size(N,), dtypetorch.long)
# target的生成方式变化使用的是每个样本的长度来生成target
target torch.randint(low1, highC, size(sum(target_lengths),), dtypetorch.long)
ctc_loss nn.CTCLoss()
loss ctc_loss(input, target, input_lengths, target_lengths)
loss.backward()print(CTCLoss损失函数的计算结果为,loss)关于seq2seq任务中的pading和unpadding 填充使所有序列达到相同的长度以便进行批处理操作。通过在短序列两侧添加特殊标记通常是0来保持相同的长度。适用要求输入序列长度相同的模型如循环神经网络RNN或卷积神经网络CNN等 不填充模型可以直接根据输入序列的实际长度进行建模。适用于模型能够处理变长输入和目标序列的情况如注意力机制Attention Mechanism或Connectionist Temporal ClassificationCTC损失 在CTC损失计算中不填充的target使得目标序列与输入序列一一对应不添加额外的标记损失计算能够更接近实际任务 参考资料
datawhale through-pytorch repoloss函数之PoissonNLLLoss, GaussianNLLLosstorch.nn.MarginRankingLoss文本排序语音识别深入理解CTC Loss原理