mxnet.context.Context¶
-
class
mxnet.context.
Context
(device_type, device_id=0)[source]¶ Constructs a context.
MXNet can run operations on CPU and different GPUs. A context describes the device type and ID on which computation should be carried on.
One can use mx.cpu and mx.gpu for short.
See also
How to run MXNet on multiple CPU/GPUs <http://mxnet.io/faq/multi_devices.html> for more details.
- Parameters
device_type ({'cpu', 'gpu'} or Context.) – String representing the device type.
device_id (int (default=0)) – The device id of the device, needed for GPU.
Note
Context can also be used as a way to change the default context.
Examples
>>> # array on cpu >>> cpu_array = mx.nd.ones((2, 3)) >>> # switch default context to GPU(2) >>> with mx.Context(mx.gpu(2)): ... gpu_array = mx.nd.ones((2, 3)) >>> gpu_array.context gpu(2)
One can also explicitly specify the context when creating an array.
>>> gpu_array = mx.nd.ones((2, 3), mx.gpu(1)) >>> gpu_array.context gpu(1)
-
__init__
(device_type, device_id=0)[source]¶ Initialize self. See help(type(self)) for accurate signature.
Methods
__init__
(device_type[, device_id])Initialize self.
Attributes
default_ctx
device_type
Returns the device type of current context.
devstr2type
devtype2str