且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

PyTorch - nn.Linear 权重的形状

更新时间:2023-12-01 23:24:16

我在这里找到了答案:nn.Linear #2159 中的高效前向传递

I found an answer here: Efficient forward pass in nn.Linear #2159

这背后似乎没有真正的推理.然而转置操作似乎并没有减慢计算速度.

It seems like there is no real reasoning behind this. However the transpose operation doesn't seem to be slowing down the computation.

根据上面提到的问题,在转发过程中,转置操作在计算方面(几乎)是免费的.而在向后传递期间省略转置操作实际上会使当前实现的计算降低效率.

According to the issue mentioned above, during the forward pass the transpose operation is (almost) free in terms of computation. While during the backward pass leaving out the transpose operation would actually make computation less efficient with the current implementation.

该问题的最后一篇文章总结得很好:

The last post in that issue sums it up quite nicely:

这是历史权重布局,更改它是向后不兼容的.除非在速度或便利方面有很大的好处,否则我们不会破坏用户空间.

It's historical weight layout, changing it is backward-incompatible. Unless there is some BIG benefit in terms of speed or convenience, we wont break userland.

https://github.com/pytorch/pytorch/问题/2159#issuecomment-390068272