快捷方式

Unflatten

torch.nn.Unflatten(dim, unflattened_size)[源][源]

展開 tensor 的一個維度,將其擴充套件到所需的形狀。用於 Sequential

  • dim 指定要展開的輸入 tensor 的維度,使用 TensorNamedTensor 時,它可以分別是 intstr

  • unflattened_size 是展開後的 tensor 維度的新形狀,對於 Tensor 輸入,它可以是 tuplelisttorch.Size 的整數;對於 NamedTensor 輸入,它可以是 NamedShape(即 (名稱, 大小) 元組的元組)。

形狀
  • 輸入: (,Sdim,)(*, S_{\text{dim}}, *), 其中 SdimS_{\text{dim}} 是維度 dim 的大小,* 表示任意數量的維度(包括零個)。

  • 輸出: (,U1,...,Un,)(*, U_1, ..., U_n, *), 其中 UU = unflattened_sizei=1nUi=Sdim\prod_{i=1}^n U_i = S_{\text{dim}}

引數
  • dim (Union[int, str]) – 要展開的維度

  • unflattened_size (Union[torch.Size, Tuple, List, NamedShape]) – 展開後的維度的新形狀

示例

>>> input = torch.randn(2, 50)
>>> # With tuple of ints
>>> m = nn.Sequential(
>>>     nn.Linear(50, 50),
>>>     nn.Unflatten(1, (2, 5, 5))
>>> )
>>> output = m(input)
>>> output.size()
torch.Size([2, 2, 5, 5])
>>> # With torch.Size
>>> m = nn.Sequential(
>>>     nn.Linear(50, 50),
>>>     nn.Unflatten(1, torch.Size([2, 5, 5]))
>>> )
>>> output = m(input)
>>> output.size()
torch.Size([2, 2, 5, 5])
>>> # With namedshape (tuple of tuples)
>>> input = torch.randn(2, 50, names=('N', 'features'))
>>> unflatten = nn.Unflatten('features', (('C', 2), ('H', 5), ('W', 5)))
>>> output = unflatten(input)
>>> output.size()
torch.Size([2, 2, 5, 5])
NamedShape

tuple[tuple[str, int]] 的別名

文件

查閱 PyTorch 的全面開發者文件

檢視文件

教程

獲取針對初學者和高階開發者的深入教程

檢視教程

資源

查詢開發資源並獲得問題解答

檢視資源