Unflatten#
- class torch.nn.Unflatten(dim, unflattened_size)[source]#
将张量的某个维度解扁平化,将其扩展到所需的形状。与
Sequential
一起使用。dim
指定要解扁平化的输入张量的维度,当分别使用 Tensor 或 NamedTensor 时,它可以是 int 或 str。unflattened_size
是张量解扁平化维度的新的形状,对于 Tensor 输入,它可以是 tuple of ints 或 list of ints 或 torch.Size;对于 NamedTensor 输入,它可以是 NamedShape(由 (name, size) 元组组成的元组)。
- 形状
输入: ,其中 是维度
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])