gluon.Constant¶
-
class
mxnet.gluon.
Constant
(name, value)[source]¶ Bases:
mxnet.gluon.parameter.Parameter
A constant parameter for holding immutable tensors. Constant`s are ignored by `autograd and Trainer, thus their values will not change during training. But you can still update their values manually with the set_data method.
Constant s can be created with either:
const = mx.gluon.Constant('const', [[1,2],[3,4]])
Methods
cast
(dtype)Cast data and gradient of this Parameter to a new data type.
data
([ctx])Returns a copy of this parameter on one context.
grad
([ctx])Returns a gradient buffer for this parameter on one context.
initialize
([init, ctx, default_init, …])Initializes parameter and gradient arrays.
list_ctx
()Returns a list of contexts this parameter is initialized on.
Returns copies of this parameter on all contexts, in the same order as creation.
Returns gradient buffers on all contexts, in the same order as
values()
.list_row_sparse_data
(row_id)Returns copies of the ‘row_sparse’ parameter on all contexts, in the same order as creation.
reset_ctx
(ctx)Re-assign Parameter to other contexts.
row_sparse_data
(row_id)Returns a copy of the ‘row_sparse’ parameter on the same context as row_id’s.
set_data
(data)Sets this parameter’s value on all contexts.
var
()Returns a symbol representing this parameter.
Sets gradient buffer on all contexts to 0.
Attributes
The type of the parameter.
The shape of the parameter.
or:
class Block(gluon.Block): def __init__(self, **kwargs): super(Block, self).__init__(**kwargs) self.const = self.params.get_constant('const', [[1,2],[3,4]])
- Parameters
name (str) – Name of the parameter.
value (array-like) – Initial value for the constant.
-
cast
(dtype)¶ Cast data and gradient of this Parameter to a new data type.
- Parameters
dtype (str or numpy.dtype) – The new data type.
-
data
(ctx=None)¶ Returns a copy of this parameter on one context. Must have been initialized on this context before. For sparse parameters, use
Parameter.row_sparse_data()
instead.- Parameters
ctx (Context) – Desired context.
- Returns
- Return type
NDArray on ctx
-
property
dtype
¶ The type of the parameter.
Setting the dtype value is equivalent to casting the value of the parameter
-
grad
(ctx=None)¶ Returns a gradient buffer for this parameter on one context.
- Parameters
ctx (Context) – Desired context.
-
initialize
(init=None, ctx=None, default_init=<mxnet.initializer.Uniform object>, force_reinit=False)¶ Initializes parameter and gradient arrays. Only used for
NDArray
API.- Parameters
init (Initializer) – The initializer to use. Overrides
Parameter.init()
and default_init.ctx (Context or list of Context, defaults to
context.current_context()
.) –Initialize Parameter on given context. If ctx is a list of Context, a copy will be made for each context.
Note
Copies are independent arrays. User is responsible for keeping their values consistent when updating. Normally
gluon.Trainer
does this for you.default_init (Initializer) – Default initializer is used when both
init()
andParameter.init()
areNone
.force_reinit (bool, default False) – Whether to force re-initialization if parameter is already initialized.
Examples
>>> weight = mx.gluon.Parameter('weight', shape=(2, 2)) >>> weight.initialize(ctx=mx.cpu(0)) >>> weight.data() [[-0.01068833 0.01729892] [ 0.02042518 -0.01618656]] <NDArray 2x2 @cpu(0)> >>> weight.grad() [[ 0. 0.] [ 0. 0.]] <NDArray 2x2 @cpu(0)> >>> weight.initialize(ctx=[mx.gpu(0), mx.gpu(1)]) >>> weight.data(mx.gpu(0)) [[-0.00873779 -0.02834515] [ 0.05484822 -0.06206018]] <NDArray 2x2 @gpu(0)> >>> weight.data(mx.gpu(1)) [[-0.00873779 -0.02834515] [ 0.05484822 -0.06206018]] <NDArray 2x2 @gpu(1)>
-
list_ctx
()¶ Returns a list of contexts this parameter is initialized on.
-
list_data
()¶ Returns copies of this parameter on all contexts, in the same order as creation. For sparse parameters, use
Parameter.list_row_sparse_data()
instead.- Returns
- Return type
list of NDArrays
-
list_grad
()¶ Returns gradient buffers on all contexts, in the same order as
values()
.
-
list_row_sparse_data
(row_id)¶ Returns copies of the ‘row_sparse’ parameter on all contexts, in the same order as creation. The copy only retains rows whose ids occur in provided row ids. The parameter must have been initialized before.
- Parameters
row_id (NDArray) – Row ids to retain for the ‘row_sparse’ parameter.
- Returns
- Return type
list of NDArrays
-
reset_ctx
(ctx)¶ Re-assign Parameter to other contexts.
- Parameters
ctx (Context or list of Context, default
context.current_context()
.) – Assign Parameter to given context. If ctx is a list of Context, a copy will be made for each context.
-
row_sparse_data
(row_id)¶ Returns a copy of the ‘row_sparse’ parameter on the same context as row_id’s. The copy only retains rows whose ids occur in provided row ids. The parameter must have been initialized on this context before.
- Parameters
row_id (NDArray) – Row ids to retain for the ‘row_sparse’ parameter.
- Returns
- Return type
NDArray on row_id’s context
-
set_data
(data)¶ Sets this parameter’s value on all contexts.
-
property
shape
¶ The shape of the parameter.
By default, an unknown dimension size is 0. However, when the NumPy semantic is turned on, unknown dimension size is -1.
-
var
()¶ Returns a symbol representing this parameter.
-
zero_grad
()¶ Sets gradient buffer on all contexts to 0. No action is taken if parameter is uninitialized or doesn’t require gradient.