Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def Convolution2D(filter_shape, # shape of receptive field, e.g. (3,3). Must be a 2-element tuple.
num_filters=None, # e.g. 64 or None (which means 1 channel and don't add a dimension)
activation=default_override_or(identity),
init=default_override_or(C.glorot_uniform()),
pad=default_override_or(False),
strides=1,
bias=default_override_or(True),
init_bias=default_override_or(0),
reduction_rank=1, # (0 means input has no depth dimension, e.g. audio signal or B&W image)
dilation=1,
groups=1,
name=''):
'''
Convolution2D(filter_shape, num_filters=None, activation=identity, init=glorot_uniform(), pad=False, strides=1, bias=True, init_bias=0, reduction_rank=1, name='')
Layer factory function to create a 2D convolution layer with optional non-linearity.
Same as `Convolution()` except that filter_shape is verified to be 2-dimensional.
See `Convolution()` for extensive documentation.
Args:
filter_shape (`int` or `tuple` of `ints`): shape (spatial extent) of the receptive field, *not* including the input feature-map depth. E.g. (3,3) for a 2D convolution.
num_filters (int, defaults to `None`): number of filters (output feature-map depth), or ``()`` to denote scalar output items (output shape will have no depth axis).
activation (:class:`~cntk.ops.functions.Function`, defaults to `identity`): optional function to apply at the end, e.g. `relu`
init (scalar or NumPy array or :mod:`cntk.initializer`, defaults to :func:`~cntk.initializer.glorot_uniform` ): initial value of weights `W`
def Dense(shape, activation=default_override_or(identity), init=default_override_or(C.glorot_uniform()),
input_rank=None, map_rank=None,
bias=default_override_or(True), init_bias=default_override_or(0),
name=''):
'''
Dense(shape, activation=identity, init=glorot_uniform(), input_rank=None, map_rank=None, bias=True, init_bias=0, name='')
Layer factory function to create an instance of a fully-connected linear layer of the form
`activation(input @ W + b)` with weights `W` and bias `b`, and `activation` and `b` being optional.
`shape` may describe a tensor as well.
A ``Dense`` layer instance owns its parameter tensors `W` and `b`, and exposes them as attributes ``.W`` and ``.b``.
Example:
>>> f = Dense(5, activation=C.relu)
>>> x = C.input_variable(3)
>>> h = f(x)
>>> h.shape
(5,)
def Convolution(filter_shape, # shape of receptive field, e.g. (3,3)
num_filters=None, # e.g. 64 or None (which means 1 channel and don't add a dimension)
sequential=False, # time convolution if True (filter_shape[0] corresponds to dynamic axis)
activation=default_override_or(identity),
init=default_override_or(C.glorot_uniform()),
pad=default_override_or(False),
strides=1,
sharing=True, # (must be True currently)
bias=default_override_or(True),
init_bias=default_override_or(0),
reduction_rank=1, # (0 means input has no depth dimension, e.g. audio signal or B&W image) --TODO: call it item_rank?
transpose_weight=False, # (must be False currently)
dilation=1,
groups=1,
input_num_filters=None,
max_temp_mem_size_in_samples=0,
op_name='Convolution',
name=''):
'''
Convolution(filter_shape, num_filters=None, sequential=False, activation=identity, init=glorot_uniform(), pad=False, strides=1, sharing=True, bias=True, init_bias=0, reduction_rank=1, transpose_weight=False, dilation=1, groups=1, max_temp_mem_size_in_samples=0, op_name='Convolution', name='')
def Convolution2D(filter_shape, # shape of receptive field, e.g. (3,3). Must be a 2-element tuple.
num_filters=None, # e.g. 64 or None (which means 1 channel and don't add a dimension)
activation=default_override_or(identity),
init=default_override_or(C.glorot_uniform()),
pad=default_override_or(False),
strides=1,
bias=default_override_or(True),
init_bias=default_override_or(0),
reduction_rank=1, # (0 means input has no depth dimension, e.g. audio signal or B&W image)
dilation=1,
groups=1,
input_num_filters=None,
name=''):
'''
Convolution2D(filter_shape, num_filters=None, activation=identity, init=glorot_uniform(), pad=False, strides=1, bias=True, init_bias=0, reduction_rank=1, name='')
Layer factory function to create a 2D convolution layer with optional non-linearity.
Same as `Convolution()` except that filter_shape is verified to be 2-dimensional.
See `Convolution()` for extensive documentation.
Args:
filter_shape (`int` or `tuple` of `ints`): shape (spatial extent) of the receptive field, *not* including the input feature-map depth. E.g. (3,3) for a 2D convolution.
num_filters (int, defaults to `None`): number of filters (output feature-map depth), or ``()`` to denote scalar output items (output shape will have no depth axis).
activation (:class:`~cntk.ops.functions.Function`, defaults to `identity`): optional function to apply at the end, e.g. `relu`
def TransformerDecoderBlock(num_heads: int, model_dim: int, intermediate_dim: int, dropout_rate: float = None,
obey_sequence_order: bool = True, max_seq_len: int = None,
mha1_key_init=default_override_or(C.glorot_uniform()), mha1_key_init_bias=default_override_or(0),
mha1_query_init=default_override_or(C.glorot_uniform()), mha1_query_init_bias=default_override_or(0),
mha1_value_init=default_override_or(C.glorot_uniform()), mha1_value_init_bias=default_override_or(0),
mha1_init=default_override_or(C.glorot_uniform()), mha1_init_bias=default_override_or(0),
mha1_initial_scale=1, mha1_initial_bias=0,
mha2_key_init=default_override_or(C.glorot_uniform()), mha2_key_init_bias=default_override_or(0),
mha2_query_init=default_override_or(C.glorot_uniform()), mha2_query_init_bias=default_override_or(0),
mha2_value_init=default_override_or(C.glorot_uniform()), mha2_value_init_bias=default_override_or(0),
mha2_init=default_override_or(C.glorot_uniform()), mha2_init_bias=default_override_or(0),
mha2_initial_scale=1, mha2_initial_bias=0,
intermediate_init=default_override_or(C.glorot_uniform()),
intermediate_init_bias=default_override_or(0),
init=default_override_or(C.glorot_uniform()), init_bias=default_override_or(0),
initial_scale=1, initial_bias=0):
""" Decoder block of transformer as described in "Attention is all you need", https://arxiv.org/abs/1706.03762
Consist of 2 multi head attention followed by a dense layer, residual connect and layer norm
Arguments:
num_heads (int): number of attention heads
model_dim (int): number of hidden dim in final output of multi-head attention
intermediate_dim (int): hidden/ intermediate dimension within position-wise feed-forward layer
dropout_rate (float): probability of dropping out an element in the position-wise feed-forward
def WeightDroppedLSTM(shape, dropout_rate, cell_shape=None, activation=default_override_or(tanh), use_peepholes=default_override_or(False),
init=default_override_or(glorot_uniform()), init_bias=default_override_or(0),
enable_self_stabilization=default_override_or(False), seed=SentinelValueForAutoSelectRandomSeed,
name=''):
'''
WDLSTM(shape, cell_shape=None, activation=tanh, use_peepholes=False, init=glorot_uniform(), init_bias=0, enable_self_stabilization=False, name='')
Layer factory function to create an LSTM block for use inside a recurrence.
The LSTM block implements one step of the recurrence and is stateless. It accepts the previous state as its first two arguments,
and outputs its new state as a two-valued tuple ``(h,c)``.
Example:
>>> # a typical recurrent LSTM layer
>>> from cntkx.layers import *
>>> lstm_layer = Recurrence(WeightDroppedLSTM(500))
Args:
shape (`int` or `tuple` of `ints`): vector or tensor dimension of the output of this layer
def Convolution(filter_shape, # shape of receptive field, e.g. (3,3)
num_filters=None, # e.g. 64 or None (which means 1 channel and don't add a dimension)
sequential=False, # time convolution if True (filter_shape[0] corresponds to dynamic axis)
activation=default_override_or(identity),
init=default_override_or(C.glorot_uniform()),
pad=default_override_or(False),
strides=1,
sharing=True, # (must be True currently)
bias=default_override_or(True),
init_bias=default_override_or(0),
reduction_rank=1, # (0 means input has no depth dimension, e.g. audio signal or B&W image) --TODO: call it item_rank?
transpose_weight=False, # (must be False currently)
dilation=1,
groups = 1,
max_temp_mem_size_in_samples=0,
op_name='Convolution', name=''):
'''
Convolution(filter_shape, num_filters=None, sequential=False, activation=identity, init=glorot_uniform(), pad=False, strides=1, sharing=True, bias=True, init_bias=0, reduction_rank=1, transpose_weight=False, dilation=1, groups=1, max_temp_mem_size_in_samples=0, op_name='Convolution', name='')
Layer factory function to create a convolution layer.
This implements a convolution operation over items arranged on an N-dimensional grid, such as pixels in an image.
Typically, each item is a vector (e.g. pixel: R,G,B), and the result is, in turn, a vector.
The item-grid dimensions are referred to as the *spatial* dimensions (e.g. dimensions of an image),
while the vector dimension of the individual items is often called *feature-map depth*.
def SequentialConvolution(filter_shape, # shape of receptive field, e.g. (3,3). filter_shape[0] is for sequence axis.
num_filters=None, # e.g. 64 or None (which means 1 channel and don't add a dimension)
activation=default_override_or(identity),
init=default_override_or(C.glorot_uniform()),
pad=default_override_or(False),
strides=1,
sharing=True, # (must be True currently)
bias=default_override_or(True),
init_bias=default_override_or(0),
reduction_rank=1, # (0 means input has no depth dimension, e.g. audio signal or B&W image) --TODO: call it item_rank?
transpose_weight=False, # (must be False currently)
dilation=1,
groups = 1,
max_temp_mem_size_in_samples=0,
op_name='Convolution', name=''):
'''
SequentialConvolution(filter_shape, num_filters=None, activation=identity, init=glorot_uniform(), pad=False, strides=1, sharing=True, bias=True, init_bias=0, reduction_rank=1, transpose_weight=False, dilation=1, groups=1, max_temp_mem_size_in_samples=0, op_name='Convolution', name='')
Layer factory function to create a sequential convolution layer.
def Convolution3D(filter_shape, # shape of receptive field, e.g. (3,3,3). Must be a 3-element tuple.
num_filters=None, # e.g. 64 or None (which means 1 channel and don't add a dimension)
activation=default_override_or(identity),
init=default_override_or(C.glorot_uniform()),
pad=default_override_or(False),
strides=1,
bias=default_override_or(True),
init_bias=default_override_or(0),
reduction_rank=1, # (0 means input has no depth dimension, e.g. audio signal or B&W image)
dilation=1,
groups=1,
name=''):
'''
Convolution3D(filter_shape, num_filters=None, activation=identity, init=glorot_uniform(), pad=False, strides=1, bias=True, init_bias=0, reduction_rank=1, name='')
Layer factory function to create a 3D convolution layer with optional non-linearity.
Same as `Convolution()` except that filter_shape is verified to be 3-dimensional.
See `Convolution()` for extensive documentation.
Args:
filter_shape (`int` or `tuple` of `ints`): shape (spatial extent) of the receptive field, *not* including the input feature-map depth. E.g. (3,3) for a 2D convolution.
num_filters (int, defaults to `None`): number of filters (output feature-map depth), or ``()`` to denote scalar output items (output shape will have no depth axis).
activation (:class:`~cntk.ops.functions.Function`, defaults to `identity`): optional function to apply at the end, e.g. `relu`
init (scalar or NumPy array or :mod:`cntk.initializer`, defaults to :func:`~cntk.initializer.glorot_uniform` ): initial value of weights `W`
def TransformerEncoderBlock(num_heads: int, model_dim: int, intermediate_dim: int, dropout_rate: float = None,
obey_sequence_order: bool = None, max_seq_len: int = None,
key_init=default_override_or(C.glorot_uniform()), key_init_bias=default_override_or(0),
query_init=default_override_or(C.glorot_uniform()), query_init_bias=default_override_or(0),
value_init=default_override_or(C.glorot_uniform()), value_init_bias=default_override_or(0),
mha_init=default_override_or(C.glorot_uniform()), mha_init_bias=default_override_or(0),
mha_initial_scale=1, mha_initial_bias=0,
intermediate_init=default_override_or(C.glorot_uniform()), intermediate_init_bias=default_override_or(0),
init=default_override_or(C.glorot_uniform()), init_bias=default_override_or(0),
initial_scale=1, initial_bias=0, name=''):
""" Encoder block of transformer as described in "Attention is all you need", https://arxiv.org/abs/1706.03762
Consist of 1 multi head attention followed by a dense layer, residual connect and layer norm
Arguments:
num_heads (int): number of attention heads
model_dim (int): number of hidden dim in final output of multi-head attention
intermediate_dim (int): hidden/ intermediate dimension within position-wise feed-forward layer
dropout_rate (float): probability of dropping out an element in the position-wise feed-forward
obey_sequence_order: do not let attention peek into future values
max_seq_len: max sequence length possible, used to ensure that sequence order is obeyed
key_init (scalar or NumPy array or :mod:`cntk.initializer`, defaults to :func:`~cntk.initializer.glorot_uniform` ): initial value of weights `W`
key_init_bias (scalar or NumPy array or :mod:`cntk.initializer`, defaults to 0): initial value of weights `b`
query_init (scalar or NumPy array or :mod:`cntk.initializer`, defaults to :func:`~cntk.initializer.glorot_uniform` ): initial value of weights `W`
query_init_bias (scalar or NumPy array or :mod:`cntk.initializer`, defaults to 0): initial value of weights `b`