👩💻torch.Tensor vs torch.tensor
torch
tensor
inverted
code
torch Tensor 객체 생성 방법 비교와 Inverted 연산 확인하기
공통점과 차이점
공통점
둘다 data(input)에 대해서 Tensor 객체
로 만들어 주는 기능은 동일
차이점
torch.tensor()
: function- docs: https://pytorch.org/docs/stable/generated/torch.tensor.html#torch.tensor
torch.tensor(data)
에서data
는 필수 argument. (data 없을 시TypeError
)- torch.tensor는 항상 data를 복사
int
입력 시int
그대로 입력- 입력받은 데이터를 새로운 메모리 공간에 복사해
Tensor 객체
생성 (call by value
)
torch.Tensor()
: class- docs: https://pytorch.org/docs/stable/tensors.html#torch.Tensor
- 이미 생성된 객체를 tensor로 바꾸고 싶을 때 사용
- 빈 Tensor 객체를 만들 때 사용
int
입력 시float
로 변환Tensor 객체
로 데이터를 입력할 경우 입력 받은 메모리 공간 그대로 사용 (call by reference
)list
나numpy
등 다른 자료형으로 입력 받을 경우 값을 복사하여Tensor 객체
생성(call by value
)
torch.tensor()
original_data = torch.tensor([1])
new_data = torch.tensor(original_data)
print(f"original : {original_data} {original_data.dtype}")
print(f"new : {new_data} {new_data.dtype}")
# original data를 수정
original_data[0] = 2
print(f"original : {original_data} {original_data.dtype}")
print(f"new : {new_data} {new_data.dtype}")
original : tensor([1]) torch.int64
new : tensor([1]) torch.int64
original : tensor([2]) torch.int64
new : tensor([1]) torch.int64
/tmp/ipykernel_847781/1124094978.py:2: UserWarning: To copy construct from a tensor, it is recommended to use sourceTensor.clone().detach() or sourceTensor.clone().detach().requires_grad_(True), rather than torch.tensor(sourceTensor).
new_data = torch.tensor(original_data)
- int에서 int로
- call by value, 새로운 Tensor 객체이므로 변화 x
torch.Tensor()
original_data = torch.Tensor([1])
new_data = torch.Tensor(original_data)
print(f"original : {original_data} {original_data.dtype}")
print(f"new : {new_data} {new_data.dtype}")
# original data를 수정
original_data[0] = 2
print(f"original : {original_data} {original_data.dtype}")
print(f"new : {new_data} {new_data.dtype}")
original : tensor([1.]) torch.float32
new : tensor([1.]) torch.float32
original : tensor([2.]) torch.float32
new : tensor([2.]) torch.float32
- int에서 float으로
- call by reference, shallow copy
original_data = [1]
new_data = torch.Tensor(original_data)
print(f"original : {original_data}")
print(f"new : {new_data} {new_data.dtype}")
# original data를 수정
original_data[0] = 2
print(f"original : {original_data}")
print(f"new : {new_data} {new_data.dtype}")
original : [1]
new : tensor([1.]) torch.float32
original : [2]
new : tensor([1.]) torch.float32
- int에서 float으로
- call by value, deep copy
Inverted ~
연산 적용 차이
~ (operator.invert) is only implemented on integer and Boolean-type tensors
torch.tensor()
a = torch.tensor([0,1,10])
print(f"Shape of tensor: {a.shape}")
print(f"Datatype of tensor: {a.dtype}")
print(f"Device tensor is stored on: {a.device}")
Shape of tensor: torch.Size([3])
Datatype of tensor: torch.int64
Device tensor is stored on: cpu
b = torch.tensor([True, False, True])
print(f"Shape of tensor: {b.shape}")
print(f"Datatype of tensor: {b.dtype}")
print(f"Device tensor is stored on: {b.device}")
Shape of tensor: torch.Size([3])
Datatype of tensor: torch.bool
Device tensor is stored on: cpu
torch.Tensor()
a = torch.Tensor([0,1,10])
print(f"Shape of tensor: {a.shape}")
print(f"Datatype of tensor: {a.dtype}")
print(f"Device tensor is stored on: {a.device}")
Shape of tensor: torch.Size([3])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
b = torch.Tensor([True, False, True])
print(f"Shape of tensor: {b.shape}")
print(f"Datatype of tensor: {b.dtype}")
print(f"Device tensor is stored on: {b.device}")
Shape of tensor: torch.Size([3])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
c = torch.Tensor([0.13, 1.5, -.116])
print(f"Shape of tensor: {c.shape}")
print(f"Datatype of tensor: {c.dtype}")
print(f"Device tensor is stored on: {c.device}")
Shape of tensor: torch.Size([3])
Datatype of tensor: torch.float32
Device tensor is stored on: cpu
결론: torch.tensor()
로만 ~
연산 사용 가능
Reference