点乘/hadamard product

使用 torch.mul

  1. In [14]: import numpy as np
  2. In [15]: a = torch.from_numpy(np.arange(12).reshape(3, 4))
  3. In [16]: a
  4. Out[16]:
  5. tensor([[ 0, 1, 2, 3],
  6. [ 4, 5, 6, 7],
  7. [ 8, 9, 10, 11]], dtype=torch.int32)
  8. In [17]: b = torch.from_numpy(np.arange(12).reshape(3, 4))
  9. In [18]: c = torch.mul(a, b)
  10. In [19]: c
  11. Out[19]:
  12. tensor([[ 0, 1, 4, 9],
  13. [ 16, 25, 36, 49],
  14. [ 64, 81, 100, 121]], dtype=torch.int32)

使用 torch.mm 进行矩阵乘法matrix multiply。

  1. In [21]: a
  2. Out[21]:
  3. tensor([[ 0, 1, 2, 3],
  4. [ 4, 5, 6, 7],
  5. [ 8, 9, 10, 11]], dtype=torch.int32)
  6. In [22]: b = torch.from_numpy(np.arange(12).reshape(4, 3))
  7. In [23]: d = torch.mm(a, b)
  8. In [24]: d
  9. Out[24]:
  10. tensor([[ 42, 48, 54],
  11. [114, 136, 158],
  12. [186, 224, 262]], dtype=torch.int32)