둘다 data(input)에 대해서 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)
Function 형태
괄호 안에 data를 input으로 받아 tensor형태로 변환 시켜주는 기능
torch.tensor()는 항상 data를 복사하기 때문에 괄호안에 data를 넣어주지 않으면 TypeError가 발생
int를 입력시 int 형태 그대로 출력
type을 별도로 지정할 수 있고 별도로 지정하지 않은 경우 PyTorch가 알아서 변수에 맞게 type을 지정
Scalar 형태도 가능 (scalar, array 모두 가능)
입력받은 데이터를 새로운 메모리 공간에 복사해 Tensor 객체 생성 (call by value)
Data type | dtype | CPU tensor | GPU tensor |
---|---|---|---|
32-bit floating point | torch.float32 or torch.float | torch.FloatTensor | torch.cuda.FloatTensor |
64-bit floating point | torch.float64 or torch.double | torch.DoubleTensor | torch.cuda.DoubleTensor |
16-bit floating point | torch.float16 or torch.half | torch.HalfTensor | torch.cuda.HalfTensor |
16-bit floating point | torch.bfloat16 | torch.BFloat16Tensor | torch.cuda.BFloat16Tensor |
8-bit integer (unsigned) | torch.uint8 | torch.ByteTensor | torch.cuda.ByteTensor |
8-bit integer (signed) | torch.int8 | torch.CharTensor | torch.cuda.CharTensor |
16-bit integer (signed) | torch.int16 or torch.short | torch.ShortTensor | torch.cuda.ShortTensor |
32-bit integer (signed) | torch.int32 or torch.int | torch.IntTensor | torch.cuda.IntTensor |
64-bit integer (signed) | torch.int64 or torch.long | torch.LongTensor | torch.cuda.LongTensor |
Boolean | torch.bool | torch.BoolTensor | torch.cuda.BoolTensor |
Data type | dtype | CPU tensor | GPU tensor |
---|---|---|---|
32-bit complex | torch.complex32 or torch.chalf | ||
64-bit complex | torch.complex64 or torch.cfloat | ||
128-bit complex | torch.complex128 or torch.cdouble | ||
quantized 8-bit integer (unsigned) | torch.quint8 | torch.ByteTensor | |
quantized 8-bit integer (signed) | torch.qint8 | torch.CharTensor | |
quantized 32-bit integer (signed) | torch.qint32 | torch.IntTensor | |
quantized 4-bit integer (unsigned) | torch.quint4x2 | torch.ByteTensor |