You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_nn_matmul.py 1.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. import numpy as np
  2. import mindspore.context as context
  3. import mindspore.nn as nn
  4. from mindspore import Tensor
  5. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  6. class Net(nn.Cell):
  7. def __init__(self, transpose_x1, transpose_x2):
  8. super(Net, self).__init__()
  9. self.matmul = nn.MatMul(transpose_x1, transpose_x2)
  10. def construct(self, x1, x2):
  11. return self.matmul(x1, x2)
  12. def test_x1_2D_x2_3D():
  13. x1 = np.random.randn(16, 64).astype(np.float32)
  14. x2 = np.random.randn(32, 64, 20).astype(np.float32)
  15. transpose_x1 = False
  16. transpose_x2 = False
  17. net = Net(transpose_x1, transpose_x2)
  18. output = net(Tensor(x1), Tensor(x2))
  19. assert output.shape == (32, 16, 20)
  20. def test_x1_4D_x2_3D_transpose_x2_True():
  21. x1 = np.random.randn(3, 2, 3, 4).astype(np.float32)
  22. x2 = np.random.randn(1, 5, 4).astype(np.float32)
  23. transpose_x1 = False
  24. transpose_x2 = True
  25. net = Net(transpose_x1, transpose_x2)
  26. output = net(Tensor(x1), Tensor(x2))
  27. assert output.shape == (3, 2, 3, 5)
  28. def test_x1_3D_transpose_x1_True_x2_2D():
  29. x1 = np.random.randn(2, 3, 4).astype(np.float32)
  30. x2 = np.random.randn(3, 4).astype(np.float32)
  31. transpose_x1 = True
  32. transpose_x2 = False
  33. net = Net(transpose_x1, transpose_x2)
  34. output = net(Tensor(x1), Tensor(x2))
  35. assert output.shape == (2, 4, 4)
  36. def test_x1_3D_transpose_x1_True_x2_3D_transpose_x2_True():
  37. x1 = np.random.randn(2, 5, 6).astype(np.float32)
  38. x2 = np.random.randn(2, 4, 5).astype(np.float32)
  39. transpose_x1 = True
  40. transpose_x2 = True
  41. net = Net(transpose_x1, transpose_x2)
  42. output = net(Tensor(x1), Tensor(x2))
  43. assert output.shape == (2, 6, 4)