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_zero_dim_tensor.py 1.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import numpy as np
  2. import megengine.functional as F
  3. from megengine import Tensor
  4. from megengine.core._trace_option import use_symbolic_shape
  5. def test_zero_dim():
  6. a = Tensor(1)
  7. a_np = np.array(1, dtype=np.int32)
  8. np.testing.assert_equal(a, a_np)
  9. if use_symbolic_shape():
  10. np.testing.assert_equal(a.shape, np.array(a_np.shape))
  11. else:
  12. np.testing.assert_equal(a.shape, a_np.shape)
  13. def test_sum():
  14. a = Tensor([1, 2])
  15. a = a.reshape((1, 2))
  16. assert a.sum().ndim == 0
  17. assert a.sum(axis=1).ndim == 1
  18. def test_max():
  19. a = Tensor([1, 2])
  20. a = a.reshape((1, 2))
  21. assert a.max().ndim == 0
  22. assert a.max(axis=1).ndim == 1
  23. def test_reshape():
  24. a = Tensor(1)
  25. a = a.reshape((1, 1))
  26. def test_squeeze():
  27. a = Tensor(1)
  28. a = a.reshape((1, 1))
  29. assert F.squeeze(a).ndim == 0
  30. def test_elemementwise():
  31. a = Tensor(1.0)
  32. assert F.exp(a).ndim == 0
  33. assert (a + a).ndim == 0
  34. assert (a + 1).ndim == 0
  35. def test_astype():
  36. a = Tensor(1.0)
  37. assert a.astype("int32").ndim == 0

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台