Unflatten¶
- 類 torch.nn.Unflatten(dim, unflattened_size)[源][源]¶
展開 tensor 的一個維度,將其擴充套件到所需的形狀。用於
Sequential。dim指定要展開的輸入 tensor 的維度,使用 Tensor 或 NamedTensor 時,它可以分別是 int 或 str。unflattened_size是展開後的 tensor 維度的新形狀,對於 Tensor 輸入,它可以是 tuple、list 或 torch.Size 的整數;對於 NamedTensor 輸入,它可以是 NamedShape(即 (名稱, 大小) 元組的元組)。
- 形狀
輸入: , 其中 是維度
dim的大小, 表示任意數量的維度(包括零個)。輸出: , 其中 =
unflattened_size且 。
- 引數
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]] 的別名