NLLLoss

54299 ワード

基本的にはTORCHnn.LOSSはclasstorchnn.functional.LOSSは関数です.
そのため、コーチから入るポジションが違う.
私が主に使っているトレーナーでは、paramsを使って授業を受けることもできますし、トレーナーに直接機能を加えることもできます.
いつも公式文書を先に見ます.何を言っているのか分かりませんが、見てください.これは公式です.公式です.正確親切.例もいいですね.正式な書類を読む練習をしてこそ、誰も知らないことを知ることができる.
もちろんですが、税関の損失は遅いに違いありません.nanを生成することもできます.
どう計算するか知りたいだけです.
結論numpyはchannelの後,torchはdim=1であった.
これには理由がある.本当に気持ちいいですね
torchは基本的に関数を作成し、一緒にクラスを作成し、簡単な変更でクラスを継承し、クラス全体を変更したい場合は関数をインポートしてクラスを作成することができます.
torch.nn.NLLLoss
https://pytorch.org/docs/stable/generated/torch.nn.NLLLoss.html
torch.nn.functional.nll_loss
https://pytorch.org/docs/stable/generated/torch.nn.functional.nll_loss.html#torch.nn.functional.nll_loss
公式文書の儀式を見て、何を言っているのか全く理由が分かりませんでした
DLを始めたばかりの頃はCEEを学んだが、CEEをベースにして他の方法は脳に入らなかった.
これは上のCEEを作るための基準です
  • Output shape : tensor( Batch × Class × k-dimension )
  • Target shape : tensor( Batch × k-dimension )
  • loss = nn.NLLLoss(reduction='sum')
    outputs = torch.rand(10, 5, requires_grad=True)
    targets = torch.tensor([0,2,1,3,4,3,2,1,3,1])
    loss(outputs, targets)
    def myloss(outputs, targets):
        onehot = torch.nn.functional.one_hot(targets).float()
        hadamrd = outputs*onehot
        sum = torch.sum(hadamrd, dim=1)
        return -torch.sum(sum)
        
    myloss(outputs, targets)
    以上の2つの値は等しい.
    これは1 Dの時、2 Dの時もやってみましょう.
    実はこれが必要ですが、これは公式文書にはありません.
    以下のことを考えれば、分かります.pixel-wise<-強い単語のようですその単語は理解できる
    loss = nn.NLLLoss(reduction='sum')
    outputs = torch.rand(2, 5, 10, 10, requires_grad=True)
    targets = torch.tensor([[
                           [4,0,1,2,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1],
                           [2,1,1,2,4,2,2,1,3,0],
                           [0,0,1,3,2,3,3,4,3,1],
                           [4,2,1,3,4,3,2,4,2,1],
                           [4,0,1,2,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1],
                           [2,1,1,2,4,2,2,1,3,0],
                           [0,0,1,3,2,3,3,4,3,1],
                           [4,2,1,3,4,3,2,4,2,1]],
                           
                          [[4,0,1,2,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1],
                           [2,1,1,2,4,2,2,1,3,0],
                           [0,0,1,3,2,3,3,4,3,1],
                           [4,2,1,3,4,3,2,4,2,1],
                           [4,0,1,2,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1],
                           [2,1,1,2,4,2,2,1,3,0],
                           [0,0,1,3,2,3,3,4,3,1],
                           [4,2,1,3,4,3,2,4,2,1]]])
    
    
    loss(outputs, targets)
    同じようにcustomを試したことがあります.
    これは本当に数字が違うのを紛らわしてshapeを撮ったほうがいいです
    def myloss(outputs, targets):
        onehot = torch.nn.functional.one_hot(targets).float()
        reshape = np.transpose(onehot, (0,3,1,2))
        hadamrd = outputs*reshape
        sum = torch.sum(hadamrd, dim=1)
        return -torch.sum(sum)
    myloss(outputs, targets)
    Clear !
    真ん中にlog+softmaxを追加します.以下に示します.
    もう一つ追加するのはもちろんsquareは使いません.
    loss = nn.CrossEntropyLoss(reduction='sum')
    outputs = torch.rand(2, 5, 10, 15, requires_grad=True)
    targets = torch.tensor([[
                           [4,0,1,2,4,4,2,1,3,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1,4,2,1,3,4],
                           [2,1,1,2,4,2,2,1,3,0,4,2,1,3,4],
                           [0,0,1,3,2,3,3,4,3,1,4,2,1,3,4],
                           [4,2,1,3,4,3,2,4,2,1,4,2,1,3,4],
                           [4,0,1,2,4,4,2,1,3,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1,4,2,1,3,4],
                           [2,1,1,2,4,2,2,1,3,0,4,2,1,3,4],
                           [0,0,1,3,2,3,3,4,3,1,4,2,1,3,4],
                           [4,2,1,3,4,3,2,4,2,1,4,2,1,3,4]],
                           
                          [[4,0,1,2,4,4,2,1,3,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1,4,2,1,3,4],
                           [2,1,1,2,4,2,2,1,3,0,4,2,1,3,4],
                           [0,0,1,3,2,3,3,4,3,1,4,2,1,3,4],
                           [4,2,1,3,4,3,2,4,2,1,4,2,1,3,4],
                           [4,0,1,2,4,4,2,1,3,4,4,2,1,3,4],
                           [0,2,1,3,4,2,0,2,3,1,4,2,1,3,4],
                           [2,1,1,2,4,2,2,1,3,0,4,2,1,3,4],
                           [0,0,1,3,2,3,3,4,3,1,4,2,1,3,4],
                           [4,2,1,3,4,3,2,4,2,1,4,2,1,3,4]]])
    
    
    loss(outputs, targets)
    
    
    
    
    
    def myloss(outputs, targets):
        onehot = torch.nn.functional.one_hot(targets).float()
        reshape = np.transpose(onehot, (0,3,1,2))
        logsoft_out = nn.LogSoftmax(dim=1)
        logsoft_out_value = logsoft_out(outputs)
        hadamrd = logsoft_out_value*reshape
        sum = torch.sum(hadamrd, dim=1)
        return -torch.sum(sum)
    myloss(outputs, targets)