matrix - PyTorch - nn.Linear 权重的形状

标签 matrix neural-network deep-learning matrix-multiplication pytorch

昨天我遇到了this question并且第一次注意到线性层的权重 nn.Linear 申请前需要转置matmul .

Code应用权重:

output = input.matmul(weight.t())

这是什么原因?

为什么权重不是从一开始就在转置形状中,所以它们不需要在应用图层之前每次都转置?

最佳答案

我在这里找到了答案:
Efficient forward pass in nn.Linear #2159

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

根据上述问题,在期间转发 通过转置操作 (几乎)免费 在计算方面。而在反向传球期间省略转置操作 实际上会进行计算 效率较低 与当前的实现。

该问题的最后一篇文章对其进行了很好的总结:

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/issues/2159#issuecomment-390068272

关于matrix - PyTorch - nn.Linear 权重的形状,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53465608/

相关文章:

computer-vision - 从航拍图像绘制房屋轮廓

python - 制作一个类进程 A 2 by 2 矩阵并有问题通过 __str__ 返回它

python - 除以矩阵的向量列

c++ - 使用迭代方法计算矩阵的行列式

c - 使用 MPI-IO 读取多个文件

image-processing - 训练此数据的训练算法

machine-learning - 有关使用神经网络进行 Q-Learning 的问题

python - 在tensorflow中加载多个DNN模型并多次使用它们

c++ - TensorFlow 0.12 模型文件

machine-learning - 如何同时使用交叉验证和提前停止?