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_tensor_wrapper.py 1.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. # -*- coding: utf-8 -*-
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import numpy as np
  10. from megengine.core.tensor.tensor_wrapper import TensorWrapper
  11. def test_basic():
  12. x_np = np.random.rand(10).astype("float32")
  13. x = TensorWrapper(x_np)
  14. y = x * x
  15. y_np = y.numpy()
  16. np.testing.assert_almost_equal(y_np, x_np * x_np)
  17. def test_literal_arith():
  18. x_np = np.random.rand(10).astype("float32")
  19. x = TensorWrapper(x_np)
  20. y = x * 2
  21. y_np = y.numpy()
  22. np.testing.assert_almost_equal(y_np, x_np * 2)
  23. def test_matmul():
  24. A = TensorWrapper(np.random.rand(5, 7).astype("float32"))
  25. B = TensorWrapper(np.random.rand(7, 10).astype("float32"))
  26. C = A @ B
  27. np.testing.assert_almost_equal(C.numpy(), A.numpy() @ B.numpy(), decimal=6)
  28. def test_reduce():
  29. for m in ["sum", "prod", "min", "max", "mean"]:
  30. x_np = np.random.rand(10).astype("float32")
  31. x = TensorWrapper(x_np)
  32. y = getattr(x, m)(-1)
  33. np.testing.assert_almost_equal(y.numpy(), getattr(x_np, m)(-1), decimal=6)
  34. def test_set_subtensor():
  35. x = TensorWrapper([1, 2, 3])
  36. x[:] = [1, 1, 1]
  37. np.testing.assert_almost_equal(x.numpy(), [1, 1, 1], decimal=6)
  38. x[[0, 2]] = [3, 2]
  39. np.testing.assert_almost_equal(x.numpy(), [3, 1, 2], decimal=6)
  40. x[1:3] = [4, 5]
  41. np.testing.assert_almost_equal(x.numpy(), [3, 4, 5], decimal=6)

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