chainer.utils.WalkerAlias

class chainer.utils.WalkerAlias(probs)[source]

Implementation of Walker’s alias method.

This method generates a random sample from given probabilities \(p_1, \dots, p_n\) in \(O(1)\) time. It is more efficient than choice(). This class works on both CPU and GPU.

Parameters

probs (float list) – Probabilities of entries. They are normalized with sum(probs).

See: Wikipedia article

Methods

sample(shape)[source]

Generates a random sample based on given probabilities.

Parameters

shape (tuple of int) – Shape of a return value.

Returns

Returns a generated array with the given shape. If a sampler is in CPU mode the return value is a numpy.ndarray object, and if it is in GPU mode the return value is a cupy.ndarray object.

sample_gpu(shape)[source]
sample_xp(xp, shape)[source]
to_cpu()[source]

Make a sampler CPU mode.

to_device(device)[source]
to_gpu()[source]

Make a sampler GPU mode.

Attributes

device
use_gpu