定義:PyTorchにおけるview関数
以下に使用例を示す.
import torch
# 3x2x2のランダムテンソルを作成
r3 = torch.randn((3, 2, 2))
print("元のテンソル形状:", r3.shape)
print("元のテンソル形状:", r3) # torch.Size([3, 2, 2])
# viewを使って形状を変更する例
r3_reshaped = r3.view(12) # 1次元に平坦化
print("1次元化:", r3_reshaped.shape)
print("1次元化:", r3_reshaped) # torch.Size([12])
r3_reshaped2 = r3.view(3, 4) # 3x4に変形
print("3x4に変形:", r3_reshaped2.shape)
print("3x4に変形:", r3_reshaped2) # torch.Size([3, 4])
r3_reshaped3 = r3.view(-1, 2) # サイズを自動計算(6x2)
print("自動計算で変形:", r3_reshaped3.shape)
print("自動計算で変形:", r3_reshaped3) # torch.Size([6, 2])
以上により,
元のテンソル形状: tensor([[[ 0.5661, -0.3820],
[ 0.8807, 0.2710]],
[[ 0.7694, 0.3453],
[ 1.8979, -0.2357]],
[[ 0.7885, 0.3208],
[ 0.8456, -0.3621]]])
1次元化: tensor([ 0.5661, -0.3820, 0.8807, 0.2710, 0.7694, 0.3453, 1.8979, -0.2357,
0.7885, 0.3208, 0.8456, -0.3621])
3x4に変形: tensor([[ 0.5661, -0.3820, 0.8807, 0.2710],
[ 0.7694, 0.3453, 1.8979, -0.2357],
[ 0.7885, 0.3208, 0.8456, -0.3621]])
自動計算で変形: tensor([[ 0.5661, -0.3820],
[ 0.8807, 0.2710],
[ 0.7694, 0.3453],
[ 1.8979, -0.2357],
[ 0.7885, 0.3208],
[ 0.8456, -0.3621]])
Mathematics is the language with which God has written the universe.