repo
stringlengths
7
55
path
stringlengths
4
223
func_name
stringlengths
1
134
original_string
stringlengths
75
104k
language
stringclasses
1 value
code
stringlengths
75
104k
code_tokens
listlengths
19
28.4k
docstring
stringlengths
1
46.9k
docstring_tokens
listlengths
1
1.97k
sha
stringlengths
40
40
url
stringlengths
87
315
partition
stringclasses
1 value
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
_prelu
def _prelu(attrs, inputs, proto_obj): """PRelu function""" new_attrs = translation_utils._add_extra_attributes(attrs, {'act_type': 'prelu'}) return 'LeakyReLU', new_attrs, inputs
python
def _prelu(attrs, inputs, proto_obj): """PRelu function""" new_attrs = translation_utils._add_extra_attributes(attrs, {'act_type': 'prelu'}) return 'LeakyReLU', new_attrs, inputs
[ "def", "_prelu", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'act_type'", ":", "'prelu'", "}", ")", "return", "'LeakyReLU'", ",", "new_attrs", ",", "inputs" ]
PRelu function
[ "PRelu", "function" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L291-L294
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
_selu
def _selu(attrs, inputs, proto_obj): """Selu function""" new_attrs = translation_utils._add_extra_attributes(attrs, {'act_type': 'selu'}) return 'LeakyReLU', new_attrs, inputs
python
def _selu(attrs, inputs, proto_obj): """Selu function""" new_attrs = translation_utils._add_extra_attributes(attrs, {'act_type': 'selu'}) return 'LeakyReLU', new_attrs, inputs
[ "def", "_selu", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'act_type'", ":", "'selu'", "}", ")", "return", "'LeakyReLU'", ",", "new_attrs", ",", "inputs" ]
Selu function
[ "Selu", "function" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L296-L299
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
softmax
def softmax(attrs, inputs, proto_obj): """Softmax function.""" if 'axis' not in attrs: attrs = translation_utils._add_extra_attributes(attrs, {'axis': 1}) return 'softmax', attrs, inputs
python
def softmax(attrs, inputs, proto_obj): """Softmax function.""" if 'axis' not in attrs: attrs = translation_utils._add_extra_attributes(attrs, {'axis': 1}) return 'softmax', attrs, inputs
[ "def", "softmax", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "if", "'axis'", "not", "in", "attrs", ":", "attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'axis'", ":", "1", "}", ")", "return", "'softmax'", ",", "attrs", ",", "inputs" ]
Softmax function.
[ "Softmax", "function", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L301-L305
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
softplus
def softplus(attrs, inputs, proto_obj): """Applies the sofplus activation function element-wise to the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'act_type' : 'softrelu'}) return 'Activation', new_attrs, inputs
python
def softplus(attrs, inputs, proto_obj): """Applies the sofplus activation function element-wise to the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'act_type' : 'softrelu'}) return 'Activation', new_attrs, inputs
[ "def", "softplus", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'act_type'", ":", "'softrelu'", "}", ")", "return", "'Activation'", ",", "new_attrs", ",", "inputs" ]
Applies the sofplus activation function element-wise to the input.
[ "Applies", "the", "sofplus", "activation", "function", "element", "-", "wise", "to", "the", "input", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L312-L315
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
conv
def conv(attrs, inputs, proto_obj): """Compute N-D convolution on (N+2)-D input.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape' : 'kernel', 'strides' : 'stride', 'pads': 'pad', 'dilations': 'dilate', 'group': 'num_group'}) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'num_group' : 1}) new_attrs = translation_utils._fix_bias('Convolution', new_attrs, len(inputs)) new_attrs = translation_utils._fix_channels('Convolution', new_attrs, inputs, proto_obj) kernel = new_attrs['kernel'] stride = new_attrs['stride'] if 'stride' in new_attrs else [] padding = new_attrs['pad'] if 'pad' in new_attrs else [] dilations = new_attrs['dilate'] if 'dilate' in new_attrs else [] num_filter = new_attrs['num_filter'] num_group = new_attrs['num_group'] no_bias = new_attrs['no_bias'] if 'no_bias' in new_attrs else 0 bias = None if no_bias is True else inputs[2] # Unlike ONNX, MXNet's convolution operator does not support asymmetric padding, so we first # use 'Pad' operator, which supports asymmetric padding. Then use the convolution operator. pad_width = (0, 0, 0, 0) + translation_utils._pad_sequence_fix(padding, kernel_dim=len(kernel)) pad_op = symbol.pad(inputs[0], mode='constant', pad_width=pad_width) conv_op = symbol.Convolution(pad_op, inputs[1], bias, kernel=kernel, stride=stride, dilate=dilations, num_filter=num_filter, num_group=num_group, no_bias=no_bias) return conv_op, new_attrs, inputs
python
def conv(attrs, inputs, proto_obj): """Compute N-D convolution on (N+2)-D input.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape' : 'kernel', 'strides' : 'stride', 'pads': 'pad', 'dilations': 'dilate', 'group': 'num_group'}) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'num_group' : 1}) new_attrs = translation_utils._fix_bias('Convolution', new_attrs, len(inputs)) new_attrs = translation_utils._fix_channels('Convolution', new_attrs, inputs, proto_obj) kernel = new_attrs['kernel'] stride = new_attrs['stride'] if 'stride' in new_attrs else [] padding = new_attrs['pad'] if 'pad' in new_attrs else [] dilations = new_attrs['dilate'] if 'dilate' in new_attrs else [] num_filter = new_attrs['num_filter'] num_group = new_attrs['num_group'] no_bias = new_attrs['no_bias'] if 'no_bias' in new_attrs else 0 bias = None if no_bias is True else inputs[2] # Unlike ONNX, MXNet's convolution operator does not support asymmetric padding, so we first # use 'Pad' operator, which supports asymmetric padding. Then use the convolution operator. pad_width = (0, 0, 0, 0) + translation_utils._pad_sequence_fix(padding, kernel_dim=len(kernel)) pad_op = symbol.pad(inputs[0], mode='constant', pad_width=pad_width) conv_op = symbol.Convolution(pad_op, inputs[1], bias, kernel=kernel, stride=stride, dilate=dilations, num_filter=num_filter, num_group=num_group, no_bias=no_bias) return conv_op, new_attrs, inputs
[ "def", "conv", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'kernel_shape'", ":", "'kernel'", ",", "'strides'", ":", "'stride'", ",", "'pads'", ":", "'pad'", ",", "'dilations'", ":", "'dilate'", ",", "'group'", ":", "'num_group'", "}", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'num_group'", ":", "1", "}", ")", "new_attrs", "=", "translation_utils", ".", "_fix_bias", "(", "'Convolution'", ",", "new_attrs", ",", "len", "(", "inputs", ")", ")", "new_attrs", "=", "translation_utils", ".", "_fix_channels", "(", "'Convolution'", ",", "new_attrs", ",", "inputs", ",", "proto_obj", ")", "kernel", "=", "new_attrs", "[", "'kernel'", "]", "stride", "=", "new_attrs", "[", "'stride'", "]", "if", "'stride'", "in", "new_attrs", "else", "[", "]", "padding", "=", "new_attrs", "[", "'pad'", "]", "if", "'pad'", "in", "new_attrs", "else", "[", "]", "dilations", "=", "new_attrs", "[", "'dilate'", "]", "if", "'dilate'", "in", "new_attrs", "else", "[", "]", "num_filter", "=", "new_attrs", "[", "'num_filter'", "]", "num_group", "=", "new_attrs", "[", "'num_group'", "]", "no_bias", "=", "new_attrs", "[", "'no_bias'", "]", "if", "'no_bias'", "in", "new_attrs", "else", "0", "bias", "=", "None", "if", "no_bias", "is", "True", "else", "inputs", "[", "2", "]", "# Unlike ONNX, MXNet's convolution operator does not support asymmetric padding, so we first", "# use 'Pad' operator, which supports asymmetric padding. Then use the convolution operator.", "pad_width", "=", "(", "0", ",", "0", ",", "0", ",", "0", ")", "+", "translation_utils", ".", "_pad_sequence_fix", "(", "padding", ",", "kernel_dim", "=", "len", "(", "kernel", ")", ")", "pad_op", "=", "symbol", ".", "pad", "(", "inputs", "[", "0", "]", ",", "mode", "=", "'constant'", ",", "pad_width", "=", "pad_width", ")", "conv_op", "=", "symbol", ".", "Convolution", "(", "pad_op", ",", "inputs", "[", "1", "]", ",", "bias", ",", "kernel", "=", "kernel", ",", "stride", "=", "stride", ",", "dilate", "=", "dilations", ",", "num_filter", "=", "num_filter", ",", "num_group", "=", "num_group", ",", "no_bias", "=", "no_bias", ")", "return", "conv_op", ",", "new_attrs", ",", "inputs" ]
Compute N-D convolution on (N+2)-D input.
[ "Compute", "N", "-", "D", "convolution", "on", "(", "N", "+", "2", ")", "-", "D", "input", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L317-L346
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
deconv
def deconv(attrs, inputs, proto_obj): """Computes transposed convolution of the input tensor.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape' : 'kernel', 'strides' : 'stride', 'pads': 'pad', 'dilations': 'dilate', 'group': 'num_group'}) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'num_group' : 1}) new_attrs = translation_utils._fix_bias('Deconvolution', new_attrs, len(inputs)) new_attrs = translation_utils._fix_channels('Deconvolution', new_attrs, inputs, proto_obj) kernel = new_attrs['kernel'] stride = new_attrs['stride'] if 'stride' in new_attrs else [] padding = new_attrs['pad'] if 'pad' in new_attrs else [] dilations = new_attrs['dilate'] if 'dilate' in new_attrs else [] num_filter = new_attrs['num_filter'] num_group = new_attrs['num_group'] no_bias = new_attrs['no_bias'] if 'no_bias' in new_attrs else False bias = None if no_bias is True else inputs[2] # Unlike ONNX, MXNet's deconvolution operator does not support asymmetric padding, so we first # use 'Pad' operator, which supports asymmetric padding. Then use the deconvolution operator. pad_width = (0, 0, 0, 0) + translation_utils._pad_sequence_fix(padding, kernel_dim=len(kernel)) pad_op = symbol.pad(inputs[0], mode='constant', pad_width=pad_width) deconv_op = symbol.Deconvolution(pad_op, inputs[1], bias, kernel=kernel, stride=stride, dilate=dilations, num_filter=num_filter, num_group=num_group, no_bias=no_bias) return deconv_op, new_attrs, inputs
python
def deconv(attrs, inputs, proto_obj): """Computes transposed convolution of the input tensor.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape' : 'kernel', 'strides' : 'stride', 'pads': 'pad', 'dilations': 'dilate', 'group': 'num_group'}) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'num_group' : 1}) new_attrs = translation_utils._fix_bias('Deconvolution', new_attrs, len(inputs)) new_attrs = translation_utils._fix_channels('Deconvolution', new_attrs, inputs, proto_obj) kernel = new_attrs['kernel'] stride = new_attrs['stride'] if 'stride' in new_attrs else [] padding = new_attrs['pad'] if 'pad' in new_attrs else [] dilations = new_attrs['dilate'] if 'dilate' in new_attrs else [] num_filter = new_attrs['num_filter'] num_group = new_attrs['num_group'] no_bias = new_attrs['no_bias'] if 'no_bias' in new_attrs else False bias = None if no_bias is True else inputs[2] # Unlike ONNX, MXNet's deconvolution operator does not support asymmetric padding, so we first # use 'Pad' operator, which supports asymmetric padding. Then use the deconvolution operator. pad_width = (0, 0, 0, 0) + translation_utils._pad_sequence_fix(padding, kernel_dim=len(kernel)) pad_op = symbol.pad(inputs[0], mode='constant', pad_width=pad_width) deconv_op = symbol.Deconvolution(pad_op, inputs[1], bias, kernel=kernel, stride=stride, dilate=dilations, num_filter=num_filter, num_group=num_group, no_bias=no_bias) return deconv_op, new_attrs, inputs
[ "def", "deconv", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'kernel_shape'", ":", "'kernel'", ",", "'strides'", ":", "'stride'", ",", "'pads'", ":", "'pad'", ",", "'dilations'", ":", "'dilate'", ",", "'group'", ":", "'num_group'", "}", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'num_group'", ":", "1", "}", ")", "new_attrs", "=", "translation_utils", ".", "_fix_bias", "(", "'Deconvolution'", ",", "new_attrs", ",", "len", "(", "inputs", ")", ")", "new_attrs", "=", "translation_utils", ".", "_fix_channels", "(", "'Deconvolution'", ",", "new_attrs", ",", "inputs", ",", "proto_obj", ")", "kernel", "=", "new_attrs", "[", "'kernel'", "]", "stride", "=", "new_attrs", "[", "'stride'", "]", "if", "'stride'", "in", "new_attrs", "else", "[", "]", "padding", "=", "new_attrs", "[", "'pad'", "]", "if", "'pad'", "in", "new_attrs", "else", "[", "]", "dilations", "=", "new_attrs", "[", "'dilate'", "]", "if", "'dilate'", "in", "new_attrs", "else", "[", "]", "num_filter", "=", "new_attrs", "[", "'num_filter'", "]", "num_group", "=", "new_attrs", "[", "'num_group'", "]", "no_bias", "=", "new_attrs", "[", "'no_bias'", "]", "if", "'no_bias'", "in", "new_attrs", "else", "False", "bias", "=", "None", "if", "no_bias", "is", "True", "else", "inputs", "[", "2", "]", "# Unlike ONNX, MXNet's deconvolution operator does not support asymmetric padding, so we first", "# use 'Pad' operator, which supports asymmetric padding. Then use the deconvolution operator.", "pad_width", "=", "(", "0", ",", "0", ",", "0", ",", "0", ")", "+", "translation_utils", ".", "_pad_sequence_fix", "(", "padding", ",", "kernel_dim", "=", "len", "(", "kernel", ")", ")", "pad_op", "=", "symbol", ".", "pad", "(", "inputs", "[", "0", "]", ",", "mode", "=", "'constant'", ",", "pad_width", "=", "pad_width", ")", "deconv_op", "=", "symbol", ".", "Deconvolution", "(", "pad_op", ",", "inputs", "[", "1", "]", ",", "bias", ",", "kernel", "=", "kernel", ",", "stride", "=", "stride", ",", "dilate", "=", "dilations", ",", "num_filter", "=", "num_filter", ",", "num_group", "=", "num_group", ",", "no_bias", "=", "no_bias", ")", "return", "deconv_op", ",", "new_attrs", ",", "inputs" ]
Computes transposed convolution of the input tensor.
[ "Computes", "transposed", "convolution", "of", "the", "input", "tensor", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L348-L377
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
fully_connected
def fully_connected(attrs, inputs, proto_obj): """Applies a linear transformation: Y=XWT+b.""" new_attrs = translation_utils._remove_attributes(attrs, ['axis']) new_attrs = translation_utils._fix_bias('FullyConnected', new_attrs, len(inputs)) new_attrs = translation_utils._fix_channels('FullyConnected', new_attrs, inputs, proto_obj) return 'FullyConnected', new_attrs, inputs
python
def fully_connected(attrs, inputs, proto_obj): """Applies a linear transformation: Y=XWT+b.""" new_attrs = translation_utils._remove_attributes(attrs, ['axis']) new_attrs = translation_utils._fix_bias('FullyConnected', new_attrs, len(inputs)) new_attrs = translation_utils._fix_channels('FullyConnected', new_attrs, inputs, proto_obj) return 'FullyConnected', new_attrs, inputs
[ "def", "fully_connected", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "attrs", ",", "[", "'axis'", "]", ")", "new_attrs", "=", "translation_utils", ".", "_fix_bias", "(", "'FullyConnected'", ",", "new_attrs", ",", "len", "(", "inputs", ")", ")", "new_attrs", "=", "translation_utils", ".", "_fix_channels", "(", "'FullyConnected'", ",", "new_attrs", ",", "inputs", ",", "proto_obj", ")", "return", "'FullyConnected'", ",", "new_attrs", ",", "inputs" ]
Applies a linear transformation: Y=XWT+b.
[ "Applies", "a", "linear", "transformation", ":", "Y", "=", "XWT", "+", "b", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L379-L387
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
global_maxpooling
def global_maxpooling(attrs, inputs, proto_obj): """Performs max pooling on the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'max'}) return 'Pooling', new_attrs, inputs
python
def global_maxpooling(attrs, inputs, proto_obj): """Performs max pooling on the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'max'}) return 'Pooling', new_attrs, inputs
[ "def", "global_maxpooling", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'global_pool'", ":", "True", ",", "'kernel'", ":", "(", "1", ",", "1", ")", ",", "'pool_type'", ":", "'max'", "}", ")", "return", "'Pooling'", ",", "new_attrs", ",", "inputs" ]
Performs max pooling on the input.
[ "Performs", "max", "pooling", "on", "the", "input", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L390-L395
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
global_avgpooling
def global_avgpooling(attrs, inputs, proto_obj): """Performs avg pooling on the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'avg'}) return 'Pooling', new_attrs, inputs
python
def global_avgpooling(attrs, inputs, proto_obj): """Performs avg pooling on the input.""" new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'avg'}) return 'Pooling', new_attrs, inputs
[ "def", "global_avgpooling", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'global_pool'", ":", "True", ",", "'kernel'", ":", "(", "1", ",", "1", ")", ",", "'pool_type'", ":", "'avg'", "}", ")", "return", "'Pooling'", ",", "new_attrs", ",", "inputs" ]
Performs avg pooling on the input.
[ "Performs", "avg", "pooling", "on", "the", "input", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L398-L403
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
global_lppooling
def global_lppooling(attrs, inputs, proto_obj): """Performs global lp pooling on the input.""" p_value = attrs.get('p', 2) new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'lp', 'p_value': p_value}) new_attrs = translation_utils._remove_attributes(new_attrs, ['p']) return 'Pooling', new_attrs, inputs
python
def global_lppooling(attrs, inputs, proto_obj): """Performs global lp pooling on the input.""" p_value = attrs.get('p', 2) new_attrs = translation_utils._add_extra_attributes(attrs, {'global_pool': True, 'kernel': (1, 1), 'pool_type': 'lp', 'p_value': p_value}) new_attrs = translation_utils._remove_attributes(new_attrs, ['p']) return 'Pooling', new_attrs, inputs
[ "def", "global_lppooling", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "p_value", "=", "attrs", ".", "get", "(", "'p'", ",", "2", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "attrs", ",", "{", "'global_pool'", ":", "True", ",", "'kernel'", ":", "(", "1", ",", "1", ")", ",", "'pool_type'", ":", "'lp'", ",", "'p_value'", ":", "p_value", "}", ")", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "new_attrs", ",", "[", "'p'", "]", ")", "return", "'Pooling'", ",", "new_attrs", ",", "inputs" ]
Performs global lp pooling on the input.
[ "Performs", "global", "lp", "pooling", "on", "the", "input", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L405-L413
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
linalg_gemm
def linalg_gemm(attrs, inputs, proto_obj): """Performs general matrix multiplication and accumulation""" trans_a = 0 trans_b = 0 alpha = 1 beta = 1 if 'transA' in attrs: trans_a = attrs['transA'] if 'transB' in attrs: trans_b = attrs['transB'] if 'alpha' in attrs: alpha = attrs['alpha'] if 'beta' in attrs: beta = attrs['beta'] flatten_a = symbol.flatten(inputs[0]) matmul_op = symbol.linalg_gemm2(A=flatten_a, B=inputs[1], transpose_a=trans_a, transpose_b=trans_b, alpha=alpha) gemm_op = symbol.broadcast_add(matmul_op, beta*inputs[2]) new_attrs = translation_utils._fix_attribute_names(attrs, {'transA': 'transpose_a', 'transB': 'transpose_b'}) new_attrs = translation_utils._remove_attributes(new_attrs, ['broadcast']) return gemm_op, new_attrs, inputs
python
def linalg_gemm(attrs, inputs, proto_obj): """Performs general matrix multiplication and accumulation""" trans_a = 0 trans_b = 0 alpha = 1 beta = 1 if 'transA' in attrs: trans_a = attrs['transA'] if 'transB' in attrs: trans_b = attrs['transB'] if 'alpha' in attrs: alpha = attrs['alpha'] if 'beta' in attrs: beta = attrs['beta'] flatten_a = symbol.flatten(inputs[0]) matmul_op = symbol.linalg_gemm2(A=flatten_a, B=inputs[1], transpose_a=trans_a, transpose_b=trans_b, alpha=alpha) gemm_op = symbol.broadcast_add(matmul_op, beta*inputs[2]) new_attrs = translation_utils._fix_attribute_names(attrs, {'transA': 'transpose_a', 'transB': 'transpose_b'}) new_attrs = translation_utils._remove_attributes(new_attrs, ['broadcast']) return gemm_op, new_attrs, inputs
[ "def", "linalg_gemm", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "trans_a", "=", "0", "trans_b", "=", "0", "alpha", "=", "1", "beta", "=", "1", "if", "'transA'", "in", "attrs", ":", "trans_a", "=", "attrs", "[", "'transA'", "]", "if", "'transB'", "in", "attrs", ":", "trans_b", "=", "attrs", "[", "'transB'", "]", "if", "'alpha'", "in", "attrs", ":", "alpha", "=", "attrs", "[", "'alpha'", "]", "if", "'beta'", "in", "attrs", ":", "beta", "=", "attrs", "[", "'beta'", "]", "flatten_a", "=", "symbol", ".", "flatten", "(", "inputs", "[", "0", "]", ")", "matmul_op", "=", "symbol", ".", "linalg_gemm2", "(", "A", "=", "flatten_a", ",", "B", "=", "inputs", "[", "1", "]", ",", "transpose_a", "=", "trans_a", ",", "transpose_b", "=", "trans_b", ",", "alpha", "=", "alpha", ")", "gemm_op", "=", "symbol", ".", "broadcast_add", "(", "matmul_op", ",", "beta", "*", "inputs", "[", "2", "]", ")", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'transA'", ":", "'transpose_a'", ",", "'transB'", ":", "'transpose_b'", "}", ")", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "new_attrs", ",", "[", "'broadcast'", "]", ")", "return", "gemm_op", ",", "new_attrs", ",", "inputs" ]
Performs general matrix multiplication and accumulation
[ "Performs", "general", "matrix", "multiplication", "and", "accumulation" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L415-L437
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
local_response_norm
def local_response_norm(attrs, inputs, proto_obj): """Local Response Normalization.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'bias': 'knorm', 'size' : 'nsize'}) return 'LRN', new_attrs, inputs
python
def local_response_norm(attrs, inputs, proto_obj): """Local Response Normalization.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'bias': 'knorm', 'size' : 'nsize'}) return 'LRN', new_attrs, inputs
[ "def", "local_response_norm", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'bias'", ":", "'knorm'", ",", "'size'", ":", "'nsize'", "}", ")", "return", "'LRN'", ",", "new_attrs", ",", "inputs" ]
Local Response Normalization.
[ "Local", "Response", "Normalization", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L439-L444
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
dropout
def dropout(attrs, inputs, proto_obj): """Dropout Regularization.""" mode = 'training' if 'is_test' in attrs and attrs['is_test'] == 0: mode = 'always' new_attrs = translation_utils._fix_attribute_names(attrs, {'ratio': 'p'}) new_attrs = translation_utils._remove_attributes(new_attrs, ['is_test']) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'mode': mode}) return 'Dropout', new_attrs, inputs
python
def dropout(attrs, inputs, proto_obj): """Dropout Regularization.""" mode = 'training' if 'is_test' in attrs and attrs['is_test'] == 0: mode = 'always' new_attrs = translation_utils._fix_attribute_names(attrs, {'ratio': 'p'}) new_attrs = translation_utils._remove_attributes(new_attrs, ['is_test']) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'mode': mode}) return 'Dropout', new_attrs, inputs
[ "def", "dropout", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "mode", "=", "'training'", "if", "'is_test'", "in", "attrs", "and", "attrs", "[", "'is_test'", "]", "==", "0", ":", "mode", "=", "'always'", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'ratio'", ":", "'p'", "}", ")", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "new_attrs", ",", "[", "'is_test'", "]", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'mode'", ":", "mode", "}", ")", "return", "'Dropout'", ",", "new_attrs", ",", "inputs" ]
Dropout Regularization.
[ "Dropout", "Regularization", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L446-L455
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reshape
def reshape(attrs, inputs, proto_obj): """Reshape the given array by the shape attribute.""" if len(inputs) == 1: return 'reshape', attrs, inputs[0] reshape_shape = list(proto_obj._params[inputs[1].name].asnumpy()) reshape_shape = [int(i) for i in reshape_shape] new_attrs = {'shape': reshape_shape} return 'reshape', new_attrs, inputs[:1]
python
def reshape(attrs, inputs, proto_obj): """Reshape the given array by the shape attribute.""" if len(inputs) == 1: return 'reshape', attrs, inputs[0] reshape_shape = list(proto_obj._params[inputs[1].name].asnumpy()) reshape_shape = [int(i) for i in reshape_shape] new_attrs = {'shape': reshape_shape} return 'reshape', new_attrs, inputs[:1]
[ "def", "reshape", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "if", "len", "(", "inputs", ")", "==", "1", ":", "return", "'reshape'", ",", "attrs", ",", "inputs", "[", "0", "]", "reshape_shape", "=", "list", "(", "proto_obj", ".", "_params", "[", "inputs", "[", "1", "]", ".", "name", "]", ".", "asnumpy", "(", ")", ")", "reshape_shape", "=", "[", "int", "(", "i", ")", "for", "i", "in", "reshape_shape", "]", "new_attrs", "=", "{", "'shape'", ":", "reshape_shape", "}", "return", "'reshape'", ",", "new_attrs", ",", "inputs", "[", ":", "1", "]" ]
Reshape the given array by the shape attribute.
[ "Reshape", "the", "given", "array", "by", "the", "shape", "attribute", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L458-L465
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
cast
def cast(attrs, inputs, proto_obj): """ Cast input to a given dtype""" try: from onnx.mapping import TENSOR_TYPE_TO_NP_TYPE except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") new_attrs = translation_utils._fix_attribute_names(attrs, {'to' : 'dtype'}) new_attrs['dtype'] = TENSOR_TYPE_TO_NP_TYPE[int(new_attrs['dtype'])] return 'cast', new_attrs, inputs
python
def cast(attrs, inputs, proto_obj): """ Cast input to a given dtype""" try: from onnx.mapping import TENSOR_TYPE_TO_NP_TYPE except ImportError: raise ImportError("Onnx and protobuf need to be installed. " + "Instructions to install - https://github.com/onnx/onnx") new_attrs = translation_utils._fix_attribute_names(attrs, {'to' : 'dtype'}) new_attrs['dtype'] = TENSOR_TYPE_TO_NP_TYPE[int(new_attrs['dtype'])] return 'cast', new_attrs, inputs
[ "def", "cast", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "try", ":", "from", "onnx", ".", "mapping", "import", "TENSOR_TYPE_TO_NP_TYPE", "except", "ImportError", ":", "raise", "ImportError", "(", "\"Onnx and protobuf need to be installed. \"", "+", "\"Instructions to install - https://github.com/onnx/onnx\"", ")", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'to'", ":", "'dtype'", "}", ")", "new_attrs", "[", "'dtype'", "]", "=", "TENSOR_TYPE_TO_NP_TYPE", "[", "int", "(", "new_attrs", "[", "'dtype'", "]", ")", "]", "return", "'cast'", ",", "new_attrs", ",", "inputs" ]
Cast input to a given dtype
[ "Cast", "input", "to", "a", "given", "dtype" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L467-L476
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
split
def split(attrs, inputs, proto_obj): """Splits an array along a particular axis into multiple sub-arrays.""" split_list = attrs.get('split') if 'split' in attrs else [] new_attrs = translation_utils._fix_attribute_names(attrs, {'split' : 'num_outputs'}) if 'axis' not in attrs: new_attrs = translation_utils._add_extra_attributes(new_attrs, {'axis': 0}) if not split_list: num_outputs = len(proto_obj.model_metadata.get('output_tensor_data')) else: if len(set(split_list)) == 1: num_outputs = len(split_list) else: raise NotImplementedError("Operator {} in MXNet does not support variable splits." "Tracking the issue to support variable split here: " "https://github.com/apache/incubator-mxnet/issues/11594" .format('split')) new_attrs['num_outputs'] = num_outputs return 'split', new_attrs, inputs
python
def split(attrs, inputs, proto_obj): """Splits an array along a particular axis into multiple sub-arrays.""" split_list = attrs.get('split') if 'split' in attrs else [] new_attrs = translation_utils._fix_attribute_names(attrs, {'split' : 'num_outputs'}) if 'axis' not in attrs: new_attrs = translation_utils._add_extra_attributes(new_attrs, {'axis': 0}) if not split_list: num_outputs = len(proto_obj.model_metadata.get('output_tensor_data')) else: if len(set(split_list)) == 1: num_outputs = len(split_list) else: raise NotImplementedError("Operator {} in MXNet does not support variable splits." "Tracking the issue to support variable split here: " "https://github.com/apache/incubator-mxnet/issues/11594" .format('split')) new_attrs['num_outputs'] = num_outputs return 'split', new_attrs, inputs
[ "def", "split", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "split_list", "=", "attrs", ".", "get", "(", "'split'", ")", "if", "'split'", "in", "attrs", "else", "[", "]", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'split'", ":", "'num_outputs'", "}", ")", "if", "'axis'", "not", "in", "attrs", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'axis'", ":", "0", "}", ")", "if", "not", "split_list", ":", "num_outputs", "=", "len", "(", "proto_obj", ".", "model_metadata", ".", "get", "(", "'output_tensor_data'", ")", ")", "else", ":", "if", "len", "(", "set", "(", "split_list", ")", ")", "==", "1", ":", "num_outputs", "=", "len", "(", "split_list", ")", "else", ":", "raise", "NotImplementedError", "(", "\"Operator {} in MXNet does not support variable splits.\"", "\"Tracking the issue to support variable split here: \"", "\"https://github.com/apache/incubator-mxnet/issues/11594\"", ".", "format", "(", "'split'", ")", ")", "new_attrs", "[", "'num_outputs'", "]", "=", "num_outputs", "return", "'split'", ",", "new_attrs", ",", "inputs" ]
Splits an array along a particular axis into multiple sub-arrays.
[ "Splits", "an", "array", "along", "a", "particular", "axis", "into", "multiple", "sub", "-", "arrays", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L478-L498
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
_slice
def _slice(attrs, inputs, proto_obj): """Returns a slice of the input tensor along multiple axes.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes' : 'axis', 'ends' : 'end', 'starts' : 'begin'}) # onnx slice provides slicing on multiple axis. Adding multiple slice_axis operator # for multiple axes from mxnet begin = new_attrs.get('begin') end = new_attrs.get('end') axes = new_attrs.get('axis', tuple(range(len(begin)))) slice_op = symbol.slice_axis(inputs[0], axis=axes[0], begin=begin[0], end=end[0]) if len(axes) > 1: for i, axis in enumerate(axes): slice_op = symbol.slice_axis(slice_op, axis=axis, begin=begin[i], end=end[i]) return slice_op, new_attrs, inputs
python
def _slice(attrs, inputs, proto_obj): """Returns a slice of the input tensor along multiple axes.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes' : 'axis', 'ends' : 'end', 'starts' : 'begin'}) # onnx slice provides slicing on multiple axis. Adding multiple slice_axis operator # for multiple axes from mxnet begin = new_attrs.get('begin') end = new_attrs.get('end') axes = new_attrs.get('axis', tuple(range(len(begin)))) slice_op = symbol.slice_axis(inputs[0], axis=axes[0], begin=begin[0], end=end[0]) if len(axes) > 1: for i, axis in enumerate(axes): slice_op = symbol.slice_axis(slice_op, axis=axis, begin=begin[i], end=end[i]) return slice_op, new_attrs, inputs
[ "def", "_slice", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", ",", "'ends'", ":", "'end'", ",", "'starts'", ":", "'begin'", "}", ")", "# onnx slice provides slicing on multiple axis. Adding multiple slice_axis operator", "# for multiple axes from mxnet", "begin", "=", "new_attrs", ".", "get", "(", "'begin'", ")", "end", "=", "new_attrs", ".", "get", "(", "'end'", ")", "axes", "=", "new_attrs", ".", "get", "(", "'axis'", ",", "tuple", "(", "range", "(", "len", "(", "begin", ")", ")", ")", ")", "slice_op", "=", "symbol", ".", "slice_axis", "(", "inputs", "[", "0", "]", ",", "axis", "=", "axes", "[", "0", "]", ",", "begin", "=", "begin", "[", "0", "]", ",", "end", "=", "end", "[", "0", "]", ")", "if", "len", "(", "axes", ")", ">", "1", ":", "for", "i", ",", "axis", "in", "enumerate", "(", "axes", ")", ":", "slice_op", "=", "symbol", ".", "slice_axis", "(", "slice_op", ",", "axis", "=", "axis", ",", "begin", "=", "begin", "[", "i", "]", ",", "end", "=", "end", "[", "i", "]", ")", "return", "slice_op", ",", "new_attrs", ",", "inputs" ]
Returns a slice of the input tensor along multiple axes.
[ "Returns", "a", "slice", "of", "the", "input", "tensor", "along", "multiple", "axes", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L500-L515
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
transpose
def transpose(attrs, inputs, proto_obj): """Transpose the input array.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'perm' : 'axes'}) return 'transpose', new_attrs, inputs
python
def transpose(attrs, inputs, proto_obj): """Transpose the input array.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'perm' : 'axes'}) return 'transpose', new_attrs, inputs
[ "def", "transpose", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'perm'", ":", "'axes'", "}", ")", "return", "'transpose'", ",", "new_attrs", ",", "inputs" ]
Transpose the input array.
[ "Transpose", "the", "input", "array", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L517-L521
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
squeeze
def squeeze(attrs, inputs, proto_obj): """Remove single-dimensional entries from the shape of a tensor.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes' : 'axis'}) return 'squeeze', new_attrs, inputs
python
def squeeze(attrs, inputs, proto_obj): """Remove single-dimensional entries from the shape of a tensor.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes' : 'axis'}) return 'squeeze', new_attrs, inputs
[ "def", "squeeze", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'squeeze'", ",", "new_attrs", ",", "inputs" ]
Remove single-dimensional entries from the shape of a tensor.
[ "Remove", "single", "-", "dimensional", "entries", "from", "the", "shape", "of", "a", "tensor", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L523-L527
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
unsqueeze
def unsqueeze(attrs, inputs, cls): """Inserts a new axis of size 1 into the array shape""" # MXNet can only add one axis at a time. mxnet_op = inputs[0] for axis in attrs["axes"]: mxnet_op = symbol.expand_dims(mxnet_op, axis=axis) return mxnet_op, attrs, inputs
python
def unsqueeze(attrs, inputs, cls): """Inserts a new axis of size 1 into the array shape""" # MXNet can only add one axis at a time. mxnet_op = inputs[0] for axis in attrs["axes"]: mxnet_op = symbol.expand_dims(mxnet_op, axis=axis) return mxnet_op, attrs, inputs
[ "def", "unsqueeze", "(", "attrs", ",", "inputs", ",", "cls", ")", ":", "# MXNet can only add one axis at a time.", "mxnet_op", "=", "inputs", "[", "0", "]", "for", "axis", "in", "attrs", "[", "\"axes\"", "]", ":", "mxnet_op", "=", "symbol", ".", "expand_dims", "(", "mxnet_op", ",", "axis", "=", "axis", ")", "return", "mxnet_op", ",", "attrs", ",", "inputs" ]
Inserts a new axis of size 1 into the array shape
[ "Inserts", "a", "new", "axis", "of", "size", "1", "into", "the", "array", "shape" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L529-L536
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
flatten
def flatten(attrs, inputs, proto_obj): """Flattens the input array into a 2-D array by collapsing the higher dimensions.""" #Mxnet does not have axis support. By default uses axis=1 if 'axis' in attrs and attrs['axis'] != 1: raise RuntimeError("Flatten operator only supports axis=1") new_attrs = translation_utils._remove_attributes(attrs, ['axis']) return 'Flatten', new_attrs, inputs
python
def flatten(attrs, inputs, proto_obj): """Flattens the input array into a 2-D array by collapsing the higher dimensions.""" #Mxnet does not have axis support. By default uses axis=1 if 'axis' in attrs and attrs['axis'] != 1: raise RuntimeError("Flatten operator only supports axis=1") new_attrs = translation_utils._remove_attributes(attrs, ['axis']) return 'Flatten', new_attrs, inputs
[ "def", "flatten", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "#Mxnet does not have axis support. By default uses axis=1", "if", "'axis'", "in", "attrs", "and", "attrs", "[", "'axis'", "]", "!=", "1", ":", "raise", "RuntimeError", "(", "\"Flatten operator only supports axis=1\"", ")", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "attrs", ",", "[", "'axis'", "]", ")", "return", "'Flatten'", ",", "new_attrs", ",", "inputs" ]
Flattens the input array into a 2-D array by collapsing the higher dimensions.
[ "Flattens", "the", "input", "array", "into", "a", "2", "-", "D", "array", "by", "collapsing", "the", "higher", "dimensions", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L538-L544
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
clip
def clip(attrs, inputs, proto_obj): """Clips (limits) the values in an array.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'min' : 'a_min', 'max' : 'a_max'}) if 'a_max' not in new_attrs: new_attrs = translation_utils._add_extra_attributes(new_attrs, {'a_max' : np.inf}) if 'a_min' not in new_attrs: new_attrs = translation_utils._add_extra_attributes(new_attrs, {'a_min' : -np.inf}) return 'clip', new_attrs, inputs
python
def clip(attrs, inputs, proto_obj): """Clips (limits) the values in an array.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'min' : 'a_min', 'max' : 'a_max'}) if 'a_max' not in new_attrs: new_attrs = translation_utils._add_extra_attributes(new_attrs, {'a_max' : np.inf}) if 'a_min' not in new_attrs: new_attrs = translation_utils._add_extra_attributes(new_attrs, {'a_min' : -np.inf}) return 'clip', new_attrs, inputs
[ "def", "clip", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'min'", ":", "'a_min'", ",", "'max'", ":", "'a_max'", "}", ")", "if", "'a_max'", "not", "in", "new_attrs", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'a_max'", ":", "np", ".", "inf", "}", ")", "if", "'a_min'", "not", "in", "new_attrs", ":", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'a_min'", ":", "-", "np", ".", "inf", "}", ")", "return", "'clip'", ",", "new_attrs", ",", "inputs" ]
Clips (limits) the values in an array.
[ "Clips", "(", "limits", ")", "the", "values", "in", "an", "array", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L546-L554
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
power
def power(attrs, inputs, proto_obj): """Returns element-wise result of base element raised to powers from exp element.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'exponent':'exp'}) if 'broadcast' in attrs: new_attrs = translation_utils._remove_attributes(new_attrs, ['broadcast']) if attrs['broadcast'] == 1: return 'broadcast_power', new_attrs, inputs else: mxnet_op = symbol.pow(inputs[0], inputs[1]) return mxnet_op, new_attrs, inputs mxnet_op = symbol.broadcast_power(inputs[0], inputs[1]) return mxnet_op, new_attrs, inputs
python
def power(attrs, inputs, proto_obj): """Returns element-wise result of base element raised to powers from exp element.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'exponent':'exp'}) if 'broadcast' in attrs: new_attrs = translation_utils._remove_attributes(new_attrs, ['broadcast']) if attrs['broadcast'] == 1: return 'broadcast_power', new_attrs, inputs else: mxnet_op = symbol.pow(inputs[0], inputs[1]) return mxnet_op, new_attrs, inputs mxnet_op = symbol.broadcast_power(inputs[0], inputs[1]) return mxnet_op, new_attrs, inputs
[ "def", "power", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'exponent'", ":", "'exp'", "}", ")", "if", "'broadcast'", "in", "attrs", ":", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "new_attrs", ",", "[", "'broadcast'", "]", ")", "if", "attrs", "[", "'broadcast'", "]", "==", "1", ":", "return", "'broadcast_power'", ",", "new_attrs", ",", "inputs", "else", ":", "mxnet_op", "=", "symbol", ".", "pow", "(", "inputs", "[", "0", "]", ",", "inputs", "[", "1", "]", ")", "return", "mxnet_op", ",", "new_attrs", ",", "inputs", "mxnet_op", "=", "symbol", ".", "broadcast_power", "(", "inputs", "[", "0", "]", ",", "inputs", "[", "1", "]", ")", "return", "mxnet_op", ",", "new_attrs", ",", "inputs" ]
Returns element-wise result of base element raised to powers from exp element.
[ "Returns", "element", "-", "wise", "result", "of", "base", "element", "raised", "to", "powers", "from", "exp", "element", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L569-L580
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_max
def reduce_max(attrs, inputs, proto_obj): """Reduce the array along a given axis by maximum value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'max', new_attrs, inputs
python
def reduce_max(attrs, inputs, proto_obj): """Reduce the array along a given axis by maximum value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'max', new_attrs, inputs
[ "def", "reduce_max", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'max'", ",", "new_attrs", ",", "inputs" ]
Reduce the array along a given axis by maximum value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "maximum", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L615-L618
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_mean
def reduce_mean(attrs, inputs, proto_obj): """Reduce the array along a given axis by mean value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'mean', new_attrs, inputs
python
def reduce_mean(attrs, inputs, proto_obj): """Reduce the array along a given axis by mean value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'mean', new_attrs, inputs
[ "def", "reduce_mean", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'mean'", ",", "new_attrs", ",", "inputs" ]
Reduce the array along a given axis by mean value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "mean", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L620-L623
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_min
def reduce_min(attrs, inputs, proto_obj): """Reduce the array along a given axis by minimum value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'min', new_attrs, inputs
python
def reduce_min(attrs, inputs, proto_obj): """Reduce the array along a given axis by minimum value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'min', new_attrs, inputs
[ "def", "reduce_min", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'min'", ",", "new_attrs", ",", "inputs" ]
Reduce the array along a given axis by minimum value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "minimum", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L625-L628
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_sum
def reduce_sum(attrs, inputs, proto_obj): """Reduce the array along a given axis by sum value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'sum', new_attrs, inputs
python
def reduce_sum(attrs, inputs, proto_obj): """Reduce the array along a given axis by sum value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'sum', new_attrs, inputs
[ "def", "reduce_sum", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'sum'", ",", "new_attrs", ",", "inputs" ]
Reduce the array along a given axis by sum value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "sum", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L630-L633
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_prod
def reduce_prod(attrs, inputs, proto_obj): """Reduce the array along a given axis by product value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'prod', new_attrs, inputs
python
def reduce_prod(attrs, inputs, proto_obj): """Reduce the array along a given axis by product value""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'prod', new_attrs, inputs
[ "def", "reduce_prod", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'prod'", ",", "new_attrs", ",", "inputs" ]
Reduce the array along a given axis by product value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "product", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L635-L638
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_log_sum
def reduce_log_sum(attrs, inputs, proto_obj): """Reduce the array along a given axis by log sum value""" keep_dims = True if 'keepdims' not in attrs else attrs.get('keepdims') sum_op = symbol.sum(inputs[0], axis=attrs.get('axes'), keepdims=keep_dims) log_sym = symbol.log(sum_op) return log_sym, attrs, inputs
python
def reduce_log_sum(attrs, inputs, proto_obj): """Reduce the array along a given axis by log sum value""" keep_dims = True if 'keepdims' not in attrs else attrs.get('keepdims') sum_op = symbol.sum(inputs[0], axis=attrs.get('axes'), keepdims=keep_dims) log_sym = symbol.log(sum_op) return log_sym, attrs, inputs
[ "def", "reduce_log_sum", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "keep_dims", "=", "True", "if", "'keepdims'", "not", "in", "attrs", "else", "attrs", ".", "get", "(", "'keepdims'", ")", "sum_op", "=", "symbol", ".", "sum", "(", "inputs", "[", "0", "]", ",", "axis", "=", "attrs", ".", "get", "(", "'axes'", ")", ",", "keepdims", "=", "keep_dims", ")", "log_sym", "=", "symbol", ".", "log", "(", "sum_op", ")", "return", "log_sym", ",", "attrs", ",", "inputs" ]
Reduce the array along a given axis by log sum value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "log", "sum", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L640-L646
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_log_sum_exp
def reduce_log_sum_exp(attrs, inputs, proto_obj): """Reduce the array along a given axis by log sum exp value""" keep_dims = True if 'keepdims' not in attrs else attrs.get('keepdims') exp_op = symbol.exp(inputs[0]) sum_op = symbol.sum(exp_op, axis=attrs.get('axes'), keepdims=keep_dims) log_sym = symbol.log(sum_op) return log_sym, attrs, inputs
python
def reduce_log_sum_exp(attrs, inputs, proto_obj): """Reduce the array along a given axis by log sum exp value""" keep_dims = True if 'keepdims' not in attrs else attrs.get('keepdims') exp_op = symbol.exp(inputs[0]) sum_op = symbol.sum(exp_op, axis=attrs.get('axes'), keepdims=keep_dims) log_sym = symbol.log(sum_op) return log_sym, attrs, inputs
[ "def", "reduce_log_sum_exp", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "keep_dims", "=", "True", "if", "'keepdims'", "not", "in", "attrs", "else", "attrs", ".", "get", "(", "'keepdims'", ")", "exp_op", "=", "symbol", ".", "exp", "(", "inputs", "[", "0", "]", ")", "sum_op", "=", "symbol", ".", "sum", "(", "exp_op", ",", "axis", "=", "attrs", ".", "get", "(", "'axes'", ")", ",", "keepdims", "=", "keep_dims", ")", "log_sym", "=", "symbol", ".", "log", "(", "sum_op", ")", "return", "log_sym", ",", "attrs", ",", "inputs" ]
Reduce the array along a given axis by log sum exp value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "log", "sum", "exp", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L648-L655
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_sum_square
def reduce_sum_square(attrs, inputs, proto_obj): """Reduce the array along a given axis by sum square value""" square_op = symbol.square(inputs[0]) sum_op = symbol.sum(square_op, axis=attrs.get('axes'), keepdims=attrs.get('keepdims')) return sum_op, attrs, inputs
python
def reduce_sum_square(attrs, inputs, proto_obj): """Reduce the array along a given axis by sum square value""" square_op = symbol.square(inputs[0]) sum_op = symbol.sum(square_op, axis=attrs.get('axes'), keepdims=attrs.get('keepdims')) return sum_op, attrs, inputs
[ "def", "reduce_sum_square", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "square_op", "=", "symbol", ".", "square", "(", "inputs", "[", "0", "]", ")", "sum_op", "=", "symbol", ".", "sum", "(", "square_op", ",", "axis", "=", "attrs", ".", "get", "(", "'axes'", ")", ",", "keepdims", "=", "attrs", ".", "get", "(", "'keepdims'", ")", ")", "return", "sum_op", ",", "attrs", ",", "inputs" ]
Reduce the array along a given axis by sum square value
[ "Reduce", "the", "array", "along", "a", "given", "axis", "by", "sum", "square", "value" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L657-L662
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_l1
def reduce_l1(attrs, inputs, proto_obj): """Reduce input tensor by l1 normalization.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'ord' : 1}) return 'norm', new_attrs, inputs
python
def reduce_l1(attrs, inputs, proto_obj): """Reduce input tensor by l1 normalization.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'ord' : 1}) return 'norm', new_attrs, inputs
[ "def", "reduce_l1", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'ord'", ":", "1", "}", ")", "return", "'norm'", ",", "new_attrs", ",", "inputs" ]
Reduce input tensor by l1 normalization.
[ "Reduce", "input", "tensor", "by", "l1", "normalization", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L664-L669
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
reduce_l2
def reduce_l2(attrs, inputs, proto_obj): """Reduce input tensor by l2 normalization.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'norm', new_attrs, inputs
python
def reduce_l2(attrs, inputs, proto_obj): """Reduce input tensor by l2 normalization.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'axes':'axis'}) return 'norm', new_attrs, inputs
[ "def", "reduce_l2", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'axes'", ":", "'axis'", "}", ")", "return", "'norm'", ",", "new_attrs", ",", "inputs" ]
Reduce input tensor by l2 normalization.
[ "Reduce", "input", "tensor", "by", "l2", "normalization", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L679-L682
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
avg_pooling
def avg_pooling(attrs, inputs, proto_obj): """ Average pooling""" new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape': 'kernel', 'strides': 'stride', 'pads': 'pad', }) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'pooling_convention': 'valid' }) new_op = translation_utils._fix_pooling('avg', inputs, new_attrs) return new_op, new_attrs, inputs
python
def avg_pooling(attrs, inputs, proto_obj): """ Average pooling""" new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape': 'kernel', 'strides': 'stride', 'pads': 'pad', }) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'pooling_convention': 'valid' }) new_op = translation_utils._fix_pooling('avg', inputs, new_attrs) return new_op, new_attrs, inputs
[ "def", "avg_pooling", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'kernel_shape'", ":", "'kernel'", ",", "'strides'", ":", "'stride'", ",", "'pads'", ":", "'pad'", ",", "}", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'pooling_convention'", ":", "'valid'", "}", ")", "new_op", "=", "translation_utils", ".", "_fix_pooling", "(", "'avg'", ",", "inputs", ",", "new_attrs", ")", "return", "new_op", ",", "new_attrs", ",", "inputs" ]
Average pooling
[ "Average", "pooling" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L684-L696
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
lp_pooling
def lp_pooling(attrs, inputs, proto_obj): """LP Pooling""" p_value = attrs.get('p', 2) new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape': 'kernel', 'strides': 'stride', 'pads': 'pad' }) new_attrs = translation_utils._remove_attributes(new_attrs, ['p']) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'pooling_convention': 'valid', 'p_value': p_value }) new_op = translation_utils._fix_pooling('lp', inputs, new_attrs) return new_op, new_attrs, inputs
python
def lp_pooling(attrs, inputs, proto_obj): """LP Pooling""" p_value = attrs.get('p', 2) new_attrs = translation_utils._fix_attribute_names(attrs, {'kernel_shape': 'kernel', 'strides': 'stride', 'pads': 'pad' }) new_attrs = translation_utils._remove_attributes(new_attrs, ['p']) new_attrs = translation_utils._add_extra_attributes(new_attrs, {'pooling_convention': 'valid', 'p_value': p_value }) new_op = translation_utils._fix_pooling('lp', inputs, new_attrs) return new_op, new_attrs, inputs
[ "def", "lp_pooling", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "p_value", "=", "attrs", ".", "get", "(", "'p'", ",", "2", ")", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'kernel_shape'", ":", "'kernel'", ",", "'strides'", ":", "'stride'", ",", "'pads'", ":", "'pad'", "}", ")", "new_attrs", "=", "translation_utils", ".", "_remove_attributes", "(", "new_attrs", ",", "[", "'p'", "]", ")", "new_attrs", "=", "translation_utils", ".", "_add_extra_attributes", "(", "new_attrs", ",", "{", "'pooling_convention'", ":", "'valid'", ",", "'p_value'", ":", "p_value", "}", ")", "new_op", "=", "translation_utils", ".", "_fix_pooling", "(", "'lp'", ",", "inputs", ",", "new_attrs", ")", "return", "new_op", ",", "new_attrs", ",", "inputs" ]
LP Pooling
[ "LP", "Pooling" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L698-L712
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
max_roi_pooling
def max_roi_pooling(attrs, inputs, proto_obj): """Max ROI Pooling.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'pooled_shape': 'pooled_size', 'spatial_scale': 'spatial_scale' }) return 'ROIPooling', new_attrs, inputs
python
def max_roi_pooling(attrs, inputs, proto_obj): """Max ROI Pooling.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'pooled_shape': 'pooled_size', 'spatial_scale': 'spatial_scale' }) return 'ROIPooling', new_attrs, inputs
[ "def", "max_roi_pooling", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'pooled_shape'", ":", "'pooled_size'", ",", "'spatial_scale'", ":", "'spatial_scale'", "}", ")", "return", "'ROIPooling'", ",", "new_attrs", ",", "inputs" ]
Max ROI Pooling.
[ "Max", "ROI", "Pooling", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L729-L735
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
depthtospace
def depthtospace(attrs, inputs, proto_obj): """Rearranges data from depth into blocks of spatial data.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'blocksize':'block_size'}) return "depth_to_space", new_attrs, inputs
python
def depthtospace(attrs, inputs, proto_obj): """Rearranges data from depth into blocks of spatial data.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'blocksize':'block_size'}) return "depth_to_space", new_attrs, inputs
[ "def", "depthtospace", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'blocksize'", ":", "'block_size'", "}", ")", "return", "\"depth_to_space\"", ",", "new_attrs", ",", "inputs" ]
Rearranges data from depth into blocks of spatial data.
[ "Rearranges", "data", "from", "depth", "into", "blocks", "of", "spatial", "data", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L737-L741
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
spacetodepth
def spacetodepth(attrs, inputs, proto_obj): """Rearranges blocks of spatial data into depth.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'blocksize':'block_size'}) return "space_to_depth", new_attrs, inputs
python
def spacetodepth(attrs, inputs, proto_obj): """Rearranges blocks of spatial data into depth.""" new_attrs = translation_utils._fix_attribute_names(attrs, {'blocksize':'block_size'}) return "space_to_depth", new_attrs, inputs
[ "def", "spacetodepth", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'blocksize'", ":", "'block_size'", "}", ")", "return", "\"space_to_depth\"", ",", "new_attrs", ",", "inputs" ]
Rearranges blocks of spatial data into depth.
[ "Rearranges", "blocks", "of", "spatial", "data", "into", "depth", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L743-L747
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
hardmax
def hardmax(attrs, inputs, proto_obj): """Returns batched one-hot vectors.""" input_tensor_data = proto_obj.model_metadata.get('input_tensor_data')[0] input_shape = input_tensor_data[1] axis = int(attrs.get('axis', 1)) axis = axis if axis >= 0 else len(input_shape) + axis if axis == len(input_shape) - 1: amax = symbol.argmax(inputs[0], axis=-1) one_hot = symbol.one_hot(amax, depth=input_shape[-1]) return one_hot, attrs, inputs # since reshape doesn't take a tensor for shape, # computing with np.prod. This needs to be changed to # to use mx.sym.prod() when mx.sym.reshape() is fixed. # (https://github.com/apache/incubator-mxnet/issues/10789) new_shape = (int(np.prod(input_shape[:axis])), int(np.prod(input_shape[axis:]))) reshape_op = symbol.reshape(inputs[0], new_shape) amax = symbol.argmax(reshape_op, axis=-1) one_hot = symbol.one_hot(amax, depth=new_shape[-1]) hardmax_op = symbol.reshape(one_hot, input_shape) return hardmax_op, attrs, inputs
python
def hardmax(attrs, inputs, proto_obj): """Returns batched one-hot vectors.""" input_tensor_data = proto_obj.model_metadata.get('input_tensor_data')[0] input_shape = input_tensor_data[1] axis = int(attrs.get('axis', 1)) axis = axis if axis >= 0 else len(input_shape) + axis if axis == len(input_shape) - 1: amax = symbol.argmax(inputs[0], axis=-1) one_hot = symbol.one_hot(amax, depth=input_shape[-1]) return one_hot, attrs, inputs # since reshape doesn't take a tensor for shape, # computing with np.prod. This needs to be changed to # to use mx.sym.prod() when mx.sym.reshape() is fixed. # (https://github.com/apache/incubator-mxnet/issues/10789) new_shape = (int(np.prod(input_shape[:axis])), int(np.prod(input_shape[axis:]))) reshape_op = symbol.reshape(inputs[0], new_shape) amax = symbol.argmax(reshape_op, axis=-1) one_hot = symbol.one_hot(amax, depth=new_shape[-1]) hardmax_op = symbol.reshape(one_hot, input_shape) return hardmax_op, attrs, inputs
[ "def", "hardmax", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "input_tensor_data", "=", "proto_obj", ".", "model_metadata", ".", "get", "(", "'input_tensor_data'", ")", "[", "0", "]", "input_shape", "=", "input_tensor_data", "[", "1", "]", "axis", "=", "int", "(", "attrs", ".", "get", "(", "'axis'", ",", "1", ")", ")", "axis", "=", "axis", "if", "axis", ">=", "0", "else", "len", "(", "input_shape", ")", "+", "axis", "if", "axis", "==", "len", "(", "input_shape", ")", "-", "1", ":", "amax", "=", "symbol", ".", "argmax", "(", "inputs", "[", "0", "]", ",", "axis", "=", "-", "1", ")", "one_hot", "=", "symbol", ".", "one_hot", "(", "amax", ",", "depth", "=", "input_shape", "[", "-", "1", "]", ")", "return", "one_hot", ",", "attrs", ",", "inputs", "# since reshape doesn't take a tensor for shape,", "# computing with np.prod. This needs to be changed to", "# to use mx.sym.prod() when mx.sym.reshape() is fixed.", "# (https://github.com/apache/incubator-mxnet/issues/10789)", "new_shape", "=", "(", "int", "(", "np", ".", "prod", "(", "input_shape", "[", ":", "axis", "]", ")", ")", ",", "int", "(", "np", ".", "prod", "(", "input_shape", "[", "axis", ":", "]", ")", ")", ")", "reshape_op", "=", "symbol", ".", "reshape", "(", "inputs", "[", "0", "]", ",", "new_shape", ")", "amax", "=", "symbol", ".", "argmax", "(", "reshape_op", ",", "axis", "=", "-", "1", ")", "one_hot", "=", "symbol", ".", "one_hot", "(", "amax", ",", "depth", "=", "new_shape", "[", "-", "1", "]", ")", "hardmax_op", "=", "symbol", ".", "reshape", "(", "one_hot", ",", "input_shape", ")", "return", "hardmax_op", ",", "attrs", ",", "inputs" ]
Returns batched one-hot vectors.
[ "Returns", "batched", "one", "-", "hot", "vectors", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L749-L772
train
apache/incubator-mxnet
python/mxnet/contrib/onnx/onnx2mx/_op_translations.py
lpnormalization
def lpnormalization(attrs, inputs, proto_obj): """ONNX does not have eps attribute, so cannot map it to L2normalization in MXNet without that, it works as norm operator discussion in PR: https://github.com/onnx/onnx/pull/1330""" new_attrs = translation_utils._fix_attribute_names(attrs, {'p': 'ord'}) axis = int(attrs.get("axis", -1)) new_attrs.update(axis=axis) return 'norm', new_attrs, inputs
python
def lpnormalization(attrs, inputs, proto_obj): """ONNX does not have eps attribute, so cannot map it to L2normalization in MXNet without that, it works as norm operator discussion in PR: https://github.com/onnx/onnx/pull/1330""" new_attrs = translation_utils._fix_attribute_names(attrs, {'p': 'ord'}) axis = int(attrs.get("axis", -1)) new_attrs.update(axis=axis) return 'norm', new_attrs, inputs
[ "def", "lpnormalization", "(", "attrs", ",", "inputs", ",", "proto_obj", ")", ":", "new_attrs", "=", "translation_utils", ".", "_fix_attribute_names", "(", "attrs", ",", "{", "'p'", ":", "'ord'", "}", ")", "axis", "=", "int", "(", "attrs", ".", "get", "(", "\"axis\"", ",", "-", "1", ")", ")", "new_attrs", ".", "update", "(", "axis", "=", "axis", ")", "return", "'norm'", ",", "new_attrs", ",", "inputs" ]
ONNX does not have eps attribute, so cannot map it to L2normalization in MXNet without that, it works as norm operator discussion in PR: https://github.com/onnx/onnx/pull/1330
[ "ONNX", "does", "not", "have", "eps", "attribute", "so", "cannot", "map", "it", "to", "L2normalization", "in", "MXNet", "without", "that", "it", "works", "as", "norm", "operator", "discussion", "in", "PR", ":", "https", ":", "//", "github", ".", "com", "/", "onnx", "/", "onnx", "/", "pull", "/", "1330" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/contrib/onnx/onnx2mx/_op_translations.py#L774-L781
train
apache/incubator-mxnet
example/gluon/lipnet/utils/download_data.py
download_mp4
def download_mp4(from_idx, to_idx, _params): """ download mp4s """ succ = set() fail = set() for idx in range(from_idx, to_idx): name = 's' + str(idx) save_folder = '{src_path}/{nm}'.format(src_path=_params['src_path'], nm=name) if idx == 0 or os.path.isdir(save_folder): continue script = "http://spandh.dcs.shef.ac.uk/gridcorpus/{nm}/video/{nm}.mpg_vcd.zip".format( \ nm=name) down_sc = 'cd {src_path} && curl {script} --output {nm}.mpg_vcd.zip && \ unzip {nm}.mpg_vcd.zip'.format(script=script, nm=name, src_path=_params['src_path']) try: print(down_sc) os.system(down_sc) succ.add(idx) except OSError as error: print(error) fail.add(idx) return (succ, fail)
python
def download_mp4(from_idx, to_idx, _params): """ download mp4s """ succ = set() fail = set() for idx in range(from_idx, to_idx): name = 's' + str(idx) save_folder = '{src_path}/{nm}'.format(src_path=_params['src_path'], nm=name) if idx == 0 or os.path.isdir(save_folder): continue script = "http://spandh.dcs.shef.ac.uk/gridcorpus/{nm}/video/{nm}.mpg_vcd.zip".format( \ nm=name) down_sc = 'cd {src_path} && curl {script} --output {nm}.mpg_vcd.zip && \ unzip {nm}.mpg_vcd.zip'.format(script=script, nm=name, src_path=_params['src_path']) try: print(down_sc) os.system(down_sc) succ.add(idx) except OSError as error: print(error) fail.add(idx) return (succ, fail)
[ "def", "download_mp4", "(", "from_idx", ",", "to_idx", ",", "_params", ")", ":", "succ", "=", "set", "(", ")", "fail", "=", "set", "(", ")", "for", "idx", "in", "range", "(", "from_idx", ",", "to_idx", ")", ":", "name", "=", "'s'", "+", "str", "(", "idx", ")", "save_folder", "=", "'{src_path}/{nm}'", ".", "format", "(", "src_path", "=", "_params", "[", "'src_path'", "]", ",", "nm", "=", "name", ")", "if", "idx", "==", "0", "or", "os", ".", "path", ".", "isdir", "(", "save_folder", ")", ":", "continue", "script", "=", "\"http://spandh.dcs.shef.ac.uk/gridcorpus/{nm}/video/{nm}.mpg_vcd.zip\"", ".", "format", "(", "nm", "=", "name", ")", "down_sc", "=", "'cd {src_path} && curl {script} --output {nm}.mpg_vcd.zip && \\\n unzip {nm}.mpg_vcd.zip'", ".", "format", "(", "script", "=", "script", ",", "nm", "=", "name", ",", "src_path", "=", "_params", "[", "'src_path'", "]", ")", "try", ":", "print", "(", "down_sc", ")", "os", ".", "system", "(", "down_sc", ")", "succ", ".", "add", "(", "idx", ")", "except", "OSError", "as", "error", ":", "print", "(", "error", ")", "fail", ".", "add", "(", "idx", ")", "return", "(", "succ", ",", "fail", ")" ]
download mp4s
[ "download", "mp4s" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/lipnet/utils/download_data.py#L28-L52
train
apache/incubator-mxnet
example/gluon/lipnet/utils/download_data.py
download_align
def download_align(from_idx, to_idx, _params): """ download aligns """ succ = set() fail = set() for idx in range(from_idx, to_idx): name = 's' + str(idx) if idx == 0: continue script = "http://spandh.dcs.shef.ac.uk/gridcorpus/{nm}/align/{nm}.tar".format(nm=name) down_sc = 'cd {align_path} && wget {script} && \ tar -xvf {nm}.tar'.format(script=script, nm=name, align_path=_params['align_path']) try: print(down_sc) os.system(down_sc) succ.add(idx) except OSError as error: print(error) fail.add(idx) return (succ, fail)
python
def download_align(from_idx, to_idx, _params): """ download aligns """ succ = set() fail = set() for idx in range(from_idx, to_idx): name = 's' + str(idx) if idx == 0: continue script = "http://spandh.dcs.shef.ac.uk/gridcorpus/{nm}/align/{nm}.tar".format(nm=name) down_sc = 'cd {align_path} && wget {script} && \ tar -xvf {nm}.tar'.format(script=script, nm=name, align_path=_params['align_path']) try: print(down_sc) os.system(down_sc) succ.add(idx) except OSError as error: print(error) fail.add(idx) return (succ, fail)
[ "def", "download_align", "(", "from_idx", ",", "to_idx", ",", "_params", ")", ":", "succ", "=", "set", "(", ")", "fail", "=", "set", "(", ")", "for", "idx", "in", "range", "(", "from_idx", ",", "to_idx", ")", ":", "name", "=", "'s'", "+", "str", "(", "idx", ")", "if", "idx", "==", "0", ":", "continue", "script", "=", "\"http://spandh.dcs.shef.ac.uk/gridcorpus/{nm}/align/{nm}.tar\"", ".", "format", "(", "nm", "=", "name", ")", "down_sc", "=", "'cd {align_path} && wget {script} && \\\n tar -xvf {nm}.tar'", ".", "format", "(", "script", "=", "script", ",", "nm", "=", "name", ",", "align_path", "=", "_params", "[", "'align_path'", "]", ")", "try", ":", "print", "(", "down_sc", ")", "os", ".", "system", "(", "down_sc", ")", "succ", ".", "add", "(", "idx", ")", "except", "OSError", "as", "error", ":", "print", "(", "error", ")", "fail", ".", "add", "(", "idx", ")", "return", "(", "succ", ",", "fail", ")" ]
download aligns
[ "download", "aligns" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/gluon/lipnet/utils/download_data.py#L55-L77
train
apache/incubator-mxnet
ci/docker/qemu/runtime_functions.py
run_ut_py3_qemu
def run_ut_py3_qemu(): """Run unit tests in the emulator and copy the results back to the host through the mounted volume in /mxnet""" from vmcontrol import VM with VM() as vm: qemu_provision(vm.ssh_port) logging.info("execute tests") qemu_ssh(vm.ssh_port, "./runtime_functions.py", "run_ut_python3_qemu_internal") qemu_rsync_to_host(vm.ssh_port, "*.xml", "mxnet") logging.info("copied to host") logging.info("tests finished, vm shutdown.") vm.shutdown()
python
def run_ut_py3_qemu(): """Run unit tests in the emulator and copy the results back to the host through the mounted volume in /mxnet""" from vmcontrol import VM with VM() as vm: qemu_provision(vm.ssh_port) logging.info("execute tests") qemu_ssh(vm.ssh_port, "./runtime_functions.py", "run_ut_python3_qemu_internal") qemu_rsync_to_host(vm.ssh_port, "*.xml", "mxnet") logging.info("copied to host") logging.info("tests finished, vm shutdown.") vm.shutdown()
[ "def", "run_ut_py3_qemu", "(", ")", ":", "from", "vmcontrol", "import", "VM", "with", "VM", "(", ")", "as", "vm", ":", "qemu_provision", "(", "vm", ".", "ssh_port", ")", "logging", ".", "info", "(", "\"execute tests\"", ")", "qemu_ssh", "(", "vm", ".", "ssh_port", ",", "\"./runtime_functions.py\"", ",", "\"run_ut_python3_qemu_internal\"", ")", "qemu_rsync_to_host", "(", "vm", ".", "ssh_port", ",", "\"*.xml\"", ",", "\"mxnet\"", ")", "logging", ".", "info", "(", "\"copied to host\"", ")", "logging", ".", "info", "(", "\"tests finished, vm shutdown.\"", ")", "vm", ".", "shutdown", "(", ")" ]
Run unit tests in the emulator and copy the results back to the host through the mounted volume in /mxnet
[ "Run", "unit", "tests", "in", "the", "emulator", "and", "copy", "the", "results", "back", "to", "the", "host", "through", "the", "mounted", "volume", "in", "/", "mxnet" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker/qemu/runtime_functions.py#L61-L72
train
apache/incubator-mxnet
ci/docker/qemu/runtime_functions.py
run_ut_python3_qemu_internal
def run_ut_python3_qemu_internal(): """this runs inside the vm""" pkg = glob.glob('mxnet_dist/*.whl')[0] logging.info("=== NOW Running inside QEMU ===") logging.info("PIP Installing %s", pkg) check_call(['sudo', 'pip3', 'install', pkg]) logging.info("PIP Installing mxnet/test_requirements.txt") check_call(['sudo', 'pip3', 'install', '-r', 'mxnet/test_requirements.txt']) logging.info("Running tests in mxnet/tests/python/unittest/") check_call(['nosetests', '--with-timer', '--with-xunit', '--xunit-file', 'nosetests_unittest.xml', '--verbose', 'mxnet/tests/python/unittest/test_engine.py'])
python
def run_ut_python3_qemu_internal(): """this runs inside the vm""" pkg = glob.glob('mxnet_dist/*.whl')[0] logging.info("=== NOW Running inside QEMU ===") logging.info("PIP Installing %s", pkg) check_call(['sudo', 'pip3', 'install', pkg]) logging.info("PIP Installing mxnet/test_requirements.txt") check_call(['sudo', 'pip3', 'install', '-r', 'mxnet/test_requirements.txt']) logging.info("Running tests in mxnet/tests/python/unittest/") check_call(['nosetests', '--with-timer', '--with-xunit', '--xunit-file', 'nosetests_unittest.xml', '--verbose', 'mxnet/tests/python/unittest/test_engine.py'])
[ "def", "run_ut_python3_qemu_internal", "(", ")", ":", "pkg", "=", "glob", ".", "glob", "(", "'mxnet_dist/*.whl'", ")", "[", "0", "]", "logging", ".", "info", "(", "\"=== NOW Running inside QEMU ===\"", ")", "logging", ".", "info", "(", "\"PIP Installing %s\"", ",", "pkg", ")", "check_call", "(", "[", "'sudo'", ",", "'pip3'", ",", "'install'", ",", "pkg", "]", ")", "logging", ".", "info", "(", "\"PIP Installing mxnet/test_requirements.txt\"", ")", "check_call", "(", "[", "'sudo'", ",", "'pip3'", ",", "'install'", ",", "'-r'", ",", "'mxnet/test_requirements.txt'", "]", ")", "logging", ".", "info", "(", "\"Running tests in mxnet/tests/python/unittest/\"", ")", "check_call", "(", "[", "'nosetests'", ",", "'--with-timer'", ",", "'--with-xunit'", ",", "'--xunit-file'", ",", "'nosetests_unittest.xml'", ",", "'--verbose'", ",", "'mxnet/tests/python/unittest/test_engine.py'", "]", ")" ]
this runs inside the vm
[ "this", "runs", "inside", "the", "vm" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/ci/docker/qemu/runtime_functions.py#L74-L83
train
apache/incubator-mxnet
example/nce-loss/text8_data.py
_get_subword_units
def _get_subword_units(token, gram): """Return subword-units presentation, given a word/token. """ if token == '</s>': # special token for padding purpose. return [token] t = '#' + token + '#' return [t[i:i + gram] for i in range(0, len(t) - gram + 1)]
python
def _get_subword_units(token, gram): """Return subword-units presentation, given a word/token. """ if token == '</s>': # special token for padding purpose. return [token] t = '#' + token + '#' return [t[i:i + gram] for i in range(0, len(t) - gram + 1)]
[ "def", "_get_subword_units", "(", "token", ",", "gram", ")", ":", "if", "token", "==", "'</s>'", ":", "# special token for padding purpose.", "return", "[", "token", "]", "t", "=", "'#'", "+", "token", "+", "'#'", "return", "[", "t", "[", "i", ":", "i", "+", "gram", "]", "for", "i", "in", "range", "(", "0", ",", "len", "(", "t", ")", "-", "gram", "+", "1", ")", "]" ]
Return subword-units presentation, given a word/token.
[ "Return", "subword", "-", "units", "presentation", "given", "a", "word", "/", "token", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/nce-loss/text8_data.py#L68-L74
train
apache/incubator-mxnet
example/caffe/train_model.py
fit
def fit(args, network, data_loader, eval_metrics=None, batch_end_callback=None): """Train the model using Caffe operator in MXNet""" # kvstore kv = mx.kvstore.create(args.kv_store) # logging head = '%(asctime)-15s Node[' + str(kv.rank) + '] %(message)s' if 'log_file' in args and args.log_file is not None: log_file = args.log_file log_dir = args.log_dir log_file_full_name = os.path.join(log_dir, log_file) if not os.path.exists(log_dir): os.mkdir(log_dir) logger = logging.getLogger() handler = logging.FileHandler(log_file_full_name) formatter = logging.Formatter(head) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) logger.info('start with arguments %s', args) else: logging.basicConfig(level=logging.DEBUG, format=head) logging.info('start with arguments %s', args) # load model model_prefix = args.model_prefix if model_prefix is not None: model_prefix += "-%d" % (kv.rank) model_args = {} if args.load_epoch is not None: assert model_prefix is not None tmp = mx.model.FeedForward.load(model_prefix, args.load_epoch) model_args = {'arg_params' : tmp.arg_params, 'aux_params' : tmp.aux_params, 'begin_epoch' : args.load_epoch} # save model save_model_prefix = args.save_model_prefix if save_model_prefix is None: save_model_prefix = model_prefix checkpoint = None if save_model_prefix is None else mx.callback.do_checkpoint(save_model_prefix) # data (train, val) = data_loader(args, kv) # train devs = mx.cpu() if args.gpus is None else [ mx.gpu(int(i)) for i in args.gpus.split(',')] epoch_size = args.num_examples / args.batch_size if args.kv_store == 'dist_sync': epoch_size /= kv.num_workers model_args['epoch_size'] = epoch_size if 'lr_factor' in args and args.lr_factor < 1: model_args['lr_scheduler'] = mx.lr_scheduler.FactorScheduler( step=max(int(epoch_size * args.lr_factor_epoch), 1), factor=args.lr_factor) if 'clip_gradient' in args and args.clip_gradient is not None: model_args['clip_gradient'] = args.clip_gradient # disable kvstore for single device if 'local' in kv.type and ( args.gpus is None or len(args.gpus.split(',')) is 1): kv = None mod = mx.mod.Module(network, context=devs) if eval_metrics is None: eval_metrics = ['accuracy'] # TopKAccuracy only allows top_k > 1 for top_k in [5, 10, 20]: eval_metrics.append(mx.metric.create('top_k_accuracy', top_k=top_k)) if batch_end_callback is not None: if not isinstance(batch_end_callback, list): batch_end_callback = [batch_end_callback] else: batch_end_callback = [] batch_end_callback.append(mx.callback.Speedometer(args.batch_size, 50)) mod.fit(train_data=train, eval_metric=eval_metrics, eval_data=val, optimizer='sgd', optimizer_params={'learning_rate':args.lr, 'momentum': 0.9, 'wd': 0.00001}, num_epoch=args.num_epochs, batch_end_callback=batch_end_callback, initializer=mx.init.Xavier(factor_type="in", magnitude=2.34), kvstore=kv, epoch_end_callback=checkpoint, **model_args)
python
def fit(args, network, data_loader, eval_metrics=None, batch_end_callback=None): """Train the model using Caffe operator in MXNet""" # kvstore kv = mx.kvstore.create(args.kv_store) # logging head = '%(asctime)-15s Node[' + str(kv.rank) + '] %(message)s' if 'log_file' in args and args.log_file is not None: log_file = args.log_file log_dir = args.log_dir log_file_full_name = os.path.join(log_dir, log_file) if not os.path.exists(log_dir): os.mkdir(log_dir) logger = logging.getLogger() handler = logging.FileHandler(log_file_full_name) formatter = logging.Formatter(head) handler.setFormatter(formatter) logger.addHandler(handler) logger.setLevel(logging.DEBUG) logger.info('start with arguments %s', args) else: logging.basicConfig(level=logging.DEBUG, format=head) logging.info('start with arguments %s', args) # load model model_prefix = args.model_prefix if model_prefix is not None: model_prefix += "-%d" % (kv.rank) model_args = {} if args.load_epoch is not None: assert model_prefix is not None tmp = mx.model.FeedForward.load(model_prefix, args.load_epoch) model_args = {'arg_params' : tmp.arg_params, 'aux_params' : tmp.aux_params, 'begin_epoch' : args.load_epoch} # save model save_model_prefix = args.save_model_prefix if save_model_prefix is None: save_model_prefix = model_prefix checkpoint = None if save_model_prefix is None else mx.callback.do_checkpoint(save_model_prefix) # data (train, val) = data_loader(args, kv) # train devs = mx.cpu() if args.gpus is None else [ mx.gpu(int(i)) for i in args.gpus.split(',')] epoch_size = args.num_examples / args.batch_size if args.kv_store == 'dist_sync': epoch_size /= kv.num_workers model_args['epoch_size'] = epoch_size if 'lr_factor' in args and args.lr_factor < 1: model_args['lr_scheduler'] = mx.lr_scheduler.FactorScheduler( step=max(int(epoch_size * args.lr_factor_epoch), 1), factor=args.lr_factor) if 'clip_gradient' in args and args.clip_gradient is not None: model_args['clip_gradient'] = args.clip_gradient # disable kvstore for single device if 'local' in kv.type and ( args.gpus is None or len(args.gpus.split(',')) is 1): kv = None mod = mx.mod.Module(network, context=devs) if eval_metrics is None: eval_metrics = ['accuracy'] # TopKAccuracy only allows top_k > 1 for top_k in [5, 10, 20]: eval_metrics.append(mx.metric.create('top_k_accuracy', top_k=top_k)) if batch_end_callback is not None: if not isinstance(batch_end_callback, list): batch_end_callback = [batch_end_callback] else: batch_end_callback = [] batch_end_callback.append(mx.callback.Speedometer(args.batch_size, 50)) mod.fit(train_data=train, eval_metric=eval_metrics, eval_data=val, optimizer='sgd', optimizer_params={'learning_rate':args.lr, 'momentum': 0.9, 'wd': 0.00001}, num_epoch=args.num_epochs, batch_end_callback=batch_end_callback, initializer=mx.init.Xavier(factor_type="in", magnitude=2.34), kvstore=kv, epoch_end_callback=checkpoint, **model_args)
[ "def", "fit", "(", "args", ",", "network", ",", "data_loader", ",", "eval_metrics", "=", "None", ",", "batch_end_callback", "=", "None", ")", ":", "# kvstore", "kv", "=", "mx", ".", "kvstore", ".", "create", "(", "args", ".", "kv_store", ")", "# logging", "head", "=", "'%(asctime)-15s Node['", "+", "str", "(", "kv", ".", "rank", ")", "+", "'] %(message)s'", "if", "'log_file'", "in", "args", "and", "args", ".", "log_file", "is", "not", "None", ":", "log_file", "=", "args", ".", "log_file", "log_dir", "=", "args", ".", "log_dir", "log_file_full_name", "=", "os", ".", "path", ".", "join", "(", "log_dir", ",", "log_file", ")", "if", "not", "os", ".", "path", ".", "exists", "(", "log_dir", ")", ":", "os", ".", "mkdir", "(", "log_dir", ")", "logger", "=", "logging", ".", "getLogger", "(", ")", "handler", "=", "logging", ".", "FileHandler", "(", "log_file_full_name", ")", "formatter", "=", "logging", ".", "Formatter", "(", "head", ")", "handler", ".", "setFormatter", "(", "formatter", ")", "logger", ".", "addHandler", "(", "handler", ")", "logger", ".", "setLevel", "(", "logging", ".", "DEBUG", ")", "logger", ".", "info", "(", "'start with arguments %s'", ",", "args", ")", "else", ":", "logging", ".", "basicConfig", "(", "level", "=", "logging", ".", "DEBUG", ",", "format", "=", "head", ")", "logging", ".", "info", "(", "'start with arguments %s'", ",", "args", ")", "# load model", "model_prefix", "=", "args", ".", "model_prefix", "if", "model_prefix", "is", "not", "None", ":", "model_prefix", "+=", "\"-%d\"", "%", "(", "kv", ".", "rank", ")", "model_args", "=", "{", "}", "if", "args", ".", "load_epoch", "is", "not", "None", ":", "assert", "model_prefix", "is", "not", "None", "tmp", "=", "mx", ".", "model", ".", "FeedForward", ".", "load", "(", "model_prefix", ",", "args", ".", "load_epoch", ")", "model_args", "=", "{", "'arg_params'", ":", "tmp", ".", "arg_params", ",", "'aux_params'", ":", "tmp", ".", "aux_params", ",", "'begin_epoch'", ":", "args", ".", "load_epoch", "}", "# save model", "save_model_prefix", "=", "args", ".", "save_model_prefix", "if", "save_model_prefix", "is", "None", ":", "save_model_prefix", "=", "model_prefix", "checkpoint", "=", "None", "if", "save_model_prefix", "is", "None", "else", "mx", ".", "callback", ".", "do_checkpoint", "(", "save_model_prefix", ")", "# data", "(", "train", ",", "val", ")", "=", "data_loader", "(", "args", ",", "kv", ")", "# train", "devs", "=", "mx", ".", "cpu", "(", ")", "if", "args", ".", "gpus", "is", "None", "else", "[", "mx", ".", "gpu", "(", "int", "(", "i", ")", ")", "for", "i", "in", "args", ".", "gpus", ".", "split", "(", "','", ")", "]", "epoch_size", "=", "args", ".", "num_examples", "/", "args", ".", "batch_size", "if", "args", ".", "kv_store", "==", "'dist_sync'", ":", "epoch_size", "/=", "kv", ".", "num_workers", "model_args", "[", "'epoch_size'", "]", "=", "epoch_size", "if", "'lr_factor'", "in", "args", "and", "args", ".", "lr_factor", "<", "1", ":", "model_args", "[", "'lr_scheduler'", "]", "=", "mx", ".", "lr_scheduler", ".", "FactorScheduler", "(", "step", "=", "max", "(", "int", "(", "epoch_size", "*", "args", ".", "lr_factor_epoch", ")", ",", "1", ")", ",", "factor", "=", "args", ".", "lr_factor", ")", "if", "'clip_gradient'", "in", "args", "and", "args", ".", "clip_gradient", "is", "not", "None", ":", "model_args", "[", "'clip_gradient'", "]", "=", "args", ".", "clip_gradient", "# disable kvstore for single device", "if", "'local'", "in", "kv", ".", "type", "and", "(", "args", ".", "gpus", "is", "None", "or", "len", "(", "args", ".", "gpus", ".", "split", "(", "','", ")", ")", "is", "1", ")", ":", "kv", "=", "None", "mod", "=", "mx", ".", "mod", ".", "Module", "(", "network", ",", "context", "=", "devs", ")", "if", "eval_metrics", "is", "None", ":", "eval_metrics", "=", "[", "'accuracy'", "]", "# TopKAccuracy only allows top_k > 1", "for", "top_k", "in", "[", "5", ",", "10", ",", "20", "]", ":", "eval_metrics", ".", "append", "(", "mx", ".", "metric", ".", "create", "(", "'top_k_accuracy'", ",", "top_k", "=", "top_k", ")", ")", "if", "batch_end_callback", "is", "not", "None", ":", "if", "not", "isinstance", "(", "batch_end_callback", ",", "list", ")", ":", "batch_end_callback", "=", "[", "batch_end_callback", "]", "else", ":", "batch_end_callback", "=", "[", "]", "batch_end_callback", ".", "append", "(", "mx", ".", "callback", ".", "Speedometer", "(", "args", ".", "batch_size", ",", "50", ")", ")", "mod", ".", "fit", "(", "train_data", "=", "train", ",", "eval_metric", "=", "eval_metrics", ",", "eval_data", "=", "val", ",", "optimizer", "=", "'sgd'", ",", "optimizer_params", "=", "{", "'learning_rate'", ":", "args", ".", "lr", ",", "'momentum'", ":", "0.9", ",", "'wd'", ":", "0.00001", "}", ",", "num_epoch", "=", "args", ".", "num_epochs", ",", "batch_end_callback", "=", "batch_end_callback", ",", "initializer", "=", "mx", ".", "init", ".", "Xavier", "(", "factor_type", "=", "\"in\"", ",", "magnitude", "=", "2.34", ")", ",", "kvstore", "=", "kv", ",", "epoch_end_callback", "=", "checkpoint", ",", "*", "*", "model_args", ")" ]
Train the model using Caffe operator in MXNet
[ "Train", "the", "model", "using", "Caffe", "operator", "in", "MXNet" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/caffe/train_model.py#L23-L109
train
apache/incubator-mxnet
example/reinforcement-learning/parallel_actor_critic/envs.py
Atari8080Preprocessor.preprocess
def preprocess(self, img): """ Preprocess a 210x160x3 uint8 frame into a 6400 (80x80) (1 x input_size) float vector. """ # Crop, down-sample, erase background and set foreground to 1. # See https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5 img = img[35:195] img = img[::2, ::2, 0] img[img == 144] = 0 img[img == 109] = 0 img[img != 0] = 1 curr = np.expand_dims(img.astype(np.float).ravel(), axis=0) # Subtract the last preprocessed image. diff = (curr - self.prev if self.prev is not None else np.zeros((1, curr.shape[1]))) self.prev = curr return diff
python
def preprocess(self, img): """ Preprocess a 210x160x3 uint8 frame into a 6400 (80x80) (1 x input_size) float vector. """ # Crop, down-sample, erase background and set foreground to 1. # See https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5 img = img[35:195] img = img[::2, ::2, 0] img[img == 144] = 0 img[img == 109] = 0 img[img != 0] = 1 curr = np.expand_dims(img.astype(np.float).ravel(), axis=0) # Subtract the last preprocessed image. diff = (curr - self.prev if self.prev is not None else np.zeros((1, curr.shape[1]))) self.prev = curr return diff
[ "def", "preprocess", "(", "self", ",", "img", ")", ":", "# Crop, down-sample, erase background and set foreground to 1.", "# See https://gist.github.com/karpathy/a4166c7fe253700972fcbc77e4ea32c5", "img", "=", "img", "[", "35", ":", "195", "]", "img", "=", "img", "[", ":", ":", "2", ",", ":", ":", "2", ",", "0", "]", "img", "[", "img", "==", "144", "]", "=", "0", "img", "[", "img", "==", "109", "]", "=", "0", "img", "[", "img", "!=", "0", "]", "=", "1", "curr", "=", "np", ".", "expand_dims", "(", "img", ".", "astype", "(", "np", ".", "float", ")", ".", "ravel", "(", ")", ",", "axis", "=", "0", ")", "# Subtract the last preprocessed image.", "diff", "=", "(", "curr", "-", "self", ".", "prev", "if", "self", ".", "prev", "is", "not", "None", "else", "np", ".", "zeros", "(", "(", "1", ",", "curr", ".", "shape", "[", "1", "]", ")", ")", ")", "self", ".", "prev", "=", "curr", "return", "diff" ]
Preprocess a 210x160x3 uint8 frame into a 6400 (80x80) (1 x input_size) float vector.
[ "Preprocess", "a", "210x160x3", "uint8", "frame", "into", "a", "6400", "(", "80x80", ")", "(", "1", "x", "input_size", ")", "float", "vector", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/example/reinforcement-learning/parallel_actor_critic/envs.py#L29-L46
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_new_empty_handle
def _new_empty_handle(): """Returns a new empty handle. Empty handle can be used to hold a result. Returns ------- handle A new empty `NDArray` handle. """ hdl = NDArrayHandle() check_call(_LIB.MXNDArrayCreateNone(ctypes.byref(hdl))) return hdl
python
def _new_empty_handle(): """Returns a new empty handle. Empty handle can be used to hold a result. Returns ------- handle A new empty `NDArray` handle. """ hdl = NDArrayHandle() check_call(_LIB.MXNDArrayCreateNone(ctypes.byref(hdl))) return hdl
[ "def", "_new_empty_handle", "(", ")", ":", "hdl", "=", "NDArrayHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayCreateNone", "(", "ctypes", ".", "byref", "(", "hdl", ")", ")", ")", "return", "hdl" ]
Returns a new empty handle. Empty handle can be used to hold a result. Returns ------- handle A new empty `NDArray` handle.
[ "Returns", "a", "new", "empty", "handle", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L107-L119
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_new_alloc_handle
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t): """Return a new handle with specified shape and context. Empty handle is only used to hold results. Returns ------- handle A new empty `NDArray` handle. """ hdl = NDArrayHandle() check_call(_LIB.MXNDArrayCreateEx( c_array_buf(mx_uint, native_array('I', shape)), mx_uint(len(shape)), ctypes.c_int(ctx.device_typeid), ctypes.c_int(ctx.device_id), ctypes.c_int(int(delay_alloc)), ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])), ctypes.byref(hdl))) return hdl
python
def _new_alloc_handle(shape, ctx, delay_alloc, dtype=mx_real_t): """Return a new handle with specified shape and context. Empty handle is only used to hold results. Returns ------- handle A new empty `NDArray` handle. """ hdl = NDArrayHandle() check_call(_LIB.MXNDArrayCreateEx( c_array_buf(mx_uint, native_array('I', shape)), mx_uint(len(shape)), ctypes.c_int(ctx.device_typeid), ctypes.c_int(ctx.device_id), ctypes.c_int(int(delay_alloc)), ctypes.c_int(int(_DTYPE_NP_TO_MX[np.dtype(dtype).type])), ctypes.byref(hdl))) return hdl
[ "def", "_new_alloc_handle", "(", "shape", ",", "ctx", ",", "delay_alloc", ",", "dtype", "=", "mx_real_t", ")", ":", "hdl", "=", "NDArrayHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayCreateEx", "(", "c_array_buf", "(", "mx_uint", ",", "native_array", "(", "'I'", ",", "shape", ")", ")", ",", "mx_uint", "(", "len", "(", "shape", ")", ")", ",", "ctypes", ".", "c_int", "(", "ctx", ".", "device_typeid", ")", ",", "ctypes", ".", "c_int", "(", "ctx", ".", "device_id", ")", ",", "ctypes", ".", "c_int", "(", "int", "(", "delay_alloc", ")", ")", ",", "ctypes", ".", "c_int", "(", "int", "(", "_DTYPE_NP_TO_MX", "[", "np", ".", "dtype", "(", "dtype", ")", ".", "type", "]", ")", ")", ",", "ctypes", ".", "byref", "(", "hdl", ")", ")", ")", "return", "hdl" ]
Return a new handle with specified shape and context. Empty handle is only used to hold results. Returns ------- handle A new empty `NDArray` handle.
[ "Return", "a", "new", "handle", "with", "specified", "shape", "and", "context", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L122-L141
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_get_indexing_dispatch_code
def _get_indexing_dispatch_code(key): """Returns a dispatch code for calling basic or advanced indexing functions.""" if isinstance(key, (NDArray, np.ndarray)): return _NDARRAY_ADVANCED_INDEXING elif isinstance(key, list): # TODO(junwu): Add support for nested lists besides integer list for i in key: if not isinstance(i, integer_types): raise TypeError('Indexing NDArray only supports a list of integers as index' ' when key is of list type, received element=%s of type=%s' % (str(i), str(type(i)))) return _NDARRAY_ADVANCED_INDEXING elif isinstance(key, (integer_types, py_slice)): return _NDARRAY_BASIC_INDEXING elif isinstance(key, tuple): for idx in key: if isinstance(idx, (NDArray, np.ndarray, list, tuple)): return _NDARRAY_ADVANCED_INDEXING elif not isinstance(idx, (py_slice, integer_types)): raise ValueError("NDArray does not support slicing with key %s of type %s." % (str(idx), str(type(idx)))) return _NDARRAY_BASIC_INDEXING else: return _NDARRAY_UNSUPPORTED_INDEXING
python
def _get_indexing_dispatch_code(key): """Returns a dispatch code for calling basic or advanced indexing functions.""" if isinstance(key, (NDArray, np.ndarray)): return _NDARRAY_ADVANCED_INDEXING elif isinstance(key, list): # TODO(junwu): Add support for nested lists besides integer list for i in key: if not isinstance(i, integer_types): raise TypeError('Indexing NDArray only supports a list of integers as index' ' when key is of list type, received element=%s of type=%s' % (str(i), str(type(i)))) return _NDARRAY_ADVANCED_INDEXING elif isinstance(key, (integer_types, py_slice)): return _NDARRAY_BASIC_INDEXING elif isinstance(key, tuple): for idx in key: if isinstance(idx, (NDArray, np.ndarray, list, tuple)): return _NDARRAY_ADVANCED_INDEXING elif not isinstance(idx, (py_slice, integer_types)): raise ValueError("NDArray does not support slicing with key %s of type %s." % (str(idx), str(type(idx)))) return _NDARRAY_BASIC_INDEXING else: return _NDARRAY_UNSUPPORTED_INDEXING
[ "def", "_get_indexing_dispatch_code", "(", "key", ")", ":", "if", "isinstance", "(", "key", ",", "(", "NDArray", ",", "np", ".", "ndarray", ")", ")", ":", "return", "_NDARRAY_ADVANCED_INDEXING", "elif", "isinstance", "(", "key", ",", "list", ")", ":", "# TODO(junwu): Add support for nested lists besides integer list", "for", "i", "in", "key", ":", "if", "not", "isinstance", "(", "i", ",", "integer_types", ")", ":", "raise", "TypeError", "(", "'Indexing NDArray only supports a list of integers as index'", "' when key is of list type, received element=%s of type=%s'", "%", "(", "str", "(", "i", ")", ",", "str", "(", "type", "(", "i", ")", ")", ")", ")", "return", "_NDARRAY_ADVANCED_INDEXING", "elif", "isinstance", "(", "key", ",", "(", "integer_types", ",", "py_slice", ")", ")", ":", "return", "_NDARRAY_BASIC_INDEXING", "elif", "isinstance", "(", "key", ",", "tuple", ")", ":", "for", "idx", "in", "key", ":", "if", "isinstance", "(", "idx", ",", "(", "NDArray", ",", "np", ".", "ndarray", ",", "list", ",", "tuple", ")", ")", ":", "return", "_NDARRAY_ADVANCED_INDEXING", "elif", "not", "isinstance", "(", "idx", ",", "(", "py_slice", ",", "integer_types", ")", ")", ":", "raise", "ValueError", "(", "\"NDArray does not support slicing with key %s of type %s.\"", "%", "(", "str", "(", "idx", ")", ",", "str", "(", "type", "(", "idx", ")", ")", ")", ")", "return", "_NDARRAY_BASIC_INDEXING", "else", ":", "return", "_NDARRAY_UNSUPPORTED_INDEXING" ]
Returns a dispatch code for calling basic or advanced indexing functions.
[ "Returns", "a", "dispatch", "code", "for", "calling", "basic", "or", "advanced", "indexing", "functions", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2278-L2301
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_get_index_range
def _get_index_range(start, stop, length, step=1): """Given start, stop, step and array length, return absolute values of start, stop, and step for generating index range. The returned values have been compensated by adding length if they are less than zero for all the cases but slice(None, None, -1). Note that the returned value of stop is not necessarily >= 0, since absolute stop is -1 in the case of slice(None, None, -1).""" if step == 0: raise ValueError('step size cannot be zero') if length < 0: raise ValueError('array length cannot be less than zero') if step is None: step = 1 if start is None: if step > 0: start = 0 else: start = length - 1 elif start < 0: start += length if start < 0: raise IndexError('Slicing start %d exceeds limit of %d' % (start-length, length)) elif start >= length: raise IndexError('Slicing start %d exceeds limit of %d' % (start, length)) if stop is None: if step > 0: stop = length else: # this supports case such as ::-1 # stop = -1 here refers to the element before index 0, # instead of the last element in the array stop = -1 elif stop < 0: stop += length if stop < 0: raise IndexError('Slicing stop %d exceeds limit of %d' % (stop-length, length)) elif stop > length: raise IndexError('Slicing stop %d exceeds limit of %d' % (stop, length)) return start, stop, step
python
def _get_index_range(start, stop, length, step=1): """Given start, stop, step and array length, return absolute values of start, stop, and step for generating index range. The returned values have been compensated by adding length if they are less than zero for all the cases but slice(None, None, -1). Note that the returned value of stop is not necessarily >= 0, since absolute stop is -1 in the case of slice(None, None, -1).""" if step == 0: raise ValueError('step size cannot be zero') if length < 0: raise ValueError('array length cannot be less than zero') if step is None: step = 1 if start is None: if step > 0: start = 0 else: start = length - 1 elif start < 0: start += length if start < 0: raise IndexError('Slicing start %d exceeds limit of %d' % (start-length, length)) elif start >= length: raise IndexError('Slicing start %d exceeds limit of %d' % (start, length)) if stop is None: if step > 0: stop = length else: # this supports case such as ::-1 # stop = -1 here refers to the element before index 0, # instead of the last element in the array stop = -1 elif stop < 0: stop += length if stop < 0: raise IndexError('Slicing stop %d exceeds limit of %d' % (stop-length, length)) elif stop > length: raise IndexError('Slicing stop %d exceeds limit of %d' % (stop, length)) return start, stop, step
[ "def", "_get_index_range", "(", "start", ",", "stop", ",", "length", ",", "step", "=", "1", ")", ":", "if", "step", "==", "0", ":", "raise", "ValueError", "(", "'step size cannot be zero'", ")", "if", "length", "<", "0", ":", "raise", "ValueError", "(", "'array length cannot be less than zero'", ")", "if", "step", "is", "None", ":", "step", "=", "1", "if", "start", "is", "None", ":", "if", "step", ">", "0", ":", "start", "=", "0", "else", ":", "start", "=", "length", "-", "1", "elif", "start", "<", "0", ":", "start", "+=", "length", "if", "start", "<", "0", ":", "raise", "IndexError", "(", "'Slicing start %d exceeds limit of %d'", "%", "(", "start", "-", "length", ",", "length", ")", ")", "elif", "start", ">=", "length", ":", "raise", "IndexError", "(", "'Slicing start %d exceeds limit of %d'", "%", "(", "start", ",", "length", ")", ")", "if", "stop", "is", "None", ":", "if", "step", ">", "0", ":", "stop", "=", "length", "else", ":", "# this supports case such as ::-1", "# stop = -1 here refers to the element before index 0,", "# instead of the last element in the array", "stop", "=", "-", "1", "elif", "stop", "<", "0", ":", "stop", "+=", "length", "if", "stop", "<", "0", ":", "raise", "IndexError", "(", "'Slicing stop %d exceeds limit of %d'", "%", "(", "stop", "-", "length", ",", "length", ")", ")", "elif", "stop", ">", "length", ":", "raise", "IndexError", "(", "'Slicing stop %d exceeds limit of %d'", "%", "(", "stop", ",", "length", ")", ")", "return", "start", ",", "stop", ",", "step" ]
Given start, stop, step and array length, return absolute values of start, stop, and step for generating index range. The returned values have been compensated by adding length if they are less than zero for all the cases but slice(None, None, -1). Note that the returned value of stop is not necessarily >= 0, since absolute stop is -1 in the case of slice(None, None, -1).
[ "Given", "start", "stop", "step", "and", "array", "length", "return", "absolute", "values", "of", "start", "stop", "and", "step", "for", "generating", "index", "range", ".", "The", "returned", "values", "have", "been", "compensated", "by", "adding", "length", "if", "they", "are", "less", "than", "zero", "for", "all", "the", "cases", "but", "slice", "(", "None", "None", "-", "1", ")", ".", "Note", "that", "the", "returned", "value", "of", "stop", "is", "not", "necessarily", ">", "=", "0", "since", "absolute", "stop", "is", "-", "1", "in", "the", "case", "of", "slice", "(", "None", "None", "-", "1", ")", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2304-L2344
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_get_oshape_of_gather_nd_op
def _get_oshape_of_gather_nd_op(dshape, ishape): """Given data and index shapes, get the output `NDArray` shape. This basically implements the infer shape logic of op gather_nd.""" assert len(dshape) > 0 and len(ishape) > 0 oshape = list(ishape[1:]) if ishape[0] < len(dshape): oshape.extend(dshape[ishape[0]:]) return tuple(oshape)
python
def _get_oshape_of_gather_nd_op(dshape, ishape): """Given data and index shapes, get the output `NDArray` shape. This basically implements the infer shape logic of op gather_nd.""" assert len(dshape) > 0 and len(ishape) > 0 oshape = list(ishape[1:]) if ishape[0] < len(dshape): oshape.extend(dshape[ishape[0]:]) return tuple(oshape)
[ "def", "_get_oshape_of_gather_nd_op", "(", "dshape", ",", "ishape", ")", ":", "assert", "len", "(", "dshape", ")", ">", "0", "and", "len", "(", "ishape", ")", ">", "0", "oshape", "=", "list", "(", "ishape", "[", "1", ":", "]", ")", "if", "ishape", "[", "0", "]", "<", "len", "(", "dshape", ")", ":", "oshape", ".", "extend", "(", "dshape", "[", "ishape", "[", "0", "]", ":", "]", ")", "return", "tuple", "(", "oshape", ")" ]
Given data and index shapes, get the output `NDArray` shape. This basically implements the infer shape logic of op gather_nd.
[ "Given", "data", "and", "index", "shapes", "get", "the", "output", "NDArray", "shape", ".", "This", "basically", "implements", "the", "infer", "shape", "logic", "of", "op", "gather_nd", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2347-L2354
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_get_dim_size
def _get_dim_size(start, stop, step): """Given start, stop, and stop, calculate the number of elements of this slice.""" assert step != 0 if step > 0: assert start < stop dim_size = (stop - start - 1) // step + 1 else: assert stop < start dim_size = (start - stop - 1) // (-step) + 1 return dim_size
python
def _get_dim_size(start, stop, step): """Given start, stop, and stop, calculate the number of elements of this slice.""" assert step != 0 if step > 0: assert start < stop dim_size = (stop - start - 1) // step + 1 else: assert stop < start dim_size = (start - stop - 1) // (-step) + 1 return dim_size
[ "def", "_get_dim_size", "(", "start", ",", "stop", ",", "step", ")", ":", "assert", "step", "!=", "0", "if", "step", ">", "0", ":", "assert", "start", "<", "stop", "dim_size", "=", "(", "stop", "-", "start", "-", "1", ")", "//", "step", "+", "1", "else", ":", "assert", "stop", "<", "start", "dim_size", "=", "(", "start", "-", "stop", "-", "1", ")", "//", "(", "-", "step", ")", "+", "1", "return", "dim_size" ]
Given start, stop, and stop, calculate the number of elements of this slice.
[ "Given", "start", "stop", "and", "stop", "calculate", "the", "number", "of", "elements", "of", "this", "slice", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2357-L2367
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_get_broadcast_shape
def _get_broadcast_shape(shape1, shape2): """Given two shapes that are not identical, find the shape that both input shapes can broadcast to.""" if shape1 == shape2: return shape1 length1 = len(shape1) length2 = len(shape2) if length1 > length2: shape = list(shape1) else: shape = list(shape2) i = max(length1, length2) - 1 for a, b in zip(shape1[::-1], shape2[::-1]): if a != 1 and b != 1 and a != b: raise ValueError('shape1=%s is not broadcastable to shape2=%s' % (shape1, shape2)) shape[i] = max(a, b) i -= 1 return tuple(shape)
python
def _get_broadcast_shape(shape1, shape2): """Given two shapes that are not identical, find the shape that both input shapes can broadcast to.""" if shape1 == shape2: return shape1 length1 = len(shape1) length2 = len(shape2) if length1 > length2: shape = list(shape1) else: shape = list(shape2) i = max(length1, length2) - 1 for a, b in zip(shape1[::-1], shape2[::-1]): if a != 1 and b != 1 and a != b: raise ValueError('shape1=%s is not broadcastable to shape2=%s' % (shape1, shape2)) shape[i] = max(a, b) i -= 1 return tuple(shape)
[ "def", "_get_broadcast_shape", "(", "shape1", ",", "shape2", ")", ":", "if", "shape1", "==", "shape2", ":", "return", "shape1", "length1", "=", "len", "(", "shape1", ")", "length2", "=", "len", "(", "shape2", ")", "if", "length1", ">", "length2", ":", "shape", "=", "list", "(", "shape1", ")", "else", ":", "shape", "=", "list", "(", "shape2", ")", "i", "=", "max", "(", "length1", ",", "length2", ")", "-", "1", "for", "a", ",", "b", "in", "zip", "(", "shape1", "[", ":", ":", "-", "1", "]", ",", "shape2", "[", ":", ":", "-", "1", "]", ")", ":", "if", "a", "!=", "1", "and", "b", "!=", "1", "and", "a", "!=", "b", ":", "raise", "ValueError", "(", "'shape1=%s is not broadcastable to shape2=%s'", "%", "(", "shape1", ",", "shape2", ")", ")", "shape", "[", "i", "]", "=", "max", "(", "a", ",", "b", ")", "i", "-=", "1", "return", "tuple", "(", "shape", ")" ]
Given two shapes that are not identical, find the shape that both input shapes can broadcast to.
[ "Given", "two", "shapes", "that", "are", "not", "identical", "find", "the", "shape", "that", "both", "input", "shapes", "can", "broadcast", "to", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2370-L2388
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
ones
def ones(shape, ctx=None, dtype=None, **kwargs): """Returns a new array filled with all ones, with the given shape and type. Parameters ---------- shape : int or tuple of int or list of int The shape of the empty array. ctx : Context, optional An optional device context. Defaults to the current default context (``mxnet.context.current_context()``). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A new array of the specified shape filled with all ones. Examples -------- >>> mx.nd.ones(1).asnumpy() array([ 1.], dtype=float32) >>> mx.nd.ones((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.ones((1,2), dtype='float16').asnumpy() array([[ 1., 1.]], dtype=float16) """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._ones(shape=shape, ctx=ctx, dtype=dtype, **kwargs)
python
def ones(shape, ctx=None, dtype=None, **kwargs): """Returns a new array filled with all ones, with the given shape and type. Parameters ---------- shape : int or tuple of int or list of int The shape of the empty array. ctx : Context, optional An optional device context. Defaults to the current default context (``mxnet.context.current_context()``). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A new array of the specified shape filled with all ones. Examples -------- >>> mx.nd.ones(1).asnumpy() array([ 1.], dtype=float32) >>> mx.nd.ones((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.ones((1,2), dtype='float16').asnumpy() array([[ 1., 1.]], dtype=float16) """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._ones(shape=shape, ctx=ctx, dtype=dtype, **kwargs)
[ "def", "ones", "(", "shape", ",", "ctx", "=", "None", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "# pylint: disable= unused-argument", "if", "ctx", "is", "None", ":", "ctx", "=", "current_context", "(", ")", "dtype", "=", "mx_real_t", "if", "dtype", "is", "None", "else", "dtype", "# pylint: disable= no-member, protected-access", "return", "_internal", ".", "_ones", "(", "shape", "=", "shape", ",", "ctx", "=", "ctx", ",", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
Returns a new array filled with all ones, with the given shape and type. Parameters ---------- shape : int or tuple of int or list of int The shape of the empty array. ctx : Context, optional An optional device context. Defaults to the current default context (``mxnet.context.current_context()``). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A new array of the specified shape filled with all ones. Examples -------- >>> mx.nd.ones(1).asnumpy() array([ 1.], dtype=float32) >>> mx.nd.ones((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.ones((1,2), dtype='float16').asnumpy() array([[ 1., 1.]], dtype=float16)
[ "Returns", "a", "new", "array", "filled", "with", "all", "ones", "with", "the", "given", "shape", "and", "type", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2402-L2436
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
full
def full(shape, val, ctx=None, dtype=mx_real_t, out=None): """Returns a new array of given shape and type, filled with the given value `val`. Parameters -------- shape : int or tuple of int The shape of the new array. val : scalar Fill value. ctx : Context, optional Device context (default is the current default context). dtype : `str` or `numpy.dtype`, optional The data type of the returned `NDArray`. The default datatype is `float32`. out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray `NDArray` filled with `val`, with the given shape, ctx, and dtype. Examples -------- >>> mx.nd.full(1, 2.0).asnumpy() array([ 2.], dtype=float32) >>> mx.nd.full((1, 2), 2.0, mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.full((1, 2), 2.0, dtype='float16').asnumpy() array([[ 2., 2.]], dtype=float16) """ out = empty(shape, ctx, dtype) if out is None else out out[:] = val return out
python
def full(shape, val, ctx=None, dtype=mx_real_t, out=None): """Returns a new array of given shape and type, filled with the given value `val`. Parameters -------- shape : int or tuple of int The shape of the new array. val : scalar Fill value. ctx : Context, optional Device context (default is the current default context). dtype : `str` or `numpy.dtype`, optional The data type of the returned `NDArray`. The default datatype is `float32`. out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray `NDArray` filled with `val`, with the given shape, ctx, and dtype. Examples -------- >>> mx.nd.full(1, 2.0).asnumpy() array([ 2.], dtype=float32) >>> mx.nd.full((1, 2), 2.0, mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.full((1, 2), 2.0, dtype='float16').asnumpy() array([[ 2., 2.]], dtype=float16) """ out = empty(shape, ctx, dtype) if out is None else out out[:] = val return out
[ "def", "full", "(", "shape", ",", "val", ",", "ctx", "=", "None", ",", "dtype", "=", "mx_real_t", ",", "out", "=", "None", ")", ":", "out", "=", "empty", "(", "shape", ",", "ctx", ",", "dtype", ")", "if", "out", "is", "None", "else", "out", "out", "[", ":", "]", "=", "val", "return", "out" ]
Returns a new array of given shape and type, filled with the given value `val`. Parameters -------- shape : int or tuple of int The shape of the new array. val : scalar Fill value. ctx : Context, optional Device context (default is the current default context). dtype : `str` or `numpy.dtype`, optional The data type of the returned `NDArray`. The default datatype is `float32`. out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray `NDArray` filled with `val`, with the given shape, ctx, and dtype. Examples -------- >>> mx.nd.full(1, 2.0).asnumpy() array([ 2.], dtype=float32) >>> mx.nd.full((1, 2), 2.0, mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.full((1, 2), 2.0, dtype='float16').asnumpy() array([[ 2., 2.]], dtype=float16)
[ "Returns", "a", "new", "array", "of", "given", "shape", "and", "type", "filled", "with", "the", "given", "value", "val", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2440-L2472
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
array
def array(source_array, ctx=None, dtype=None): """Creates an array from any object exposing the array interface. Parameters ---------- source_array : array_like An object exposing the array interface, an object whose `__array__` method returns an array, or any (nested) sequence. ctx : Context, optional Device context (default is the current default context). dtype : str or numpy.dtype, optional The data type of the output array. The default dtype is ``source_array.dtype`` if `source_array` is an `NDArray`, `float32` otherwise. Returns ------- NDArray An `NDArray` with the same contents as the `source_array`. """ if isinstance(source_array, NDArray): dtype = source_array.dtype if dtype is None else dtype else: dtype = mx_real_t if dtype is None else dtype if not isinstance(source_array, np.ndarray): try: source_array = np.array(source_array, dtype=dtype) except: raise TypeError('source_array must be array like object') arr = empty(source_array.shape, ctx, dtype) arr[:] = source_array return arr
python
def array(source_array, ctx=None, dtype=None): """Creates an array from any object exposing the array interface. Parameters ---------- source_array : array_like An object exposing the array interface, an object whose `__array__` method returns an array, or any (nested) sequence. ctx : Context, optional Device context (default is the current default context). dtype : str or numpy.dtype, optional The data type of the output array. The default dtype is ``source_array.dtype`` if `source_array` is an `NDArray`, `float32` otherwise. Returns ------- NDArray An `NDArray` with the same contents as the `source_array`. """ if isinstance(source_array, NDArray): dtype = source_array.dtype if dtype is None else dtype else: dtype = mx_real_t if dtype is None else dtype if not isinstance(source_array, np.ndarray): try: source_array = np.array(source_array, dtype=dtype) except: raise TypeError('source_array must be array like object') arr = empty(source_array.shape, ctx, dtype) arr[:] = source_array return arr
[ "def", "array", "(", "source_array", ",", "ctx", "=", "None", ",", "dtype", "=", "None", ")", ":", "if", "isinstance", "(", "source_array", ",", "NDArray", ")", ":", "dtype", "=", "source_array", ".", "dtype", "if", "dtype", "is", "None", "else", "dtype", "else", ":", "dtype", "=", "mx_real_t", "if", "dtype", "is", "None", "else", "dtype", "if", "not", "isinstance", "(", "source_array", ",", "np", ".", "ndarray", ")", ":", "try", ":", "source_array", "=", "np", ".", "array", "(", "source_array", ",", "dtype", "=", "dtype", ")", "except", ":", "raise", "TypeError", "(", "'source_array must be array like object'", ")", "arr", "=", "empty", "(", "source_array", ".", "shape", ",", "ctx", ",", "dtype", ")", "arr", "[", ":", "]", "=", "source_array", "return", "arr" ]
Creates an array from any object exposing the array interface. Parameters ---------- source_array : array_like An object exposing the array interface, an object whose `__array__` method returns an array, or any (nested) sequence. ctx : Context, optional Device context (default is the current default context). dtype : str or numpy.dtype, optional The data type of the output array. The default dtype is ``source_array.dtype`` if `source_array` is an `NDArray`, `float32` otherwise. Returns ------- NDArray An `NDArray` with the same contents as the `source_array`.
[ "Creates", "an", "array", "from", "any", "object", "exposing", "the", "array", "interface", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2475-L2505
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
moveaxis
def moveaxis(tensor, source, destination): """Moves the `source` axis into the `destination` position while leaving the other axes in their original order Parameters ---------- tensor : mx.nd.array The array which axes should be reordered source : int or sequence of int Original position of the axes to move. Can be negative but must be unique. destination : int or sequence of int Destination position for each of the original axes. Can be negative but must be unique. Returns ------- result : mx.nd.array Array with moved axes. Examples -------- >>> X = mx.nd.array([[1, 2, 3], [4, 5, 6]]) >>> mx.nd.moveaxis(X, 0, 1).shape (3L, 2L) >>> X = mx.nd.zeros((3, 4, 5)) >>> mx.nd.moveaxis(X, [0, 1], [-1, -2]).shape (5, 4, 3) """ try: source = np.core.numeric.normalize_axis_tuple(source, tensor.ndim) except IndexError: raise ValueError('Source should verify 0 <= source < tensor.ndim' 'Got %d' % source) try: destination = np.core.numeric.normalize_axis_tuple(destination, tensor.ndim) except IndexError: raise ValueError('Destination should verify 0 <= destination < tensor.ndim (%d).' % tensor.ndim, 'Got %d' % destination) if len(source) != len(destination): raise ValueError('`source` and `destination` arguments must have ' 'the same number of elements') order = [n for n in range(tensor.ndim) if n not in source] for dest, src in sorted(zip(destination, source)): order.insert(dest, src) return op.transpose(tensor, order)
python
def moveaxis(tensor, source, destination): """Moves the `source` axis into the `destination` position while leaving the other axes in their original order Parameters ---------- tensor : mx.nd.array The array which axes should be reordered source : int or sequence of int Original position of the axes to move. Can be negative but must be unique. destination : int or sequence of int Destination position for each of the original axes. Can be negative but must be unique. Returns ------- result : mx.nd.array Array with moved axes. Examples -------- >>> X = mx.nd.array([[1, 2, 3], [4, 5, 6]]) >>> mx.nd.moveaxis(X, 0, 1).shape (3L, 2L) >>> X = mx.nd.zeros((3, 4, 5)) >>> mx.nd.moveaxis(X, [0, 1], [-1, -2]).shape (5, 4, 3) """ try: source = np.core.numeric.normalize_axis_tuple(source, tensor.ndim) except IndexError: raise ValueError('Source should verify 0 <= source < tensor.ndim' 'Got %d' % source) try: destination = np.core.numeric.normalize_axis_tuple(destination, tensor.ndim) except IndexError: raise ValueError('Destination should verify 0 <= destination < tensor.ndim (%d).' % tensor.ndim, 'Got %d' % destination) if len(source) != len(destination): raise ValueError('`source` and `destination` arguments must have ' 'the same number of elements') order = [n for n in range(tensor.ndim) if n not in source] for dest, src in sorted(zip(destination, source)): order.insert(dest, src) return op.transpose(tensor, order)
[ "def", "moveaxis", "(", "tensor", ",", "source", ",", "destination", ")", ":", "try", ":", "source", "=", "np", ".", "core", ".", "numeric", ".", "normalize_axis_tuple", "(", "source", ",", "tensor", ".", "ndim", ")", "except", "IndexError", ":", "raise", "ValueError", "(", "'Source should verify 0 <= source < tensor.ndim'", "'Got %d'", "%", "source", ")", "try", ":", "destination", "=", "np", ".", "core", ".", "numeric", ".", "normalize_axis_tuple", "(", "destination", ",", "tensor", ".", "ndim", ")", "except", "IndexError", ":", "raise", "ValueError", "(", "'Destination should verify 0 <= destination < tensor.ndim (%d).'", "%", "tensor", ".", "ndim", ",", "'Got %d'", "%", "destination", ")", "if", "len", "(", "source", ")", "!=", "len", "(", "destination", ")", ":", "raise", "ValueError", "(", "'`source` and `destination` arguments must have '", "'the same number of elements'", ")", "order", "=", "[", "n", "for", "n", "in", "range", "(", "tensor", ".", "ndim", ")", "if", "n", "not", "in", "source", "]", "for", "dest", ",", "src", "in", "sorted", "(", "zip", "(", "destination", ",", "source", ")", ")", ":", "order", ".", "insert", "(", "dest", ",", "src", ")", "return", "op", ".", "transpose", "(", "tensor", ",", "order", ")" ]
Moves the `source` axis into the `destination` position while leaving the other axes in their original order Parameters ---------- tensor : mx.nd.array The array which axes should be reordered source : int or sequence of int Original position of the axes to move. Can be negative but must be unique. destination : int or sequence of int Destination position for each of the original axes. Can be negative but must be unique. Returns ------- result : mx.nd.array Array with moved axes. Examples -------- >>> X = mx.nd.array([[1, 2, 3], [4, 5, 6]]) >>> mx.nd.moveaxis(X, 0, 1).shape (3L, 2L) >>> X = mx.nd.zeros((3, 4, 5)) >>> mx.nd.moveaxis(X, [0, 1], [-1, -2]).shape (5, 4, 3)
[ "Moves", "the", "source", "axis", "into", "the", "destination", "position", "while", "leaving", "the", "other", "axes", "in", "their", "original", "order" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2508-L2556
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
arange
def arange(start, stop=None, step=1.0, repeat=1, infer_range=None, ctx=None, dtype=mx_real_t): """Returns evenly spaced values within a given interval. Values are generated within the half-open interval [`start`, `stop`). In other words, the interval includes `start` but excludes `stop`. The function is similar to the built-in Python function `range` and to `numpy.arange`, but returns an `NDArray`. Parameters ---------- start : number, optional Start of interval. The default start value is 0. stop : number End of interval. step : number, optional Spacing between values. The default step size is 1. repeat : int, optional Number of times to repeat each element. The default repeat count is 1. infer_range : boolean, optional When set to True, infer the stop position from the start, step, repeat, and output tensor size. ctx : Context, optional Device context. Default context is the current default context. dtype : str or numpy.dtype, optional The data type of the `NDArray`. The default datatype is `np.float32`. Returns ------- NDArray `NDArray` of evenly spaced values in the specified range. Examples -------- >>> mx.nd.arange(3).asnumpy() array([ 0., 1., 2.], dtype=float32) >>> mx.nd.arange(2, 6).asnumpy() array([ 2., 3., 4., 5.], dtype=float32) >>> mx.nd.arange(2, 6, step=2).asnumpy() array([ 2., 4.], dtype=float32) >>> mx.nd.arange(2, 6, step=1.5, repeat=2).asnumpy() array([ 2. , 2. , 3.5, 3.5, 5. , 5. ], dtype=float32) >>> mx.nd.arange(2, 6, step=2, repeat=3, dtype='int32').asnumpy() array([2, 2, 2, 4, 4, 4], dtype=int32) """ if infer_range is not None: warnings.warn('`infer_range` argument has been deprecated', DeprecationWarning) if ctx is None: ctx = current_context() return _internal._arange(start=start, stop=stop, step=step, repeat=repeat, infer_range=False, dtype=dtype, ctx=str(ctx))
python
def arange(start, stop=None, step=1.0, repeat=1, infer_range=None, ctx=None, dtype=mx_real_t): """Returns evenly spaced values within a given interval. Values are generated within the half-open interval [`start`, `stop`). In other words, the interval includes `start` but excludes `stop`. The function is similar to the built-in Python function `range` and to `numpy.arange`, but returns an `NDArray`. Parameters ---------- start : number, optional Start of interval. The default start value is 0. stop : number End of interval. step : number, optional Spacing between values. The default step size is 1. repeat : int, optional Number of times to repeat each element. The default repeat count is 1. infer_range : boolean, optional When set to True, infer the stop position from the start, step, repeat, and output tensor size. ctx : Context, optional Device context. Default context is the current default context. dtype : str or numpy.dtype, optional The data type of the `NDArray`. The default datatype is `np.float32`. Returns ------- NDArray `NDArray` of evenly spaced values in the specified range. Examples -------- >>> mx.nd.arange(3).asnumpy() array([ 0., 1., 2.], dtype=float32) >>> mx.nd.arange(2, 6).asnumpy() array([ 2., 3., 4., 5.], dtype=float32) >>> mx.nd.arange(2, 6, step=2).asnumpy() array([ 2., 4.], dtype=float32) >>> mx.nd.arange(2, 6, step=1.5, repeat=2).asnumpy() array([ 2. , 2. , 3.5, 3.5, 5. , 5. ], dtype=float32) >>> mx.nd.arange(2, 6, step=2, repeat=3, dtype='int32').asnumpy() array([2, 2, 2, 4, 4, 4], dtype=int32) """ if infer_range is not None: warnings.warn('`infer_range` argument has been deprecated', DeprecationWarning) if ctx is None: ctx = current_context() return _internal._arange(start=start, stop=stop, step=step, repeat=repeat, infer_range=False, dtype=dtype, ctx=str(ctx))
[ "def", "arange", "(", "start", ",", "stop", "=", "None", ",", "step", "=", "1.0", ",", "repeat", "=", "1", ",", "infer_range", "=", "None", ",", "ctx", "=", "None", ",", "dtype", "=", "mx_real_t", ")", ":", "if", "infer_range", "is", "not", "None", ":", "warnings", ".", "warn", "(", "'`infer_range` argument has been deprecated'", ",", "DeprecationWarning", ")", "if", "ctx", "is", "None", ":", "ctx", "=", "current_context", "(", ")", "return", "_internal", ".", "_arange", "(", "start", "=", "start", ",", "stop", "=", "stop", ",", "step", "=", "step", ",", "repeat", "=", "repeat", ",", "infer_range", "=", "False", ",", "dtype", "=", "dtype", ",", "ctx", "=", "str", "(", "ctx", ")", ")" ]
Returns evenly spaced values within a given interval. Values are generated within the half-open interval [`start`, `stop`). In other words, the interval includes `start` but excludes `stop`. The function is similar to the built-in Python function `range` and to `numpy.arange`, but returns an `NDArray`. Parameters ---------- start : number, optional Start of interval. The default start value is 0. stop : number End of interval. step : number, optional Spacing between values. The default step size is 1. repeat : int, optional Number of times to repeat each element. The default repeat count is 1. infer_range : boolean, optional When set to True, infer the stop position from the start, step, repeat, and output tensor size. ctx : Context, optional Device context. Default context is the current default context. dtype : str or numpy.dtype, optional The data type of the `NDArray`. The default datatype is `np.float32`. Returns ------- NDArray `NDArray` of evenly spaced values in the specified range. Examples -------- >>> mx.nd.arange(3).asnumpy() array([ 0., 1., 2.], dtype=float32) >>> mx.nd.arange(2, 6).asnumpy() array([ 2., 3., 4., 5.], dtype=float32) >>> mx.nd.arange(2, 6, step=2).asnumpy() array([ 2., 4.], dtype=float32) >>> mx.nd.arange(2, 6, step=1.5, repeat=2).asnumpy() array([ 2. , 2. , 3.5, 3.5, 5. , 5. ], dtype=float32) >>> mx.nd.arange(2, 6, step=2, repeat=3, dtype='int32').asnumpy() array([2, 2, 2, 4, 4, 4], dtype=int32)
[ "Returns", "evenly", "spaced", "values", "within", "a", "given", "interval", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2560-L2610
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
_ufunc_helper
def _ufunc_helper(lhs, rhs, fn_array, fn_scalar, lfn_scalar, rfn_scalar=None): """ Helper function for element-wise operation. The function will perform numpy-like broadcasting if needed and call different functions. Parameters -------- lhs : NDArray or numeric value Left-hand side operand. rhs : NDArray or numeric value Right-hand operand, fn_array : function Function to be called if both lhs and rhs are of ``NDArray`` type. fn_scalar : function Function to be called if both lhs and rhs are numeric values. lfn_scalar : function Function to be called if lhs is ``NDArray`` while rhs is numeric value rfn_scalar : function Function to be called if lhs is numeric value while rhs is ``NDArray``; if none is provided, then the function is commutative, so rfn_scalar is equal to lfn_scalar Returns -------- NDArray result array """ if isinstance(lhs, numeric_types): if isinstance(rhs, numeric_types): return fn_scalar(lhs, rhs) else: if rfn_scalar is None: # commutative function return lfn_scalar(rhs, float(lhs)) else: return rfn_scalar(rhs, float(lhs)) elif isinstance(rhs, numeric_types): return lfn_scalar(lhs, float(rhs)) elif isinstance(rhs, NDArray): return fn_array(lhs, rhs) else: raise TypeError('type %s not supported' % str(type(rhs)))
python
def _ufunc_helper(lhs, rhs, fn_array, fn_scalar, lfn_scalar, rfn_scalar=None): """ Helper function for element-wise operation. The function will perform numpy-like broadcasting if needed and call different functions. Parameters -------- lhs : NDArray or numeric value Left-hand side operand. rhs : NDArray or numeric value Right-hand operand, fn_array : function Function to be called if both lhs and rhs are of ``NDArray`` type. fn_scalar : function Function to be called if both lhs and rhs are numeric values. lfn_scalar : function Function to be called if lhs is ``NDArray`` while rhs is numeric value rfn_scalar : function Function to be called if lhs is numeric value while rhs is ``NDArray``; if none is provided, then the function is commutative, so rfn_scalar is equal to lfn_scalar Returns -------- NDArray result array """ if isinstance(lhs, numeric_types): if isinstance(rhs, numeric_types): return fn_scalar(lhs, rhs) else: if rfn_scalar is None: # commutative function return lfn_scalar(rhs, float(lhs)) else: return rfn_scalar(rhs, float(lhs)) elif isinstance(rhs, numeric_types): return lfn_scalar(lhs, float(rhs)) elif isinstance(rhs, NDArray): return fn_array(lhs, rhs) else: raise TypeError('type %s not supported' % str(type(rhs)))
[ "def", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "fn_array", ",", "fn_scalar", ",", "lfn_scalar", ",", "rfn_scalar", "=", "None", ")", ":", "if", "isinstance", "(", "lhs", ",", "numeric_types", ")", ":", "if", "isinstance", "(", "rhs", ",", "numeric_types", ")", ":", "return", "fn_scalar", "(", "lhs", ",", "rhs", ")", "else", ":", "if", "rfn_scalar", "is", "None", ":", "# commutative function", "return", "lfn_scalar", "(", "rhs", ",", "float", "(", "lhs", ")", ")", "else", ":", "return", "rfn_scalar", "(", "rhs", ",", "float", "(", "lhs", ")", ")", "elif", "isinstance", "(", "rhs", ",", "numeric_types", ")", ":", "return", "lfn_scalar", "(", "lhs", ",", "float", "(", "rhs", ")", ")", "elif", "isinstance", "(", "rhs", ",", "NDArray", ")", ":", "return", "fn_array", "(", "lhs", ",", "rhs", ")", "else", ":", "raise", "TypeError", "(", "'type %s not supported'", "%", "str", "(", "type", "(", "rhs", ")", ")", ")" ]
Helper function for element-wise operation. The function will perform numpy-like broadcasting if needed and call different functions. Parameters -------- lhs : NDArray or numeric value Left-hand side operand. rhs : NDArray or numeric value Right-hand operand, fn_array : function Function to be called if both lhs and rhs are of ``NDArray`` type. fn_scalar : function Function to be called if both lhs and rhs are numeric values. lfn_scalar : function Function to be called if lhs is ``NDArray`` while rhs is numeric value rfn_scalar : function Function to be called if lhs is numeric value while rhs is ``NDArray``; if none is provided, then the function is commutative, so rfn_scalar is equal to lfn_scalar Returns -------- NDArray result array
[ "Helper", "function", "for", "element", "-", "wise", "operation", ".", "The", "function", "will", "perform", "numpy", "-", "like", "broadcasting", "if", "needed", "and", "call", "different", "functions", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2615-L2659
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
add
def add(lhs, rhs): """Returns element-wise sum of the input arrays with broadcasting. Equivalent to ``lhs + rhs``, ``mx.nd.broadcast_add(lhs, rhs)`` and ``mx.nd.broadcast_plus(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be added. rhs : scalar or mxnet.ndarray.array Second array to be added. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise sum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x+2).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) >>> (x+y).asnumpy() array([[ 1., 1., 1.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.add(x,y).asnumpy() array([[ 1., 1., 1.], [ 2., 2., 2.]], dtype=float32) >>> (z + y).asnumpy() array([[ 0., 1.], [ 1., 2.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_add, operator.add, _internal._plus_scalar, None)
python
def add(lhs, rhs): """Returns element-wise sum of the input arrays with broadcasting. Equivalent to ``lhs + rhs``, ``mx.nd.broadcast_add(lhs, rhs)`` and ``mx.nd.broadcast_plus(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be added. rhs : scalar or mxnet.ndarray.array Second array to be added. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise sum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x+2).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) >>> (x+y).asnumpy() array([[ 1., 1., 1.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.add(x,y).asnumpy() array([[ 1., 1., 1.], [ 2., 2., 2.]], dtype=float32) >>> (z + y).asnumpy() array([[ 0., 1.], [ 1., 2.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_add, operator.add, _internal._plus_scalar, None)
[ "def", "add", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_add", ",", "operator", ".", "add", ",", "_internal", ".", "_plus_scalar", ",", "None", ")" ]
Returns element-wise sum of the input arrays with broadcasting. Equivalent to ``lhs + rhs``, ``mx.nd.broadcast_add(lhs, rhs)`` and ``mx.nd.broadcast_plus(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be added. rhs : scalar or mxnet.ndarray.array Second array to be added. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise sum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x+2).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) >>> (x+y).asnumpy() array([[ 1., 1., 1.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.add(x,y).asnumpy() array([[ 1., 1., 1.], [ 2., 2., 2.]], dtype=float32) >>> (z + y).asnumpy() array([[ 0., 1.], [ 1., 2.]], dtype=float32)
[ "Returns", "element", "-", "wise", "sum", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2663-L2721
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
subtract
def subtract(lhs, rhs): """Returns element-wise difference of the input arrays with broadcasting. Equivalent to ``lhs - rhs``, ``mx.nd.broadcast_sub(lhs, rhs)`` and ``mx.nd.broadcast_minus(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be subtracted. rhs : scalar or mxnet.ndarray.array Second array to be subtracted. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise difference of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x-2).asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32) >>> (x-y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.subtract(x,y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z-y).asnumpy() array([[ 0., 1.], [-1., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_sub, operator.sub, _internal._minus_scalar, _internal._rminus_scalar)
python
def subtract(lhs, rhs): """Returns element-wise difference of the input arrays with broadcasting. Equivalent to ``lhs - rhs``, ``mx.nd.broadcast_sub(lhs, rhs)`` and ``mx.nd.broadcast_minus(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be subtracted. rhs : scalar or mxnet.ndarray.array Second array to be subtracted. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise difference of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x-2).asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32) >>> (x-y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.subtract(x,y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z-y).asnumpy() array([[ 0., 1.], [-1., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_sub, operator.sub, _internal._minus_scalar, _internal._rminus_scalar)
[ "def", "subtract", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_sub", ",", "operator", ".", "sub", ",", "_internal", ".", "_minus_scalar", ",", "_internal", ".", "_rminus_scalar", ")" ]
Returns element-wise difference of the input arrays with broadcasting. Equivalent to ``lhs - rhs``, ``mx.nd.broadcast_sub(lhs, rhs)`` and ``mx.nd.broadcast_minus(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be subtracted. rhs : scalar or mxnet.ndarray.array Second array to be subtracted. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise difference of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x-2).asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32) >>> (x-y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.subtract(x,y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z-y).asnumpy() array([[ 0., 1.], [-1., 0.]], dtype=float32)
[ "Returns", "element", "-", "wise", "difference", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2725-L2783
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
multiply
def multiply(lhs, rhs): """Returns element-wise product of the input arrays with broadcasting. Equivalent to ``lhs * rhs`` and ``mx.nd.broadcast_mul(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be multiplied. rhs : scalar or mxnet.ndarray.array Second array to be multiplied. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise multiplication of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x*2).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> (x*y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.multiply(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z*y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_mul, operator.mul, _internal._mul_scalar, None)
python
def multiply(lhs, rhs): """Returns element-wise product of the input arrays with broadcasting. Equivalent to ``lhs * rhs`` and ``mx.nd.broadcast_mul(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be multiplied. rhs : scalar or mxnet.ndarray.array Second array to be multiplied. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise multiplication of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x*2).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> (x*y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.multiply(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z*y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_mul, operator.mul, _internal._mul_scalar, None)
[ "def", "multiply", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_mul", ",", "operator", ".", "mul", ",", "_internal", ".", "_mul_scalar", ",", "None", ")" ]
Returns element-wise product of the input arrays with broadcasting. Equivalent to ``lhs * rhs`` and ``mx.nd.broadcast_mul(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be multiplied. rhs : scalar or mxnet.ndarray.array Second array to be multiplied. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise multiplication of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x*2).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> (x*y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.multiply(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z*y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32)
[ "Returns", "element", "-", "wise", "product", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2787-L2844
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
divide
def divide(lhs, rhs): """Returns element-wise division of the input arrays with broadcasting. Equivalent to ``lhs / rhs`` and ``mx.nd.broadcast_div(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array in division. rhs : scalar or mxnet.ndarray.array Second array in division. The arrays to be divided. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise division of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3))*6 >>> y = mx.nd.ones((2,1))*2 >>> x.asnumpy() array([[ 6., 6., 6.], [ 6., 6., 6.]], dtype=float32) >>> y.asnumpy() array([[ 2.], [ 2.]], dtype=float32) >>> x/2 <NDArray 2x3 @cpu(0)> >>> (x/3).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> (x/y).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) >>> mx.nd.divide(x,y).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_div, operator.truediv, _internal._div_scalar, _internal._rdiv_scalar)
python
def divide(lhs, rhs): """Returns element-wise division of the input arrays with broadcasting. Equivalent to ``lhs / rhs`` and ``mx.nd.broadcast_div(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array in division. rhs : scalar or mxnet.ndarray.array Second array in division. The arrays to be divided. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise division of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3))*6 >>> y = mx.nd.ones((2,1))*2 >>> x.asnumpy() array([[ 6., 6., 6.], [ 6., 6., 6.]], dtype=float32) >>> y.asnumpy() array([[ 2.], [ 2.]], dtype=float32) >>> x/2 <NDArray 2x3 @cpu(0)> >>> (x/3).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> (x/y).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) >>> mx.nd.divide(x,y).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_div, operator.truediv, _internal._div_scalar, _internal._rdiv_scalar)
[ "def", "divide", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_div", ",", "operator", ".", "truediv", ",", "_internal", ".", "_div_scalar", ",", "_internal", ".", "_rdiv_scalar", ")" ]
Returns element-wise division of the input arrays with broadcasting. Equivalent to ``lhs / rhs`` and ``mx.nd.broadcast_div(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array in division. rhs : scalar or mxnet.ndarray.array Second array in division. The arrays to be divided. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise division of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3))*6 >>> y = mx.nd.ones((2,1))*2 >>> x.asnumpy() array([[ 6., 6., 6.], [ 6., 6., 6.]], dtype=float32) >>> y.asnumpy() array([[ 2.], [ 2.]], dtype=float32) >>> x/2 <NDArray 2x3 @cpu(0)> >>> (x/3).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> (x/y).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32) >>> mx.nd.divide(x,y).asnumpy() array([[ 3., 3., 3.], [ 3., 3., 3.]], dtype=float32)
[ "Returns", "element", "-", "wise", "division", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2848-L2901
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
modulo
def modulo(lhs, rhs): """Returns element-wise modulo of the input arrays with broadcasting. Equivalent to ``lhs % rhs`` and ``mx.nd.broadcast_mod(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array in modulo. rhs : scalar or mxnet.ndarray.array Second array in modulo. The arrays to be taken modulo. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise modulo of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3))*6 >>> y = mx.nd.ones((2,1))*4 >>> x.asnumpy() array([[ 6., 6., 6.], [ 6., 6., 6.]], dtype=float32) >>> y.asnumpy() array([[ 4.], [ 4.]], dtype=float32) >>> x%5 <NDArray 2x3 @cpu(0)> >>> (x%5).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x%y).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.modulo(x,y).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_mod, operator.mod, _internal._mod_scalar, _internal._rmod_scalar)
python
def modulo(lhs, rhs): """Returns element-wise modulo of the input arrays with broadcasting. Equivalent to ``lhs % rhs`` and ``mx.nd.broadcast_mod(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array in modulo. rhs : scalar or mxnet.ndarray.array Second array in modulo. The arrays to be taken modulo. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise modulo of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3))*6 >>> y = mx.nd.ones((2,1))*4 >>> x.asnumpy() array([[ 6., 6., 6.], [ 6., 6., 6.]], dtype=float32) >>> y.asnumpy() array([[ 4.], [ 4.]], dtype=float32) >>> x%5 <NDArray 2x3 @cpu(0)> >>> (x%5).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x%y).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.modulo(x,y).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_mod, operator.mod, _internal._mod_scalar, _internal._rmod_scalar)
[ "def", "modulo", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_mod", ",", "operator", ".", "mod", ",", "_internal", ".", "_mod_scalar", ",", "_internal", ".", "_rmod_scalar", ")" ]
Returns element-wise modulo of the input arrays with broadcasting. Equivalent to ``lhs % rhs`` and ``mx.nd.broadcast_mod(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array in modulo. rhs : scalar or mxnet.ndarray.array Second array in modulo. The arrays to be taken modulo. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise modulo of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3))*6 >>> y = mx.nd.ones((2,1))*4 >>> x.asnumpy() array([[ 6., 6., 6.], [ 6., 6., 6.]], dtype=float32) >>> y.asnumpy() array([[ 4.], [ 4.]], dtype=float32) >>> x%5 <NDArray 2x3 @cpu(0)> >>> (x%5).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x%y).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.modulo(x,y).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32)
[ "Returns", "element", "-", "wise", "modulo", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2905-L2958
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
power
def power(base, exp): """Returns result of first array elements raised to powers from second array, element-wise with broadcasting. Equivalent to ``base ** exp`` and ``mx.nd.broadcast_power(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- base : scalar or NDArray The base array exp : scalar or NDArray The exponent array. If ``base.shape != exp.shape``, they must be broadcastable to a common shape. Returns -------- NDArray The bases in x raised to the exponents in y. Examples -------- >>> x = mx.nd.ones((2,3))*2 >>> y = mx.nd.arange(1,3).reshape((2,1)) >>> z = mx.nd.arange(1,3).reshape((2,1)) >>> x.asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> y.asnumpy() array([[ 1.], [ 2.]], dtype=float32) >>> z.asnumpy() array([[ 1.], [ 2.]], dtype=float32) >>> (x**2).asnumpy() array([[ 4., 4., 4.], [ 4., 4., 4.]], dtype=float32) >>> (x**y).asnumpy() array([[ 2., 2., 2.], [ 4., 4., 4.]], dtype=float32) >>> mx.nd.power(x,y).asnumpy() array([[ 2., 2., 2.], [ 4., 4., 4.]], dtype=float32) >>> (z**y).asnumpy() array([[ 1.], [ 4.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( base, exp, op.broadcast_power, operator.pow, _internal._power_scalar, _internal._rpower_scalar)
python
def power(base, exp): """Returns result of first array elements raised to powers from second array, element-wise with broadcasting. Equivalent to ``base ** exp`` and ``mx.nd.broadcast_power(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- base : scalar or NDArray The base array exp : scalar or NDArray The exponent array. If ``base.shape != exp.shape``, they must be broadcastable to a common shape. Returns -------- NDArray The bases in x raised to the exponents in y. Examples -------- >>> x = mx.nd.ones((2,3))*2 >>> y = mx.nd.arange(1,3).reshape((2,1)) >>> z = mx.nd.arange(1,3).reshape((2,1)) >>> x.asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> y.asnumpy() array([[ 1.], [ 2.]], dtype=float32) >>> z.asnumpy() array([[ 1.], [ 2.]], dtype=float32) >>> (x**2).asnumpy() array([[ 4., 4., 4.], [ 4., 4., 4.]], dtype=float32) >>> (x**y).asnumpy() array([[ 2., 2., 2.], [ 4., 4., 4.]], dtype=float32) >>> mx.nd.power(x,y).asnumpy() array([[ 2., 2., 2.], [ 4., 4., 4.]], dtype=float32) >>> (z**y).asnumpy() array([[ 1.], [ 4.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( base, exp, op.broadcast_power, operator.pow, _internal._power_scalar, _internal._rpower_scalar)
[ "def", "power", "(", "base", ",", "exp", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "base", ",", "exp", ",", "op", ".", "broadcast_power", ",", "operator", ".", "pow", ",", "_internal", ".", "_power_scalar", ",", "_internal", ".", "_rpower_scalar", ")" ]
Returns result of first array elements raised to powers from second array, element-wise with broadcasting. Equivalent to ``base ** exp`` and ``mx.nd.broadcast_power(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- base : scalar or NDArray The base array exp : scalar or NDArray The exponent array. If ``base.shape != exp.shape``, they must be broadcastable to a common shape. Returns -------- NDArray The bases in x raised to the exponents in y. Examples -------- >>> x = mx.nd.ones((2,3))*2 >>> y = mx.nd.arange(1,3).reshape((2,1)) >>> z = mx.nd.arange(1,3).reshape((2,1)) >>> x.asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> y.asnumpy() array([[ 1.], [ 2.]], dtype=float32) >>> z.asnumpy() array([[ 1.], [ 2.]], dtype=float32) >>> (x**2).asnumpy() array([[ 4., 4., 4.], [ 4., 4., 4.]], dtype=float32) >>> (x**y).asnumpy() array([[ 2., 2., 2.], [ 4., 4., 4.]], dtype=float32) >>> mx.nd.power(x,y).asnumpy() array([[ 2., 2., 2.], [ 4., 4., 4.]], dtype=float32) >>> (z**y).asnumpy() array([[ 1.], [ 4.]], dtype=float32)
[ "Returns", "result", "of", "first", "array", "elements", "raised", "to", "powers", "from", "second", "array", "element", "-", "wise", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L2962-L3020
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
maximum
def maximum(lhs, rhs): """Returns element-wise maximum of the input arrays with broadcasting. Equivalent to ``mx.nd.broadcast_maximum(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise maximum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.maximum(x, 2).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.maximum(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.maximum(y, z).asnumpy() array([[ 0., 1.], [ 1., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_maximum, lambda x, y: x if x > y else y, _internal._maximum_scalar, None)
python
def maximum(lhs, rhs): """Returns element-wise maximum of the input arrays with broadcasting. Equivalent to ``mx.nd.broadcast_maximum(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise maximum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.maximum(x, 2).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.maximum(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.maximum(y, z).asnumpy() array([[ 0., 1.], [ 1., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_maximum, lambda x, y: x if x > y else y, _internal._maximum_scalar, None)
[ "def", "maximum", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_maximum", ",", "lambda", "x", ",", "y", ":", "x", "if", "x", ">", "y", "else", "y", ",", "_internal", ".", "_maximum_scalar", ",", "None", ")" ]
Returns element-wise maximum of the input arrays with broadcasting. Equivalent to ``mx.nd.broadcast_maximum(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise maximum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.maximum(x, 2).asnumpy() array([[ 2., 2., 2.], [ 2., 2., 2.]], dtype=float32) >>> mx.nd.maximum(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.maximum(y, z).asnumpy() array([[ 0., 1.], [ 1., 1.]], dtype=float32)
[ "Returns", "element", "-", "wise", "maximum", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3024-L3077
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
minimum
def minimum(lhs, rhs): """Returns element-wise minimum of the input arrays with broadcasting. Equivalent to ``mx.nd.broadcast_minimum(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise minimum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.minimum(x, 2).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.minimum(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.minimum(z, y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_minimum, lambda x, y: x if x < y else y, _internal._minimum_scalar, None)
python
def minimum(lhs, rhs): """Returns element-wise minimum of the input arrays with broadcasting. Equivalent to ``mx.nd.broadcast_minimum(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise minimum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.minimum(x, 2).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.minimum(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.minimum(z, y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_minimum, lambda x, y: x if x < y else y, _internal._minimum_scalar, None)
[ "def", "minimum", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_minimum", ",", "lambda", "x", ",", "y", ":", "x", "if", "x", "<", "y", "else", "y", ",", "_internal", ".", "_minimum_scalar", ",", "None", ")" ]
Returns element-wise minimum of the input arrays with broadcasting. Equivalent to ``mx.nd.broadcast_minimum(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray The element-wise minimum of the input arrays. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.minimum(x, 2).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.minimum(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.minimum(z, y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32)
[ "Returns", "element", "-", "wise", "minimum", "of", "the", "input", "arrays", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3081-L3134
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
equal
def equal(lhs, rhs): """Returns the result of element-wise **equal to** (==) comparison operation with broadcasting. For each element in input arrays, return 1(true) if corresponding elements are same, otherwise return 0(false). Equivalent to ``lhs == rhs`` and ``mx.nd.broadcast_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x == 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x == y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.equal(x,y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z == y).asnumpy() array([[ 1., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_equal, lambda x, y: 1 if x == y else 0, _internal._equal_scalar, None)
python
def equal(lhs, rhs): """Returns the result of element-wise **equal to** (==) comparison operation with broadcasting. For each element in input arrays, return 1(true) if corresponding elements are same, otherwise return 0(false). Equivalent to ``lhs == rhs`` and ``mx.nd.broadcast_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x == 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x == y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.equal(x,y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z == y).asnumpy() array([[ 1., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_equal, lambda x, y: 1 if x == y else 0, _internal._equal_scalar, None)
[ "def", "equal", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_equal", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", "==", "y", "else", "0", ",", "_internal", ".", "_equal_scalar", ",", "None", ")" ]
Returns the result of element-wise **equal to** (==) comparison operation with broadcasting. For each element in input arrays, return 1(true) if corresponding elements are same, otherwise return 0(false). Equivalent to ``lhs == rhs`` and ``mx.nd.broadcast_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x == 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x == y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.equal(x,y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z == y).asnumpy() array([[ 1., 0.], [ 0., 1.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "equal", "to", "**", "(", "==", ")", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3138-L3198
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
not_equal
def not_equal(lhs, rhs): """Returns the result of element-wise **not equal to** (!=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if corresponding elements are different, otherwise return 0(false). Equivalent to ``lhs != rhs`` and ``mx.nd.broadcast_not_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (z == y).asnumpy() array([[ 1., 0.], [ 0., 1.]], dtype=float32) >>> (x != 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x != y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.not_equal(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z != y).asnumpy() array([[ 0., 1.], [ 1., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_not_equal, lambda x, y: 1 if x != y else 0, _internal._not_equal_scalar, None)
python
def not_equal(lhs, rhs): """Returns the result of element-wise **not equal to** (!=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if corresponding elements are different, otherwise return 0(false). Equivalent to ``lhs != rhs`` and ``mx.nd.broadcast_not_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (z == y).asnumpy() array([[ 1., 0.], [ 0., 1.]], dtype=float32) >>> (x != 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x != y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.not_equal(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z != y).asnumpy() array([[ 0., 1.], [ 1., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_not_equal, lambda x, y: 1 if x != y else 0, _internal._not_equal_scalar, None)
[ "def", "not_equal", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_not_equal", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", "!=", "y", "else", "0", ",", "_internal", ".", "_not_equal_scalar", ",", "None", ")" ]
Returns the result of element-wise **not equal to** (!=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if corresponding elements are different, otherwise return 0(false). Equivalent to ``lhs != rhs`` and ``mx.nd.broadcast_not_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (z == y).asnumpy() array([[ 1., 0.], [ 0., 1.]], dtype=float32) >>> (x != 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x != y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.not_equal(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z != y).asnumpy() array([[ 0., 1.], [ 1., 0.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "not", "equal", "to", "**", "(", "!", "=", ")", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3202-L3265
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
greater
def greater(lhs, rhs): """Returns the result of element-wise **greater than** (>) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are greater than rhs, otherwise return 0(false). Equivalent to ``lhs > rhs`` and ``mx.nd.broadcast_greater(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x > 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x > y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.greater(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z > y).asnumpy() array([[ 0., 1.], [ 0., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_greater, lambda x, y: 1 if x > y else 0, _internal._greater_scalar, _internal._lesser_scalar)
python
def greater(lhs, rhs): """Returns the result of element-wise **greater than** (>) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are greater than rhs, otherwise return 0(false). Equivalent to ``lhs > rhs`` and ``mx.nd.broadcast_greater(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x > 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x > y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.greater(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z > y).asnumpy() array([[ 0., 1.], [ 0., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_greater, lambda x, y: 1 if x > y else 0, _internal._greater_scalar, _internal._lesser_scalar)
[ "def", "greater", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_greater", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", ">", "y", "else", "0", ",", "_internal", ".", "_greater_scalar", ",", "_internal", ".", "_lesser_scalar", ")" ]
Returns the result of element-wise **greater than** (>) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are greater than rhs, otherwise return 0(false). Equivalent to ``lhs > rhs`` and ``mx.nd.broadcast_greater(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x > 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x > y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.greater(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) >>> (z > y).asnumpy() array([[ 0., 1.], [ 0., 0.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "greater", "than", "**", "(", ">", ")", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3269-L3329
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
greater_equal
def greater_equal(lhs, rhs): """Returns the result of element-wise **greater than or equal to** (>=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are greater than equal to rhs, otherwise return 0(false). Equivalent to ``lhs >= rhs`` and ``mx.nd.broadcast_greater_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x >= 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x >= y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.greater_equal(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (z >= y).asnumpy() array([[ 1., 1.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_greater_equal, lambda x, y: 1 if x >= y else 0, _internal._greater_equal_scalar, _internal._lesser_equal_scalar)
python
def greater_equal(lhs, rhs): """Returns the result of element-wise **greater than or equal to** (>=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are greater than equal to rhs, otherwise return 0(false). Equivalent to ``lhs >= rhs`` and ``mx.nd.broadcast_greater_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x >= 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x >= y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.greater_equal(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (z >= y).asnumpy() array([[ 1., 1.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_greater_equal, lambda x, y: 1 if x >= y else 0, _internal._greater_equal_scalar, _internal._lesser_equal_scalar)
[ "def", "greater_equal", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_greater_equal", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", ">=", "y", "else", "0", ",", "_internal", ".", "_greater_equal_scalar", ",", "_internal", ".", "_lesser_equal_scalar", ")" ]
Returns the result of element-wise **greater than or equal to** (>=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are greater than equal to rhs, otherwise return 0(false). Equivalent to ``lhs >= rhs`` and ``mx.nd.broadcast_greater_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x >= 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x >= y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.greater_equal(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (z >= y).asnumpy() array([[ 1., 1.], [ 0., 1.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "greater", "than", "or", "equal", "to", "**", "(", ">", "=", ")", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3333-L3393
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
lesser
def lesser(lhs, rhs): """Returns the result of element-wise **lesser than** (<) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are less than rhs, otherwise return 0(false). Equivalent to ``lhs < rhs`` and ``mx.nd.broadcast_lesser(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x < 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x < y).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.lesser(x, y).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (z < y).asnumpy() array([[ 0., 0.], [ 1., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_lesser, lambda x, y: 1 if x < y else 0, _internal._lesser_scalar, _internal._greater_scalar)
python
def lesser(lhs, rhs): """Returns the result of element-wise **lesser than** (<) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are less than rhs, otherwise return 0(false). Equivalent to ``lhs < rhs`` and ``mx.nd.broadcast_lesser(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x < 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x < y).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.lesser(x, y).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (z < y).asnumpy() array([[ 0., 0.], [ 1., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_lesser, lambda x, y: 1 if x < y else 0, _internal._lesser_scalar, _internal._greater_scalar)
[ "def", "lesser", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_lesser", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", "<", "y", "else", "0", ",", "_internal", ".", "_lesser_scalar", ",", "_internal", ".", "_greater_scalar", ")" ]
Returns the result of element-wise **lesser than** (<) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are less than rhs, otherwise return 0(false). Equivalent to ``lhs < rhs`` and ``mx.nd.broadcast_lesser(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x < 1).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (x < y).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> mx.nd.lesser(x, y).asnumpy() array([[ 0., 0., 0.], [ 0., 0., 0.]], dtype=float32) >>> (z < y).asnumpy() array([[ 0., 0.], [ 1., 0.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "lesser", "than", "**", "(", "<", ")", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3397-L3457
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
lesser_equal
def lesser_equal(lhs, rhs): """Returns the result of element-wise **lesser than or equal to** (<=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are lesser than equal to rhs, otherwise return 0(false). Equivalent to ``lhs <= rhs`` and ``mx.nd.broadcast_lesser_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x <= 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x <= y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.lesser_equal(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z <= y).asnumpy() array([[ 1., 0.], [ 1., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_lesser_equal, lambda x, y: 1 if x <= y else 0, _internal._lesser_equal_scalar, _internal._greater_equal_scalar)
python
def lesser_equal(lhs, rhs): """Returns the result of element-wise **lesser than or equal to** (<=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are lesser than equal to rhs, otherwise return 0(false). Equivalent to ``lhs <= rhs`` and ``mx.nd.broadcast_lesser_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x <= 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x <= y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.lesser_equal(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z <= y).asnumpy() array([[ 1., 0.], [ 1., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_lesser_equal, lambda x, y: 1 if x <= y else 0, _internal._lesser_equal_scalar, _internal._greater_equal_scalar)
[ "def", "lesser_equal", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_lesser_equal", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", "<=", "y", "else", "0", ",", "_internal", ".", "_lesser_equal_scalar", ",", "_internal", ".", "_greater_equal_scalar", ")" ]
Returns the result of element-wise **lesser than or equal to** (<=) comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements are lesser than equal to rhs, otherwise return 0(false). Equivalent to ``lhs <= rhs`` and ``mx.nd.broadcast_lesser_equal(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First array to be compared. rhs : scalar or mxnet.ndarray.array Second array to be compared. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> (x <= 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> (x <= y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.lesser_equal(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> (z <= y).asnumpy() array([[ 1., 0.], [ 1., 1.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "lesser", "than", "or", "equal", "to", "**", "(", "<", "=", ")", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3461-L3521
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
logical_and
def logical_and(lhs, rhs): """Returns the result of element-wise **logical and** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements and rhs elements are true, otherwise return 0(false). Equivalent to ``lhs and rhs`` and ``mx.nd.broadcast_logical_and(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_and(x, 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_and(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_and(z, y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_logical_and, lambda x, y: 1 if x and y else 0, _internal._logical_and_scalar, None)
python
def logical_and(lhs, rhs): """Returns the result of element-wise **logical and** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements and rhs elements are true, otherwise return 0(false). Equivalent to ``lhs and rhs`` and ``mx.nd.broadcast_logical_and(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_and(x, 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_and(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_and(z, y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_logical_and, lambda x, y: 1 if x and y else 0, _internal._logical_and_scalar, None)
[ "def", "logical_and", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_logical_and", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", "and", "y", "else", "0", ",", "_internal", ".", "_logical_and_scalar", ",", "None", ")" ]
Returns the result of element-wise **logical and** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements and rhs elements are true, otherwise return 0(false). Equivalent to ``lhs and rhs`` and ``mx.nd.broadcast_logical_and(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_and(x, 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_and(x, y).asnumpy() array([[ 0., 0., 0.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_and(z, y).asnumpy() array([[ 0., 0.], [ 0., 1.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "logical", "and", "**", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3524-L3581
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
logical_or
def logical_or(lhs, rhs): """Returns the result of element-wise **logical or** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false). Equivalent to ``lhs or rhs`` and ``mx.nd.broadcast_logical_or(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_or(x, 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_or(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_or(z, y).asnumpy() array([[ 0., 1.], [ 1., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_logical_or, lambda x, y: 1 if x or y else 0, _internal._logical_or_scalar, None)
python
def logical_or(lhs, rhs): """Returns the result of element-wise **logical or** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false). Equivalent to ``lhs or rhs`` and ``mx.nd.broadcast_logical_or(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_or(x, 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_or(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_or(z, y).asnumpy() array([[ 0., 1.], [ 1., 1.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_logical_or, lambda x, y: 1 if x or y else 0, _internal._logical_or_scalar, None)
[ "def", "logical_or", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_logical_or", ",", "lambda", "x", ",", "y", ":", "1", "if", "x", "or", "y", "else", "0", ",", "_internal", ".", "_logical_or_scalar", ",", "None", ")" ]
Returns the result of element-wise **logical or** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false). Equivalent to ``lhs or rhs`` and ``mx.nd.broadcast_logical_or(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_or(x, 1).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_or(x, y).asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> mx.nd.logical_or(z, y).asnumpy() array([[ 0., 1.], [ 1., 1.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "logical", "or", "**", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3584-L3641
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
logical_xor
def logical_xor(lhs, rhs): """Returns the result of element-wise **logical xor** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false). Equivalent to ``bool(lhs) ^ bool(rhs)`` and ``mx.nd.broadcast_logical_xor(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_xor(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_logical_xor, lambda x, y: 1 if bool(x) ^ bool(y) else 0, _internal._logical_xor_scalar, None)
python
def logical_xor(lhs, rhs): """Returns the result of element-wise **logical xor** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false). Equivalent to ``bool(lhs) ^ bool(rhs)`` and ``mx.nd.broadcast_logical_xor(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_xor(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32) """ # pylint: disable= no-member, protected-access return _ufunc_helper( lhs, rhs, op.broadcast_logical_xor, lambda x, y: 1 if bool(x) ^ bool(y) else 0, _internal._logical_xor_scalar, None)
[ "def", "logical_xor", "(", "lhs", ",", "rhs", ")", ":", "# pylint: disable= no-member, protected-access", "return", "_ufunc_helper", "(", "lhs", ",", "rhs", ",", "op", ".", "broadcast_logical_xor", ",", "lambda", "x", ",", "y", ":", "1", "if", "bool", "(", "x", ")", "^", "bool", "(", "y", ")", "else", "0", ",", "_internal", ".", "_logical_xor_scalar", ",", "None", ")" ]
Returns the result of element-wise **logical xor** comparison operation with broadcasting. For each element in input arrays, return 1(true) if lhs elements or rhs elements are true, otherwise return 0(false). Equivalent to ``bool(lhs) ^ bool(rhs)`` and ``mx.nd.broadcast_logical_xor(lhs, rhs)``. .. note:: If the corresponding dimensions of two arrays have the same size or one of them has size 1, then the arrays are broadcastable to a common shape. Parameters ---------- lhs : scalar or mxnet.ndarray.array First input of the function. rhs : scalar or mxnet.ndarray.array Second input of the function. If ``lhs.shape != rhs.shape``, they must be broadcastable to a common shape. Returns ------- NDArray Output array of boolean values. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.arange(2).reshape((2,1)) >>> z = mx.nd.arange(2).reshape((1,2)) >>> x.asnumpy() array([[ 1., 1., 1.], [ 1., 1., 1.]], dtype=float32) >>> y.asnumpy() array([[ 0.], [ 1.]], dtype=float32) >>> z.asnumpy() array([[ 0., 1.]], dtype=float32) >>> mx.nd.logical_xor(x, y).asnumpy() array([[ 1., 1., 1.], [ 0., 0., 0.]], dtype=float32)
[ "Returns", "the", "result", "of", "element", "-", "wise", "**", "logical", "xor", "**", "comparison", "operation", "with", "broadcasting", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3644-L3695
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
concatenate
def concatenate(arrays, axis=0, always_copy=True): """DEPRECATED, use ``concat`` instead Parameters ---------- arrays : list of `NDArray` Arrays to be concatenate. They must have identical shape except the first dimension. They also must have the same data type. axis : int The axis along which to concatenate. always_copy : bool Default `True`. When not `True`, if the arrays only contain one `NDArray`, that element will be returned directly, avoid copying. Returns ------- NDArray An `NDArray` that lives on the same context as `arrays[0].context`. """ assert isinstance(arrays, list) assert len(arrays) > 0 assert isinstance(arrays[0], NDArray) if not always_copy and len(arrays) == 1: return arrays[0] shape_axis = arrays[0].shape[axis] shape_rest1 = arrays[0].shape[0:axis] shape_rest2 = arrays[0].shape[axis+1:] dtype = arrays[0].dtype for arr in arrays[1:]: shape_axis += arr.shape[axis] assert shape_rest1 == arr.shape[0:axis] assert shape_rest2 == arr.shape[axis+1:] assert dtype == arr.dtype ret_shape = shape_rest1 + (shape_axis,) + shape_rest2 ret = empty(ret_shape, ctx=arrays[0].context, dtype=dtype) idx = 0 begin = [0 for _ in ret_shape] end = list(ret_shape) for arr in arrays: if axis == 0: ret[idx:idx+arr.shape[0]] = arr else: begin[axis] = idx end[axis] = idx+arr.shape[axis] # pylint: disable=no-member,protected-access _internal._crop_assign(ret, arr, out=ret, begin=tuple(begin), end=tuple(end)) # pylint: enable=no-member,protected-access idx += arr.shape[axis] return ret
python
def concatenate(arrays, axis=0, always_copy=True): """DEPRECATED, use ``concat`` instead Parameters ---------- arrays : list of `NDArray` Arrays to be concatenate. They must have identical shape except the first dimension. They also must have the same data type. axis : int The axis along which to concatenate. always_copy : bool Default `True`. When not `True`, if the arrays only contain one `NDArray`, that element will be returned directly, avoid copying. Returns ------- NDArray An `NDArray` that lives on the same context as `arrays[0].context`. """ assert isinstance(arrays, list) assert len(arrays) > 0 assert isinstance(arrays[0], NDArray) if not always_copy and len(arrays) == 1: return arrays[0] shape_axis = arrays[0].shape[axis] shape_rest1 = arrays[0].shape[0:axis] shape_rest2 = arrays[0].shape[axis+1:] dtype = arrays[0].dtype for arr in arrays[1:]: shape_axis += arr.shape[axis] assert shape_rest1 == arr.shape[0:axis] assert shape_rest2 == arr.shape[axis+1:] assert dtype == arr.dtype ret_shape = shape_rest1 + (shape_axis,) + shape_rest2 ret = empty(ret_shape, ctx=arrays[0].context, dtype=dtype) idx = 0 begin = [0 for _ in ret_shape] end = list(ret_shape) for arr in arrays: if axis == 0: ret[idx:idx+arr.shape[0]] = arr else: begin[axis] = idx end[axis] = idx+arr.shape[axis] # pylint: disable=no-member,protected-access _internal._crop_assign(ret, arr, out=ret, begin=tuple(begin), end=tuple(end)) # pylint: enable=no-member,protected-access idx += arr.shape[axis] return ret
[ "def", "concatenate", "(", "arrays", ",", "axis", "=", "0", ",", "always_copy", "=", "True", ")", ":", "assert", "isinstance", "(", "arrays", ",", "list", ")", "assert", "len", "(", "arrays", ")", ">", "0", "assert", "isinstance", "(", "arrays", "[", "0", "]", ",", "NDArray", ")", "if", "not", "always_copy", "and", "len", "(", "arrays", ")", "==", "1", ":", "return", "arrays", "[", "0", "]", "shape_axis", "=", "arrays", "[", "0", "]", ".", "shape", "[", "axis", "]", "shape_rest1", "=", "arrays", "[", "0", "]", ".", "shape", "[", "0", ":", "axis", "]", "shape_rest2", "=", "arrays", "[", "0", "]", ".", "shape", "[", "axis", "+", "1", ":", "]", "dtype", "=", "arrays", "[", "0", "]", ".", "dtype", "for", "arr", "in", "arrays", "[", "1", ":", "]", ":", "shape_axis", "+=", "arr", ".", "shape", "[", "axis", "]", "assert", "shape_rest1", "==", "arr", ".", "shape", "[", "0", ":", "axis", "]", "assert", "shape_rest2", "==", "arr", ".", "shape", "[", "axis", "+", "1", ":", "]", "assert", "dtype", "==", "arr", ".", "dtype", "ret_shape", "=", "shape_rest1", "+", "(", "shape_axis", ",", ")", "+", "shape_rest2", "ret", "=", "empty", "(", "ret_shape", ",", "ctx", "=", "arrays", "[", "0", "]", ".", "context", ",", "dtype", "=", "dtype", ")", "idx", "=", "0", "begin", "=", "[", "0", "for", "_", "in", "ret_shape", "]", "end", "=", "list", "(", "ret_shape", ")", "for", "arr", "in", "arrays", ":", "if", "axis", "==", "0", ":", "ret", "[", "idx", ":", "idx", "+", "arr", ".", "shape", "[", "0", "]", "]", "=", "arr", "else", ":", "begin", "[", "axis", "]", "=", "idx", "end", "[", "axis", "]", "=", "idx", "+", "arr", ".", "shape", "[", "axis", "]", "# pylint: disable=no-member,protected-access", "_internal", ".", "_crop_assign", "(", "ret", ",", "arr", ",", "out", "=", "ret", ",", "begin", "=", "tuple", "(", "begin", ")", ",", "end", "=", "tuple", "(", "end", ")", ")", "# pylint: enable=no-member,protected-access", "idx", "+=", "arr", ".", "shape", "[", "axis", "]", "return", "ret" ]
DEPRECATED, use ``concat`` instead Parameters ---------- arrays : list of `NDArray` Arrays to be concatenate. They must have identical shape except the first dimension. They also must have the same data type. axis : int The axis along which to concatenate. always_copy : bool Default `True`. When not `True`, if the arrays only contain one `NDArray`, that element will be returned directly, avoid copying. Returns ------- NDArray An `NDArray` that lives on the same context as `arrays[0].context`.
[ "DEPRECATED", "use", "concat", "instead" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3705-L3759
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
imdecode
def imdecode(str_img, clip_rect=(0, 0, 0, 0), out=None, index=0, channels=3, mean=None): """DEPRECATED, use mx.img instead Parameters ---------- str_img : str Binary image data clip_rect : iterable of 4 int Clip decoded image to rectangle (x0, y0, x1, y1). out : NDArray Output buffer. Can be 3 dimensional (c, h, w) or 4 dimensional (n, c, h, w). index : int Output decoded image to i-th slice of 4 dimensional buffer. channels : int Number of channels to output. Decode to grey scale when channels = 1. mean : NDArray Subtract mean from decode image before outputing. """ # pylint: disable= no-member, protected-access, too-many-arguments if mean is None: mean = NDArray(_new_empty_handle()) if out is None: return _internal._imdecode(mean, index, clip_rect[0], clip_rect[1], clip_rect[2], clip_rect[3], channels, len(str_img), str_img=str_img) else: return _internal._imdecode(mean, index, clip_rect[0], clip_rect[1], clip_rect[2], clip_rect[3], channels, len(str_img), str_img=str_img, out=out)
python
def imdecode(str_img, clip_rect=(0, 0, 0, 0), out=None, index=0, channels=3, mean=None): """DEPRECATED, use mx.img instead Parameters ---------- str_img : str Binary image data clip_rect : iterable of 4 int Clip decoded image to rectangle (x0, y0, x1, y1). out : NDArray Output buffer. Can be 3 dimensional (c, h, w) or 4 dimensional (n, c, h, w). index : int Output decoded image to i-th slice of 4 dimensional buffer. channels : int Number of channels to output. Decode to grey scale when channels = 1. mean : NDArray Subtract mean from decode image before outputing. """ # pylint: disable= no-member, protected-access, too-many-arguments if mean is None: mean = NDArray(_new_empty_handle()) if out is None: return _internal._imdecode(mean, index, clip_rect[0], clip_rect[1], clip_rect[2], clip_rect[3], channels, len(str_img), str_img=str_img) else: return _internal._imdecode(mean, index, clip_rect[0], clip_rect[1], clip_rect[2], clip_rect[3], channels, len(str_img), str_img=str_img, out=out)
[ "def", "imdecode", "(", "str_img", ",", "clip_rect", "=", "(", "0", ",", "0", ",", "0", ",", "0", ")", ",", "out", "=", "None", ",", "index", "=", "0", ",", "channels", "=", "3", ",", "mean", "=", "None", ")", ":", "# pylint: disable= no-member, protected-access, too-many-arguments", "if", "mean", "is", "None", ":", "mean", "=", "NDArray", "(", "_new_empty_handle", "(", ")", ")", "if", "out", "is", "None", ":", "return", "_internal", ".", "_imdecode", "(", "mean", ",", "index", ",", "clip_rect", "[", "0", "]", ",", "clip_rect", "[", "1", "]", ",", "clip_rect", "[", "2", "]", ",", "clip_rect", "[", "3", "]", ",", "channels", ",", "len", "(", "str_img", ")", ",", "str_img", "=", "str_img", ")", "else", ":", "return", "_internal", ".", "_imdecode", "(", "mean", ",", "index", ",", "clip_rect", "[", "0", "]", ",", "clip_rect", "[", "1", "]", ",", "clip_rect", "[", "2", "]", ",", "clip_rect", "[", "3", "]", ",", "channels", ",", "len", "(", "str_img", ")", ",", "str_img", "=", "str_img", ",", "out", "=", "out", ")" ]
DEPRECATED, use mx.img instead Parameters ---------- str_img : str Binary image data clip_rect : iterable of 4 int Clip decoded image to rectangle (x0, y0, x1, y1). out : NDArray Output buffer. Can be 3 dimensional (c, h, w) or 4 dimensional (n, c, h, w). index : int Output decoded image to i-th slice of 4 dimensional buffer. channels : int Number of channels to output. Decode to grey scale when channels = 1. mean : NDArray Subtract mean from decode image before outputing.
[ "DEPRECATED", "use", "mx", ".", "img", "instead" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3763-L3802
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
zeros
def zeros(shape, ctx=None, dtype=None, **kwargs): """Returns a new array filled with all zeros, with the given shape and type. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A created array Examples -------- >>> mx.nd.zeros(1).asnumpy() array([ 0.], dtype=float32) >>> mx.nd.zeros((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.zeros((1,2), mx.gpu(0), 'float16').asnumpy() array([[ 0., 0.]], dtype=float16) """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._zeros(shape=shape, ctx=ctx, dtype=dtype, **kwargs)
python
def zeros(shape, ctx=None, dtype=None, **kwargs): """Returns a new array filled with all zeros, with the given shape and type. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A created array Examples -------- >>> mx.nd.zeros(1).asnumpy() array([ 0.], dtype=float32) >>> mx.nd.zeros((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.zeros((1,2), mx.gpu(0), 'float16').asnumpy() array([[ 0., 0.]], dtype=float16) """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._zeros(shape=shape, ctx=ctx, dtype=dtype, **kwargs)
[ "def", "zeros", "(", "shape", ",", "ctx", "=", "None", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "# pylint: disable= unused-argument", "if", "ctx", "is", "None", ":", "ctx", "=", "current_context", "(", ")", "dtype", "=", "mx_real_t", "if", "dtype", "is", "None", "else", "dtype", "# pylint: disable= no-member, protected-access", "return", "_internal", ".", "_zeros", "(", "shape", "=", "shape", ",", "ctx", "=", "ctx", ",", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
Returns a new array filled with all zeros, with the given shape and type. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). out : NDArray, optional The output NDArray (default is `None`). Returns ------- NDArray A created array Examples -------- >>> mx.nd.zeros(1).asnumpy() array([ 0.], dtype=float32) >>> mx.nd.zeros((1,2), mx.gpu(0)) <NDArray 1x2 @gpu(0)> >>> mx.nd.zeros((1,2), mx.gpu(0), 'float16').asnumpy() array([[ 0., 0.]], dtype=float16)
[ "Returns", "a", "new", "array", "filled", "with", "all", "zeros", "with", "the", "given", "shape", "and", "type", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3805-L3838
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
eye
def eye(N, M=0, k=0, ctx=None, dtype=None, **kwargs): """Return a 2-D array with ones on the diagonal and zeros elsewhere. Parameters ---------- N: int Number of rows in the output. M: int, optional Number of columns in the output. If 0, defaults to N. k: int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. ctx: Context, optional An optional device context (default is the current default context) dtype: str or numpy.dtype, optional An optional value type (default is `float32`) Returns ------- NDArray A created array Examples -------- >>> mx.nd.eye(2) [[ 1. 0.] [ 0. 1.]] <NDArray 2x2 @cpu(0)> >>> mx.nd.eye(2, 3, 1) [[ 0. 1. 0.] [ 0. 0. 1.]] <NDArray 2x3 @cpu(0)> """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._eye(N=N, M=M, k=k, ctx=ctx, dtype=dtype, **kwargs)
python
def eye(N, M=0, k=0, ctx=None, dtype=None, **kwargs): """Return a 2-D array with ones on the diagonal and zeros elsewhere. Parameters ---------- N: int Number of rows in the output. M: int, optional Number of columns in the output. If 0, defaults to N. k: int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. ctx: Context, optional An optional device context (default is the current default context) dtype: str or numpy.dtype, optional An optional value type (default is `float32`) Returns ------- NDArray A created array Examples -------- >>> mx.nd.eye(2) [[ 1. 0.] [ 0. 1.]] <NDArray 2x2 @cpu(0)> >>> mx.nd.eye(2, 3, 1) [[ 0. 1. 0.] [ 0. 0. 1.]] <NDArray 2x3 @cpu(0)> """ # pylint: disable= unused-argument if ctx is None: ctx = current_context() dtype = mx_real_t if dtype is None else dtype # pylint: disable= no-member, protected-access return _internal._eye(N=N, M=M, k=k, ctx=ctx, dtype=dtype, **kwargs)
[ "def", "eye", "(", "N", ",", "M", "=", "0", ",", "k", "=", "0", ",", "ctx", "=", "None", ",", "dtype", "=", "None", ",", "*", "*", "kwargs", ")", ":", "# pylint: disable= unused-argument", "if", "ctx", "is", "None", ":", "ctx", "=", "current_context", "(", ")", "dtype", "=", "mx_real_t", "if", "dtype", "is", "None", "else", "dtype", "# pylint: disable= no-member, protected-access", "return", "_internal", ".", "_eye", "(", "N", "=", "N", ",", "M", "=", "M", ",", "k", "=", "k", ",", "ctx", "=", "ctx", ",", "dtype", "=", "dtype", ",", "*", "*", "kwargs", ")" ]
Return a 2-D array with ones on the diagonal and zeros elsewhere. Parameters ---------- N: int Number of rows in the output. M: int, optional Number of columns in the output. If 0, defaults to N. k: int, optional Index of the diagonal: 0 (the default) refers to the main diagonal, a positive value refers to an upper diagonal, and a negative value to a lower diagonal. ctx: Context, optional An optional device context (default is the current default context) dtype: str or numpy.dtype, optional An optional value type (default is `float32`) Returns ------- NDArray A created array Examples -------- >>> mx.nd.eye(2) [[ 1. 0.] [ 0. 1.]] <NDArray 2x2 @cpu(0)> >>> mx.nd.eye(2, 3, 1) [[ 0. 1. 0.] [ 0. 0. 1.]] <NDArray 2x3 @cpu(0)>
[ "Return", "a", "2", "-", "D", "array", "with", "ones", "on", "the", "diagonal", "and", "zeros", "elsewhere", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3841-L3880
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
empty
def empty(shape, ctx=None, dtype=None): """Returns a new array of given shape and type, without initializing entries. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). Returns ------- NDArray A created array. """ if isinstance(shape, int): shape = (shape, ) if ctx is None: ctx = current_context() if dtype is None: dtype = mx_real_t return NDArray(handle=_new_alloc_handle(shape, ctx, False, dtype))
python
def empty(shape, ctx=None, dtype=None): """Returns a new array of given shape and type, without initializing entries. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). Returns ------- NDArray A created array. """ if isinstance(shape, int): shape = (shape, ) if ctx is None: ctx = current_context() if dtype is None: dtype = mx_real_t return NDArray(handle=_new_alloc_handle(shape, ctx, False, dtype))
[ "def", "empty", "(", "shape", ",", "ctx", "=", "None", ",", "dtype", "=", "None", ")", ":", "if", "isinstance", "(", "shape", ",", "int", ")", ":", "shape", "=", "(", "shape", ",", ")", "if", "ctx", "is", "None", ":", "ctx", "=", "current_context", "(", ")", "if", "dtype", "is", "None", ":", "dtype", "=", "mx_real_t", "return", "NDArray", "(", "handle", "=", "_new_alloc_handle", "(", "shape", ",", "ctx", ",", "False", ",", "dtype", ")", ")" ]
Returns a new array of given shape and type, without initializing entries. Parameters ---------- shape : int or tuple of int The shape of the empty array. ctx : Context, optional An optional device context (default is the current default context). dtype : str or numpy.dtype, optional An optional value type (default is `float32`). Returns ------- NDArray A created array.
[ "Returns", "a", "new", "array", "of", "given", "shape", "and", "type", "without", "initializing", "entries", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3884-L3908
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
histogram
def histogram(a, bins=10, range=None): """Compute the histogram of the input data. Parameters ---------- a : NDArray Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), optional The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins. Returns ------- NDArray A created array. """ # pylint: disable= no-member, protected-access if isinstance(bins, NDArray): return _internal._histogram(data=a, bins=bins) elif isinstance(bins, integer_types): if range is None: warnings.warn("range is not specified, using numpy's result " "to ensure consistency with numpy") res, bin_bounds = np.histogram(a.asnumpy(), bins=bins) return array(res), array(bin_bounds) return _internal._histogram(data=a, bin_cnt=bins, range=range) raise ValueError("bins argument should be either an integer or an NDArray")
python
def histogram(a, bins=10, range=None): """Compute the histogram of the input data. Parameters ---------- a : NDArray Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), optional The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins. Returns ------- NDArray A created array. """ # pylint: disable= no-member, protected-access if isinstance(bins, NDArray): return _internal._histogram(data=a, bins=bins) elif isinstance(bins, integer_types): if range is None: warnings.warn("range is not specified, using numpy's result " "to ensure consistency with numpy") res, bin_bounds = np.histogram(a.asnumpy(), bins=bins) return array(res), array(bin_bounds) return _internal._histogram(data=a, bin_cnt=bins, range=range) raise ValueError("bins argument should be either an integer or an NDArray")
[ "def", "histogram", "(", "a", ",", "bins", "=", "10", ",", "range", "=", "None", ")", ":", "# pylint: disable= no-member, protected-access", "if", "isinstance", "(", "bins", ",", "NDArray", ")", ":", "return", "_internal", ".", "_histogram", "(", "data", "=", "a", ",", "bins", "=", "bins", ")", "elif", "isinstance", "(", "bins", ",", "integer_types", ")", ":", "if", "range", "is", "None", ":", "warnings", ".", "warn", "(", "\"range is not specified, using numpy's result \"", "\"to ensure consistency with numpy\"", ")", "res", ",", "bin_bounds", "=", "np", ".", "histogram", "(", "a", ".", "asnumpy", "(", ")", ",", "bins", "=", "bins", ")", "return", "array", "(", "res", ")", ",", "array", "(", "bin_bounds", ")", "return", "_internal", ".", "_histogram", "(", "data", "=", "a", ",", "bin_cnt", "=", "bins", ",", "range", "=", "range", ")", "raise", "ValueError", "(", "\"bins argument should be either an integer or an NDArray\"", ")" ]
Compute the histogram of the input data. Parameters ---------- a : NDArray Input data. The histogram is computed over the flattened array. bins : int or sequence of scalars If bins is an int, it defines the number of equal-width bins in the given range (10, by default). If bins is a sequence, it defines the bin edges, including the rightmost edge, allowing for non-uniform bin widths. range : (float, float), optional The lower and upper range of the bins. If not provided, range is simply (a.min(), a.max()). Values outside the range are ignored. The first element of the range must be less than or equal to the second. range affects the automatic bin computation as well, the range will be equally divided by the number of bins. Returns ------- NDArray A created array.
[ "Compute", "the", "histogram", "of", "the", "input", "data", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3912-L3946
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
split_v2
def split_v2(ary, indices_or_sections, axis=0, squeeze_axis=False): """Split an array into multiple sub-arrays. Parameters ---------- ary : NDArray Array to be divided into sub-arrays. indices_or_sections : int or tuple of ints If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible, an error is raised. If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example, ``[2, 3]`` would, for ``axis=0``, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along `axis`, an empty sub-array is returned correspondingly. axis : int, optional The axis along which to split, default is 0. squeeze_axis: boolean, optional Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the `axis`. Default is False. Returns ------- NDArray A created array. """ indices = [] axis_size = ary.shape[axis] if isinstance(indices_or_sections, int): sections = indices_or_sections if axis_size % sections: raise ValueError('array split does not result in an equal division') section_size = int(axis_size / sections) indices = [i * section_size for i in range(sections)] elif isinstance(indices_or_sections, tuple): indices = [0] + list(indices_or_sections) else: raise ValueError('indices_or_sections must either int or tuple of ints') return _internal._split_v2(ary, indices, axis, squeeze_axis)
python
def split_v2(ary, indices_or_sections, axis=0, squeeze_axis=False): """Split an array into multiple sub-arrays. Parameters ---------- ary : NDArray Array to be divided into sub-arrays. indices_or_sections : int or tuple of ints If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible, an error is raised. If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example, ``[2, 3]`` would, for ``axis=0``, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along `axis`, an empty sub-array is returned correspondingly. axis : int, optional The axis along which to split, default is 0. squeeze_axis: boolean, optional Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the `axis`. Default is False. Returns ------- NDArray A created array. """ indices = [] axis_size = ary.shape[axis] if isinstance(indices_or_sections, int): sections = indices_or_sections if axis_size % sections: raise ValueError('array split does not result in an equal division') section_size = int(axis_size / sections) indices = [i * section_size for i in range(sections)] elif isinstance(indices_or_sections, tuple): indices = [0] + list(indices_or_sections) else: raise ValueError('indices_or_sections must either int or tuple of ints') return _internal._split_v2(ary, indices, axis, squeeze_axis)
[ "def", "split_v2", "(", "ary", ",", "indices_or_sections", ",", "axis", "=", "0", ",", "squeeze_axis", "=", "False", ")", ":", "indices", "=", "[", "]", "axis_size", "=", "ary", ".", "shape", "[", "axis", "]", "if", "isinstance", "(", "indices_or_sections", ",", "int", ")", ":", "sections", "=", "indices_or_sections", "if", "axis_size", "%", "sections", ":", "raise", "ValueError", "(", "'array split does not result in an equal division'", ")", "section_size", "=", "int", "(", "axis_size", "/", "sections", ")", "indices", "=", "[", "i", "*", "section_size", "for", "i", "in", "range", "(", "sections", ")", "]", "elif", "isinstance", "(", "indices_or_sections", ",", "tuple", ")", ":", "indices", "=", "[", "0", "]", "+", "list", "(", "indices_or_sections", ")", "else", ":", "raise", "ValueError", "(", "'indices_or_sections must either int or tuple of ints'", ")", "return", "_internal", ".", "_split_v2", "(", "ary", ",", "indices", ",", "axis", ",", "squeeze_axis", ")" ]
Split an array into multiple sub-arrays. Parameters ---------- ary : NDArray Array to be divided into sub-arrays. indices_or_sections : int or tuple of ints If `indices_or_sections` is an integer, N, the array will be divided into N equal arrays along `axis`. If such a split is not possible, an error is raised. If `indices_or_sections` is a 1-D array of sorted integers, the entries indicate where along `axis` the array is split. For example, ``[2, 3]`` would, for ``axis=0``, result in - ary[:2] - ary[2:3] - ary[3:] If an index exceeds the dimension of the array along `axis`, an empty sub-array is returned correspondingly. axis : int, optional The axis along which to split, default is 0. squeeze_axis: boolean, optional Whether to squeeze the axis of sub-arrays or not, only useful when size of the sub-arrays are 1 on the `axis`. Default is False. Returns ------- NDArray A created array.
[ "Split", "an", "array", "into", "multiple", "sub", "-", "arrays", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L3949-L3992
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
to_dlpack_for_read
def to_dlpack_for_read(data): """Returns a reference view of NDArray that represents as DLManagedTensor until all previous write operations on the current array are finished. Parameters ---------- data: NDArray input data. Returns ------- PyCapsule (the pointer of DLManagedTensor) a reference view of NDArray that represents as DLManagedTensor. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.nd.from_dlpack(y) >>> z [[1. 1. 1.] [1. 1. 1.]] <NDArray 2x3 @cpu(0)> """ data.wait_to_read() dlpack = DLPackHandle() check_call(_LIB.MXNDArrayToDLPack(data.handle, ctypes.byref(dlpack))) return ctypes.pythonapi.PyCapsule_New(dlpack, _c_str_dltensor, _c_dlpack_deleter)
python
def to_dlpack_for_read(data): """Returns a reference view of NDArray that represents as DLManagedTensor until all previous write operations on the current array are finished. Parameters ---------- data: NDArray input data. Returns ------- PyCapsule (the pointer of DLManagedTensor) a reference view of NDArray that represents as DLManagedTensor. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.nd.from_dlpack(y) >>> z [[1. 1. 1.] [1. 1. 1.]] <NDArray 2x3 @cpu(0)> """ data.wait_to_read() dlpack = DLPackHandle() check_call(_LIB.MXNDArrayToDLPack(data.handle, ctypes.byref(dlpack))) return ctypes.pythonapi.PyCapsule_New(dlpack, _c_str_dltensor, _c_dlpack_deleter)
[ "def", "to_dlpack_for_read", "(", "data", ")", ":", "data", ".", "wait_to_read", "(", ")", "dlpack", "=", "DLPackHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayToDLPack", "(", "data", ".", "handle", ",", "ctypes", ".", "byref", "(", "dlpack", ")", ")", ")", "return", "ctypes", ".", "pythonapi", ".", "PyCapsule_New", "(", "dlpack", ",", "_c_str_dltensor", ",", "_c_dlpack_deleter", ")" ]
Returns a reference view of NDArray that represents as DLManagedTensor until all previous write operations on the current array are finished. Parameters ---------- data: NDArray input data. Returns ------- PyCapsule (the pointer of DLManagedTensor) a reference view of NDArray that represents as DLManagedTensor. Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.nd.from_dlpack(y) >>> z [[1. 1. 1.] [1. 1. 1.]] <NDArray 2x3 @cpu(0)>
[ "Returns", "a", "reference", "view", "of", "NDArray", "that", "represents", "as", "DLManagedTensor", "until", "all", "previous", "write", "operations", "on", "the", "current", "array", "are", "finished", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L4007-L4036
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
to_dlpack_for_write
def to_dlpack_for_write(data): """Returns a reference view of NDArray that represents as DLManagedTensor until all previous read/write operations on the current array are finished. Parameters ---------- data: NDArray input data. Returns ------- PyCapsule (the pointer of DLManagedTensor) a reference view of NDArray that represents as DLManagedTensor. Examples -------- >>> x = mx.nd.ones((2,3)) >>> w = mx.nd.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.nd.from_dlpack(w) >>> u += 1 >>> x [[2. 2. 2.] [2. 2. 2.]] <NDArray 2x3 @cpu(0)> """ check_call(_LIB.MXNDArrayWaitToWrite(data.handle)) dlpack = DLPackHandle() check_call(_LIB.MXNDArrayToDLPack(data.handle, ctypes.byref(dlpack))) return ctypes.pythonapi.PyCapsule_New(dlpack, _c_str_dltensor, _c_dlpack_deleter)
python
def to_dlpack_for_write(data): """Returns a reference view of NDArray that represents as DLManagedTensor until all previous read/write operations on the current array are finished. Parameters ---------- data: NDArray input data. Returns ------- PyCapsule (the pointer of DLManagedTensor) a reference view of NDArray that represents as DLManagedTensor. Examples -------- >>> x = mx.nd.ones((2,3)) >>> w = mx.nd.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.nd.from_dlpack(w) >>> u += 1 >>> x [[2. 2. 2.] [2. 2. 2.]] <NDArray 2x3 @cpu(0)> """ check_call(_LIB.MXNDArrayWaitToWrite(data.handle)) dlpack = DLPackHandle() check_call(_LIB.MXNDArrayToDLPack(data.handle, ctypes.byref(dlpack))) return ctypes.pythonapi.PyCapsule_New(dlpack, _c_str_dltensor, _c_dlpack_deleter)
[ "def", "to_dlpack_for_write", "(", "data", ")", ":", "check_call", "(", "_LIB", ".", "MXNDArrayWaitToWrite", "(", "data", ".", "handle", ")", ")", "dlpack", "=", "DLPackHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayToDLPack", "(", "data", ".", "handle", ",", "ctypes", ".", "byref", "(", "dlpack", ")", ")", ")", "return", "ctypes", ".", "pythonapi", ".", "PyCapsule_New", "(", "dlpack", ",", "_c_str_dltensor", ",", "_c_dlpack_deleter", ")" ]
Returns a reference view of NDArray that represents as DLManagedTensor until all previous read/write operations on the current array are finished. Parameters ---------- data: NDArray input data. Returns ------- PyCapsule (the pointer of DLManagedTensor) a reference view of NDArray that represents as DLManagedTensor. Examples -------- >>> x = mx.nd.ones((2,3)) >>> w = mx.nd.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.nd.from_dlpack(w) >>> u += 1 >>> x [[2. 2. 2.] [2. 2. 2.]] <NDArray 2x3 @cpu(0)>
[ "Returns", "a", "reference", "view", "of", "NDArray", "that", "represents", "as", "DLManagedTensor", "until", "all", "previous", "read", "/", "write", "operations", "on", "the", "current", "array", "are", "finished", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L4038-L4068
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
from_dlpack
def from_dlpack(dlpack): """Returns a NDArray backed by a dlpack tensor. Parameters ---------- dlpack: PyCapsule (the pointer of DLManagedTensor) input data Returns ------- NDArray a NDArray backed by a dlpack tensor Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.nd.from_dlpack(y) >>> type(z) <class 'mxnet.ndarray.ndarray.NDArray'> >>> z [[ 1. 1. 1.] [ 1. 1. 1.]] <NDArray 2x3 @cpu(0)> >>> w = mx.nd.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.nd.from_dlpack(w) >>> u += 1 >>> x [[2. 2. 2.] [2. 2. 2.]] <NDArray 2x3 @cpu(0)> """ handle = NDArrayHandle() dlpack = ctypes.py_object(dlpack) assert ctypes.pythonapi.PyCapsule_IsValid(dlpack, _c_str_dltensor), ValueError( 'Invalid DLPack Tensor. DLTensor capsules can be consumed only once.') dlpack_handle = ctypes.c_void_p(ctypes.pythonapi.PyCapsule_GetPointer(dlpack, _c_str_dltensor)) check_call(_LIB.MXNDArrayFromDLPack(dlpack_handle, ctypes.byref(handle))) # Rename PyCapsule (DLPack) ctypes.pythonapi.PyCapsule_SetName(dlpack, _c_str_used_dltensor) # delete the deleter of the old dlpack ctypes.pythonapi.PyCapsule_SetDestructor(dlpack, None) return NDArray(handle=handle)
python
def from_dlpack(dlpack): """Returns a NDArray backed by a dlpack tensor. Parameters ---------- dlpack: PyCapsule (the pointer of DLManagedTensor) input data Returns ------- NDArray a NDArray backed by a dlpack tensor Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.nd.from_dlpack(y) >>> type(z) <class 'mxnet.ndarray.ndarray.NDArray'> >>> z [[ 1. 1. 1.] [ 1. 1. 1.]] <NDArray 2x3 @cpu(0)> >>> w = mx.nd.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.nd.from_dlpack(w) >>> u += 1 >>> x [[2. 2. 2.] [2. 2. 2.]] <NDArray 2x3 @cpu(0)> """ handle = NDArrayHandle() dlpack = ctypes.py_object(dlpack) assert ctypes.pythonapi.PyCapsule_IsValid(dlpack, _c_str_dltensor), ValueError( 'Invalid DLPack Tensor. DLTensor capsules can be consumed only once.') dlpack_handle = ctypes.c_void_p(ctypes.pythonapi.PyCapsule_GetPointer(dlpack, _c_str_dltensor)) check_call(_LIB.MXNDArrayFromDLPack(dlpack_handle, ctypes.byref(handle))) # Rename PyCapsule (DLPack) ctypes.pythonapi.PyCapsule_SetName(dlpack, _c_str_used_dltensor) # delete the deleter of the old dlpack ctypes.pythonapi.PyCapsule_SetDestructor(dlpack, None) return NDArray(handle=handle)
[ "def", "from_dlpack", "(", "dlpack", ")", ":", "handle", "=", "NDArrayHandle", "(", ")", "dlpack", "=", "ctypes", ".", "py_object", "(", "dlpack", ")", "assert", "ctypes", ".", "pythonapi", ".", "PyCapsule_IsValid", "(", "dlpack", ",", "_c_str_dltensor", ")", ",", "ValueError", "(", "'Invalid DLPack Tensor. DLTensor capsules can be consumed only once.'", ")", "dlpack_handle", "=", "ctypes", ".", "c_void_p", "(", "ctypes", ".", "pythonapi", ".", "PyCapsule_GetPointer", "(", "dlpack", ",", "_c_str_dltensor", ")", ")", "check_call", "(", "_LIB", ".", "MXNDArrayFromDLPack", "(", "dlpack_handle", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "# Rename PyCapsule (DLPack)", "ctypes", ".", "pythonapi", ".", "PyCapsule_SetName", "(", "dlpack", ",", "_c_str_used_dltensor", ")", "# delete the deleter of the old dlpack", "ctypes", ".", "pythonapi", ".", "PyCapsule_SetDestructor", "(", "dlpack", ",", "None", ")", "return", "NDArray", "(", "handle", "=", "handle", ")" ]
Returns a NDArray backed by a dlpack tensor. Parameters ---------- dlpack: PyCapsule (the pointer of DLManagedTensor) input data Returns ------- NDArray a NDArray backed by a dlpack tensor Examples -------- >>> x = mx.nd.ones((2,3)) >>> y = mx.nd.to_dlpack_for_read(x) >>> type(y) <class 'PyCapsule'> >>> z = mx.nd.from_dlpack(y) >>> type(z) <class 'mxnet.ndarray.ndarray.NDArray'> >>> z [[ 1. 1. 1.] [ 1. 1. 1.]] <NDArray 2x3 @cpu(0)> >>> w = mx.nd.to_dlpack_for_write(x) >>> type(w) <class 'PyCapsule'> >>> u = mx.nd.from_dlpack(w) >>> u += 1 >>> x [[2. 2. 2.] [2. 2. 2.]] <NDArray 2x3 @cpu(0)>
[ "Returns", "a", "NDArray", "backed", "by", "a", "dlpack", "tensor", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L4070-L4117
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
from_numpy
def from_numpy(ndarray, zero_copy=True): """Returns an MXNet's NDArray backed by Numpy's ndarray. Parameters ---------- ndarray: numpy.ndarray input data zero_copy: bool Whether we use DLPack's zero-copy conversion to convert to MXNet's NDArray. This is only available for c-contiguous arrays, i.e. array.flags[C_CONTIGUOUS] == True. Returns ------- NDArray a NDArray backed by a dlpack tensor """ def _make_manager_ctx(obj): pyobj = ctypes.py_object(obj) void_p = ctypes.c_void_p.from_buffer(pyobj) ctypes.pythonapi.Py_IncRef(pyobj) return void_p def _make_dl_tensor(array): if str(array.dtype) not in DLDataType.TYPE_MAP: raise ValueError(str(array.dtype) + " is not supported.") dl_tensor = DLTensor() dl_tensor.data = array.ctypes.data_as(ctypes.c_void_p) dl_tensor.ctx = DLContext(1, 0) dl_tensor.ndim = array.ndim dl_tensor.dtype = DLDataType.TYPE_MAP[str(array.dtype)] dl_tensor.shape = array.ctypes.shape_as(ctypes.c_int64) dl_tensor.strides = None dl_tensor.byte_offset = 0 return dl_tensor def _make_dl_managed_tensor(array): c_obj = DLManagedTensor() c_obj.dl_tensor = _make_dl_tensor(array) c_obj.manager_ctx = _make_manager_ctx(array) c_obj.deleter = dl_managed_tensor_deleter return c_obj if not zero_copy: return array(ndarray, dtype=ndarray.dtype) if not ndarray.flags['C_CONTIGUOUS']: raise ValueError("Only c-contiguous arrays are supported for zero-copy") c_obj = _make_dl_managed_tensor(ndarray) address = ctypes.addressof(c_obj) address = ctypes.cast(address, ctypes.c_void_p) handle = NDArrayHandle() check_call(_LIB.MXNDArrayFromDLPack(address, ctypes.byref(handle))) return NDArray(handle=handle)
python
def from_numpy(ndarray, zero_copy=True): """Returns an MXNet's NDArray backed by Numpy's ndarray. Parameters ---------- ndarray: numpy.ndarray input data zero_copy: bool Whether we use DLPack's zero-copy conversion to convert to MXNet's NDArray. This is only available for c-contiguous arrays, i.e. array.flags[C_CONTIGUOUS] == True. Returns ------- NDArray a NDArray backed by a dlpack tensor """ def _make_manager_ctx(obj): pyobj = ctypes.py_object(obj) void_p = ctypes.c_void_p.from_buffer(pyobj) ctypes.pythonapi.Py_IncRef(pyobj) return void_p def _make_dl_tensor(array): if str(array.dtype) not in DLDataType.TYPE_MAP: raise ValueError(str(array.dtype) + " is not supported.") dl_tensor = DLTensor() dl_tensor.data = array.ctypes.data_as(ctypes.c_void_p) dl_tensor.ctx = DLContext(1, 0) dl_tensor.ndim = array.ndim dl_tensor.dtype = DLDataType.TYPE_MAP[str(array.dtype)] dl_tensor.shape = array.ctypes.shape_as(ctypes.c_int64) dl_tensor.strides = None dl_tensor.byte_offset = 0 return dl_tensor def _make_dl_managed_tensor(array): c_obj = DLManagedTensor() c_obj.dl_tensor = _make_dl_tensor(array) c_obj.manager_ctx = _make_manager_ctx(array) c_obj.deleter = dl_managed_tensor_deleter return c_obj if not zero_copy: return array(ndarray, dtype=ndarray.dtype) if not ndarray.flags['C_CONTIGUOUS']: raise ValueError("Only c-contiguous arrays are supported for zero-copy") c_obj = _make_dl_managed_tensor(ndarray) address = ctypes.addressof(c_obj) address = ctypes.cast(address, ctypes.c_void_p) handle = NDArrayHandle() check_call(_LIB.MXNDArrayFromDLPack(address, ctypes.byref(handle))) return NDArray(handle=handle)
[ "def", "from_numpy", "(", "ndarray", ",", "zero_copy", "=", "True", ")", ":", "def", "_make_manager_ctx", "(", "obj", ")", ":", "pyobj", "=", "ctypes", ".", "py_object", "(", "obj", ")", "void_p", "=", "ctypes", ".", "c_void_p", ".", "from_buffer", "(", "pyobj", ")", "ctypes", ".", "pythonapi", ".", "Py_IncRef", "(", "pyobj", ")", "return", "void_p", "def", "_make_dl_tensor", "(", "array", ")", ":", "if", "str", "(", "array", ".", "dtype", ")", "not", "in", "DLDataType", ".", "TYPE_MAP", ":", "raise", "ValueError", "(", "str", "(", "array", ".", "dtype", ")", "+", "\" is not supported.\"", ")", "dl_tensor", "=", "DLTensor", "(", ")", "dl_tensor", ".", "data", "=", "array", ".", "ctypes", ".", "data_as", "(", "ctypes", ".", "c_void_p", ")", "dl_tensor", ".", "ctx", "=", "DLContext", "(", "1", ",", "0", ")", "dl_tensor", ".", "ndim", "=", "array", ".", "ndim", "dl_tensor", ".", "dtype", "=", "DLDataType", ".", "TYPE_MAP", "[", "str", "(", "array", ".", "dtype", ")", "]", "dl_tensor", ".", "shape", "=", "array", ".", "ctypes", ".", "shape_as", "(", "ctypes", ".", "c_int64", ")", "dl_tensor", ".", "strides", "=", "None", "dl_tensor", ".", "byte_offset", "=", "0", "return", "dl_tensor", "def", "_make_dl_managed_tensor", "(", "array", ")", ":", "c_obj", "=", "DLManagedTensor", "(", ")", "c_obj", ".", "dl_tensor", "=", "_make_dl_tensor", "(", "array", ")", "c_obj", ".", "manager_ctx", "=", "_make_manager_ctx", "(", "array", ")", "c_obj", ".", "deleter", "=", "dl_managed_tensor_deleter", "return", "c_obj", "if", "not", "zero_copy", ":", "return", "array", "(", "ndarray", ",", "dtype", "=", "ndarray", ".", "dtype", ")", "if", "not", "ndarray", ".", "flags", "[", "'C_CONTIGUOUS'", "]", ":", "raise", "ValueError", "(", "\"Only c-contiguous arrays are supported for zero-copy\"", ")", "c_obj", "=", "_make_dl_managed_tensor", "(", "ndarray", ")", "address", "=", "ctypes", ".", "addressof", "(", "c_obj", ")", "address", "=", "ctypes", ".", "cast", "(", "address", ",", "ctypes", ".", "c_void_p", ")", "handle", "=", "NDArrayHandle", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayFromDLPack", "(", "address", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "NDArray", "(", "handle", "=", "handle", ")" ]
Returns an MXNet's NDArray backed by Numpy's ndarray. Parameters ---------- ndarray: numpy.ndarray input data zero_copy: bool Whether we use DLPack's zero-copy conversion to convert to MXNet's NDArray. This is only available for c-contiguous arrays, i.e. array.flags[C_CONTIGUOUS] == True. Returns ------- NDArray a NDArray backed by a dlpack tensor
[ "Returns", "an", "MXNet", "s", "NDArray", "backed", "by", "Numpy", "s", "ndarray", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L4167-L4222
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._get_index_nd
def _get_index_nd(self, key): """Returns an index array for use in scatter_nd and gather_nd.""" def _is_advanced_index(index): """The definition of advanced index here includes integers as well, while integers are considered as basic index type when the key contains only slices and integers.""" return not isinstance(index, py_slice) if isinstance(key, (NDArray, np.ndarray, list, integer_types, py_slice)): key = (key,) assert isinstance(key, tuple),\ 'index=%s must be a NDArray, or np.ndarray, or list, or tuple ' \ ' type to use advanced indexing, received type=%s' % (str(key), str(type(key))) assert len(key) > 0, "Cannot slice with empty indices" shape = self.shape assert len(shape) >= len(key),\ "Slicing dimensions exceeds array dimensions, %d vs %d" % (len(key), len(shape)) indices = [] dtype = 'int32' # index data type passed to gather_nd op need_broadcast = (len(key) != 1) advanced_indices = [] # include list, NDArray, np.ndarray, integer basic_indices = [] # include only slices advanced_index_bshape = None # final advanced index shape for i, idx_i in enumerate(key): is_advanced_index = True if isinstance(idx_i, (np.ndarray, list, tuple)): idx_i = array(idx_i, ctx=self.context, dtype=dtype) advanced_indices.append(i) elif isinstance(idx_i, py_slice): start, stop, step = _get_index_range(idx_i.start, idx_i.stop, shape[i], idx_i.step) idx_i = arange(start, stop, step, ctx=self.context, dtype=dtype) basic_indices.append(i) is_advanced_index = False elif isinstance(idx_i, integer_types): start, stop, step = _get_index_range(idx_i, idx_i+1, shape[i], 1) idx_i = arange(start, stop, step, ctx=self.context, dtype=dtype) advanced_indices.append(i) elif isinstance(idx_i, NDArray): if dtype != idx_i.dtype: idx_i = idx_i.astype(dtype) advanced_indices.append(i) else: raise IndexError('Indexing NDArray with index=%s of type=%s is not supported' % (str(key), str(type(key)))) if is_advanced_index: if advanced_index_bshape is None: advanced_index_bshape = idx_i.shape elif advanced_index_bshape != idx_i.shape: need_broadcast = True advanced_index_bshape = _get_broadcast_shape(advanced_index_bshape, idx_i.shape) indices.append(idx_i) # Get final index shape for gather_nd. See the following reference # for determining the output array shape. # https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#combining-advanced-and-basic-indexing # pylint: disable=line-too-long if len(advanced_indices) == 0: raise ValueError('Advanced index tuple must contain at least one of the following types:' ' list, tuple, NDArray, np.ndarray, integer, received index=%s' % key) # determine the output array's shape by checking whether advanced_indices are all adjacent # or separated by slices advanced_indices_adjacent = True for i in range(0, len(advanced_indices)-1): if advanced_indices[i] + 1 != advanced_indices[i+1]: advanced_indices_adjacent = False break index_bshape_list = [] # index broadcasted shape if advanced_indices_adjacent: for i in range(0, advanced_indices[0]): index_bshape_list.extend(indices[i].shape) if not need_broadcast and indices[i].shape != advanced_index_bshape: need_broadcast = True index_bshape_list.extend(advanced_index_bshape) for i in range(advanced_indices[-1]+1, len(indices)): if not need_broadcast and indices[i].shape != advanced_index_bshape: need_broadcast = True index_bshape_list.extend(indices[i].shape) else: index_bshape_list.extend(advanced_index_bshape) for i in basic_indices: index_bshape_list.extend(indices[i].shape) if not need_broadcast and indices[i].shape != advanced_index_bshape: need_broadcast = True index_bshape = tuple(index_bshape_list) # Need to broadcast all ndarrays in indices to the final shape. # For example, suppose an array has shape=(5, 6, 7, 8) and # key=(slice(1, 5), [[1, 2]], slice(2, 5), [1]). # Since key[1] and key[3] are two advanced indices here and they are # separated by basic indices key[0] and key[2], the output shape # is (1, 2, 4, 3), where the first two elements come from the shape # that key[1] and key[3] should broadcast to, which is (1, 2), and # the last two elements come from the shape of two basic indices. # In order to broadcast all basic and advanced indices to the output shape, # we need to reshape them based on their axis. For example, to broadcast key[0], # with shape=(4,), we first need to reshape it into (1, 1, 4, 1), and then # broadcast the reshaped array to (1, 2, 4, 3); to broadcast key[1], we first # reshape it into (1, 2, 1, 1), then broadcast the reshaped array to (1, 2, 4, 3). if need_broadcast: broadcasted_indices = [] idx_rshape = [1] * len(index_bshape) if advanced_indices_adjacent: advanced_index_bshape_start = advanced_indices[0] # start index of advanced_index_bshape in index_shape advanced_index_bshape_stop = advanced_index_bshape_start + len(advanced_index_bshape) for i, idx in enumerate(key): if _is_advanced_index(idx): k = advanced_index_bshape_stop # find the reshaped shape for indices[i] for dim_size in indices[i].shape[::-1]: k -= 1 idx_rshape[k] = dim_size else: if i < advanced_indices[0]: # slice is on the left side of advanced indices idx_rshape[i] = indices[i].shape[0] elif i > advanced_indices[-1]: # slice is on the right side of advanced indices idx_rshape[i-len(key)] = indices[i].shape[0] else: raise ValueError('basic index i=%d cannot be between advanced index i=%d and i=%d' % (i, advanced_indices[0], advanced_indices[-1])) # broadcast current index to the final shape broadcasted_indices.append(indices[i].reshape(tuple(idx_rshape)).broadcast_to(index_bshape)) # reset idx_rshape to ones for j, _ in enumerate(idx_rshape): idx_rshape[j] = 1 else: basic_index_offset = len(advanced_index_bshape) for i, idx in enumerate(key): if _is_advanced_index(idx): k = len(advanced_index_bshape) for dim_size in indices[i].shape[::-1]: k -= 1 idx_rshape[k] = dim_size else: idx_rshape[basic_index_offset] = indices[i].shape[0] basic_index_offset += 1 # broadcast current index to the final shape broadcasted_indices.append(indices[i].reshape(tuple(idx_rshape)).broadcast_to(index_bshape)) # reset idx_rshape to ones for j, _ in enumerate(idx_rshape): idx_rshape[j] = 1 indices = broadcasted_indices return op.stack(*indices)
python
def _get_index_nd(self, key): """Returns an index array for use in scatter_nd and gather_nd.""" def _is_advanced_index(index): """The definition of advanced index here includes integers as well, while integers are considered as basic index type when the key contains only slices and integers.""" return not isinstance(index, py_slice) if isinstance(key, (NDArray, np.ndarray, list, integer_types, py_slice)): key = (key,) assert isinstance(key, tuple),\ 'index=%s must be a NDArray, or np.ndarray, or list, or tuple ' \ ' type to use advanced indexing, received type=%s' % (str(key), str(type(key))) assert len(key) > 0, "Cannot slice with empty indices" shape = self.shape assert len(shape) >= len(key),\ "Slicing dimensions exceeds array dimensions, %d vs %d" % (len(key), len(shape)) indices = [] dtype = 'int32' # index data type passed to gather_nd op need_broadcast = (len(key) != 1) advanced_indices = [] # include list, NDArray, np.ndarray, integer basic_indices = [] # include only slices advanced_index_bshape = None # final advanced index shape for i, idx_i in enumerate(key): is_advanced_index = True if isinstance(idx_i, (np.ndarray, list, tuple)): idx_i = array(idx_i, ctx=self.context, dtype=dtype) advanced_indices.append(i) elif isinstance(idx_i, py_slice): start, stop, step = _get_index_range(idx_i.start, idx_i.stop, shape[i], idx_i.step) idx_i = arange(start, stop, step, ctx=self.context, dtype=dtype) basic_indices.append(i) is_advanced_index = False elif isinstance(idx_i, integer_types): start, stop, step = _get_index_range(idx_i, idx_i+1, shape[i], 1) idx_i = arange(start, stop, step, ctx=self.context, dtype=dtype) advanced_indices.append(i) elif isinstance(idx_i, NDArray): if dtype != idx_i.dtype: idx_i = idx_i.astype(dtype) advanced_indices.append(i) else: raise IndexError('Indexing NDArray with index=%s of type=%s is not supported' % (str(key), str(type(key)))) if is_advanced_index: if advanced_index_bshape is None: advanced_index_bshape = idx_i.shape elif advanced_index_bshape != idx_i.shape: need_broadcast = True advanced_index_bshape = _get_broadcast_shape(advanced_index_bshape, idx_i.shape) indices.append(idx_i) # Get final index shape for gather_nd. See the following reference # for determining the output array shape. # https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#combining-advanced-and-basic-indexing # pylint: disable=line-too-long if len(advanced_indices) == 0: raise ValueError('Advanced index tuple must contain at least one of the following types:' ' list, tuple, NDArray, np.ndarray, integer, received index=%s' % key) # determine the output array's shape by checking whether advanced_indices are all adjacent # or separated by slices advanced_indices_adjacent = True for i in range(0, len(advanced_indices)-1): if advanced_indices[i] + 1 != advanced_indices[i+1]: advanced_indices_adjacent = False break index_bshape_list = [] # index broadcasted shape if advanced_indices_adjacent: for i in range(0, advanced_indices[0]): index_bshape_list.extend(indices[i].shape) if not need_broadcast and indices[i].shape != advanced_index_bshape: need_broadcast = True index_bshape_list.extend(advanced_index_bshape) for i in range(advanced_indices[-1]+1, len(indices)): if not need_broadcast and indices[i].shape != advanced_index_bshape: need_broadcast = True index_bshape_list.extend(indices[i].shape) else: index_bshape_list.extend(advanced_index_bshape) for i in basic_indices: index_bshape_list.extend(indices[i].shape) if not need_broadcast and indices[i].shape != advanced_index_bshape: need_broadcast = True index_bshape = tuple(index_bshape_list) # Need to broadcast all ndarrays in indices to the final shape. # For example, suppose an array has shape=(5, 6, 7, 8) and # key=(slice(1, 5), [[1, 2]], slice(2, 5), [1]). # Since key[1] and key[3] are two advanced indices here and they are # separated by basic indices key[0] and key[2], the output shape # is (1, 2, 4, 3), where the first two elements come from the shape # that key[1] and key[3] should broadcast to, which is (1, 2), and # the last two elements come from the shape of two basic indices. # In order to broadcast all basic and advanced indices to the output shape, # we need to reshape them based on their axis. For example, to broadcast key[0], # with shape=(4,), we first need to reshape it into (1, 1, 4, 1), and then # broadcast the reshaped array to (1, 2, 4, 3); to broadcast key[1], we first # reshape it into (1, 2, 1, 1), then broadcast the reshaped array to (1, 2, 4, 3). if need_broadcast: broadcasted_indices = [] idx_rshape = [1] * len(index_bshape) if advanced_indices_adjacent: advanced_index_bshape_start = advanced_indices[0] # start index of advanced_index_bshape in index_shape advanced_index_bshape_stop = advanced_index_bshape_start + len(advanced_index_bshape) for i, idx in enumerate(key): if _is_advanced_index(idx): k = advanced_index_bshape_stop # find the reshaped shape for indices[i] for dim_size in indices[i].shape[::-1]: k -= 1 idx_rshape[k] = dim_size else: if i < advanced_indices[0]: # slice is on the left side of advanced indices idx_rshape[i] = indices[i].shape[0] elif i > advanced_indices[-1]: # slice is on the right side of advanced indices idx_rshape[i-len(key)] = indices[i].shape[0] else: raise ValueError('basic index i=%d cannot be between advanced index i=%d and i=%d' % (i, advanced_indices[0], advanced_indices[-1])) # broadcast current index to the final shape broadcasted_indices.append(indices[i].reshape(tuple(idx_rshape)).broadcast_to(index_bshape)) # reset idx_rshape to ones for j, _ in enumerate(idx_rshape): idx_rshape[j] = 1 else: basic_index_offset = len(advanced_index_bshape) for i, idx in enumerate(key): if _is_advanced_index(idx): k = len(advanced_index_bshape) for dim_size in indices[i].shape[::-1]: k -= 1 idx_rshape[k] = dim_size else: idx_rshape[basic_index_offset] = indices[i].shape[0] basic_index_offset += 1 # broadcast current index to the final shape broadcasted_indices.append(indices[i].reshape(tuple(idx_rshape)).broadcast_to(index_bshape)) # reset idx_rshape to ones for j, _ in enumerate(idx_rshape): idx_rshape[j] = 1 indices = broadcasted_indices return op.stack(*indices)
[ "def", "_get_index_nd", "(", "self", ",", "key", ")", ":", "def", "_is_advanced_index", "(", "index", ")", ":", "\"\"\"The definition of advanced index here includes integers as well, while\n integers are considered as basic index type when the key contains only\n slices and integers.\"\"\"", "return", "not", "isinstance", "(", "index", ",", "py_slice", ")", "if", "isinstance", "(", "key", ",", "(", "NDArray", ",", "np", ".", "ndarray", ",", "list", ",", "integer_types", ",", "py_slice", ")", ")", ":", "key", "=", "(", "key", ",", ")", "assert", "isinstance", "(", "key", ",", "tuple", ")", ",", "'index=%s must be a NDArray, or np.ndarray, or list, or tuple '", "' type to use advanced indexing, received type=%s'", "%", "(", "str", "(", "key", ")", ",", "str", "(", "type", "(", "key", ")", ")", ")", "assert", "len", "(", "key", ")", ">", "0", ",", "\"Cannot slice with empty indices\"", "shape", "=", "self", ".", "shape", "assert", "len", "(", "shape", ")", ">=", "len", "(", "key", ")", ",", "\"Slicing dimensions exceeds array dimensions, %d vs %d\"", "%", "(", "len", "(", "key", ")", ",", "len", "(", "shape", ")", ")", "indices", "=", "[", "]", "dtype", "=", "'int32'", "# index data type passed to gather_nd op", "need_broadcast", "=", "(", "len", "(", "key", ")", "!=", "1", ")", "advanced_indices", "=", "[", "]", "# include list, NDArray, np.ndarray, integer", "basic_indices", "=", "[", "]", "# include only slices", "advanced_index_bshape", "=", "None", "# final advanced index shape", "for", "i", ",", "idx_i", "in", "enumerate", "(", "key", ")", ":", "is_advanced_index", "=", "True", "if", "isinstance", "(", "idx_i", ",", "(", "np", ".", "ndarray", ",", "list", ",", "tuple", ")", ")", ":", "idx_i", "=", "array", "(", "idx_i", ",", "ctx", "=", "self", ".", "context", ",", "dtype", "=", "dtype", ")", "advanced_indices", ".", "append", "(", "i", ")", "elif", "isinstance", "(", "idx_i", ",", "py_slice", ")", ":", "start", ",", "stop", ",", "step", "=", "_get_index_range", "(", "idx_i", ".", "start", ",", "idx_i", ".", "stop", ",", "shape", "[", "i", "]", ",", "idx_i", ".", "step", ")", "idx_i", "=", "arange", "(", "start", ",", "stop", ",", "step", ",", "ctx", "=", "self", ".", "context", ",", "dtype", "=", "dtype", ")", "basic_indices", ".", "append", "(", "i", ")", "is_advanced_index", "=", "False", "elif", "isinstance", "(", "idx_i", ",", "integer_types", ")", ":", "start", ",", "stop", ",", "step", "=", "_get_index_range", "(", "idx_i", ",", "idx_i", "+", "1", ",", "shape", "[", "i", "]", ",", "1", ")", "idx_i", "=", "arange", "(", "start", ",", "stop", ",", "step", ",", "ctx", "=", "self", ".", "context", ",", "dtype", "=", "dtype", ")", "advanced_indices", ".", "append", "(", "i", ")", "elif", "isinstance", "(", "idx_i", ",", "NDArray", ")", ":", "if", "dtype", "!=", "idx_i", ".", "dtype", ":", "idx_i", "=", "idx_i", ".", "astype", "(", "dtype", ")", "advanced_indices", ".", "append", "(", "i", ")", "else", ":", "raise", "IndexError", "(", "'Indexing NDArray with index=%s of type=%s is not supported'", "%", "(", "str", "(", "key", ")", ",", "str", "(", "type", "(", "key", ")", ")", ")", ")", "if", "is_advanced_index", ":", "if", "advanced_index_bshape", "is", "None", ":", "advanced_index_bshape", "=", "idx_i", ".", "shape", "elif", "advanced_index_bshape", "!=", "idx_i", ".", "shape", ":", "need_broadcast", "=", "True", "advanced_index_bshape", "=", "_get_broadcast_shape", "(", "advanced_index_bshape", ",", "idx_i", ".", "shape", ")", "indices", ".", "append", "(", "idx_i", ")", "# Get final index shape for gather_nd. See the following reference", "# for determining the output array shape.", "# https://docs.scipy.org/doc/numpy-1.13.0/reference/arrays.indexing.html#combining-advanced-and-basic-indexing # pylint: disable=line-too-long", "if", "len", "(", "advanced_indices", ")", "==", "0", ":", "raise", "ValueError", "(", "'Advanced index tuple must contain at least one of the following types:'", "' list, tuple, NDArray, np.ndarray, integer, received index=%s'", "%", "key", ")", "# determine the output array's shape by checking whether advanced_indices are all adjacent", "# or separated by slices", "advanced_indices_adjacent", "=", "True", "for", "i", "in", "range", "(", "0", ",", "len", "(", "advanced_indices", ")", "-", "1", ")", ":", "if", "advanced_indices", "[", "i", "]", "+", "1", "!=", "advanced_indices", "[", "i", "+", "1", "]", ":", "advanced_indices_adjacent", "=", "False", "break", "index_bshape_list", "=", "[", "]", "# index broadcasted shape", "if", "advanced_indices_adjacent", ":", "for", "i", "in", "range", "(", "0", ",", "advanced_indices", "[", "0", "]", ")", ":", "index_bshape_list", ".", "extend", "(", "indices", "[", "i", "]", ".", "shape", ")", "if", "not", "need_broadcast", "and", "indices", "[", "i", "]", ".", "shape", "!=", "advanced_index_bshape", ":", "need_broadcast", "=", "True", "index_bshape_list", ".", "extend", "(", "advanced_index_bshape", ")", "for", "i", "in", "range", "(", "advanced_indices", "[", "-", "1", "]", "+", "1", ",", "len", "(", "indices", ")", ")", ":", "if", "not", "need_broadcast", "and", "indices", "[", "i", "]", ".", "shape", "!=", "advanced_index_bshape", ":", "need_broadcast", "=", "True", "index_bshape_list", ".", "extend", "(", "indices", "[", "i", "]", ".", "shape", ")", "else", ":", "index_bshape_list", ".", "extend", "(", "advanced_index_bshape", ")", "for", "i", "in", "basic_indices", ":", "index_bshape_list", ".", "extend", "(", "indices", "[", "i", "]", ".", "shape", ")", "if", "not", "need_broadcast", "and", "indices", "[", "i", "]", ".", "shape", "!=", "advanced_index_bshape", ":", "need_broadcast", "=", "True", "index_bshape", "=", "tuple", "(", "index_bshape_list", ")", "# Need to broadcast all ndarrays in indices to the final shape.", "# For example, suppose an array has shape=(5, 6, 7, 8) and", "# key=(slice(1, 5), [[1, 2]], slice(2, 5), [1]).", "# Since key[1] and key[3] are two advanced indices here and they are", "# separated by basic indices key[0] and key[2], the output shape", "# is (1, 2, 4, 3), where the first two elements come from the shape", "# that key[1] and key[3] should broadcast to, which is (1, 2), and", "# the last two elements come from the shape of two basic indices.", "# In order to broadcast all basic and advanced indices to the output shape,", "# we need to reshape them based on their axis. For example, to broadcast key[0],", "# with shape=(4,), we first need to reshape it into (1, 1, 4, 1), and then", "# broadcast the reshaped array to (1, 2, 4, 3); to broadcast key[1], we first", "# reshape it into (1, 2, 1, 1), then broadcast the reshaped array to (1, 2, 4, 3).", "if", "need_broadcast", ":", "broadcasted_indices", "=", "[", "]", "idx_rshape", "=", "[", "1", "]", "*", "len", "(", "index_bshape", ")", "if", "advanced_indices_adjacent", ":", "advanced_index_bshape_start", "=", "advanced_indices", "[", "0", "]", "# start index of advanced_index_bshape in index_shape", "advanced_index_bshape_stop", "=", "advanced_index_bshape_start", "+", "len", "(", "advanced_index_bshape", ")", "for", "i", ",", "idx", "in", "enumerate", "(", "key", ")", ":", "if", "_is_advanced_index", "(", "idx", ")", ":", "k", "=", "advanced_index_bshape_stop", "# find the reshaped shape for indices[i]", "for", "dim_size", "in", "indices", "[", "i", "]", ".", "shape", "[", ":", ":", "-", "1", "]", ":", "k", "-=", "1", "idx_rshape", "[", "k", "]", "=", "dim_size", "else", ":", "if", "i", "<", "advanced_indices", "[", "0", "]", ":", "# slice is on the left side of advanced indices", "idx_rshape", "[", "i", "]", "=", "indices", "[", "i", "]", ".", "shape", "[", "0", "]", "elif", "i", ">", "advanced_indices", "[", "-", "1", "]", ":", "# slice is on the right side of advanced indices", "idx_rshape", "[", "i", "-", "len", "(", "key", ")", "]", "=", "indices", "[", "i", "]", ".", "shape", "[", "0", "]", "else", ":", "raise", "ValueError", "(", "'basic index i=%d cannot be between advanced index i=%d and i=%d'", "%", "(", "i", ",", "advanced_indices", "[", "0", "]", ",", "advanced_indices", "[", "-", "1", "]", ")", ")", "# broadcast current index to the final shape", "broadcasted_indices", ".", "append", "(", "indices", "[", "i", "]", ".", "reshape", "(", "tuple", "(", "idx_rshape", ")", ")", ".", "broadcast_to", "(", "index_bshape", ")", ")", "# reset idx_rshape to ones", "for", "j", ",", "_", "in", "enumerate", "(", "idx_rshape", ")", ":", "idx_rshape", "[", "j", "]", "=", "1", "else", ":", "basic_index_offset", "=", "len", "(", "advanced_index_bshape", ")", "for", "i", ",", "idx", "in", "enumerate", "(", "key", ")", ":", "if", "_is_advanced_index", "(", "idx", ")", ":", "k", "=", "len", "(", "advanced_index_bshape", ")", "for", "dim_size", "in", "indices", "[", "i", "]", ".", "shape", "[", ":", ":", "-", "1", "]", ":", "k", "-=", "1", "idx_rshape", "[", "k", "]", "=", "dim_size", "else", ":", "idx_rshape", "[", "basic_index_offset", "]", "=", "indices", "[", "i", "]", ".", "shape", "[", "0", "]", "basic_index_offset", "+=", "1", "# broadcast current index to the final shape", "broadcasted_indices", ".", "append", "(", "indices", "[", "i", "]", ".", "reshape", "(", "tuple", "(", "idx_rshape", ")", ")", ".", "broadcast_to", "(", "index_bshape", ")", ")", "# reset idx_rshape to ones", "for", "j", ",", "_", "in", "enumerate", "(", "idx_rshape", ")", ":", "idx_rshape", "[", "j", "]", "=", "1", "indices", "=", "broadcasted_indices", "return", "op", ".", "stack", "(", "*", "indices", ")" ]
Returns an index array for use in scatter_nd and gather_nd.
[ "Returns", "an", "index", "array", "for", "use", "in", "scatter_nd", "and", "gather_nd", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L518-L662
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._prepare_value_nd
def _prepare_value_nd(self, value, vshape): """Given value and vshape, create an `NDArray` from value with the same context and dtype as the current one and broadcast it to vshape.""" if isinstance(value, numeric_types): value_nd = full(shape=vshape, val=value, ctx=self.context, dtype=self.dtype) elif isinstance(value, NDArray): value_nd = value.as_in_context(self.context) if value_nd.dtype != self.dtype: value_nd = value_nd.astype(self.dtype) else: try: value_nd = array(value, ctx=self.context, dtype=self.dtype) except: raise TypeError('NDArray does not support assignment with non-array-like' ' object %s of type %s' % (str(value), str(type(value)))) if value_nd.shape != vshape: value_nd = value_nd.broadcast_to(vshape) return value_nd
python
def _prepare_value_nd(self, value, vshape): """Given value and vshape, create an `NDArray` from value with the same context and dtype as the current one and broadcast it to vshape.""" if isinstance(value, numeric_types): value_nd = full(shape=vshape, val=value, ctx=self.context, dtype=self.dtype) elif isinstance(value, NDArray): value_nd = value.as_in_context(self.context) if value_nd.dtype != self.dtype: value_nd = value_nd.astype(self.dtype) else: try: value_nd = array(value, ctx=self.context, dtype=self.dtype) except: raise TypeError('NDArray does not support assignment with non-array-like' ' object %s of type %s' % (str(value), str(type(value)))) if value_nd.shape != vshape: value_nd = value_nd.broadcast_to(vshape) return value_nd
[ "def", "_prepare_value_nd", "(", "self", ",", "value", ",", "vshape", ")", ":", "if", "isinstance", "(", "value", ",", "numeric_types", ")", ":", "value_nd", "=", "full", "(", "shape", "=", "vshape", ",", "val", "=", "value", ",", "ctx", "=", "self", ".", "context", ",", "dtype", "=", "self", ".", "dtype", ")", "elif", "isinstance", "(", "value", ",", "NDArray", ")", ":", "value_nd", "=", "value", ".", "as_in_context", "(", "self", ".", "context", ")", "if", "value_nd", ".", "dtype", "!=", "self", ".", "dtype", ":", "value_nd", "=", "value_nd", ".", "astype", "(", "self", ".", "dtype", ")", "else", ":", "try", ":", "value_nd", "=", "array", "(", "value", ",", "ctx", "=", "self", ".", "context", ",", "dtype", "=", "self", ".", "dtype", ")", "except", ":", "raise", "TypeError", "(", "'NDArray does not support assignment with non-array-like'", "' object %s of type %s'", "%", "(", "str", "(", "value", ")", ",", "str", "(", "type", "(", "value", ")", ")", ")", ")", "if", "value_nd", ".", "shape", "!=", "vshape", ":", "value_nd", "=", "value_nd", ".", "broadcast_to", "(", "vshape", ")", "return", "value_nd" ]
Given value and vshape, create an `NDArray` from value with the same context and dtype as the current one and broadcast it to vshape.
[ "Given", "value", "and", "vshape", "create", "an", "NDArray", "from", "value", "with", "the", "same", "context", "and", "dtype", "as", "the", "current", "one", "and", "broadcast", "it", "to", "vshape", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L664-L681
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._set_nd_basic_indexing
def _set_nd_basic_indexing(self, key, value): """This function is called by __setitem__ when key is a basic index, i.e. an integer, or a slice, or a tuple of integers and slices. No restrictions on the values of slices' steps.""" shape = self.shape if isinstance(key, integer_types): if key < 0: key += shape[0] if key < 0 or key >= shape[0]: if key < 0: key -= shape[0] raise IndexError('index %d is out of bounds for axis 0 with size %d' % (key, shape[0])) key = py_slice(key, key+1) # key must be >= 0 here if isinstance(key, py_slice): assign_to_self = key.step is None or key.step == 1 assign_to_self &= key.start is None or key.start == 0 assign_to_self &= key.stop is None or key.stop == shape[0] if assign_to_self: # trivial case, assign value to self if isinstance(value, NDArray): if value.handle is not self.handle: if value.shape != shape: value = value.broadcast_to(shape) value.copyto(self) elif isinstance(value, numeric_types): _internal._full(shape=shape, ctx=self.context, dtype=self.dtype, value=float(value), out=self) elif isinstance(value, (np.ndarray, np.generic)): if isinstance(value, np.generic) or value.shape != shape: value = np.broadcast_to(value, shape) self._sync_copyfrom(value) else: # value might be a list or a tuple value_nd = self._prepare_value_nd(value, shape) value_nd.copyto(self) return else: # non-trivial case, use _slice_assign or _slice_assign_scalar key = (key,) assert isinstance(key, tuple), "key=%s must be a tuple of slices and integers" % str(key) assert len(key) <= len(shape), "Indexing dimensions exceed array dimensions, %d vs %d"\ % (len(key), len(shape)) begin = [] end = [] steps = [] oshape = [] # output shape of slice using key vshape = [] # value shape of data[key] for i, slice_i in enumerate(key): dim_size = 1 if isinstance(slice_i, py_slice): begin.append(slice_i.start) end.append(slice_i.stop) steps.append(slice_i.step) start, stop, step = _get_index_range(slice_i.start, slice_i.stop, shape[i], slice_i.step) dim_size = _get_dim_size(start, stop, step) vshape.append(dim_size) elif isinstance(slice_i, integer_types): begin.append(slice_i) end.append(slice_i+1 if slice_i != -1 else self.shape[i]) steps.append(1) else: raise ValueError("basic indexing does not support index=%s of type=%s" % (str(slice_i), str(type(slice_i)))) oshape.append(dim_size) oshape.extend(shape[len(key):]) vshape.extend(shape[len(key):]) # if key contains all integers, vshape should be (1,) if len(vshape) == 0: vshape.append(1) oshape = tuple(oshape) vshape = tuple(vshape) if isinstance(value, numeric_types): _internal._slice_assign_scalar(self, out=self, begin=begin, end=end, step=steps, scalar=float(value)) else: value_nd = self._prepare_value_nd(value, vshape) if vshape != oshape: value_nd = value_nd.reshape(oshape) _internal._slice_assign(self, value_nd, begin, end, steps, out=self)
python
def _set_nd_basic_indexing(self, key, value): """This function is called by __setitem__ when key is a basic index, i.e. an integer, or a slice, or a tuple of integers and slices. No restrictions on the values of slices' steps.""" shape = self.shape if isinstance(key, integer_types): if key < 0: key += shape[0] if key < 0 or key >= shape[0]: if key < 0: key -= shape[0] raise IndexError('index %d is out of bounds for axis 0 with size %d' % (key, shape[0])) key = py_slice(key, key+1) # key must be >= 0 here if isinstance(key, py_slice): assign_to_self = key.step is None or key.step == 1 assign_to_self &= key.start is None or key.start == 0 assign_to_self &= key.stop is None or key.stop == shape[0] if assign_to_self: # trivial case, assign value to self if isinstance(value, NDArray): if value.handle is not self.handle: if value.shape != shape: value = value.broadcast_to(shape) value.copyto(self) elif isinstance(value, numeric_types): _internal._full(shape=shape, ctx=self.context, dtype=self.dtype, value=float(value), out=self) elif isinstance(value, (np.ndarray, np.generic)): if isinstance(value, np.generic) or value.shape != shape: value = np.broadcast_to(value, shape) self._sync_copyfrom(value) else: # value might be a list or a tuple value_nd = self._prepare_value_nd(value, shape) value_nd.copyto(self) return else: # non-trivial case, use _slice_assign or _slice_assign_scalar key = (key,) assert isinstance(key, tuple), "key=%s must be a tuple of slices and integers" % str(key) assert len(key) <= len(shape), "Indexing dimensions exceed array dimensions, %d vs %d"\ % (len(key), len(shape)) begin = [] end = [] steps = [] oshape = [] # output shape of slice using key vshape = [] # value shape of data[key] for i, slice_i in enumerate(key): dim_size = 1 if isinstance(slice_i, py_slice): begin.append(slice_i.start) end.append(slice_i.stop) steps.append(slice_i.step) start, stop, step = _get_index_range(slice_i.start, slice_i.stop, shape[i], slice_i.step) dim_size = _get_dim_size(start, stop, step) vshape.append(dim_size) elif isinstance(slice_i, integer_types): begin.append(slice_i) end.append(slice_i+1 if slice_i != -1 else self.shape[i]) steps.append(1) else: raise ValueError("basic indexing does not support index=%s of type=%s" % (str(slice_i), str(type(slice_i)))) oshape.append(dim_size) oshape.extend(shape[len(key):]) vshape.extend(shape[len(key):]) # if key contains all integers, vshape should be (1,) if len(vshape) == 0: vshape.append(1) oshape = tuple(oshape) vshape = tuple(vshape) if isinstance(value, numeric_types): _internal._slice_assign_scalar(self, out=self, begin=begin, end=end, step=steps, scalar=float(value)) else: value_nd = self._prepare_value_nd(value, vshape) if vshape != oshape: value_nd = value_nd.reshape(oshape) _internal._slice_assign(self, value_nd, begin, end, steps, out=self)
[ "def", "_set_nd_basic_indexing", "(", "self", ",", "key", ",", "value", ")", ":", "shape", "=", "self", ".", "shape", "if", "isinstance", "(", "key", ",", "integer_types", ")", ":", "if", "key", "<", "0", ":", "key", "+=", "shape", "[", "0", "]", "if", "key", "<", "0", "or", "key", ">=", "shape", "[", "0", "]", ":", "if", "key", "<", "0", ":", "key", "-=", "shape", "[", "0", "]", "raise", "IndexError", "(", "'index %d is out of bounds for axis 0 with size %d'", "%", "(", "key", ",", "shape", "[", "0", "]", ")", ")", "key", "=", "py_slice", "(", "key", ",", "key", "+", "1", ")", "# key must be >= 0 here", "if", "isinstance", "(", "key", ",", "py_slice", ")", ":", "assign_to_self", "=", "key", ".", "step", "is", "None", "or", "key", ".", "step", "==", "1", "assign_to_self", "&=", "key", ".", "start", "is", "None", "or", "key", ".", "start", "==", "0", "assign_to_self", "&=", "key", ".", "stop", "is", "None", "or", "key", ".", "stop", "==", "shape", "[", "0", "]", "if", "assign_to_self", ":", "# trivial case, assign value to self", "if", "isinstance", "(", "value", ",", "NDArray", ")", ":", "if", "value", ".", "handle", "is", "not", "self", ".", "handle", ":", "if", "value", ".", "shape", "!=", "shape", ":", "value", "=", "value", ".", "broadcast_to", "(", "shape", ")", "value", ".", "copyto", "(", "self", ")", "elif", "isinstance", "(", "value", ",", "numeric_types", ")", ":", "_internal", ".", "_full", "(", "shape", "=", "shape", ",", "ctx", "=", "self", ".", "context", ",", "dtype", "=", "self", ".", "dtype", ",", "value", "=", "float", "(", "value", ")", ",", "out", "=", "self", ")", "elif", "isinstance", "(", "value", ",", "(", "np", ".", "ndarray", ",", "np", ".", "generic", ")", ")", ":", "if", "isinstance", "(", "value", ",", "np", ".", "generic", ")", "or", "value", ".", "shape", "!=", "shape", ":", "value", "=", "np", ".", "broadcast_to", "(", "value", ",", "shape", ")", "self", ".", "_sync_copyfrom", "(", "value", ")", "else", ":", "# value might be a list or a tuple", "value_nd", "=", "self", ".", "_prepare_value_nd", "(", "value", ",", "shape", ")", "value_nd", ".", "copyto", "(", "self", ")", "return", "else", ":", "# non-trivial case, use _slice_assign or _slice_assign_scalar", "key", "=", "(", "key", ",", ")", "assert", "isinstance", "(", "key", ",", "tuple", ")", ",", "\"key=%s must be a tuple of slices and integers\"", "%", "str", "(", "key", ")", "assert", "len", "(", "key", ")", "<=", "len", "(", "shape", ")", ",", "\"Indexing dimensions exceed array dimensions, %d vs %d\"", "%", "(", "len", "(", "key", ")", ",", "len", "(", "shape", ")", ")", "begin", "=", "[", "]", "end", "=", "[", "]", "steps", "=", "[", "]", "oshape", "=", "[", "]", "# output shape of slice using key", "vshape", "=", "[", "]", "# value shape of data[key]", "for", "i", ",", "slice_i", "in", "enumerate", "(", "key", ")", ":", "dim_size", "=", "1", "if", "isinstance", "(", "slice_i", ",", "py_slice", ")", ":", "begin", ".", "append", "(", "slice_i", ".", "start", ")", "end", ".", "append", "(", "slice_i", ".", "stop", ")", "steps", ".", "append", "(", "slice_i", ".", "step", ")", "start", ",", "stop", ",", "step", "=", "_get_index_range", "(", "slice_i", ".", "start", ",", "slice_i", ".", "stop", ",", "shape", "[", "i", "]", ",", "slice_i", ".", "step", ")", "dim_size", "=", "_get_dim_size", "(", "start", ",", "stop", ",", "step", ")", "vshape", ".", "append", "(", "dim_size", ")", "elif", "isinstance", "(", "slice_i", ",", "integer_types", ")", ":", "begin", ".", "append", "(", "slice_i", ")", "end", ".", "append", "(", "slice_i", "+", "1", "if", "slice_i", "!=", "-", "1", "else", "self", ".", "shape", "[", "i", "]", ")", "steps", ".", "append", "(", "1", ")", "else", ":", "raise", "ValueError", "(", "\"basic indexing does not support index=%s of type=%s\"", "%", "(", "str", "(", "slice_i", ")", ",", "str", "(", "type", "(", "slice_i", ")", ")", ")", ")", "oshape", ".", "append", "(", "dim_size", ")", "oshape", ".", "extend", "(", "shape", "[", "len", "(", "key", ")", ":", "]", ")", "vshape", ".", "extend", "(", "shape", "[", "len", "(", "key", ")", ":", "]", ")", "# if key contains all integers, vshape should be (1,)", "if", "len", "(", "vshape", ")", "==", "0", ":", "vshape", ".", "append", "(", "1", ")", "oshape", "=", "tuple", "(", "oshape", ")", "vshape", "=", "tuple", "(", "vshape", ")", "if", "isinstance", "(", "value", ",", "numeric_types", ")", ":", "_internal", ".", "_slice_assign_scalar", "(", "self", ",", "out", "=", "self", ",", "begin", "=", "begin", ",", "end", "=", "end", ",", "step", "=", "steps", ",", "scalar", "=", "float", "(", "value", ")", ")", "else", ":", "value_nd", "=", "self", ".", "_prepare_value_nd", "(", "value", ",", "vshape", ")", "if", "vshape", "!=", "oshape", ":", "value_nd", "=", "value_nd", ".", "reshape", "(", "oshape", ")", "_internal", ".", "_slice_assign", "(", "self", ",", "value_nd", ",", "begin", ",", "end", ",", "steps", ",", "out", "=", "self", ")" ]
This function is called by __setitem__ when key is a basic index, i.e. an integer, or a slice, or a tuple of integers and slices. No restrictions on the values of slices' steps.
[ "This", "function", "is", "called", "by", "__setitem__", "when", "key", "is", "a", "basic", "index", "i", ".", "e", ".", "an", "integer", "or", "a", "slice", "or", "a", "tuple", "of", "integers", "and", "slices", ".", "No", "restrictions", "on", "the", "values", "of", "slices", "steps", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L683-L765
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._set_nd_advanced_indexing
def _set_nd_advanced_indexing(self, key, value): """This function is called by __setitem__ when key is an advanced index.""" indices = self._get_index_nd(key) vshape = _get_oshape_of_gather_nd_op(self.shape, indices.shape) value_nd = self._prepare_value_nd(value, vshape) _internal._scatter_set_nd(lhs=self, rhs=value_nd, indices=indices, shape=self.shape, out=self)
python
def _set_nd_advanced_indexing(self, key, value): """This function is called by __setitem__ when key is an advanced index.""" indices = self._get_index_nd(key) vshape = _get_oshape_of_gather_nd_op(self.shape, indices.shape) value_nd = self._prepare_value_nd(value, vshape) _internal._scatter_set_nd(lhs=self, rhs=value_nd, indices=indices, shape=self.shape, out=self)
[ "def", "_set_nd_advanced_indexing", "(", "self", ",", "key", ",", "value", ")", ":", "indices", "=", "self", ".", "_get_index_nd", "(", "key", ")", "vshape", "=", "_get_oshape_of_gather_nd_op", "(", "self", ".", "shape", ",", "indices", ".", "shape", ")", "value_nd", "=", "self", ".", "_prepare_value_nd", "(", "value", ",", "vshape", ")", "_internal", ".", "_scatter_set_nd", "(", "lhs", "=", "self", ",", "rhs", "=", "value_nd", ",", "indices", "=", "indices", ",", "shape", "=", "self", ".", "shape", ",", "out", "=", "self", ")" ]
This function is called by __setitem__ when key is an advanced index.
[ "This", "function", "is", "called", "by", "__setitem__", "when", "key", "is", "an", "advanced", "index", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L767-L773
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._get_nd_basic_indexing
def _get_nd_basic_indexing(self, key): """This function is called when key is a slice, or an integer, or a tuple of slices or integers""" shape = self.shape if isinstance(key, integer_types): if key > shape[0] - 1: raise IndexError( 'index {} is out of bounds for axis 0 with size {}'.format( key, shape[0])) return self._at(key) elif isinstance(key, py_slice): if key.step is not None and key.step != 1: if key.step == 0: raise ValueError("slice step cannot be zero") return op.slice(self, begin=(key.start,), end=(key.stop,), step=(key.step,)) elif key.start is not None or key.stop is not None: return self._slice(key.start, key.stop) else: return self if not isinstance(key, tuple): raise ValueError('index=%s must be a slice, or an ineger, or a tuple' ' of slices and integers to use basic indexing, received type=%s' % (str(key), str(type(key)))) assert len(key) != 0, 'basic index cannot be an empty tuple' begin = [] end = [] step = [] kept_axes = [] # axes where slice_i is a slice i = -1 for i, slice_i in enumerate(key): if isinstance(slice_i, integer_types): begin.append(slice_i) end.append(slice_i+1 if slice_i != -1 else self.shape[i]) step.append(1) elif isinstance(slice_i, py_slice): if slice_i.step == 0: raise ValueError('basic index=%s cannot have slice=%s with step = 0' % (str(key), str(slice_i))) begin.append(slice_i.start) end.append(slice_i.stop) step.append(slice_i.step) kept_axes.append(i) else: raise ValueError('basic_indexing does not support slicing with ' 'index=%s of type=%s.' % (str(slice_i), str(type(slice_i)))) kept_axes.extend(range(i+1, len(shape))) sliced_nd = op.slice(self, begin, end, step) if len(kept_axes) == len(shape): return sliced_nd # squeeze sliced_shape to remove the axes indexed by integers oshape = [] sliced_shape = sliced_nd.shape for axis in kept_axes: oshape.append(sliced_shape[axis]) # if key is a tuple of integers, still need to keep 1 dim # while in Numpy, the output will become an value instead of an ndarray if len(oshape) == 0: oshape.append(1) oshape = tuple(oshape) assert np.prod(oshape) == np.prod(sliced_shape), 'oshape=%s has different size'\ ' than sliced_shape=%s'\ % (oshape, sliced_shape) return sliced_nd.reshape(oshape)
python
def _get_nd_basic_indexing(self, key): """This function is called when key is a slice, or an integer, or a tuple of slices or integers""" shape = self.shape if isinstance(key, integer_types): if key > shape[0] - 1: raise IndexError( 'index {} is out of bounds for axis 0 with size {}'.format( key, shape[0])) return self._at(key) elif isinstance(key, py_slice): if key.step is not None and key.step != 1: if key.step == 0: raise ValueError("slice step cannot be zero") return op.slice(self, begin=(key.start,), end=(key.stop,), step=(key.step,)) elif key.start is not None or key.stop is not None: return self._slice(key.start, key.stop) else: return self if not isinstance(key, tuple): raise ValueError('index=%s must be a slice, or an ineger, or a tuple' ' of slices and integers to use basic indexing, received type=%s' % (str(key), str(type(key)))) assert len(key) != 0, 'basic index cannot be an empty tuple' begin = [] end = [] step = [] kept_axes = [] # axes where slice_i is a slice i = -1 for i, slice_i in enumerate(key): if isinstance(slice_i, integer_types): begin.append(slice_i) end.append(slice_i+1 if slice_i != -1 else self.shape[i]) step.append(1) elif isinstance(slice_i, py_slice): if slice_i.step == 0: raise ValueError('basic index=%s cannot have slice=%s with step = 0' % (str(key), str(slice_i))) begin.append(slice_i.start) end.append(slice_i.stop) step.append(slice_i.step) kept_axes.append(i) else: raise ValueError('basic_indexing does not support slicing with ' 'index=%s of type=%s.' % (str(slice_i), str(type(slice_i)))) kept_axes.extend(range(i+1, len(shape))) sliced_nd = op.slice(self, begin, end, step) if len(kept_axes) == len(shape): return sliced_nd # squeeze sliced_shape to remove the axes indexed by integers oshape = [] sliced_shape = sliced_nd.shape for axis in kept_axes: oshape.append(sliced_shape[axis]) # if key is a tuple of integers, still need to keep 1 dim # while in Numpy, the output will become an value instead of an ndarray if len(oshape) == 0: oshape.append(1) oshape = tuple(oshape) assert np.prod(oshape) == np.prod(sliced_shape), 'oshape=%s has different size'\ ' than sliced_shape=%s'\ % (oshape, sliced_shape) return sliced_nd.reshape(oshape)
[ "def", "_get_nd_basic_indexing", "(", "self", ",", "key", ")", ":", "shape", "=", "self", ".", "shape", "if", "isinstance", "(", "key", ",", "integer_types", ")", ":", "if", "key", ">", "shape", "[", "0", "]", "-", "1", ":", "raise", "IndexError", "(", "'index {} is out of bounds for axis 0 with size {}'", ".", "format", "(", "key", ",", "shape", "[", "0", "]", ")", ")", "return", "self", ".", "_at", "(", "key", ")", "elif", "isinstance", "(", "key", ",", "py_slice", ")", ":", "if", "key", ".", "step", "is", "not", "None", "and", "key", ".", "step", "!=", "1", ":", "if", "key", ".", "step", "==", "0", ":", "raise", "ValueError", "(", "\"slice step cannot be zero\"", ")", "return", "op", ".", "slice", "(", "self", ",", "begin", "=", "(", "key", ".", "start", ",", ")", ",", "end", "=", "(", "key", ".", "stop", ",", ")", ",", "step", "=", "(", "key", ".", "step", ",", ")", ")", "elif", "key", ".", "start", "is", "not", "None", "or", "key", ".", "stop", "is", "not", "None", ":", "return", "self", ".", "_slice", "(", "key", ".", "start", ",", "key", ".", "stop", ")", "else", ":", "return", "self", "if", "not", "isinstance", "(", "key", ",", "tuple", ")", ":", "raise", "ValueError", "(", "'index=%s must be a slice, or an ineger, or a tuple'", "' of slices and integers to use basic indexing, received type=%s'", "%", "(", "str", "(", "key", ")", ",", "str", "(", "type", "(", "key", ")", ")", ")", ")", "assert", "len", "(", "key", ")", "!=", "0", ",", "'basic index cannot be an empty tuple'", "begin", "=", "[", "]", "end", "=", "[", "]", "step", "=", "[", "]", "kept_axes", "=", "[", "]", "# axes where slice_i is a slice", "i", "=", "-", "1", "for", "i", ",", "slice_i", "in", "enumerate", "(", "key", ")", ":", "if", "isinstance", "(", "slice_i", ",", "integer_types", ")", ":", "begin", ".", "append", "(", "slice_i", ")", "end", ".", "append", "(", "slice_i", "+", "1", "if", "slice_i", "!=", "-", "1", "else", "self", ".", "shape", "[", "i", "]", ")", "step", ".", "append", "(", "1", ")", "elif", "isinstance", "(", "slice_i", ",", "py_slice", ")", ":", "if", "slice_i", ".", "step", "==", "0", ":", "raise", "ValueError", "(", "'basic index=%s cannot have slice=%s with step = 0'", "%", "(", "str", "(", "key", ")", ",", "str", "(", "slice_i", ")", ")", ")", "begin", ".", "append", "(", "slice_i", ".", "start", ")", "end", ".", "append", "(", "slice_i", ".", "stop", ")", "step", ".", "append", "(", "slice_i", ".", "step", ")", "kept_axes", ".", "append", "(", "i", ")", "else", ":", "raise", "ValueError", "(", "'basic_indexing does not support slicing with '", "'index=%s of type=%s.'", "%", "(", "str", "(", "slice_i", ")", ",", "str", "(", "type", "(", "slice_i", ")", ")", ")", ")", "kept_axes", ".", "extend", "(", "range", "(", "i", "+", "1", ",", "len", "(", "shape", ")", ")", ")", "sliced_nd", "=", "op", ".", "slice", "(", "self", ",", "begin", ",", "end", ",", "step", ")", "if", "len", "(", "kept_axes", ")", "==", "len", "(", "shape", ")", ":", "return", "sliced_nd", "# squeeze sliced_shape to remove the axes indexed by integers", "oshape", "=", "[", "]", "sliced_shape", "=", "sliced_nd", ".", "shape", "for", "axis", "in", "kept_axes", ":", "oshape", ".", "append", "(", "sliced_shape", "[", "axis", "]", ")", "# if key is a tuple of integers, still need to keep 1 dim", "# while in Numpy, the output will become an value instead of an ndarray", "if", "len", "(", "oshape", ")", "==", "0", ":", "oshape", ".", "append", "(", "1", ")", "oshape", "=", "tuple", "(", "oshape", ")", "assert", "np", ".", "prod", "(", "oshape", ")", "==", "np", ".", "prod", "(", "sliced_shape", ")", ",", "'oshape=%s has different size'", "' than sliced_shape=%s'", "%", "(", "oshape", ",", "sliced_shape", ")", "return", "sliced_nd", ".", "reshape", "(", "oshape", ")" ]
This function is called when key is a slice, or an integer, or a tuple of slices or integers
[ "This", "function", "is", "called", "when", "key", "is", "a", "slice", "or", "an", "integer", "or", "a", "tuple", "of", "slices", "or", "integers" ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L775-L838
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._sync_copyfrom
def _sync_copyfrom(self, source_array): """Performs a synchronized copy from the `source_array` to the current array. This is called through ``x[:] = source_array``, where the `source_array` is a `numpy.ndarray` or array-like object. This function blocks until all the pending read/write operations with respect to the current `NDArray` are finished and carry out the copy operation to the current NDArray. Parameters ---------- source_array : array_like The data source we would like to copy from. Example ------- >>> a = mx.nd.array([1, 2]) >>> a.asnumpy() array([ 1., 2.], dtype=float32) >>> a[:] = np.array([3, 4]) >> a.asnumpy() array([ 3., 4.], dtype=float32) """ if not isinstance(source_array, np.ndarray): try: source_array = np.array(source_array, dtype=self.dtype) except: raise TypeError('array must consist of array-like data,' + 'type %s is not supported' % str(type(array))) source_array = np.asarray(source_array, dtype=self.dtype, order='C') if source_array.shape != self.shape: raise ValueError('Shape inconsistent: expected %s vs got %s'%( str(source_array.shape), str(self.shape))) check_call(_LIB.MXNDArraySyncCopyFromCPU( self.handle, source_array.ctypes.data_as(ctypes.c_void_p), ctypes.c_size_t(source_array.size)))
python
def _sync_copyfrom(self, source_array): """Performs a synchronized copy from the `source_array` to the current array. This is called through ``x[:] = source_array``, where the `source_array` is a `numpy.ndarray` or array-like object. This function blocks until all the pending read/write operations with respect to the current `NDArray` are finished and carry out the copy operation to the current NDArray. Parameters ---------- source_array : array_like The data source we would like to copy from. Example ------- >>> a = mx.nd.array([1, 2]) >>> a.asnumpy() array([ 1., 2.], dtype=float32) >>> a[:] = np.array([3, 4]) >> a.asnumpy() array([ 3., 4.], dtype=float32) """ if not isinstance(source_array, np.ndarray): try: source_array = np.array(source_array, dtype=self.dtype) except: raise TypeError('array must consist of array-like data,' + 'type %s is not supported' % str(type(array))) source_array = np.asarray(source_array, dtype=self.dtype, order='C') if source_array.shape != self.shape: raise ValueError('Shape inconsistent: expected %s vs got %s'%( str(source_array.shape), str(self.shape))) check_call(_LIB.MXNDArraySyncCopyFromCPU( self.handle, source_array.ctypes.data_as(ctypes.c_void_p), ctypes.c_size_t(source_array.size)))
[ "def", "_sync_copyfrom", "(", "self", ",", "source_array", ")", ":", "if", "not", "isinstance", "(", "source_array", ",", "np", ".", "ndarray", ")", ":", "try", ":", "source_array", "=", "np", ".", "array", "(", "source_array", ",", "dtype", "=", "self", ".", "dtype", ")", "except", ":", "raise", "TypeError", "(", "'array must consist of array-like data,'", "+", "'type %s is not supported'", "%", "str", "(", "type", "(", "array", ")", ")", ")", "source_array", "=", "np", ".", "asarray", "(", "source_array", ",", "dtype", "=", "self", ".", "dtype", ",", "order", "=", "'C'", ")", "if", "source_array", ".", "shape", "!=", "self", ".", "shape", ":", "raise", "ValueError", "(", "'Shape inconsistent: expected %s vs got %s'", "%", "(", "str", "(", "source_array", ".", "shape", ")", ",", "str", "(", "self", ".", "shape", ")", ")", ")", "check_call", "(", "_LIB", ".", "MXNDArraySyncCopyFromCPU", "(", "self", ".", "handle", ",", "source_array", ".", "ctypes", ".", "data_as", "(", "ctypes", ".", "c_void_p", ")", ",", "ctypes", ".", "c_size_t", "(", "source_array", ".", "size", ")", ")", ")" ]
Performs a synchronized copy from the `source_array` to the current array. This is called through ``x[:] = source_array``, where the `source_array` is a `numpy.ndarray` or array-like object. This function blocks until all the pending read/write operations with respect to the current `NDArray` are finished and carry out the copy operation to the current NDArray. Parameters ---------- source_array : array_like The data source we would like to copy from. Example ------- >>> a = mx.nd.array([1, 2]) >>> a.asnumpy() array([ 1., 2.], dtype=float32) >>> a[:] = np.array([3, 4]) >> a.asnumpy() array([ 3., 4.], dtype=float32)
[ "Performs", "a", "synchronized", "copy", "from", "the", "source_array", "to", "the", "current", "array", ".", "This", "is", "called", "through", "x", "[", ":", "]", "=", "source_array", "where", "the", "source_array", "is", "a", "numpy", ".", "ndarray", "or", "array", "-", "like", "object", ".", "This", "function", "blocks", "until", "all", "the", "pending", "read", "/", "write", "operations", "with", "respect", "to", "the", "current", "NDArray", "are", "finished", "and", "carry", "out", "the", "copy", "operation", "to", "the", "current", "NDArray", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L845-L880
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._slice
def _slice(self, start, stop): """Returns a sliced NDArray that shares memory with the current one. This is called through ``x[start:stop]``. Parameters ---------- start : int Starting inclusive index of slice in the first dim. stop : int Finishing exclusive index of slice in the first dim. Returns ------- `NDArray` sharing the memory with the current one sliced from start to stop in the first dim. Examples: >>> a = mx.nd.array([[1,2], [3, 4], [5, 6], [7, 8]]) >>> a[1:2].asnumpy() array([[ 3., 4.]], dtype=float32) >>> a[1:1].asnumpy() array([], shape=(0, 2), dtype=float32) """ handle = NDArrayHandle() start, stop, _ = _get_index_range(start, stop, self.shape[0]) check_call(_LIB.MXNDArraySlice( self.handle, mx_uint(start), mx_uint(stop), ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable)
python
def _slice(self, start, stop): """Returns a sliced NDArray that shares memory with the current one. This is called through ``x[start:stop]``. Parameters ---------- start : int Starting inclusive index of slice in the first dim. stop : int Finishing exclusive index of slice in the first dim. Returns ------- `NDArray` sharing the memory with the current one sliced from start to stop in the first dim. Examples: >>> a = mx.nd.array([[1,2], [3, 4], [5, 6], [7, 8]]) >>> a[1:2].asnumpy() array([[ 3., 4.]], dtype=float32) >>> a[1:1].asnumpy() array([], shape=(0, 2), dtype=float32) """ handle = NDArrayHandle() start, stop, _ = _get_index_range(start, stop, self.shape[0]) check_call(_LIB.MXNDArraySlice( self.handle, mx_uint(start), mx_uint(stop), ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable)
[ "def", "_slice", "(", "self", ",", "start", ",", "stop", ")", ":", "handle", "=", "NDArrayHandle", "(", ")", "start", ",", "stop", ",", "_", "=", "_get_index_range", "(", "start", ",", "stop", ",", "self", ".", "shape", "[", "0", "]", ")", "check_call", "(", "_LIB", ".", "MXNDArraySlice", "(", "self", ".", "handle", ",", "mx_uint", "(", "start", ")", ",", "mx_uint", "(", "stop", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "NDArray", "(", "handle", "=", "handle", ",", "writable", "=", "self", ".", "writable", ")" ]
Returns a sliced NDArray that shares memory with the current one. This is called through ``x[start:stop]``. Parameters ---------- start : int Starting inclusive index of slice in the first dim. stop : int Finishing exclusive index of slice in the first dim. Returns ------- `NDArray` sharing the memory with the current one sliced from start to stop in the first dim. Examples: >>> a = mx.nd.array([[1,2], [3, 4], [5, 6], [7, 8]]) >>> a[1:2].asnumpy() array([[ 3., 4.]], dtype=float32) >>> a[1:1].asnumpy() array([], shape=(0, 2), dtype=float32)
[ "Returns", "a", "sliced", "NDArray", "that", "shares", "memory", "with", "the", "current", "one", ".", "This", "is", "called", "through", "x", "[", "start", ":", "stop", "]", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L882-L910
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray._at
def _at(self, idx): """Returns a view of the array sliced at `idx` in the first dim. This is called through ``x[idx]``. Parameters ---------- idx : int index for slicing the `NDArray` in the first dim. Returns ------- NDArray `NDArray` sharing the memory with the current one sliced at `idx` in the first dim. Examples -------- >>> a = mx.nd.array([[1,2], [3, 4]]) >>> a[1].asnumpy() array([ 3., 4.], dtype=float32) >>> b = mx.nd.array([1, 2, 3, 4]) >>> b[0].asnumpy() array([ 1.], dtype=float32) """ handle = NDArrayHandle() if idx < 0: length = self.shape[0] idx += length if idx < 0: raise IndexError('index %d is out of bounds for axis 0 with size %d' % (idx-length, length)) check_call(_LIB.MXNDArrayAt( self.handle, mx_uint(idx), ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable)
python
def _at(self, idx): """Returns a view of the array sliced at `idx` in the first dim. This is called through ``x[idx]``. Parameters ---------- idx : int index for slicing the `NDArray` in the first dim. Returns ------- NDArray `NDArray` sharing the memory with the current one sliced at `idx` in the first dim. Examples -------- >>> a = mx.nd.array([[1,2], [3, 4]]) >>> a[1].asnumpy() array([ 3., 4.], dtype=float32) >>> b = mx.nd.array([1, 2, 3, 4]) >>> b[0].asnumpy() array([ 1.], dtype=float32) """ handle = NDArrayHandle() if idx < 0: length = self.shape[0] idx += length if idx < 0: raise IndexError('index %d is out of bounds for axis 0 with size %d' % (idx-length, length)) check_call(_LIB.MXNDArrayAt( self.handle, mx_uint(idx), ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable)
[ "def", "_at", "(", "self", ",", "idx", ")", ":", "handle", "=", "NDArrayHandle", "(", ")", "if", "idx", "<", "0", ":", "length", "=", "self", ".", "shape", "[", "0", "]", "idx", "+=", "length", "if", "idx", "<", "0", ":", "raise", "IndexError", "(", "'index %d is out of bounds for axis 0 with size %d'", "%", "(", "idx", "-", "length", ",", "length", ")", ")", "check_call", "(", "_LIB", ".", "MXNDArrayAt", "(", "self", ".", "handle", ",", "mx_uint", "(", "idx", ")", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "NDArray", "(", "handle", "=", "handle", ",", "writable", "=", "self", ".", "writable", ")" ]
Returns a view of the array sliced at `idx` in the first dim. This is called through ``x[idx]``. Parameters ---------- idx : int index for slicing the `NDArray` in the first dim. Returns ------- NDArray `NDArray` sharing the memory with the current one sliced at `idx` in the first dim. Examples -------- >>> a = mx.nd.array([[1,2], [3, 4]]) >>> a[1].asnumpy() array([ 3., 4.], dtype=float32) >>> b = mx.nd.array([1, 2, 3, 4]) >>> b[0].asnumpy() array([ 1.], dtype=float32)
[ "Returns", "a", "view", "of", "the", "array", "sliced", "at", "idx", "in", "the", "first", "dim", ".", "This", "is", "called", "through", "x", "[", "idx", "]", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L912-L944
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray.reshape
def reshape(self, *shape, **kwargs): """Returns a **view** of this array with a new shape without altering any data. Parameters ---------- shape : tuple of int, or n ints The new shape should not change the array size, namely ``np.prod(new_shape)`` should be equal to ``np.prod(self.shape)``. Some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below: - ``0`` copy this dimension from the input to the output shape. Example:: - input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2) - input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4) - ``-1`` infers the dimension of the output shape by using the remainder of the input dimensions keeping the size of the new array same as that of the input array. At most one dimension of shape can be -1. Example:: - input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4) - input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8) - input shape = (2,3,4), shape=(-1,), output shape = (24,) - ``-2`` copy all/remainder of the input dimensions to the output shape. Example:: - input shape = (2,3,4), shape = (-2,), output shape = (2,3,4) - input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4) - input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1) - ``-3`` use the product of two consecutive dimensions of the input shape as the output dimension. Example:: - input shape = (2,3,4), shape = (-3,4), output shape = (6,4) - input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20) - input shape = (2,3,4), shape = (0,-3), output shape = (2,12) - input shape = (2,3,4), shape = (-3,-2), output shape = (6,4) - ``-4`` split one dimension of the input into two dimensions passed subsequent to -4 in shape (can contain -1). Example:: - input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4) - input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4) - If the argument `reverse` is set to 1, then the special values are inferred from right to left. Example:: - without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be \ (40,5). - with reverse=1, output shape will be (50,4). reverse : bool, default False If true then the special values are inferred from right to left. Only supported as keyword argument. Returns ------- NDArray An array with desired shape that shares data with this array. Examples -------- >>> x = mx.nd.arange(0,6).reshape(2,3) >>> x.asnumpy() array([[ 0., 1., 2.], [ 3., 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,-1) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(-3) >>> y.asnumpy() array([ 0. 1. 2. 3. 4. 5.], dtype=float32) >>> y[:] = -1 >>> x.asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32) """ if len(shape) == 1 and isinstance(shape[0], (list, tuple)): shape = shape[0] elif not shape: shape = kwargs.get('shape') assert shape, "Shape must be provided." if not all(k in ['shape', 'reverse'] for k in kwargs): raise TypeError( "Got unknown keywords in reshape: {}. " \ "Accepted keyword arguments are 'shape' and 'reverse'.".format( ', '.join([k for k in kwargs if k not in ['shape', 'reverse']]))) reverse = kwargs.get('reverse', False) handle = NDArrayHandle() # Actual reshape check_call(_LIB.MXNDArrayReshape64(self.handle, len(shape), c_array(ctypes.c_int64, shape), reverse, ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable)
python
def reshape(self, *shape, **kwargs): """Returns a **view** of this array with a new shape without altering any data. Parameters ---------- shape : tuple of int, or n ints The new shape should not change the array size, namely ``np.prod(new_shape)`` should be equal to ``np.prod(self.shape)``. Some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below: - ``0`` copy this dimension from the input to the output shape. Example:: - input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2) - input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4) - ``-1`` infers the dimension of the output shape by using the remainder of the input dimensions keeping the size of the new array same as that of the input array. At most one dimension of shape can be -1. Example:: - input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4) - input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8) - input shape = (2,3,4), shape=(-1,), output shape = (24,) - ``-2`` copy all/remainder of the input dimensions to the output shape. Example:: - input shape = (2,3,4), shape = (-2,), output shape = (2,3,4) - input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4) - input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1) - ``-3`` use the product of two consecutive dimensions of the input shape as the output dimension. Example:: - input shape = (2,3,4), shape = (-3,4), output shape = (6,4) - input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20) - input shape = (2,3,4), shape = (0,-3), output shape = (2,12) - input shape = (2,3,4), shape = (-3,-2), output shape = (6,4) - ``-4`` split one dimension of the input into two dimensions passed subsequent to -4 in shape (can contain -1). Example:: - input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4) - input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4) - If the argument `reverse` is set to 1, then the special values are inferred from right to left. Example:: - without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be \ (40,5). - with reverse=1, output shape will be (50,4). reverse : bool, default False If true then the special values are inferred from right to left. Only supported as keyword argument. Returns ------- NDArray An array with desired shape that shares data with this array. Examples -------- >>> x = mx.nd.arange(0,6).reshape(2,3) >>> x.asnumpy() array([[ 0., 1., 2.], [ 3., 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,-1) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(-3) >>> y.asnumpy() array([ 0. 1. 2. 3. 4. 5.], dtype=float32) >>> y[:] = -1 >>> x.asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32) """ if len(shape) == 1 and isinstance(shape[0], (list, tuple)): shape = shape[0] elif not shape: shape = kwargs.get('shape') assert shape, "Shape must be provided." if not all(k in ['shape', 'reverse'] for k in kwargs): raise TypeError( "Got unknown keywords in reshape: {}. " \ "Accepted keyword arguments are 'shape' and 'reverse'.".format( ', '.join([k for k in kwargs if k not in ['shape', 'reverse']]))) reverse = kwargs.get('reverse', False) handle = NDArrayHandle() # Actual reshape check_call(_LIB.MXNDArrayReshape64(self.handle, len(shape), c_array(ctypes.c_int64, shape), reverse, ctypes.byref(handle))) return NDArray(handle=handle, writable=self.writable)
[ "def", "reshape", "(", "self", ",", "*", "shape", ",", "*", "*", "kwargs", ")", ":", "if", "len", "(", "shape", ")", "==", "1", "and", "isinstance", "(", "shape", "[", "0", "]", ",", "(", "list", ",", "tuple", ")", ")", ":", "shape", "=", "shape", "[", "0", "]", "elif", "not", "shape", ":", "shape", "=", "kwargs", ".", "get", "(", "'shape'", ")", "assert", "shape", ",", "\"Shape must be provided.\"", "if", "not", "all", "(", "k", "in", "[", "'shape'", ",", "'reverse'", "]", "for", "k", "in", "kwargs", ")", ":", "raise", "TypeError", "(", "\"Got unknown keywords in reshape: {}. \"", "\"Accepted keyword arguments are 'shape' and 'reverse'.\"", ".", "format", "(", "', '", ".", "join", "(", "[", "k", "for", "k", "in", "kwargs", "if", "k", "not", "in", "[", "'shape'", ",", "'reverse'", "]", "]", ")", ")", ")", "reverse", "=", "kwargs", ".", "get", "(", "'reverse'", ",", "False", ")", "handle", "=", "NDArrayHandle", "(", ")", "# Actual reshape", "check_call", "(", "_LIB", ".", "MXNDArrayReshape64", "(", "self", ".", "handle", ",", "len", "(", "shape", ")", ",", "c_array", "(", "ctypes", ".", "c_int64", ",", "shape", ")", ",", "reverse", ",", "ctypes", ".", "byref", "(", "handle", ")", ")", ")", "return", "NDArray", "(", "handle", "=", "handle", ",", "writable", "=", "self", ".", "writable", ")" ]
Returns a **view** of this array with a new shape without altering any data. Parameters ---------- shape : tuple of int, or n ints The new shape should not change the array size, namely ``np.prod(new_shape)`` should be equal to ``np.prod(self.shape)``. Some dimensions of the shape can take special values from the set {0, -1, -2, -3, -4}. The significance of each is explained below: - ``0`` copy this dimension from the input to the output shape. Example:: - input shape = (2,3,4), shape = (4,0,2), output shape = (4,3,2) - input shape = (2,3,4), shape = (2,0,0), output shape = (2,3,4) - ``-1`` infers the dimension of the output shape by using the remainder of the input dimensions keeping the size of the new array same as that of the input array. At most one dimension of shape can be -1. Example:: - input shape = (2,3,4), shape = (6,1,-1), output shape = (6,1,4) - input shape = (2,3,4), shape = (3,-1,8), output shape = (3,1,8) - input shape = (2,3,4), shape=(-1,), output shape = (24,) - ``-2`` copy all/remainder of the input dimensions to the output shape. Example:: - input shape = (2,3,4), shape = (-2,), output shape = (2,3,4) - input shape = (2,3,4), shape = (2,-2), output shape = (2,3,4) - input shape = (2,3,4), shape = (-2,1,1), output shape = (2,3,4,1,1) - ``-3`` use the product of two consecutive dimensions of the input shape as the output dimension. Example:: - input shape = (2,3,4), shape = (-3,4), output shape = (6,4) - input shape = (2,3,4,5), shape = (-3,-3), output shape = (6,20) - input shape = (2,3,4), shape = (0,-3), output shape = (2,12) - input shape = (2,3,4), shape = (-3,-2), output shape = (6,4) - ``-4`` split one dimension of the input into two dimensions passed subsequent to -4 in shape (can contain -1). Example:: - input shape = (2,3,4), shape = (-4,1,2,-2), output shape =(1,2,3,4) - input shape = (2,3,4), shape = (2,-4,-1,3,-2), output shape = (2,1,3,4) - If the argument `reverse` is set to 1, then the special values are inferred from right to left. Example:: - without reverse=1, for input shape = (10,5,4), shape = (-1,0), output shape would be \ (40,5). - with reverse=1, output shape will be (50,4). reverse : bool, default False If true then the special values are inferred from right to left. Only supported as keyword argument. Returns ------- NDArray An array with desired shape that shares data with this array. Examples -------- >>> x = mx.nd.arange(0,6).reshape(2,3) >>> x.asnumpy() array([[ 0., 1., 2.], [ 3., 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,-1) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(3,2) >>> y.asnumpy() array([[ 0., 1.], [ 2., 3.], [ 4., 5.]], dtype=float32) >>> y = x.reshape(-3) >>> y.asnumpy() array([ 0. 1. 2. 3. 4. 5.], dtype=float32) >>> y[:] = -1 >>> x.asnumpy() array([[-1., -1., -1.], [-1., -1., -1.]], dtype=float32)
[ "Returns", "a", "**", "view", "**", "of", "this", "array", "with", "a", "new", "shape", "without", "altering", "any", "data", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L946-L1067
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray.broadcast_to
def broadcast_to(self, shape): """Broadcasts the input array to a new shape. Broadcasting is only allowed on axes with size 1. The new shape cannot change the number of dimensions. For example, you could broadcast from shape (2, 1) to (2, 3), but not from shape (2, 3) to (2, 3, 3). Parameters ---------- shape : tuple of int The shape of the desired array. Returns ------- NDArray A NDArray with the desired shape that is not sharing data with this array, even if the new shape is the same as ``self.shape``. Examples -------- >>> x = mx.nd.arange(0,3).reshape((1,3,1)) >>> x.asnumpy() array([[[ 0.], [ 1.], [ 2.]]], dtype=float32) >>> y = x.broadcast_to((2,3,3)) >>> y.asnumpy() array([[[ 0., 0., 0.], [ 1., 1., 1.], [ 2., 2., 2.]], <BLANKLINE> [[ 0., 0., 0.], [ 1., 1., 1.], [ 2., 2., 2.]]], dtype=float32) """ cur_shape = self.shape err_str = 'operands could not be broadcast together with remapped shapes' \ '[original->remapped]: {} and requested shape {}'.format(cur_shape, shape) if len(shape) < len(cur_shape): raise ValueError(err_str) cur_shape = (1,) * (len(shape) - len(cur_shape)) + cur_shape cur_shape_arr = np.array(cur_shape) broadcasting_axes = np.nonzero(cur_shape_arr != np.array(shape)) if (cur_shape_arr[broadcasting_axes] != 1).any(): raise ValueError(err_str) if cur_shape != self.shape: return op.broadcast_to(self.reshape(cur_shape), shape=shape) else: return op.broadcast_to(self, shape=tuple(shape))
python
def broadcast_to(self, shape): """Broadcasts the input array to a new shape. Broadcasting is only allowed on axes with size 1. The new shape cannot change the number of dimensions. For example, you could broadcast from shape (2, 1) to (2, 3), but not from shape (2, 3) to (2, 3, 3). Parameters ---------- shape : tuple of int The shape of the desired array. Returns ------- NDArray A NDArray with the desired shape that is not sharing data with this array, even if the new shape is the same as ``self.shape``. Examples -------- >>> x = mx.nd.arange(0,3).reshape((1,3,1)) >>> x.asnumpy() array([[[ 0.], [ 1.], [ 2.]]], dtype=float32) >>> y = x.broadcast_to((2,3,3)) >>> y.asnumpy() array([[[ 0., 0., 0.], [ 1., 1., 1.], [ 2., 2., 2.]], <BLANKLINE> [[ 0., 0., 0.], [ 1., 1., 1.], [ 2., 2., 2.]]], dtype=float32) """ cur_shape = self.shape err_str = 'operands could not be broadcast together with remapped shapes' \ '[original->remapped]: {} and requested shape {}'.format(cur_shape, shape) if len(shape) < len(cur_shape): raise ValueError(err_str) cur_shape = (1,) * (len(shape) - len(cur_shape)) + cur_shape cur_shape_arr = np.array(cur_shape) broadcasting_axes = np.nonzero(cur_shape_arr != np.array(shape)) if (cur_shape_arr[broadcasting_axes] != 1).any(): raise ValueError(err_str) if cur_shape != self.shape: return op.broadcast_to(self.reshape(cur_shape), shape=shape) else: return op.broadcast_to(self, shape=tuple(shape))
[ "def", "broadcast_to", "(", "self", ",", "shape", ")", ":", "cur_shape", "=", "self", ".", "shape", "err_str", "=", "'operands could not be broadcast together with remapped shapes'", "'[original->remapped]: {} and requested shape {}'", ".", "format", "(", "cur_shape", ",", "shape", ")", "if", "len", "(", "shape", ")", "<", "len", "(", "cur_shape", ")", ":", "raise", "ValueError", "(", "err_str", ")", "cur_shape", "=", "(", "1", ",", ")", "*", "(", "len", "(", "shape", ")", "-", "len", "(", "cur_shape", ")", ")", "+", "cur_shape", "cur_shape_arr", "=", "np", ".", "array", "(", "cur_shape", ")", "broadcasting_axes", "=", "np", ".", "nonzero", "(", "cur_shape_arr", "!=", "np", ".", "array", "(", "shape", ")", ")", "if", "(", "cur_shape_arr", "[", "broadcasting_axes", "]", "!=", "1", ")", ".", "any", "(", ")", ":", "raise", "ValueError", "(", "err_str", ")", "if", "cur_shape", "!=", "self", ".", "shape", ":", "return", "op", ".", "broadcast_to", "(", "self", ".", "reshape", "(", "cur_shape", ")", ",", "shape", "=", "shape", ")", "else", ":", "return", "op", ".", "broadcast_to", "(", "self", ",", "shape", "=", "tuple", "(", "shape", ")", ")" ]
Broadcasts the input array to a new shape. Broadcasting is only allowed on axes with size 1. The new shape cannot change the number of dimensions. For example, you could broadcast from shape (2, 1) to (2, 3), but not from shape (2, 3) to (2, 3, 3). Parameters ---------- shape : tuple of int The shape of the desired array. Returns ------- NDArray A NDArray with the desired shape that is not sharing data with this array, even if the new shape is the same as ``self.shape``. Examples -------- >>> x = mx.nd.arange(0,3).reshape((1,3,1)) >>> x.asnumpy() array([[[ 0.], [ 1.], [ 2.]]], dtype=float32) >>> y = x.broadcast_to((2,3,3)) >>> y.asnumpy() array([[[ 0., 0., 0.], [ 1., 1., 1.], [ 2., 2., 2.]], <BLANKLINE> [[ 0., 0., 0.], [ 1., 1., 1.], [ 2., 2., 2.]]], dtype=float32)
[ "Broadcasts", "the", "input", "array", "to", "a", "new", "shape", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L1710-L1759
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray.shape
def shape(self): """Tuple of array dimensions. Examples -------- >>> x = mx.nd.array([1, 2, 3, 4]) >>> x.shape (4L,) >>> y = mx.nd.zeros((2, 3, 4)) >>> y.shape (2L, 3L, 4L) """ ndim = mx_int() pdata = ctypes.POINTER(mx_int)() check_call(_LIB.MXNDArrayGetShapeEx( self.handle, ctypes.byref(ndim), ctypes.byref(pdata))) if ndim.value == -1: return None else: return tuple(pdata[:ndim.value])
python
def shape(self): """Tuple of array dimensions. Examples -------- >>> x = mx.nd.array([1, 2, 3, 4]) >>> x.shape (4L,) >>> y = mx.nd.zeros((2, 3, 4)) >>> y.shape (2L, 3L, 4L) """ ndim = mx_int() pdata = ctypes.POINTER(mx_int)() check_call(_LIB.MXNDArrayGetShapeEx( self.handle, ctypes.byref(ndim), ctypes.byref(pdata))) if ndim.value == -1: return None else: return tuple(pdata[:ndim.value])
[ "def", "shape", "(", "self", ")", ":", "ndim", "=", "mx_int", "(", ")", "pdata", "=", "ctypes", ".", "POINTER", "(", "mx_int", ")", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayGetShapeEx", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "ndim", ")", ",", "ctypes", ".", "byref", "(", "pdata", ")", ")", ")", "if", "ndim", ".", "value", "==", "-", "1", ":", "return", "None", "else", ":", "return", "tuple", "(", "pdata", "[", ":", "ndim", ".", "value", "]", ")" ]
Tuple of array dimensions. Examples -------- >>> x = mx.nd.array([1, 2, 3, 4]) >>> x.shape (4L,) >>> y = mx.nd.zeros((2, 3, 4)) >>> y.shape (2L, 3L, 4L)
[ "Tuple", "of", "array", "dimensions", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L1836-L1855
train
apache/incubator-mxnet
python/mxnet/ndarray/ndarray.py
NDArray.context
def context(self): """Device context of the array. Examples -------- >>> x = mx.nd.array([1, 2, 3, 4]) >>> x.context cpu(0) >>> type(x.context) <class 'mxnet.context.Context'> >>> y = mx.nd.zeros((2,3), mx.gpu(0)) >>> y.context gpu(0) """ dev_typeid = ctypes.c_int() dev_id = ctypes.c_int() check_call(_LIB.MXNDArrayGetContext( self.handle, ctypes.byref(dev_typeid), ctypes.byref(dev_id))) return Context(Context.devtype2str[dev_typeid.value], dev_id.value)
python
def context(self): """Device context of the array. Examples -------- >>> x = mx.nd.array([1, 2, 3, 4]) >>> x.context cpu(0) >>> type(x.context) <class 'mxnet.context.Context'> >>> y = mx.nd.zeros((2,3), mx.gpu(0)) >>> y.context gpu(0) """ dev_typeid = ctypes.c_int() dev_id = ctypes.c_int() check_call(_LIB.MXNDArrayGetContext( self.handle, ctypes.byref(dev_typeid), ctypes.byref(dev_id))) return Context(Context.devtype2str[dev_typeid.value], dev_id.value)
[ "def", "context", "(", "self", ")", ":", "dev_typeid", "=", "ctypes", ".", "c_int", "(", ")", "dev_id", "=", "ctypes", ".", "c_int", "(", ")", "check_call", "(", "_LIB", ".", "MXNDArrayGetContext", "(", "self", ".", "handle", ",", "ctypes", ".", "byref", "(", "dev_typeid", ")", ",", "ctypes", ".", "byref", "(", "dev_id", ")", ")", ")", "return", "Context", "(", "Context", ".", "devtype2str", "[", "dev_typeid", ".", "value", "]", ",", "dev_id", ".", "value", ")" ]
Device context of the array. Examples -------- >>> x = mx.nd.array([1, 2, 3, 4]) >>> x.context cpu(0) >>> type(x.context) <class 'mxnet.context.Context'> >>> y = mx.nd.zeros((2,3), mx.gpu(0)) >>> y.context gpu(0)
[ "Device", "context", "of", "the", "array", "." ]
1af29e9c060a4c7d60eeaacba32afdb9a7775ba7
https://github.com/apache/incubator-mxnet/blob/1af29e9c060a4c7d60eeaacba32afdb9a7775ba7/python/mxnet/ndarray/ndarray.py#L1879-L1897
train