在 PyTorch 中展開

Linda Hamilton
發布: 2024-11-06 14:38:02
原創
547 人瀏覽過

Unflatten in PyTorch

請我喝杯咖啡☕

*備忘錄:

  • 我的貼文解釋了 unflatten()。
  • 我的貼文解釋了 flatten() 和 ravel()。
  • 我的帖子解釋了 Flatten()。

Unflatten() 可以為零個或多個元素的一維或多個D 張量添加零個或多個維度,得到零個或多個元素的一維或多個D 張量,如下所顯示:

*備忘錄:

  • 初始化的第一個參數是dim(Required-Type:int)。
  • 初始化的第二個參數是 unflattened_size(必要型別:元組或 int 清單)。
  • 第一個參數是輸入(必要類型:int、float、complex 或 bool 的張量)。 *-1 推斷並調整大小。
  • Unflatten() 和 unflatten() 的差別是:
    • Unflatten() 具有 unflattened_size 參數,該參數與 unflatten() 的 size 參數相同。
    • 基本上,Unflatten() 用來定義模型,而 unflatten() 不用於定義模型。
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]])
登入後複製

以上是在 PyTorch 中展開的詳細內容。更多資訊請關注PHP中文網其他相關文章!

來源:dev.to
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
作者最新文章
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!