请我喝杯咖啡☕
*备忘录:
我的帖子解释了 unflatten()。我的帖子解释了 flatten() 和 ravel()。我的帖子解释了 flatten()。
unflatten() 可以向零个或多个元素的一维或多个 d 张量添加零个或多个维度,得到零个或多个元素的一维或多个 d 张量,如下所示:
*备忘录:
初始化的第一个参数是dim(required-type:int)。初始化的第二个参数是 unflattened_size(必需类型:元组或 int 列表)。第一个参数是输入(必需类型:int、float、plex 或 bool 的张量)。 *-1 推断并调整大小。unflatten() 和 unflatten() 的区别是:unflatten() 具有 unflattened_size 参数,该参数与 unflatten() 的 size 参数相同。基本上,unflatten() 用于定义模型,而 unflatten() 不用于定义模型。
import torchfrom torch import nnunflatten = nn.Unflatten()unflatten# Unflatten(dim=0, unflattened_size=(6,))unflatten.dim# 0unflatten.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]]])etcmy_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 中展开的详细内容,更多请关注范的资源库其它相关文章!
转载请注明:范的资源库 » 在PyTorch中展开