code: def test_random_choice_with_replacement(ctx, func_name, seed):
      trials = 1000000
      x = nn.Variable([100], need_grad=True)
      x.d = np.random.random(x.size).astype(np.float32)
      w = nn.Variable([x.size], need_grad=True)
      w.d = np.random.randint(1, 100, w.size)
      with nn.context_scope(ctx), nn.auto_forward(True):
          y = F.random_choice(
              x, w, shape=[trials], replace=True, seed=seed
          )
      hist_nn, _ = np.histogram(y.d)
      hist_np, _ = np.histogram(
          np.random.choice(
              x.d, trials, True, w.d / np.float32(w.d.sum())
          )
      )
      assert_allclose(hist_nn / trials, hist_np / trials, atol=1e-2)
      x.grad.zero()
      w.grad.zero()
      y.backward()
      assert_allclose(
          x.g / trials, w.d / np.float32(w.d.sum()), atol=1e-2
      )
      assert_allclose(
          w.g / trials, w.d / np.float32(w.d.sum()), atol=1e-2
      )
  
      x = nn.Variable.from_numpy_array(
          np.array([[1, 2, 3], [-1, -2, -3]])
      )
      w = nn.Variable.from_numpy_array(
          np.array([[1, 1, 1], [10, 10, 10]])
      )
      with nn.context_scope(ctx), nn.auto_forward():
          y = F.random_choice(
              x, w, shape=(10,), replace=True, seed=seed
          )
      assert (
          y.shape == (2, 10)
          and np.all(y.d[0] > 0)
          and np.all(y.d[1] < 0)
      )
  
      # Check recomputation
      from nbla_test_utils import recomputation_test
  
      x = nn.Variable([100], need_grad=True)
      x.d = np.random.random(x.size).astype(np.float32)
      w = nn.Variable([x.size], need_grad=True)
      w.d = np.random.randint(1, 100, w.size)
      rng = np.random.RandomState(0)
  
      vinputs = [x, w]
      func_kwargs = {"shape": (trials,), "replace": True, "seed": seed}
  
      recomputation_test(
          rng=rng,
          func=F.random_choice,
          vinputs=vinputs,
          func_args=[],
          func_kwargs=func_kwargs,
          ctx=ctx,
      )
  
      return
      x = nn.Variable((3, 3), need_grad=True)
      w = nn.Variable((3, 3), need_grad=True)
      w.d = np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])
      with nn.context_scope(ctx), nn.auto_forward(True):
          y = F.random_choice(x, w, shape=[10], replace=True, seed=seed)
      x.grad.zero()
      w.grad.zero()
      y.backward(1)
      assert np.all(
          x.g == np.array([[10, 0, 0], [0, 10, 0], [0, 0, 10]])
      )
      assert np.all(
          w.g == np.array([[10, 0, 0], [0, 10, 0], [0, 0, 10]])
      )
  
file_path: nnabla/python/test/function/test_random_choice.py
imports: import pytest
  import numpy
  import nnabla
  import nnabla.functions
  from nbla_test_utils import list_context
  from nnabla.testing import assert_allclose
label_desc: Use image node_id 1 for calling a global function with example usage: test_random_choice_with_replacement(ctx, func_name, seed) without return types