import torch
t_array = torch.FloatTensor(n_array)
print(t_array)
print("ndim :", t_array.ndim, "shape :", t_array.shape)
data = [[3, 5, 20],[10, 5, 50], [1, 5, 10]]
x_data = torch.tensor(data)
x_data[1:]
# tensor([[10, 5, 50],
# [ 1, 5, 10]])
x_data[:2, 1:]
# tensor([[ 5, 20],
# [ 5, 50]])
x_data.flatten()
# tensor([ 3, 5, 20, 10, 5, 50, 1, 5, 10])
torch.ones_like(x_data)
# tensor([[1, 1, 1],
# [1, 1, 1],
# [1, 1, 1]])
x_data.numpy()
# array([[ 3, 5, 20],
# [10, 5, 50],
# [ 1, 5, 10]], dtype=int64)
x_data.shape
# torch.Size([3, 3])
x_data.dtype
# torch.int64
x_data.device
# device(type='cpu')
if torch.cuda.is_available(): #가능하면 cuda(gpu)에 올려서 실행
x_data_cuda = x_data.to('cuda')
x_data_cuda.device
# device(type='cuda', index=0)
추가로 view, squeeze, unsqueeze 등으로 tensor 조정이 가능하다.
tensor_ex = torch.rand(size=(2, 1, 2))
tensor_ex.squeeze()
# tensor([[0.8510, 0.8263],
# [0.7602, 0.1309]])
tensor_ex = torch.rand(size=(2, 2))
tensor_ex.unsqueeze(0).shape
# torch.Size([1, 2, 2])
tensor_ex.unsqueeze(1).shape
# torch.Size([2, 1, 2])
tensor_ex.unsqueeze(2).shape
# torch.Size([2, 2, 1]) https://bit
PyTorch의 핵심은 자동 미분의 지원 → backward 함수 사용