dictionary

Tensor creation and initialization

tensor

주어진 데이터로부터 텐서를 생성함.

torch.tensor(data, *, dtype=None, device=None, requires_grad=False, pin_memory=False)

# 1. a simple 1-dimensional tensor living on cuda
torch.tensor([0, 1], dtype=torch.int, device="cpu")

# 2. a 2x2 fp tensor that requires grad
torch.tensor([[0.0, 1.0], [1.0, 2.0]], requires_grad=True)

Tensor, LongTensor

Tensor : float32 datatype의 텐서를 생성함.

t = torch.Tensor([1, 2])
t.dtype #torch.float32
s = torch.tensor([1., 2.], dtype=torch.float32) # <--이거랑 동일함

LongTensor: int64 datatype의 텐서를 생성함

t = torch.LongTensor([[1, 2, 3], [4, 5, 6]])
t.dtype # torch.int64
s = torch.LongTensor([[1, 2, 3], [4, 5, 6]], dtype=torch.int64) # <-이거랑 동일함

fill_(), uniform_(), normal_()

주로 텐서의 값을 초기화 하는데 사용되는 함수들 →

t = torch.zeros(2, 2, 3).fill_(3.)
t = torch.zeros(20).uniform_(0, 1)
t = torch.zeros(20).normal_(mean=0, std=0.01)