2018-07-20-pytroch小知识
2018-07-20 本文已影响0人
陆小杰_642e
1. 将多个二维矩阵与一个二维矩阵相乘得到多个二维矩阵
- In numpy, when i have a 3D tensor X with shape [A, B, C] and a 2D tensor Y with shape [C, D], then np.dot(X, Y) gives a 3D tensor with shape [A, B, D].
In PyTorch, i can do this as below.
result = torch.mm(X.view(-1, C), Y)
result = result.view(-1, B, D)
[原问题链接](https://discuss.pytorch.org/t/how-can-i-compute-3d-tensor-2d-tensor-multiplication/639)
2. 批对角矩阵处理
- Hi !
I have a matrix n*m of n different vectors of dimensions m.
I would like to get n matrices of size m*m with each matrix being a diagonal of a vector.
I guess I could do:
d = []
for vec in mat:
d.append(torch.diag(vec))
torch.stack(d)
[问题链接](https://discuss.pytorch.org/t/batch-of-diagonal-matrix/13560)