Rumah  >  Artikel  >  pembangunan bahagian belakang  >  Unflatten dalam PyTorch

Unflatten dalam PyTorch

Linda Hamilton
Linda Hamiltonasal
2024-11-06 14:38:02552semak imbas

Unflatten in PyTorch

Beli Saya Kopi☕

*Memo:

  • Siaran saya menerangkan unflatten().
  • Siaran saya menerangkan flatten() dan ravel().
  • Siaran saya menerangkan Flatten().

Unflatten() boleh menambah sifar atau lebih dimensi pada 1D atau lebih D tensor sifar atau lebih elemen, mendapatkan 1D atau lebih D tensor sifar atau lebih elemen seperti yang ditunjukkan di bawah:

*Memo:

  • Argumen pertama untuk permulaan ialah malap(Jenis-Jenis:int).
  • Argumen ke-2 untuk pemula ialah unflattened_size(Required-Type:tuple or list of int).
  • Argumen pertama ialah input(Required-Type:tensor of int, float, complex atau bool). *-1 menyimpulkan dan melaraskan saiz.
  • Perbezaan antara Unflatten() dan unflatten() ialah:
    • Unflatten() mempunyai hujah unflattened_size yang sama dengan hujah saiz unflatten().
    • Pada asasnya, Unflatten() digunakan untuk mentakrifkan model manakala unflatten() tidak digunakan untuk menentukan model.
import torch
from torch import nn

unflatten = nn.Unflatten()
unflatten
# Unflatten(dim=0, unflattened_size=(6,))

unflatten.dim
# 0

unflatten.unflattened_size
# (6,)

my_tensor = torch.tensor([7, 1, -8, 3, -6, 0])

unflatten = nn.Unflatten(dim=0, unflattened_size=(6,))
unflatten = nn.Unflatten(dim=0, unflattened_size=(-1,))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(6,))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1,))
unflatten(input=my_tensor)
# tensor([7, 1, -8, 3, -6, 0])

unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 6))
unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 6))
unflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 6))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 6))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1))
unflatten(input=my_tensor)
# tensor([[7, 1, -8, 3, -6, 0]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(2, 3))
unflatten = nn.Unflatten(dim=0, unflattened_size=(2, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(2, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(2, -1))
unflatten(input=my_tensor)
# tensor([[7, 1, -8], [3, -6, 0]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(3, 2))
unflatten = nn.Unflatten(dim=0, unflattened_size=(3, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, 2))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, -1))
unflatten(input=my_tensor)
# tensor([[7, 1], [-8, 3], [-6, 0]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(6, 1))
unflatten = nn.Unflatten(dim=0, unflattened_size=(6, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(6, 1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(6, -1))
unflatten(input=my_tensor)
# tensor([[7], [1], [-8], [3], [-6], [0]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2, 3))
unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 2, 3))
unflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1, 3))
unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 2, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 2, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 2, -1))
unflatten(input=my_tensor)
# tensor([[[7, 1, -8], [3, -6, 0]]])
etc

my_tensor = torch.tensor([[7, 1, -8], [3, -6, 0]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(2,))
unflatten = nn.Unflatten(dim=0, unflattened_size=(-1,))
unflatten = nn.Unflatten(dim=1, unflattened_size=(3,))
unflatten = nn.Unflatten(dim=1, unflattened_size=(-1,))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(3,))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1,))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(2,))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(-1,))
unflatten(input=my_tensor)
# tensor([[7, 1, -8], [3, -6, 0]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 2))
unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 2))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 2))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(-1, 2))
unflatten(input=my_tensor)
# tensor([[[7, 1, -8], [3, -6, 0]]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(2, 1))
unflatten = nn.Unflatten(dim=0, unflattened_size=(2, -1))
unflatten = nn.Unflatten(dim=1, unflattened_size=(1, 3))
unflatten = nn.Unflatten(dim=1, unflattened_size=(-1, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 3))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(2, 1))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(2, -1))
unflatten(input=my_tensor)
# tensor([[[7, 1, -8]], [[3, -6, 0]]])

unflatten = nn.Unflatten(dim=1, unflattened_size=(3, 1))
unflatten = nn.Unflatten(dim=1, unflattened_size=(3, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, 1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(3, -1))
unflatten(input=my_tensor)
# tensor([[[7], [1], [-8]], [[3], [-6], [0]]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 1, 2))
unflatten = nn.Unflatten(dim=0, unflattened_size=(-1, 1, 2))
unflatten = nn.Unflatten(dim=0, unflattened_size=(1, -1, 2))
unflatten = nn.Unflatten(dim=0, unflattened_size=(1, 1, -1))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 1, 2))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(-1, 1, 2))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, -1, 2))
unflatten = nn.Unflatten(dim=-2, unflattened_size=(1, 1, -1))
unflatten(input=my_tensor)
# tensor([[[[7, 1, -8], [3, -6, 0]]]])

unflatten = nn.Unflatten(dim=1, unflattened_size=(1, 1, 3))
unflatten = nn.Unflatten(dim=1, unflattened_size=(-1, 1, 3))
unflatten = nn.Unflatten(dim=1, unflattened_size=(1, -1, 3))
unflatten = nn.Unflatten(dim=1, unflattened_size=(1, 1, -1))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 1, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(-1, 1, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, -1, 3))
unflatten = nn.Unflatten(dim=-1, unflattened_size=(1, 1, -1))
unflatten(input=my_tensor)
# tensor([[[[7, 1, -8]]], [[[3, -6, 0]]]])

my_tensor = torch.tensor([[7., 1., -8.], [3., -6., 0.]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(2,))
unflatten(input=my_tensor)
# tensor([[7., 1., -8.], [3., -6., 0.]])

my_tensor = torch.tensor([[7.+0.j, 1.+0.j, -8.+0.j],
                          [3.+0.j, -6.+0.j, 0.+0.j]])
unflatten = nn.Unflatten(dim=0, unflattened_size=(2,))
unflatten(input=my_tensor)
# tensor([[7.+0.j, 1.+0.j, -8.+0.j],
#         [3.+0.j, -6.+0.j, 0.+0.j]])

my_tensor = torch.tensor([[True, False, True], [False, True, False]])

unflatten = nn.Unflatten(dim=0, unflattened_size=(2,))
unflatten(input=my_tensor)
# tensor([[True, False, True], [False, True, False]])

Atas ialah kandungan terperinci Unflatten dalam PyTorch. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!

Kenyataan:
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn