Section 3 [실습] PyTorch로 구현해보는 Loss Function의 Cross Entropy 구현 관련하여 질문이 있습니다.
안녕하세요 선생님,
batch_size = 16
n_class = 10
def generate_classification(batch_size=16, n_class=10):
pred = torch.nn.Softmax()(torch.rand(batch_size, n_class))
ground_truth = torch.argmax(torch.rand(batch_size, n_class), dim=1)
return pred, ground_truth
def CE_loss(pred, label):
loss = 0.
exp_pred = torch.exp(pred) # 이 부분 관련 질문이 있습니다.
for batch_i in range(len(pred)):
for j in range(len(pred[0])):
if j == label[batch_i]:
print(pred[0], j)
loss = loss + torch.log(exp_pred[batch_i][j] / torch.sum(exp_pred, axis=1)[batch_i])
return -loss / len(pred)CE loss를 구현하는 과정에서 exp_pred = torch.exp(pred) 행이 왜 필요한 것인지 궁금합니다!
exp를 취해주는 이유는 모델의 출력값 logits에 exp를 적용해 각 클래스에 대한 예측값을 양수로 변환한다고 알고 있는데generate_classification위에서 이미 softmax를 취해서 확률분포로 변환해주기 때문에 음수 값은 나오지 않는데 왜 exp를 적용해주어야 하는지 모르겠어서 여쭤봅니다!
답변 1
1
안녕하세요 변정현입니다!
네 말씀해주신 것처럼 예측값이 이미 SoftMax 함수로 0~1 사이의 값과 합이 1로 normalize되어 있어서 CE_loss에 있는 SoftMax 함수 (Exponential 취한 후 Exponential의 합으로 나누는 것) 를 별도로 또 취할 필요가 없습니다!
제가 앞서서 정의한 예측값을 재활용하는 과정에서 혼선이 있었네요 ㅎㅎ
잘 발견해주셔서 감사합니다!
노션URL 이동문제
0
56
1
섹션12 실습코드 의 initialization 각각 적용 코드 문의
0
97
0
오타
0
82
0
혹시 이론강의 자료도 공유가 가능하실까요?
1
166
1
[sec. 2-8 실습] 배치사이즈의 선택
0
171
2
[section 14 / [실습] 직접 만든 CNN 모델과 ResNet, VGGNet을 활용한 CV 프로젝트] transforms.Normalize 질문
1
226
1
[section 14 / VGGNet ] receptive filed 질문
1
131
2
[섹션3, PyTorch로 구현해보는 Loss Function] 분류task loss함수 질문입니다.
1
233
2
KL Div Loss에서 Negative Entropy를 더해주는 의미에 대해서
1
193
2
GoogleNet Inception 모듈
1
166
2
Batch Normalization 효과
1
183
2
Layer Norm이 언어모델에 적합한 이유
1
520
1
9-2 fully connected NN
1
129
1
Mini-batch Gradient Descent computation time 질문
1
175
2
Huber Loss에 대한 질문
1
303
2
Batch size 가 극단적으로 작은 경우 (예를들어 batch_size =1) Normalization 적용 방안
3
351
1
Normalization 질문
1
270
2
Section 7 [Activation Function의 종류] Softmax logit 분포와 Entropy 질문이 있습니다
2
210
1
Section 3의 [이론] Regression task의 Loss: L1, L2, Huber, Log Cosh Loss "미분가능"관련 에서 질문이 있습니다.
1
280
1
Section 15 실습 중, lstm 클래스의 forward 함수 질문
1
236
2
Section 16 [이론] Transformer의 Encoder 질문
1
177
1
Auto Differentiation에서 computational Graph 질문있습니다
1
212
1
Section 3 이론 classification Loss function 질문
1
235
1
section4의 entropy 시그마 식 전개에 대한 질문입니다.
1
265
2





