insung3511 / torch-study

Studying repository for Pytorch library

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Softmax dimension error.

insung3511 opened this issue · comments

RuntimeError: 0D or 1D target tensor expected, multi-target not supported
위와 같은 에러가 발생한다. 발생하는 부분의 코드는 다음과 같다.

class Model(nn.Module):
    def __init__(self):
        super(Model, self).__init__()
        self.conv1d_a = nn.Conv1d(64, 64, kernel_size=2, stride=2)
        self.softmax  = nn.Softmax(1)

    def forward(self, x):
        x = self.conv1d_a(x)
        x = self.softmax(x)

        return x

model.py 에 있는 코드로 정말 간단한 모델이다. 합성곱 레이어를 통과하고 softmax로 단순 분류를 하는 것을 확인하고자 제작을 하였다. 명확한 에러의 사유를 찾고 있지만 내가 아직 제대로 몰라서 생기는 문제 인 것은 확실하다. Pytorch 에서는 Softmax를 nn.Softmax(), nn.LogSoftmax() 이렇게 두개의 함수를 활용해 Softmax 함수를 쓸수가 있다. LogSoftmax는 기존 Softmax에다가 Log 함수가 함께 연산된 방법으로 크게 다를건 없지만 수학적으로 계산되는 방법이 다른 것으로 확인된다.

이 부분에 대해서는 더욱 공부하고 다시 작성할 예정이다.