テンソル化
 テンソル作成
 サイズ確認
 乱数の生成

 機能・要件 
 構成・方式
 タスク
 導入
 sample

 テンソル化
transform = transforms.Compose([transforms.ToTensor()])
data = np.array([np.array([0 for i in range(10)]),
np.array([1 for i in range(10)]),
np.array([2 for i in range(10)]),
np.array([3 for i in range(10)]),
np.array([4 for i in range(10)]),
np.array([5 for i in range(10)]),
np.array([6 for i in range(10)]),
np.array([7 for i in range(10)]),
np.array([8 for i in range(10)]),
np.array([9 for i in range(10)])])
label = np.array([i for i in range(10)])
print(data)
print(label)
[[0 0 0 0 0 0 0 0 0 0]
[1 1 1 1 1 1 1 1 1 1]
[2 2 2 2 2 2 2 2 2 2]
[3 3 3 3 3 3 3 3 3 3]
[4 4 4 4 4 4 4 4 4 4]
[5 5 5 5 5 5 5 5 5 5]
[6 6 6 6 6 6 6 6 6 6]
[7 7 7 7 7 7 7 7 7 7]
[8 8 8 8 8 8 8 8 8 8]
[9 9 9 9 9 9 9 9 9 9]]
[0 1 2 3 4 5 6 7 8 9]
transform = transforms.Compose([transforms.ToTensor()])
print(transform(data))
tensor([[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]]], dtype=torch.int32)
dataloader_shuffle = torch.utils.data.DataLoader(dataset, batch_size=2, shuffle=True)
dataloader_nonshuffle = torch.utils.data.DataLoader(dataset, batch_size=2, shuffle=False)
print('shuffle batch_size=2')
for i in dataloader_shuffle:
print(i)
[tensor([[5, 5, 5, 5, 5, 5, 5, 5, 5, 5],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], dtype=torch.int32), tensor([5, 3], dtype=torch.int32)]
[tensor([[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6]], dtype=torch.int32), tensor([8, 6], dtype=torch.int32)]
[tensor([[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]], dtype=torch.int32), tensor([2, 0], dtype=torch.int32)]
[tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]], dtype=torch.int32), tensor([1, 9], dtype=torch.int32)]
[tensor([[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]], dtype=torch.int32), tensor([4, 7], dtype=torch.int32)]
print('nonshuffle batch_size=2')
for i in dataloader_nonshuffle:
print(i)
[tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1]], dtype=torch.int32), tensor([0, 1], dtype=torch.int32)]
[tensor([[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], dtype=torch.int32), tensor([2, 3], dtype=torch.int32)]
[tensor([[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]], dtype=torch.int32), tensor([4, 5], dtype=torch.int32)]
[tensor([[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]], dtype=torch.int32), tensor([6, 7], dtype=torch.int32)]
[tensor([[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]], dtype=torch.int32), tensor([8, 9], dtype=torch.int32)]
dataloader_shuffle = torch.utils.data.DataLoader(dataset, batch_size=3, shuffle=True)
dataloader_nonshuffle = torch.utils.data.DataLoader(dataset, batch_size=2, shuffle=False)
print('shuffle batch_size=3')
for i in dataloader_shuffle:
print(i)
[tensor([[8, 8, 8, 8, 8, 8, 8, 8, 8, 8],
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[3, 3, 3, 3, 3, 3, 3, 3, 3, 3]], dtype=torch.int32), tensor([8, 0, 3], dtype=torch.int32)]
[tensor([[9, 9, 9, 9, 9, 9, 9, 9, 9, 9],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]], dtype=torch.int32), tensor([9, 2, 5], dtype=torch.int32)]
[tensor([[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7]], dtype=torch.int32), tensor([1, 6, 7], dtype=torch.int32)]
[tensor([[4, 4, 4, 4, 4, 4, 4, 4, 4, 4]], dtype=torch.int32), tensor([4], dtype=torch.int32)]
print('nonshuffle batch_size=3')
for i in dataloader_nonshuffle:
print(i)
[tensor([[0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1],
[2, 2, 2, 2, 2, 2, 2, 2, 2, 2]], dtype=torch.int32), tensor([0, 1, 2], dtype=torch.int32)]
[tensor([[3, 3, 3, 3, 3, 3, 3, 3, 3, 3],
[4, 4, 4, 4, 4, 4, 4, 4, 4, 4],
[5, 5, 5, 5, 5, 5, 5, 5, 5, 5]], dtype=torch.int32), tensor([3, 4, 5], dtype=torch.int32)]
[tensor([[6, 6, 6, 6, 6, 6, 6, 6, 6, 6],
[7, 7, 7, 7, 7, 7, 7, 7, 7, 7],
[8, 8, 8, 8, 8, 8, 8, 8, 8, 8]], dtype=torch.int32), tensor([6, 7, 8], dtype=torch.int32)]
[tensor([[9, 9, 9, 9, 9, 9, 9, 9, 9, 9]], dtype=torch.int32), tensor([9], dtype=torch.int32)]

 テンソルの作成 (多次元配列(multi-dimensional matrix)の作成)
python3
>>> import torch
>>> x = torch.Tensor(3, 2)  (Tensor は 型が自動で float)
>>> print(x)
tensor([[8.6580e-38, 0.0000e+00],
    [9.2216e-37, 0.0000e+00],
    [5.0000e+00, 6.0000e+00]])
>>> list = [[1,2,3],[4,5,6]]
>>> x2 = torch.Tensor(list)
>>> print(x2)
tensor([[1., 2., 3.],
    [4., 5., 6.]])

 サイズ確認
python3
>>> import numpy as np
>>> list = [[1,2,3],[4,5,6]]
>>> x2 = torch.Tensor(list)
>>> x2.size()
torch.Size([2, 3])

 乱数の生成
python3
>>> import torch
>>> torch.rand(2,2)
tensor([[0.3940, 0.1066],
    [0.7057, 0.7686]])