티스토리 뷰

 

1 .data

.data

가 헷갈려 쓰는 포스팅

.data

was the primary way to get the underlying Tensor from a Variable. After this merge, calling y = x.data still has similar semantics. So y will be a Tensor that shares the same data with x, is unrelated with the computation history of x, and has requires_grad=False.

However,

.data

can be unsafe in some cases. Any changes on x.data wouldn't be tracked by autograd, and the computed gradients would be incorrect if x is needed in a backward pass. A safer alternative is to use x.detach(), which also returns a Tensor that shares data with requires_grad=False, but will have its in-place changes reported by autograd if x is needed in backward.

정리하자면

.data

는 tensor 데이터는 공유 하되 required_grad 는 False로 설정된다. 중요한 건 데이터를 공유하기 때문에 .data로 할당된 어떤 tensor를 바꾼다면 원래 tensor도 바뀐다.

밑에 있는 케이스는 어떤 상황인지 아직 잘 모르겠다. pass

2 .item

size가 1 인 torch tensor를 python scalar로 변환

'컴퓨터 과학 이야기(Computer Science) > PyTorch' 카테고리의 다른 글

Pytorch Variable  (0) 2019.02.27
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/04   »
1 2 3 4 5
6 7 8 9 10 11 12
13 14 15 16 17 18 19
20 21 22 23 24 25 26
27 28 29 30
글 보관함