Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PyTorch lab-06_1_softsoftmax_classification 16번째 쉘 질문 #26

Open
SONG-WONHO opened this issue Apr 12, 2019 · 1 comment
Open

Comments

@SONG-WONHO
Copy link

# Cost 계산 (1)
hypothesis = F.softmax(x_train.matmul(W) + b, dim=1) # or .mm or @
y_one_hot = torch.zeros_like(hypothesis)
y_one_hot.scatter_(1, y_train.unsqueeze(1), 1)
cost = (y_one_hot * -torch.log(F.softmax(hypothesis, dim=1))).sum(dim=1).mean()

# cost로 H(x) 개선
optimizer.zero_grad()
cost.backward()
optimizer.step()

# 100번마다 로그 출력
if epoch % 100 == 0:
    print('Epoch {:4d}/{} Cost: {:.6f}'.format(
        epoch, nb_epochs, cost.item()
    ))

위와 같이 16번째 쉘의 cost 함수를 계산하는 부분에 softmax가 2번 들어간 것으로 확인됩니다.
물론 2번 넣어도 계산이 잘 되겠지만, 의도하신건지 궁금합니다.

감사합니다.

@jkee58
Copy link

jkee58 commented May 31, 2019

제 생각에는 오타같아 보입니다.

두번째 softmax() 를 빼고 계산하여도 같은 결과 값이 나옵니다.
이미 softmax()를 통해 0 ~1 사이의 분포로 바꾸었는데 그걸 다시 softmax() 해도 같은 값만 나옵니다.

그리고 슬라이드 설명에서는 cost = (y_one_hot * -torch.log(hypothesis)).sum(dim=1).mean()으로 적혀 있습니다.

해당 문서를 수정하는게 좋을 것 같네요.
@kh-kim

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants