본문 바로가기
728x90

Deep Learning (AI)/Training Loss5

[Pytorch] Cosin Similarity Loss +역전파 1. Cosin Similarity Loss import torch import torch.nn.functional as F import torch.nn as nn feat1 = torch.tensor([0.1,0.2,0.3], dtype=torch.float32) feat2 = torch.tensor([0.4,0.2,0.5], dtype=torch.float32) loss = F.cosine_similarity(feat1, feat2) # negative cosin loss loss = -F.cosine_similarity(feat1, feat2) # positive cosin loss 두 벡터(feature)를 가깝게 혹은 멀게 만들도록 학습합니다. 저는 여기서 의문점이 들었습니다. 위의 수식의 역전.. 2023. 7. 30.
[Pytorch] Negative Pair Loss 코드 두 feature가 멀어지도록 학습하는 loss Negative Pair Loss = max(margin - distance_metric(feature_1, feature_2), 0) -------- (1) where, max(f(x),0)은 f(x) > 0 이면 backpropagation 수행, margin은 feature_1과 feature_2를 얼마나 멀게 할 것인지, distance_metric은 두 feature의 거리를 구하는 function. 즉, 두 feature의 거리를 구하고 margin을 더했을 시, 0보다 크면 역전파하여 멀어지게 한다. 이 term은 triplet loss에서 positive pair과 같이 사용되기도 한다. 그렇다면 어떻게 두 feature를 멀어지게 한다는 것.. 2023. 7. 21.
[Pytorch] L1-loss, L2-loss, KLD-loss 역전파 L1 loss 역전파 및 언제 사용, 기대효과 L2 loss 역전파 및 언제 사용, 기대효과 + L1-loss의 미분값이 어떻게 업데이트 되는가? 딥러닝에서 학습되는 가중치 Weight는 다음과 같이 업데이트 됩니다. W = W - learning_rate * gradient_of_loss_with_respect_to_W 그렇기 떄문에, L1-loss로 인한 미분값 1 or -1의 값은 weight의 학습 뱡향을 바꾸게 됩니다. 예를 들어, 예측값 = 0.5, 정답값 = 0.7일 경우에 L1-loss의 gradient는 0.5-0.7 < 0 이므로, -1의 값이 됩니다. 이를, 가중치 업데이트에 반영하면, W = W + learning_rate * gradient_others (#others는 다른 lo.. 2023. 7. 20.
[Pytorch] Softmax with CrossEntropyLoss 역전파 Softmax & Sigmoid with CrossEntropyLoss (CE Loss) forward and backward (calculate gradient). 아래 사진에 자세한 풀이과정을 적어놓음. softmax-with-CELoss의 입력(x)에 대한 역전파(미분값)는 softmax(x) - target(label). feaures of taget=1 and taget=0에 대해서는 부호를 반대로 역전파하여 모델이 cateogry classification ability를 학습한다. target이 one-hot label (single classification)이라는 전제하에, Softmax를 Sigmoid로 대체해도 미분값이 같기 때문에 동일한 결과를 얻을 수 있다. multi-class .. 2023. 7. 20.
[Pytorch] torch kld-loss (KD-loss) 코드 일반적인 힌튼 KD loss 코드 import torch.nn as nn import torch.nn.functional as F KL_loss = nn.KLDivLoss(reduction='batchmean')(F.log_softmax(outputs/T, dim=1), F.softmax(targets/T, dim=1))*(alpha*T*T) outputs student model output (feature -> classifer 결과) shape : (batch size, class num) targets teacher model outputs (feature -> classifier 결과) shape : (batch size, class num) T (Temperature) 얼마나 지식을 부드럽게 .. 2023. 7. 14.
728x90