일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
- 도커
- Deeplearning
- dl
- NLP
- vs code
- 자료구조
- rnn
- Algorithm
- contiguous
- docker
- Python
- 인디음악
- Transformation
- CS
- Data Structure
- COLAB
- Analog Rebellion
- tutorial
- torchtext
- attention
- deep learning
- pytorch
- gru
- lstm
- Today
- Total
Deep learning/Machine Learning/CS 공부기록
PyTorch에서 tensor를 copy하는 법 본문
https://stackoverflow.com/questions/55266154/pytorch-preferred-way-to-copy-a-tensor
PyTorch에서 tensor를 복사하는 방법은 여러가지가 있다.
y = tensor.new_tensor(x) #a
y = x.clone().detach() #b
y = torch.empty_like(x).copy_(x) #c
y = torch.tensor(x) #d
과연 어떠한 방법이 올바르게 tensor를 복사하는 방법일까?
a: y = tensor.new_tensor(x)
new_tensor()
는 parameter가 뭐든간에 이를 읽어서 leaf variable을 생성한다. 따라서 근본적으로 b와 동일한 코드이다. 만약 grad가 흐르게 하고 싶다면, require_grad 옵션을 따로 넣어주면 된다.
y = tensor.new_tensor(x, requires_grad=True)
b: y = x.clone().detach()
이 방법의 경우 computational graph에서 더 이상 필요하지 않을 때 사용할 수 있다. 즉, 다음과 같이 tensor가 있다고 할 때,
x = torch.rand(3, requires_grad=True)
print(x.requires_grad) # True
y = x.clone().detach()
print(y.requires_grad) # True
y를 통해 어떠한 연산을 진행하더라도 x에 미치는 영향은 없다. 따라서 weight를 통해 특정 작업을 하고 싶다면 이를 이용하면 된다.
이는 a와 동일한 코드이며, computation graph에서 분리하고 싶을 때 권장된다. clone
후에 detach
를 하는 이유는 밑을 참고.
Unlike copy_(), this function is recorded in the computation graph. Gradients propagating to the cloned tensor will propagate to the original tensor.
c: y = torch.empty_like(x).copy_(x)
이는 y에 gradient가 흐를 수 있으므로, 나머지 셋과 성격이 가장 다르다.
d: y = torch.tensor(x)
a와 동일. 근데 근본적으로 a와 같은데, 왜 이 방법이 추천되지 않는지를 모르겠다.
좀 더 명확하고, 빠른 방법임. 아래 참고.
결론:
Since in general for a copy operation one wants a clean copy which can't lead to unforeseen side effects the preferred way to copy a tensors is .clone().detach().
'ML, DL > PyTorch' 카테고리의 다른 글
PyTorch contiguous (0) | 2020.06.25 |
---|---|
torch.expand() vs. torch.repeat() (0) | 2020.06.20 |