[RuntimeError]: element 0 of tensors does not require grad and does not have a grad_fn

Hyun·2022년 8월 2일
0

ERROR

목록 보기
4/6

RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn

기울기 계산 기능을 끈 tensor로 기울기 계산을 할 경우 발생하는 에러이다.

grad_fn : 기울기 계산을 하는 function

해결 방법 :
1. tensor를 생성할 때, requires_grad=True로 명시
2. tensor 생성 후, x.requires_grad(True) 메소드를 사용하여 지정

x = torch.tensor([1.,2.,3.],requires_grad=True)	# 1
x.requires_grad_(True)	# 2

print(x.grad_fn)	# function의 메모리 위치와 형태
print(x.grad)		# 도함수 값

기울기 계산 기능을 키는 경우

0개의 댓글