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_onehot.py 633 B

1234567891011121314151617181920212223242526
  1. import numpy as np
  2. import pytest
  3. import megengine.functional as F
  4. from megengine import tensor
  5. from megengine.test import assertTensorClose
  6. def test_onehot_low_dimension():
  7. inp = tensor(np.arange(1, 4, dtype=np.int32))
  8. out = F.one_hot(inp)
  9. assertTensorClose(
  10. out.numpy(), np.eye(4, dtype=np.int32)[np.arange(1, 4, dtype=np.int32)]
  11. )
  12. def test_onehot_high_dimension():
  13. arr = np.array(
  14. [[3, 2, 4, 4, 2, 4, 0, 4, 4, 1], [4, 1, 1, 3, 2, 2, 4, 2, 4, 3]], dtype=np.int32
  15. )
  16. inp = tensor(arr)
  17. out = F.one_hot(inp, 10)
  18. assertTensorClose(out.numpy(), np.eye(10, dtype=np.int32)[arr])

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