gpt4 book ai didi

torch - Torch 中的 setNumInputDims 应该做什么?

转载 作者:行者123 更新时间:2023-12-05 05:21:32 25 4
gpt4 key购买 nike

 minibatch = torch.Tensor(5, 2, 3,5)
m = nn.View(-1):setNumInputDims(1)

m:forward(minibatch)

给出一个大小的张量

 30x5

m = nn.View(-1):setNumInputDims(3)
m:forward(minibatch)

给出一个大小的张量

 5 x 30

m = nn.View(-1):setNumInputDims(2)
m:forward(minibatch)

给出一个大小的张量

 10 x 15

这是怎么回事?我不明白为什么我会得到我现在的尺寸。我认为我不理解的原因是我认为 View m 期望 n dims 作为输入。因此,如果 n = 1,那么我们将 5 作为第一个 dim,将 30 作为第二个 dim,这似乎是当 numInputDims 设置为 2 时发生的情况。

最佳答案

View(-1):setNumInputDims(n)顾名思义就是设置View(-1)的输入维数。

了解View(-1)的作用,请引用How view() method works for tensor in torch

If there is any situation that you don't know how many rows you want but are sure of the number of columns then you can mention it as -1(You can extend this to tensors with more dimensions. Only one of the axis value can be -1). This is a way of telling the library; give me a tensor that has these many columns and you compute the appropriate number of rows that is necessary to make this happen.

因此 View(-1) 将输入转换为二维矩阵。注意 View(-1) 对应于该矩阵的列。因此它的输入维度是完整输入的后半部分。它的维度数表示为列“分配”了多少维度,以及这些维度之前的任何维度用于行。

因此在您的示例中:

minibatch = torch.Tensor(5, 2, 3,5)
m = nn.View(-1):setNumInputDims(2)

它将最后两个维度 (3*5) 分配给列,将前两个维度 (5*2) 分配给行。结果张量为 10*15。

关于torch - Torch 中的 setNumInputDims 应该做什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42795554/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com