Curieux.JY
  • Post
  • Note
  • Jung Yeon Lee

On this page

  • 공통점과 차이점
    • 공통점
    • 차이점
    • torch.tensor()
    • torch.Tensor()
  • Inverted ~ 연산 적용 차이
    • torch.tensor()
    • torch.Tensor()

👩‍💻torch.Tensor vs torch.tensor

torch
tensor
inverted
code
torch Tensor 객체 생성 방법 비교와 Inverted 연산 확인하기
Published

December 21, 2022

공통점과 차이점

공통점

둘다 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()

import torch
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
~a
tensor([ -1,  -2, -11])
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
~b
tensor([False,  True, False])
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
~c
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [10], in <cell line: 1>()
----> 1 ~c

TypeError: ~ (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.float32
Device tensor is stored on: cpu
a
tensor([ 0.,  1., 10.])
~a
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [13], in <cell line: 1>()
----> 1 ~a

TypeError: ~ (operator.invert) is only implemented on integer and Boolean-type tensors
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
b
tensor([1., 0., 1.])
~b
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [16], in <cell line: 1>()
----> 1 ~b

TypeError: ~ (operator.invert) is only implemented on integer and Boolean-type tensors
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
c
tensor([ 0.1300,  1.5000, -0.1160])
~c
---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
Input In [19], in <cell line: 1>()
----> 1 ~c

TypeError: ~ (operator.invert) is only implemented on integer and Boolean-type tensors

결론: torch.tensor()로만 ~연산 사용 가능

Reference

  • torch.Tensor와 torch.tensor의 차이

Copyright 2024, Jung Yeon Lee