IndexError: index -1 is out of bounds for dimension 0 with size 0

SSW·2024년 4월 19일
0

Trouble Shooting

목록 보기
9/9

Error Message

File "C:\Users\xxxx\xxxx\xxxx\xxxxx\xxxx\..\utils\criterion.py", line 73, in _ohem_forward
    min_value = pred[min(self.min_kept, pred.numel() - 1)]
IndexError: index -1 is out of bounds for dimension 0 with size 0

Solution

min_value = pred[min(self.min_kept, pred.numel() - 1)]

위 코드를 아래와 같이 수정

if pred.numel() > 0:
	min_value = pred[min(self.min_kept, pred.numel() - 1)]
else:
	return score.new_tensor(0.0)

Reason

pred 텐서가 비어있거나 0일 때 발생하는 오류이기 때문에 0이라면 오류를 방지하기 위해 값이 0.0인 텐서를 반환하도록 예외처리를 통해 해결할 수 있다.

profile
ssw

0개의 댓글