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

device_resident_accept(visitor)[source]

Applies the visitor to all the device objects in this instance.

Parameters

visitor (DeviceResidentsVisitor) – Visitor.

This method should be overridden if the concrete class has custom sub-hierarchy of device resident objects.

from_chx()[source]

Converts parameter variables and persistent values from ChainerX to NumPy/CuPy devices without any copy.

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_chx()[source]

Converts parameter variables and persistent values to ChainerX without any copy.

This method does not handle non-registered attributes. If some of such attributes must be copied to ChainerX, the link implementation must override this method to do so.

Returns: self

to_cpu() chainer.device_resident.DeviceResident[source]

Copies parameter variables and persistent values to CPU.

Deprecated since version v7.0.0: Use to_device() instead.

This method does not handle non-registered attributes. If some of such attributes must be copied to CPU, the link implementation should override device_resident_accept() to do so.

Returns: self

to_device(device: Union[backend.Device, chainerx.Device, cuda.Device, str, Tuple[str, int], ModuleType, Tuple[ModuleType, int]]) DeviceResident[source]

Copies parameter variables and persistent values to the specified device.

This method does not handle non-registered attributes. If some of such attributes must be copied to the device, the link implementation must override this method to do so.

Parameters

device – Target device specifier. See get_device() for available values.

Returns: self

to_gpu(device: Optional[Union[chainer.backends.cuda.Device, int, numpy.integer]] = None) chainer.device_resident.DeviceResident[source]

Copies parameter variables and persistent values to GPU.

Deprecated since version v7.0.0: Use to_device() instead.

This method does not handle non-registered attributes. If some of such attributes must be copied to GPU, the link implementation must override device_resident_accept() to do so.

Warning

This method does not transfer the parameters if they are already on GPU. Use to_device to perform inter-GPU transfer.

Parameters

device – Target device specifier. If omitted, the current device is used.

Returns: self

to_intel64() chainer.device_resident.DeviceResident[source]

Copies parameter variables and persistent values to CPU.

Deprecated since version v7.0.0: Use to_device() instead.

__eq__(value, /)

Return self==value.

__ne__(value, /)

Return self!=value.

__lt__(value, /)

Return self<value.

__le__(value, /)

Return self<=value.

__gt__(value, /)

Return self>value.

__ge__(value, /)

Return self>=value.

Attributes

device

Device instance.

use_gpu
xp

Array module corresponding to the device.

Depending on the device in which this object resides, this property returns numpy, cupy or chainerx.