cntk.variables module

CNTK variables, parameters, constants, and records.

class Constant(self, value=None, shape=None, dtype=np.float32, device=None, name='')[source]

Bases: cntk.variables.VariableMixin, cntk.tensor.TensorOpsMixin, cntk.cntk_py.Constant

A constant value. It can be a scalar, vector, matrix, or tensor of floating point numbers that cannot be modified.

A Constant is a Variable and therefore inherits all its methods.

Example

>>> c = C.Constant(1, (2,3))
>>> c.value
    array([[1., 1., 1.],
           [1., 1., 1.]], dtype=float32)
Parameters:
  • value (np.ndarray or list or float or int) – Initial value.
  • dtype (np.float32 or np.float64 or np.float16) – data type to store the values as.
  • device (DeviceDescriptor) – the device on which the values should reside.
  • name (str) – an optional name for this constant.
value

NumPy array of the value

class Parameter(self, shape=None, init=None, dtype=np.float32, device=None, name='')[source]

Bases: cntk.variables.VariableMixin, cntk.tensor.TensorOpsMixin, cntk.cntk_py.Parameter

A trainable parameter. It can be a scalar, vector, matrix, or tensor of floating point numbers that can be modified by a training procedure.

Example

>>> p = C.Parameter((13,42,7), init=C.glorot_uniform())
>>> p.shape
    (13, 42, 7)
>>> # example with inferred dimensions
>>> W = C.Parameter((C.InferredDimension, 42), init=C.glorot_uniform())
>>> W.shape   # -1 indicates dimension yet to be inferred
    (-1, 42)
>>> x = C.input_variable(13)
>>> y = C.times(x, W)  # times operation now knows that the input dimension of W must be 13
>>> W.shape          # hence, the shape has been updated
    (13, 42)
Parameters:
  • shape (tuple) – the shape of the tensor holding the parameters
  • init (value (np.ndarray, list, float, int) – initializer: Initial value. If a numpy array is specified the shape argument is ignored and the tensor gets the shape of this argument. Alternatively, an initializer from initializer can be specified.
  • dtype (np.float32 or np.float64 or np.float16) – data type of the values stored.
  • device (DeviceDescriptor) – the device on which the values should reside.
  • name (str) – an optional name for this parameter

Parameters are Variables and therefore they inherit all their methods.

value

Value of the Parameter

Parameters:
  • getter – gets the Parameter’s value as a NumPy array
  • setter – sets the Parameter’s value to the provided NumPy array
class Record(**args_dict)[source]

Bases: dict

Easy construction of a record (=immutable singleton class) from keyword arguments.

Example

>>> r = Record(x = 13, y = 42)
>>> r.x
    13
Parameters:kwargs – keyword arguments to turn into the record members
Returns:A singleton class instance that has all passed kw args as immutable class members.
updated_with(**kwargs)[source]

Create a new Record from an existing one with members modified or added.

Example

>>> r = Record(x = 13)
>>> r.x
    13
>>> r2 = r.updated_with(x = 42)
>>> r2.x
    42
Parameters:kwargs – keyword arguments to turn into the record members
Returns:A singleton class instance that has all passed kw args as immutable class members.
class Variable(shape=None, dtype=None, needs_gradient=False, is_sparse=False, dynamic_axes=[Axis.default_batch_axis(), Axis.default_dynamic_axis()], name='')[source]

Bases: cntk.variables.VariableMixin, cntk.tensor.TensorOpsMixin, cntk.cntk_py.Variable

Denotes a symbolic entity corresponding to the inputs and outputs of a Function.

Parameters:
  • shape (tuple) – the shape of this variable.
  • dtype (np.float32 or np.float64 or np.float16) – data type of the values that will be bound to this variable. Default is np.float32
  • needs_gradient (bool) – if set to True any expression that contains this variable will also be differentiated with respect to this variable.
  • is_sparse (bool) – whether this is a sparse or dense input (or output)
  • dynamic_axes (list of Axis) – the dynamic axes of this variable. These express dimensions that can vary across examples or minibatches.
  • name (str) – an optional name for this parameter.
as_constant()[source]

Converts this instance into a Constant

as_parameter()[source]

Converts this instance into a Parameter

class VariableMixin[source]

Bases: object

Standard properties for Variable and its derived classes Parameter and Constant.

dtype

The NumPy type of this variable.

dynamic_axes

The dynamic axes of this variable.

is_constant

Whether this variable is a constant.

is_input

Whether this variable is an input.

is_output

Whether this variable is an output.

is_parameter

Whether this variable is a parameter.

is_placeholder

Whether this variable is a placeholder.

is_sparse

Whether this variable is sparse.

name

The name of this variable.

needs_gradient

Whether this variable needs gradients.

owner

The function this variable is an output of.

shape

The shape of this variable as a tuple.

uid

The internally generated unique name of the variable.