深度学习网络各类函数理解

229 阅读2分钟

2022 年什么会火?什么该学?本文正在参与“聊聊 2022 技术趋势”征文活动

Softmax()函数

  1. 理解:是对指定维度计算,使得指定维度的所有数加起来的值为1
  2. 代码
import  torch
from torch import nn
inputs = torch.tensor([[[1.0, 1], [1, 1]], [[1, 1], [1, 2]]])
func = nn.Softmax(dim=0)
print(inputs.size())
print(func(inputs))
  1. 输出为:
torch.Size([2, 2, 2]) 
tensor([[[0.5000, 0.5000], [0.5000, 0.2689]],[[0.5000, 0.5000],[0.5000, 0.7311]]])

torch.stack()函数

  1. 理解:顾名思义是一个拼接函数,可以将一个list里面的多个tensor按照想要的维度进行拼接,之后可以通过view()进行reshape的操作
  2. 代码
import torch
q = torch.tensor([1,2])
w = torch.tensor([4,5])
e = torch.tensor([7,8])
r = torch.tensor([10,11])
a = [q,w,e,r]
b = torch.stack(a, dim=1).view(-1)
print(b)
  1. 输出为: tensor([ 1, 4, 7, 10, 2, 5, 8, 11])

BCELoss和BCEWithLogitsLoss损失函数

参考:blog.csdn.net/qq_22210253…

  • BCELoss
  1. 此损失函数用于图像多标签,比如两张图片20个标签,则会输入经过模型生成的一个预测的2202*20的张量。同样:真实标签也是2*20的张量。对应下面的xnx_nyny_n
  2. BCELoss的公式是

image.png

  1. 其中yny_n是target,xnx_n是模型输出的值。
  2. 代码
import torch
inputs = torch.randn(2, 20)
target = torch.FloatTensor([[1,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],
                            [1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]])
print(inputs)
sig = torch.nn.Sigmoid()
inputs = sig(inputs)
print(inputs)
loss = torch.nn.BCELoss()
outputs = loss(inputs, target)
print(outputs)
  1. 输出
tensor([[ 2.3800, -0.0300,  0.7595,  0.4508, -2.2203,  1.8122, -1.4829, -0.8614,
          0.3155,  1.5903,  0.3403, -0.5875,  0.0085,  0.0692,  0.3217, -0.4210,
          1.8025,  0.3711,  1.4365, -2.1535],
        [ 0.1578, -2.1776, -1.0426, -1.2001,  0.7868, -0.8415,  0.4470,  1.5186,
          0.1289, -0.2912, -1.0111,  0.5232, -0.7549, -1.0644,  0.9567, -1.3635,
          0.7643,  0.9816, -0.3902, -1.5900]])
tensor([[0.9153, 0.4925, 0.6812, 0.6108, 0.0979, 0.8596, 0.1850, 0.2970, 0.5782,
         0.8307, 0.5843, 0.3572, 0.5021, 0.5173, 0.5797, 0.3963, 0.8585, 0.5917,
         0.8079, 0.1040],
        [0.5394, 0.1018, 0.2606, 0.2315, 0.6871, 0.3012, 0.6099, 0.8203, 0.5322,
         0.4277, 0.2668, 0.6279, 0.3198, 0.2565, 0.7225, 0.2037, 0.6823, 0.7274,
         0.4037, 0.1694]])
tensor(0.7922)
  • BCEWithLogitsLoss
  1. BCEWithLogitsLoss就是把Sigmoid+BCELoss合成一步;
  2. BCEWithLogitsLoss就是在输入BCELoss的公式之前,将预测值xnx_n进行Sigmoid函数的归一化操作,再作为预测概率的输入。
  3. 为什么要加sigmoid的个人理解:因为很多时候的真实多分类标签是0和1构成,所以将预测的概率化为0-1之间能更好的优化,而不至于将loss的值搞得很大。

Sigmoid函数

  1. 作用:将所有的数值都框定在0~1之间

  2. Sigmoid函数的表达式: image.png

  3. Sigmoid函数的函数曲线:

image.png