torch.Tensor() VS torch.tensor()

AI Scientist를 목표로!·2022년 12월 8일
0

둘다 data(input)에 대해서 Tensor 객체로 만들어 주는 기능은 동일하나
구조적인 차이점이 있다.


차이점

torch.Tensor()

  • Class 형태

  • 이미 존재하는 data를 Tensor 형태로 바꾸고 싶을 때 사용

  • int를 입력시 float형태로 변환이 됨

  • type 지정 불가
    항상 torch.float (torch.float32) type이며 (torch.FloatTensor로 생성)

  • 반드시 input은 sequence (array)형태

  • 데이터 입력 시(Tensor 객체로) 입력 받은 메모리 공간을 그대로 사용 (call by reference)

  • 데이터 입력 시(list나 numpy 로) 입력 받은 값을 복사하여 Tensor 객체 생성(call by value)

torch.tensor()

  • Function 형태

  • 괄호 안에 data를 input으로 받아 tensor형태로 변환 시켜주는 기능

  • torch.tensor()는 항상 data를 복사하기 때문에 괄호안에 data를 넣어주지 않으면 TypeError가 발생

  • int를 입력시 int 형태 그대로 출력

  • type을 별도로 지정할 수 있고 별도로 지정하지 않은 경우 PyTorch가 알아서 변수에 맞게 type을 지정

  • Scalar 형태도 가능 (scalar, array 모두 가능)

  • 입력받은 데이터를 새로운 메모리 공간에 복사해 Tensor 객체 생성 (call by value)


Tensor Type

자주 사용하는 Type

Data typedtypeCPU tensorGPU tensor
32-bit floating pointtorch.float32 or torch.floattorch.FloatTensortorch.cuda.FloatTensor
64-bit floating pointtorch.float64 or torch.doubletorch.DoubleTensortorch.cuda.DoubleTensor
16-bit floating pointtorch.float16 or torch.halftorch.HalfTensortorch.cuda.HalfTensor
16-bit floating pointtorch.bfloat16torch.BFloat16Tensortorch.cuda.BFloat16Tensor
8-bit integer (unsigned)torch.uint8torch.ByteTensortorch.cuda.ByteTensor
8-bit integer (signed)torch.int8torch.CharTensortorch.cuda.CharTensor
16-bit integer (signed)torch.int16 or torch.shorttorch.ShortTensortorch.cuda.ShortTensor
32-bit integer (signed)torch.int32 or torch.inttorch.IntTensortorch.cuda.IntTensor
64-bit integer (signed)torch.int64 or torch.longtorch.LongTensortorch.cuda.LongTensor
Booleantorch.booltorch.BoolTensortorch.cuda.BoolTensor
  • 실수형(Float)의 경우 부호를 따지지 않고 Floating Point에 따라 half(16), float(32), double(64)로 나뉜다.
  • 정수형(Int)의 경우 8bit unsigned를 제외하고 나머지 모두 signed(부호가있는)이며, 8,16,32,64 비트로 구분된다.
  • 메모리 여유에 따라 데이터 자체의 유효범위를 조절하면 된다.

그외

Data typedtypeCPU tensorGPU tensor
32-bit complextorch.complex32 or torch.chalf
64-bit complextorch.complex64 or torch.cfloat
128-bit complextorch.complex128 or torch.cdouble
quantized 8-bit integer (unsigned)torch.quint8torch.ByteTensor
quantized 8-bit integer (signed)torch.qint8torch.CharTensor
quantized 32-bit integer (signed)torch.qint32torch.IntTensor
quantized 4-bit integer (unsigned)torch.quint4x2torch.ByteTensor
profile
딥러닝 지식의 백지에서 깜지까지

0개의 댓글