Standalone-DeepLearning icon indicating copy to clipboard operation
Standalone-DeepLearning copied to clipboard

acc가 0.0으로 고정되는 문제

Open mekty2012 opened this issue 6 years ago • 5 comments

  1. Train & Evaluation 셀에서 acc 관련 문제입니다.
    correct = 0
    model.eval()
    optimizer.zero_grad()

    for input_X, true_y in test_loader:
        input_X = input_X.squeeze()
        input_X = input_X.view(-1, 784)
        pred_y = model(input_X).max(1, keepdim=True)[1].squeeze()
        correct += pred_y.eq(true_y).sum()

    print(correct.numpy(), len(test_loader.dataset))

    acc = correct.numpy() / len(test_loader.dataset)
    print(acc)
    list_acc.append(acc)
    list_acc_epoch.append(i)

Output :

(array(6235), 10000)
0
Epoch: 0, Train Loss: 1.1313279015, Val Loss: 1.2628234821, Test Acc: 0%
(array(5080), 10000)
0
Epoch: 1, Train Loss: 1.26448708956, Val Loss: 1.57953677902, Test Acc: 0%
(array(3074), 10000)
0
Epoch: 2, Train Loss: 1.55898885273, Val Loss: 1.82879043531, Test Acc: 0%
(array(2884), 10000)
0
Epoch: 3, Train Loss: 1.83698086543, Val Loss: 1.95342421833, Test Acc: 0%
...

와 같이 acc가 0이 아니어야 함에도 0이 나옵니다. 테스트를 위해

t = torch.Tensor([6235])
t.numpy() / 10000

를 실행한 경우 array([0.6235], dtype=float32) 이 출력되어 numpy.ndarray 나 torch.Tensor 자체의 문제로 보이지는 않습니다.

mekty2012 avatar Jan 22 '19 12:01 mekty2012

Epoch: 3, Train Loss: 1.83698086543, Val Loss: 1.95342421833, Test Acc: 0% 가 출력되는 print문도 같이 보여주시겠어요?

heartcored98 avatar Jan 22 '19 13:01 heartcored98

Epoch: 3, Train Loss: 1.83698086543, Val Loss: 1.95342421833, Test Acc: 0% 가 출력되는 print문도 같이 보여주시겠어요?

    correct = 0
    model.eval()
    optimizer.zero_grad()

    for input_X, true_y in test_loader:
        input_X = input_X.squeeze()
        input_X = input_X.view(-1, 784)
        pred_y = model(input_X).max(1, keepdim=True)[1].squeeze()
        correct += pred_y.eq(true_y).sum()

    print(correct.numpy(), len(test_loader.dataset))

    acc = correct.numpy() / len(test_loader.dataset)
    print(acc)
    list_acc.append(acc)
    list_acc_epoch.append(i)
    print('Epoch: {}, Train Loss: {}, Val Loss: {}, Test Acc: {}%'.format(i, train_loss, val_loss, acc*100))

입니다.

mekty2012 avatar Jan 22 '19 13:01 mekty2012

  1. Train & Evaluation 셀에서 acc 관련 문제입니다. ...

안녕하세요, 저도 처음에는 accurency가 0으로 고정되어서 의구심을 품고 봤었는데, 런타임 유형이 python2라서 발생하는 문제인 것 같습니다.

   correct = 0
    model.eval()
    optimizer.zero_grad()

    for input_X, true_y in test_loader:
        input_X = input_X.squeeze()
        input_X = input_X.view(-1, 784)
        pred_y = model(input_X).max(1, keepdim=True)[1].squeeze()
        correct += pred_y.eq(true_y).sum()

    acc = correct.numpy() / len(test_loader.dataset)
    list_acc.append(acc)
    list_acc_epoch.append(i)
    
    print('Epoch: {}, Train Loss: {}, Val Loss: {}, Test Acc: {}%'.format(i, train_loss, val_loss, acc*100))

위 코드에서 acc를 계산하는 부분에서 len은 integer로 나오는데 런타임 유형이 python2 로 자동으로 설정되어 있습니다. python 2에서는 나눗셈에 integer가 포함되어 있으면 무조건 정수형을 리턴하게 됩니다.

따라서 다음과 같이 바꾸거나,

acc = correct.numpy() / float(len(test_loader.dataset))

런타임 유형을 python 3로 바꾸시면 해결될 것 같습니다!

** flaot 이라고 오타가 나있었는데 수정했습니당 **

dongjoo0-0 avatar Jan 22 '19 13:01 dongjoo0-0

오호! 그러네요 잘 해결하신 것 같습니다 👍 마지막에서 두번째 줄에 flaot -> float 으로 바꾸면 더 정확할 것 같습니다 😄

다른 분들도 꼭 시작하실 때 python3 으로 런타임 유형이 설정되어 있는지 체크하시면 좋을 것 같네요

heartcored98 avatar Jan 22 '19 14:01 heartcored98

그리고 혹시 모르니 다른 분들도 보실 수 있게 이 이슈는 다시 열어놓겠습니다 :)

heartcored98 avatar Jan 22 '19 14:01 heartcored98