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_sampler.py 790 B

123456789101112131415161718192021222324252627282930
  1. import torch
  2. from fastNLP.core.sampler import convert_to_torch_tensor, SequentialSampler, RandomSampler
  3. def test_convert_to_torch_tensor():
  4. data = [[1, 2, 3, 4, 5], [5, 4, 3, 2, 1], [1, 3, 4, 5, 2]]
  5. ans = convert_to_torch_tensor(data, False)
  6. assert isinstance(ans, torch.Tensor)
  7. assert tuple(ans.shape) == (3, 5)
  8. def test_sequential_sampler():
  9. sampler = SequentialSampler()
  10. data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
  11. for idx, i in enumerate(sampler(data)):
  12. assert idx == i
  13. def test_random_sampler():
  14. sampler = RandomSampler()
  15. data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 10]
  16. ans = [data[i] for i in sampler(data)]
  17. assert len(ans) == len(data)
  18. for d in ans:
  19. assert d in data
  20. if __name__ == "__main__":
  21. test_sequential_sampler()