日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

randn函数加噪声_损失函数 (Loss Function)

發布時間:2025/3/19 编程问答 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 randn函数加噪声_损失函数 (Loss Function) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
  • 損失函數
  • MSELoss(均方誤差損失函數)
  • BCELoss、BCEWithLogitsLoss
  • CrossEntropyLoss
  • L1Loss
  • SmoothL1Loss
  • 各個損失函數的導數
  • 計算值:

    真實值:

    pytorch很多的loss 函數都有size_average和reduce兩個布爾類型的參數,需要解釋一下。因為一般損失函數都是直接計算 batch 的數據,因此返回的 loss 結果都是維度為(batch_size, ) 的向量。

    • 如果 reduce = False,那么 size_average 參數失效,直接返回向量形式的 loss;
    • 如果 reduce = True,那么 loss 返回的是標量
      • 如果 size_average = True,返回 loss.mean();
      • 如果 size_average = True,返回 loss.sum();

    若把這兩個參數設置成 False,這樣子比較好理解原始的損失函數定義。

    1、MSELoss(均方誤差損失函數)

    def prac_MSE_loss():loss_fn = torch.nn.MSELoss(reduce=False, size_average=False)input = torch.autograd.Variable(torch.randn(3, 4))target = torch.autograd.Variable(torch.randn(3, 4))loss = loss_fn(input, target)print(input);print(target);print(loss)print(input.size(), target.size(), loss.size()) prac_MSE_loss()

    2、BCELoss(二分類用的交叉熵損失函數)

    用的時候需要在該層前面加上Sigmoid函數

    因為只有正例和反例,且兩者的概率和為 1,那么只需要預測一個概率就好了,可以簡化成:

    loss_fn = torch.nn.BCELoss(reduce=False, size_average=False) input = Variable(torch.randn(3, 4)) target = Variable(torch.FloatTensor(3, 4).random_(2)) loss = loss_fn(F.sigmoid(input), target) print(input); print(target); print(loss)

    BCEWithLogitsLoss:

    上面的 nn.BCELoss 需要手動加上一個 Sigmoid 層,這里是結合了兩者,不需要加sigmoid層,就能得到BCELoss一樣的結果。

    loss_fn = torch.nn.BCELoss(reduce=False, size_average=False) input = Variable(torch.randn(3, 4)) target = Variable(torch.FloatTensor(3, 4).random_(2)) loss = loss_fn(input, target) print(input); print(target); print(loss)

    3、CrossEntropyLoss(交叉熵損失函數)

    def prac_cross():# weight = torch.Tensor([1, 2, 1, 1, 10])loss_fn = torch.nn.CrossEntropyLoss(reduce=False, size_average=False)input = Variable(torch.randn(3, 5)) # (batch_size, C)target = Variable(torch.LongTensor(3).random_(5))loss = loss_fn(input, target)print(input)print(target)print(loss)print('---手算第一個---')import mathexp = math.esigma_exp_x = pow(exp, input[0][0]) + pow(exp, input[0][1]) + pow(exp, input[0][2]) + pow(exp, input[0][3]) + pow(exp, input[0][4])log_sigma_exp_x = math.log(sigma_exp_x)loss_1 = -input[0][2] + log_sigma_exp_xprint('第一個樣本的loss:', loss_1)

    假設共有三個類別cat、dog、bird,那么一張cat的圖片標簽應該為

    。并且訓練過程中,這張cat的圖片經過網絡后得到三個類別網絡的輸出分別為3、1、-3。那么經過softmax可以得到對應的概率值,如下圖:

    4、Hinge Loss(折頁損失函數)

    參考:

    https://blog.csdn.net/shanglianlm/article/details/85019768

    https://blog.csdn.net/zhangjunp3/article/details/80467350

    PyTorch中的Loss Fucntion

    https://blog.csdn.net/zhangxb35/article/details/72464152?utm_source=itdadao&utm_medium=referral

    總結

    以上是生活随笔為你收集整理的randn函数加噪声_损失函数 (Loss Function)的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。