megengine.functional.debug_param.
get_conv_execution_strategy
Returns the execuation strategy of Conv2d.
Conv2d
See set_conv_execution_strategy() for possible return values
set_conv_execution_strategy()
str
set_conv_execution_strategy
Sets the execuation strategy of Conv2d.
option (str) –
Decides how Conv2d algorithm is chosen. Available values:
’HEURISTIC’ uses heuristic to choose the fastest algorithm.
’PROFILE’ runs possible algorithms on real device to find the best one.
’PROFILE_HEURISTIC’ uses profiling result and heuristic to choose the fastest algorithm.
’PROFILE_REPRODUCIBLE’ uses the fastest of profiling result that is also reproducible.
’HEURISTIC_REPRODUCIBLE’ uses heuristic to choose the fastest algorithm that is also reproducible.
The default strategy is ‘HEURISTIC’.
It can also be set through the environment variable ‘MEGENGINE_CONV_EXECUTION_STRATEGY’.
megengine.functional.elemwise.
abs
Element-wise absolute value.
acos
Element-wise inverse cosine.
acosh
Element-wise inverse hyperbolic cosine.
add
Element-wise addition. At least one operand should be tensor.
Same for sub/mul/div/floor_div/pow/mod/atan2/equal/not_equal/less/less_equal/greater/greater_equal/maximum/minmium.
x – input tensor.
computed tensor.
Examples:
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) y = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) out = F.add(x, y) print(out.numpy())
Outputs:
[[ 0. 2. 4.] [ 6. 8. 10.]]
asin
Element-wise inverse sine.
asinh
Element-wise inverse hyperbolic sine.
atan
Element-wise inverse tangent.
atan2
Element-wise 2-argument arctangent.
atanh
Element-wise inverse hyperbolic tangent.
ceil
Element-wise ceiling.
clip
Clamps all elements in input tensor into the range [ lower, upper ] and returns a resulting tensor:
lower
upper
x (Tensor) – input tensor.
Tensor
lower – lower-bound of the range to be clamped to.
upper – upper-bound of the range to be clamped to.
output clamped tensor.
import numpy as np from megengine import tensor import megengine.functional as F a = tensor(np.arange(5).astype(np.int32)) print(F.clip(a, 2, 4).numpy()) print(F.clip(a, lower=3).numpy()) print(F.clip(a, upper=3).numpy())
[2 2 2 3 4] [3 3 3 3 4] [0 1 2 3 3]
cos
Element-wise cosine.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) out = F.cos(x) print(out.numpy().round(decimals=4))
[[ 1. 0.5403 -0.4161] [-0.99 -0.6536 0.2837]]
cosh
Element-wise hyperbolic cosine.
div
Element-wise (x / y).
equal
Element-wise (x == y).
x – input tensor 1.
y – input tensor 2.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) y = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) out = F.equal(x, y) print(out.numpy())
[[1. 1. 1.] [1. 1. 1.]]
exp
Element-wise exponential.
expm1
Element-wise exp(x)-1.
floor
Element-wise floor.
floor_div
Element-wise floor(x / y).
greater
Element-wise (x > y).
greater_equal
Element-wise (x >= y).
hsigmoid
Element-wise relu6(x + 3) / 6.
hswish
Element-wise x * relu6(x + 3) / 6.
Example:
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(5).astype(np.float32)) out = F.hswish(x) print(out.numpy().round(decimals=4))
[0. 0.6667 1.6667 3. 4. ]
left_shift
Element-wise bitwise binary: x << y.
x – input tensor, should be int.
y – how many bits to be left-shifted.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(0, 6, dtype=np.int32).reshape(2, 3)) out = F.left_shift(x, 2) print(out.numpy())
[[ 0 4 8] [12 16 20]]
less
Element-wise (x < y).
less_equal
Element-wise (x <= y).
log
Element-wise logarithm (base e).
log1p
Element-wise log(x+1) (base e).
logical_and
Element-wise logical and: x && y.
logical_not
Element-wise logical not: ~x.
logical_or
Element-wise logical or: x || y.
logical_xor
Element-wise logical xor: x ^ y.
maximum
Element-wise maximum of array elements.
minimum
Element-wise minimum of array elements.
mod
Element-wise remainder of division.
mul
Element-wise multiplication.
neg
Element-wise negation.
not_equal
Element-wise (x != y).
pow
Element-wise power.
relu
Element-wise max(x, 0).
relu6
Element-wise min(max(x, 0), 6).
right_shift
Element-wise bitwise binary: x >> y.
round
Element-wise rounding to int.
sigmoid
Element-wise 1 / ( 1 + exp( -x ) ).
sin
Element-wise sine.
sinh
Element-wise hyperbolic sine.
sqrt
Element-wise sqrt. Returns NaN for negative input value.
NaN
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) out = F.sqrt(x) print(out.numpy().round(decimals=4))
[[0. 1. 1.4142] [1.7321 2. 2.2361]]
square
Returns a new tensor with the square of the elements of input tensor.
inp – input tensor.
import numpy as np import megengine as mge import megengine.functional as F data = mge.tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) out = F.square(data) print(out.numpy().round(decimals=4))
[[ 0. 1. 4.] [ 9. 16. 25.]]
sub
Element-wise subtraction.
tan
Element-wise tangent.
tanh
Element-wise hyperbolic tangent.
megengine.functional.inplace.
apply
megengine.functional.loss.
binary_cross_entropy
Computes the binary cross entropy loss (using logits by default).
By default(with_logitis is True), pred is assumed to be logits, class probabilities are given by sigmoid.
with_logitis
pred
pred (Tensor) – (N, *), where * means any number of additional dimensions.
label (Tensor) – (N, *), same shape as the input.
with_logits (bool) – bool, whether to apply sigmoid first. Default: True
bool
loss value.
import numpy as np from megengine import tensor import megengine.functional as F pred = tensor(np.array([0, 0], dtype=np.float32).reshape(1, 2)) label = tensor(np.ones((1, 2), dtype=np.float32)) loss = F.nn.binary_cross_entropy(pred, label) print(loss.numpy().round(decimals=4))
0.6931
cross_entropy
Computes the multi-class cross entropy loss (using logits by default).
By default(with_logitis is True), pred is assumed to be logits, class probabilities are given by softmax.
It has better numerical stability compared with sequential calls to softmax() and cross_entropy().
softmax()
cross_entropy()
When using label smoothing, the label distribution is as follows:
where \(y^{LS}\) and \(y\) are new label distribution and origin label distribution respectively. k is the index of label distribution. \(\alpha\) is label_smooth and \(K\) is the number of classes.
label_smooth
pred (Tensor) – input tensor representing the predicted probability.
label (Tensor) – input tensor representing the classification label.
axis (int) – an axis along which softmax will be applied. Default: 1
int
with_logits (bool) – whether to apply softmax first. Default: True
label_smooth (float) – a label smoothing of parameter that can re-distribute target distribution. Default: 0
float
import numpy as np from megengine import tensor import megengine.functional as F data_shape = (1, 2) label_shape = (1, ) pred = tensor(np.array([0, 0], dtype=np.float32).reshape(data_shape)) label = tensor(np.ones(label_shape, dtype=np.int32)) loss = F.nn.cross_entropy(pred, label) print(loss.numpy().round(decimals=4))
hinge_loss
Caculates the hinge loss which is often used in SVM.
The hinge loss can be described as:
pred (Tensor) – input tensor representing the predicted probability, shape is (N, C).
label (Tensor) – input tensor representing the binary classification label, shape is (N, C).
norm (str) – specify the norm to caculate the loss, should be “L1” or “L2”.
from megengine import tensor import megengine.functional as F pred = tensor([[0.5, -0.5, 0.1], [-0.6, 0.7, 0.8]], dtype="float32") label = tensor([[1, -1, -1], [-1, 1, 1]], dtype="float32") loss = F.nn.hinge_loss(pred, label) print(loss.numpy())
1.5
l1_loss
Calculates the mean absolute error (MAE) between each element in the pred \(x\) and label \(y\).
The mean absolute error can be described as:
where
\(x\) and \(y\) are tensors of arbitrary shapes with a total of \(N\) elements each. \(N\) is the batch size.
pred (Tensor) – predicted result from model.
label (Tensor) – ground truth to compare.
import numpy as np import megengine as mge import megengine.functional as F ipt = mge.tensor(np.array([3, 3, 3, 3]).astype(np.float32)) tgt = mge.tensor(np.array([2, 8, 6, 1]).astype(np.float32)) loss = F.nn.l1_loss(ipt, tgt) print(loss.numpy())
2.75
square_loss
Calculates the mean squared error (squared L2 norm) between each element in the pred \(x\) and label \(y\).
The mean squared error can be described as:
pred: \((N, *)\) where \(*\) means any number of additional dimensions.
label: \((N, *)\). Same shape as pred.
import numpy as np import megengine as mge import megengine.functional as F ipt = mge.tensor(np.array([3, 3, 3, 3]).astype(np.float32)) tgt = mge.tensor(np.array([2, 8, 6, 1]).astype(np.float32)) loss = F.nn.square_loss(ipt, tgt) print(loss.numpy())
9.75
megengine.functional.math.
argmax
Returns the indices of the maximum values along given axis. If axis is a list of dimensions, reduce over all of them.
inp (Tensor) – input tensor.
axis (Union[int, Sequence[int], None]) – dimension to reduce. If None, all dimensions will be reduced. Default: None
Union
Sequence
None
keepdims (bool) – whether the output tensor has axis retained or not. Default: False
output tensor.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3)) out = F.argmax(x) print(out.numpy())
5
argmin
Returns the indices of the minimum values along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3)) out = F.argmin(x) print(out.numpy())
0
argsort
Returns the indices that would sort the input tensor.
inp (Tensor) – input tensor. If it’s 2d, the result would be array of indices show how to sort each row in the input tensor.
descending (bool) – sort in descending order, where the largest comes first. Default: False
indices of int32 indicates how to sort the input.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.array([1,2], dtype=np.float32)) indices = F.argsort(x) print(indices.numpy())
[0 1]
isinf
Returns a new tensor representing if each element is Inf or not.
Inf
result tensor.
from megengine import tensor import megengine.functional as F x = tensor([1, float("inf"), 0]) print(F.isinf(x).numpy())
[False True False]
isnan
Returns a new tensor representing if each element is NaN or not.
from megengine import tensor import megengine.functional as F x = tensor([1, float("nan"), 0]) print(F.isnan(x).numpy())
max
Returns the max value of the input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3)) out = F.max(x) print(out.numpy())
6
mean
Returns the mean value of input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3)) out = F.mean(x) print(out.numpy())
3.5
min
Returns the min value of input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3)) out = F.min(x) print(out.numpy())
1
norm
Calculates p-norm of input tensor along given axis.
p
ord (Optional[float]) – power of value applied to inp. Default: 2
Optional
axis (Optional[int]) – dimension to reduce. If None, input must be a vector. Default: None
keepdims – whether the output tensor has axis retained or not. Default: False
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-3, 3, dtype=np.float32)) out = F.norm(x) print(out.numpy().round(decimals=4))
4.3589
normalize
Performs \(L_p\) normalization of input tensor along given axis.
For a tensor of shape \((n_0, ..., n_{dim}, ..., n_k)\), each \(n_{dim}\) -element vector \(v\) along dimension axis is transformed as:
axis
ord (Optional[float]) – power of value applied to input tensor. Default: 2
axis (Optional[int]) – dimension to reduce.If None, input must be a vector. Default: None
eps (float) – a small value to avoid division by zero. Default: 1e-12
normalized output tensor.
prod
Returns the product of input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3)) out = F.prod(x) print(out.numpy())
720
sign
Returns a new tensor representing the sign of each element in input tensor.
input tensor.
the sign of input tensor.
from megengine import tensor import megengine.functional as F x = tensor([1, -1, 0]) print(F.sign(x).numpy())
[ 1 -1 0]
sort
Returns sorted tensor and the indices would sort the input tensor.
inp (Tensor) – input tensor. If it’s 2d, the result would be sorted by row.
Tuple[Tensor, Tensor]
Tuple
tuple of two tensors (sorted_tensor, indices_of_int32).
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.array([1,2], dtype=np.float32)) out, indices = F.sort(x) print(out.numpy())
[1. 2.]
std
Returns the standard deviation of input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F data = tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3)) out = F.std(data, axis=1) print(out.numpy().round(decimals=4))
[0.8165 0.8165]
sum
Returns the sum of input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 7, dtype=np.int32).reshape(2, 3)) out = F.sum(x) print(out.numpy())
21
topk
Selects the ``Top-K``(by default) smallest elements of 2d matrix by row.
inp (Tensor) – input tensor. If input tensor is 2d, each row will be sorted.
k (int) – number of elements needed.
descending (bool) – if True, return the largest elements instead. Default: False
kth_only (bool) – if True, only the k-th element will be returned. Default: False
no_sort (bool) – if True, the returned elements can be unordered. Default: False
tuple of two tensors (topk_tensor, indices_of_int32).
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.array([2, 4, 6, 8, 7, 5, 3, 1], dtype=np.float32)) top, indices = F.topk(x, 5) print(top.numpy(), indices.numpy())
[1. 2. 3. 4. 5.] [7 0 6 1 5]
var
Returns the variance value of input tensor along given axis. If axis is a list of dimensions, reduce over all of them.
import numpy as np from megengine import tensor import megengine.functional as F data = tensor(np.arange(1, 7, dtype=np.float32).reshape(2, 3)) out = F.var(data) print(out.numpy().round(decimals=4))
2.9167
megengine.functional.nn.
conv_bias_activation
Convolution bias with activation operation, only for inference.
inp (Tensor) – feature map of the convolution operation.
weight (Tensor) – convolution kernel.
bias (Tensor) – bias added to the result of convolution
stride (Union[int, Tuple[int, int]]) – stride of the 2D convolution operation. Default: 1
padding (Union[int, Tuple[int, int]]) – size of the paddings added to the input on both sides of its spatial dimensions. Only zero-padding is supported. Default: 0
dilation (Union[int, Tuple[int, int]]) – dilation of the 2D convolution operation. Default: 1
groups (int) – number of groups into which the input and output channels are divided, so as to perform a “grouped convolution”. When groups is not 1, in_channels and out_channels must be divisible by groups, and the shape of weight should be (groups, out_channel // groups, in_channels // groups, height, width).
groups
in_channels
out_channels
conv_mode (string or Convolution.Mode.) – supports ‘CROSS_CORRELATION’ or ‘CONVOLUTION’. Default: ‘CROSS_CORRELATION’
Convolution.Mode
dtype – support for np.dtype, Default: np.int8
np.dtype
compute_mode (string or Convolution.ComputeMode.) – when set to “DEFAULT”, no special requirements will be placed on the precision of intermediate results. When set to “FLOAT32”, “Float32” would be used for accumulator and intermediate result, but only effective when input and output are of Float16 dtype.
Convolution.ComputeMode
embedding
Applies lookup table for embedding.
inp (Tensor) – tensor with indices.
weight (Tensor) – learnable weights which embeds from.
padding_idx (Optional[int]) – should be set to None, not supported now.
max_norm (Optional[float]) – should be set to None, not supported now.
norm_type (Optional[float]) – should be set to None, not supported now.
Refer to Embedding for more information.
Embedding
interpolate
Down/up samples the input tensor to either the given size or with the given scale_factor. size can not coexist with scale_factor.
size
scale_factor
size (Union[int, Tuple[int, int], None]) – size of the output tensor. Default: None
scale_factor (Union[float, Tuple[float, float], None]) – scaling factor of the output tensor. Default: None
mode (str) – interpolation methods, acceptable values are: “BILINEAR”, “LINEAR”. Default: “BILINEAR”
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 5, dtype=np.float32).reshape(1, 1, 2, 2)) out = F.nn.interpolate(x, [4, 4], align_corners=False) print(out.numpy()) out2 = F.nn.interpolate(x, scale_factor=2.) np.testing.assert_allclose(out.numpy(), out2.numpy())
[[[[1. 1.25 1.75 2. ] [1.5 1.75 2.25 2.5 ] [2.5 2.75 3.25 3.5 ] [3. 3.25 3.75 4. ]]]]
linear
Applies a linear transformation to the input tensor.
Refer to Linear for more information.
Linear
inp (Tensor) – input tensor with shape (N, in_features).
weight (Tensor) – weight with shape (out_features, in_features).
bias (Optional[Tensor]) – bias with shape (out_features,). Default: None
nms
Performs non-maximum suppression (NMS) on the boxes according to their intersection-over-union(IoU).
boxes (Tensor) – tensor of shape (N, 4); the boxes to perform nms on; each box is expected to be in (x1, y1, x2, y2) format.
iou_thresh (float) – IoU threshold for overlapping.
scores (Tensor) – tensor of shape (N,), the score of boxes.
max_output (Optional[int]) – the maximum number of boxes to keep; it is optional if this operator is not traced otherwise it required to be specified; if it is not specified, all boxes are kept.
indices of the elements that have been kept by NMS.
import numpy as np from megengine import tensor import megengine.functional as F x = np.zeros((100,4)) np.random.seed(42) x[:,:2] = np.random.rand(100,2)*20 x[:,2:] = np.random.rand(100,2)*20 + 100 scores = tensor(np.random.rand(100)) inp = tensor(x) result = F.nn.nms(inp, scores, iou_thresh=0.7) print(result.numpy())
[75 69]
roi_align
Applies roi align on input feature.
inp (Tensor) – tensor that represents the input feature, shape is (N, C, H, W).
rois (Tensor) – (N, 5) boxes. First column is the box index. The other 4 columns are xyxy.
xyxy
output_shape (Union[int, tuple, list]) – (height, width) shape of output rois feature.
tuple
list
mode (str) – “max” or “average”, use max/average align just like max/average pooling. Default: “average”
spatial_scale (float) – scale the input boxes by this number. Default: 1.0
sample_points (Union[int, tuple, list]) – number of inputs samples to take for each output sample. 0 to take samples densely. Default: 2
aligned (bool) – wheather to align the input feature, with aligned=True, we first appropriately scale the ROI and then shift it by -0.5. Default: True
import numpy as np from megengine import tensor import megengine.functional as F np.random.seed(42) inp = tensor(np.random.randn(1, 1, 128, 128)) rois = tensor(np.random.random((4, 5))) y = F.nn.roi_align(inp, rois, (2, 2)) print(y.numpy()[0].round(decimals=4))
[[[0.175 0.175 ] [0.1359 0.1359]]]
roi_pooling
Applies roi pooling on input feature.
inp (Tensor) – tensor that represents the input feature, (N, C, H, W) images.
rois (Tensor) – (K, 5) boxes. First column is the index into N. The other 4 columns are xyxy.
output_shape (Union[int, tuple, list]) – (height, width) of output rois feature.
mode (str) – “max” or “average”, use max/average align just like max/average pooling. Default: “max”
scale (float) – scale the input boxes by this number. Default: 1.0
(K, C, output_shape[0], output_shape[1]) feature of rois.
import numpy as np from megengine import tensor import megengine.functional as F np.random.seed(42) inp = tensor(np.random.randn(1, 1, 128, 128)) rois = tensor(np.random.random((4, 5))) y = F.nn.roi_pooling(inp, rois, (2, 2)) print(y.numpy()[0].round(decimals=4))
[[[-0.1383 -0.1383] [-0.5035 -0.5035]]]
sync_batch_norm
Applies synchronized batch normalization to the input.
Refer to BatchNorm2d and BatchNorm1d for more information.
BatchNorm2d
BatchNorm1d
running_mean (Tensor) – tensor to store running mean.
running_var (Tensor) – tensor to store running variance.
weight (Optional[Tensor]) – scaling tensor in the learnable affine parameters. See \(\gamma\) in BatchNorm2d.
bias (Optional[Tensor]) – bias tensor in the learnable affine parameters. See \(\beta\) in BatchNorm2d.
training (bool) – a boolean value to indicate whether batch norm is performed in traning mode. Default: False
momentum (Union[float, Tensor]) – value used for the running_mean and running_var computation. Default: 0.9
running_mean
running_var
eps (float) – a value added to the denominator for numerical stability. Default: 1e-5
adaptive_avg_pool2d
Applies a 2D average adaptive pooling over an input.
Refer to AvgAdaptivePool2d for more information.
AvgAdaptivePool2d
oshp (Union[Tuple[int, int], int, Tensor]) – (OH, OW) size of the output shape.
adaptive_max_pool2d
Applies a 2D max adaptive pooling over an input.
Refer to MaxAdaptivePool2d for more information.
MaxAdaptivePool2d
avg_pool2d
Applies 2D average pooling over an input tensor.
Refer to AvgPool2d for more information.
AvgPool2d
kernel_size (Union[int, Tuple[int, int]]) – size of the window.
stride (Union[int, Tuple[int, int], None]) – stride of the window. If not provided, its value is set to kernel_size. Default: None
kernel_size
padding (Union[int, Tuple[int, int]]) – implicit zero padding added on both sides. Default: 0
mode (str) – whether to count padding values. Default: “AVERAGE_COUNT_EXCLUDE_PADDING”
batch_norm
Applies batch normalization to the input.
running_mean (Optional[Tensor]) – tensor to store running mean.
running_var (Optional[Tensor]) – tensor to store running variance.
training (bool) – a boolean value to indicate whether batch norm is performed in training mode. Default: False
momentum (float) – value used for the running_mean and running_var computation. Default: 0.9
inplace (bool) – whether to update running_mean and running_var inplace or return new tensors Default: True
conv1d
1D convolution operation.
Refer to Conv1d for more information.
Conv1d
inp (Tensor) – The feature map of the convolution operation
weight (Tensor) – The convolution kernel
bias (Optional[Tensor]) – The bias added to the result of convolution (if given)
stride (int) – Stride of the 1D convolution operation. Default: 1
padding (int) – Size of the paddings added to the input on both sides of its spatial dimensions. Only zero-padding is supported. Default: 0
dilation (int) – Dilation of the 1D convolution operation. Default: 1
groups (int) – number of groups to divide input and output channels into, so as to perform a “grouped convolution”. When groups is not 1, in_channels and out_channels must be divisible by groups, and the shape of weight should be (groups, out_channel // groups, in_channels // groups, height, width).
(groups, out_channel // groups, in_channels // groups, height, width)
conv_mode (string or mgb.opr_param_defs.Convolution.Mode) – Supports ‘CROSS_CORRELATION’. Default: ‘CROSS_CORRELATION’.
mgb.opr_param_defs.Convolution.Mode
compute_mode (string or mgb.opr_param_defs.Convolution.ComputeMode) – When set to ‘DEFAULT’, no special requirements will be placed on the precision of intermediate results. When set to ‘FLOAT32’, Float32 would be used for accumulator and intermediate result, but only effective when input and output are of Float16 dtype.
mgb.opr_param_defs.Convolution.ComputeMode
conv2d
2D convolution operation.
Refer to Conv2d for more information.
bias (Optional[Tensor]) – bias added to the result of convolution (if given).
groups (int) – number of groups into which the input and output channels are divided, so as to perform a grouped convolution. When groups is not 1, in_channels and out_channels must be divisible by groups, and the shape of weight should be (groups, out_channel // groups, in_channels // groups, height, width).
grouped convolution
conv_mode (string or Convolution.Mode) – supports “CROSS_CORRELATION”. Default: “CROSS_CORRELATION”
compute_mode (string or Convolution.ComputeMode) – when set to “DEFAULT”, no special requirements will be placed on the precision of intermediate results. When set to “FLOAT32”, “Float32” would be used for accumulator and intermediate result, but only effective when input and output are of Float16 dtype.
conv_transpose2d
2D transposed convolution operation.
Refer to ConvTranspose2d for more information.
ConvTranspose2d
groups (int) – number of groups into which the input and output channels are divided, so as to perform a grouped convolution. When groups is not 1, in_channels and out_channels must be divisible by groups, and the shape of weight should be (groups, out_channel // groups, in_channels // groups, height, width). Default: 1
dot
Computes dot-product of two vectors inp1 and inp2. inputs must be 1-dimensional or scalar. A scalar input is automatically broadcasted. Refer to matmul() for more general usage.
inp1
inp2
matmul()
inp1 (Tensor) – first vector.
inp2 (Tensor) – second vector.
output value.
import numpy as np from megengine import tensor import megengine.functional as F data1 = tensor(np.arange(0, 6, dtype=np.float32)) data2 = tensor(np.arange(0, 6, dtype=np.float32)) out = F.dot(data1, data2) print(out.numpy())
55.
dropout
Returns a new tensor where each of the elements are randomly set to zero with probability P = drop_prob. Optionally rescale the output tensor if training is True.
drop_prob
training
drop_prob (float) – probability to drop (set to zero) a single element.
training (bool) – the default behavior of dropout during training is to rescale the output, then it can be replaced by an Identity during inference. Default: True
Identity
the output tensor
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.ones(10, dtype=np.float32)) out = F.dropout(x, 1./3.) print(out.numpy())
[1.5 1.5 0. 1.5 1.5 1.5 1.5 1.5 1.5 1.5]
indexing_one_hot
One-hot indexing for some axes.
src (Tensor) – input tensor.
index (Tensor) – index tensor.
axis (int) – axis on src for which values in index index. Default: 1
keepdims – whether not to remove the axis in result. Default: False
import megengine.functional as F from megengine import tensor src = tensor([[1.0, 2.0]]) index = tensor([0]) val = F.indexing_one_hot(src, index) print(val.numpy())
[1.]
leaky_relu
Applies the element-wise leaky_relu function
Refer to LeakyReLU for more information.
LeakyReLU
local_conv2d
Applies spatial 2D convolution over an groupped channeled image with untied kernels.
logsigmoid
Applies the element-wise function:
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-5, 5, dtype=np.float32)) y = F.logsigmoid(x) print(y.numpy().round(decimals=4))
[-5.0067 -4.0182 -3.0486 -2.1269 -1.3133 -0.6931 -0.3133 -0.1269 -0.0486 -0.0181]
logsoftmax
Applies the \(\log(\text{softmax}(x))\) function to an n-dimensional input tensor. The \(\text{logsoftmax}(x)\) formulation can be simplified as:
For numerical stability the implementation follows this transformation:
axis (Union[int, Sequence[int]]) – axis along which \(\text{logsoftmax}(x)\) will be applied.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-5, 5, dtype=np.float32)).reshape(2,5) y = F.logsoftmax(x, axis=1) print(y.numpy().round(decimals=4))
[[-4.4519 -3.4519 -2.4519 -1.4519 -0.4519] [-4.4519 -3.4519 -2.4519 -1.4519 -0.4519]]
logsumexp
Calculates the logarithm of the inputs’ exponential sum along the given axis.
For numerical stability, the implementation follows this transformation:
axis (Union[int, Sequence[int]]) – axis over which the sum is taken. It could be single axis or list of axes.
keepdims (bool) – whether to retain axis or not for the output tensor.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-5, 5, dtype=np.float32)).reshape(2,5) y = F.logsumexp(x, axis=1, keepdims=False) print(y.numpy().round(decimals=4))
[-0.5481 4.4519]
matmul
Performs a matrix multiplication of the matrices inp1 and inp2.
With different inputs dim, this function behaves differently:
Both 1-D tensor, simply forward to dot.
Both 2-D tensor, normal matrix multiplication.
If one input tensor is 1-D, matrix vector multiplication.
If at least one tensor are 3-dimensional or >3-dimensional, the other tensor should have dim >= 2, the batched matrix-matrix is returned, and the tensor with smaller dimension will be broadcasted. For example:
inp1: (n, k, m), inp2: (n, m, p), return: (n, k, p) inp1: (n, k, m), inp2: (m, p), return: (n, k, p) inp1: (n, j, k, m), inp2: (n, j, m, p), return: (n, j, k, p)
inp1: (n, k, m), inp2: (n, m, p), return: (n, k, p)
inp1: (n, k, m), inp2: (m, p), return: (n, k, p)
inp1: (n, j, k, m), inp2: (n, j, m, p), return: (n, j, k, p)
inp1 (Tensor) – first matrix to be multiplied.
inp2 (Tensor) – second matrix to be multiplied.
import numpy as np from megengine import tensor import megengine.functional as F data1 = tensor(np.arange(0, 6, dtype=np.float32).reshape(2, 3)) data2 = tensor(np.arange(0, 6, dtype=np.float32).reshape(3, 2)) out = F.matmul(data1, data2) print(out.numpy())
[[10. 13.] [28. 40.]]
max_pool2d
Applies a 2D max pooling over an input tensor.
Refer to MaxPool2d for more information.
MaxPool2d
one_hot
Performs one-hot encoding for the input tensor.
num_classes (int) – number of classes denotes the last dimension of the output tensor.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(1, 4, dtype=np.int32)) out = F.one_hot(x, num_classes=4) print(out.numpy())
[[0 1 0 0] [0 0 1 0] [0 0 0 1]]
prelu
Applies the element-wise PReLU function.
Refer to PReLU for more information.
PReLU
remap
Applies remap transformation to batched 2D images.
The input images are transformed to the output images by the tensor map_xy. The output’s H and W are same as map_xy’s H and W.
inp (Tensor) – input image
map_xy (Tensor) – (batch, oh, ow, 2) transformation matrix
border_mode (str) – pixel extrapolation method. Default: “REPLICATE”. Currently also support “CONSTANT”, “REFLECT”, “REFLECT_101”, “WRAP”.
scalar (float) – value used in case of a constant border. Default: 0
interp_mode (str) – interpolation methods. Default: “LINEAR”. Currently only support “LINEAR” mode.
import numpy as np from megengine import tensor import megengine.functional as F inp_shape = (1, 1, 4, 4) inp = tensor(np.arange(16, dtype=np.float32).reshape(inp_shape)) map_xy_shape = (1, 2, 2, 2) map_xy = tensor(np.array([[[1., 0.],[0., 1.]], [[0., 1.],[0., 1.]]], dtype=np.float32).reshape(map_xy_shape)) out = F.remap(inp, map_xy) print(out.numpy())
[[[[1. 4.] [4. 4.]]]]
softmax
Applies a \(\text{softmax}(x)\) function. \(\text{softmax}(x)\) is defined as:
It is applied to all elements along axis, and rescales elements so that they stay in the range [0, 1] and sum to 1.
See Softmax for more details.
Softmax
axis (Optional[int]) – an axis along which \(\text{softmax}(x)\) will be applied. By default, \(\text{softmax}(x)\) will apply along the highest ranked axis.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-5, 5, dtype=np.float32)).reshape(2,5) out = F.softmax(x) print(out.numpy().round(decimals=4))
[[0.0117 0.0317 0.0861 0.2341 0.6364] [0.0117 0.0317 0.0861 0.2341 0.6364]]
softplus
softplus is a smooth approximation to the ReLU function and can be used to constrain the output to be always positive. For numerical stability the implementation follows this transformation:
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(-3, 3, dtype=np.float32)) y = F.softplus(x) print(y.numpy().round(decimals=4))
[0.0486 0.1269 0.3133 0.6931 1.3133 2.1269]
svd
Computes the singular value decompositions of input matrix.
inp (Tensor) – input matrix, must has shape […, M, N].
output matrices, (U, sigma, V).
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(0, 6, dtype=np.float32).reshape(2,3)) _, y, _ = F.svd(x) print(y.numpy().round(decimals=3))
[7.348 1. ]
warp_perspective
Applies perspective transformation to batched 2D images.
The input images are transformed to the output images by the transformation matrix:
inp (Tensor) – input image.
M (Tensor) – (batch, 3, 3) transformation matrix.
dsize (Union[Tuple[int, int], int, Tensor]) – (h, w) size of the output image.
border_val (float) – value used in case of a constant border. Default: 0
Note:
The transformation matrix is the inverse of that used by cv2.warpPerspective.
import numpy as np from megengine import tensor import megengine.functional as F inp_shape = (1, 1, 4, 4) x = tensor(np.arange(16, dtype=np.float32).reshape(inp_shape)) M_shape = (1, 3, 3) # M defines a translation: dst(1, 1, h, w) = rst(1, 1, h+1, w+1) M = tensor(np.array([[1., 0., 1.], [0., 1., 1.], [0., 0., 1.]], dtype=np.float32).reshape(M_shape)) out = F.warp_perspective(x, M, (2, 2)) print(out.numpy())
[[[[ 5. 6.] [ 9. 10.]]]]
megengine.functional.quantized.
batch_conv_bias_activation
Batch convolution bias with activation operation, only for inference.
weight (Tensor) – convolution kernel in batched way.
megengine.functional.tensor.
arange
Returns a tensor with values from start to stop with adjacent interval step.
start (Union[int, float, Tensor]) – starting value of the squence, shoule be scalar.
stop (Union[int, float, Tensor, None]) – ending value of the squence, shoule be scalar.
step (Union[int, float, Tensor]) – gap between each pair of adjacent values. Default: 1
dtype – result data type.
generated tensor.
import numpy as np import megengine.functional as F a = F.arange(5) print(a.numpy())
[0. 1. 2. 3. 4.]
broadcast_to
Broadcasts a tensor to given shape.
shape (Union[int, Iterable[int]]) – target shape.
Iterable
import numpy as np from megengine import tensor import megengine.functional as F data = tensor(np.arange(0, 3, dtype=np.float32).reshape(3)) out = F.broadcast_to(data, (2, 3)) print(out.numpy())
[[0. 1. 2.] [0. 1. 2.]]
concat
Concat some tensors
inps (Iterable[Tensor]) – input tensors to concat.
axis (int) – over which dimension the tensors are concatenated. Default: 0
device – which device output will be. Default: None
import numpy as np from megengine import tensor import megengine.functional as F data1 = tensor(np.arange(0, 6, dtype=np.float32).reshape((2, 3))) data2 = tensor(np.arange(6, 12, dtype=np.float32).reshape((2, 3))) out = F.concat([data1, data2]) print(out.numpy())
[[ 0. 1. 2.] [ 3. 4. 5.] [ 6. 7. 8.] [ 9. 10. 11.]]
cond_take
Takes elements from data if specific condition is satisfied on mask. This operator has two outputs: the first is the elements taken, and the second is the indices corresponding to those elements; they are both 1-dimensional. High-dimension input would first be flattened.
mask (Tensor) – condition param; must be the same shape with data.
x (Tensor) – input tensor from which to take elements.
import numpy as np from megengine import tensor import megengine.functional as F mask = tensor(np.array([[True, False], [False, True]], dtype=np.bool_)) x = tensor(np.array([[1, np.inf], [np.nan, 4]], dtype=np.float32)) v, index = F.cond_take(mask, x) print(v.numpy(), index.numpy())
[1. 4.] [0 3]
expand_dims
Adds dimension before given axis.
axis (Union[int, Sequence[int]]) – place of new axes.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor([1, 2]) out = F.expand_dims(x, 0) print(out.numpy().shape)
(1, 2)
eye
Returns a 2D tensor with ones on the diagonal and zeros elsewhere.
shape – expected shape of output tensor.
dtype – data type. Default: None
device (Optional[CompNode]) – compute node of the matrix. Default: None
CompNode
eye matrix.
import numpy as np import megengine.functional as F out = F.eye(4, 6, dtype=np.float32) print(out.numpy())
[[1. 0. 0. 0. 0. 0.] [0. 1. 0. 0. 0. 0.] [0. 0. 1. 0. 0. 0.] [0. 0. 0. 1. 0. 0.]]
flatten
Reshapes the tensor by flattening the sub-tensor from dimension start_axis to dimension end_axis.
start_axis
end_axis
start_axis (int) – start dimension that the sub-tensor to be flattened. Default: 0
end_axis (int) – end dimension that the sub-tensor to be flattened. Default: -1
import numpy as np from megengine import tensor import megengine.functional as F inp_shape = (2, 2, 3, 3) x = tensor( np.arange(36, dtype=np.int32).reshape(inp_shape), ) out = F.flatten(x, 2) print(x.numpy().shape) print(out.numpy().shape)
(2, 2, 3, 3) (2, 2, 9)
full
Returns a tensor with given shape and value.
full_like
Returns a tensor filled with given value with the same shape as input tensor.
gather
Gathers data from input tensor on axis using index.
For a 3-D tensor, the output is specified by:
out[i][j][k] = inp[index[i][j][k]][j][k] # if axis == 0 out[i][j][k] = inp[i][index[i][j][k]][k] # if axis == 1 out[i][j][k] = inp[i][j][index[i][j][k]] # if axis == 2
if input tensor is a n-dimensional tensor with size \((x_0,x_1,...,x_{i-1},x_i,x_{i+1},...,x_{n-1})\) and axis=i, then index must be a n-dimensional tensor with size \((x_0,x_1,...,x_{i-1},y,x_{i+1},...,x_{n-1})\) where \(y\ge 1\) and output will have the same size as index.
axis (int) – along which axis to index.
index (Tensor) – indices of elements to gather.
import megengine.functional as F from megengine import tensor inp = tensor([ [1,2], [3,4], [5,6], ]) index = tensor([[0,2], [1,0]]) oup = F.gather(inp, 0, index) print(oup.numpy())
[[1 6] [3 2]]
linspace
Returns equally spaced numbers over a specified interval.
stop (Union[int, float, Tensor]) – last value of the squence, shoule be scalar.
num (Union[int, Tensor]) – number of values to generate.
import numpy as np import megengine.functional as F a = F.linspace(3,10,5) print(a.numpy())
[ 3. 4.75 6.5 8.25 10. ]
ones
Returns a ones tensor with given shape.
output zero tensor.
import megengine.functional as F out = F.ones((2, 1)) print(out.numpy())
[[1.] [1.]]
ones_like
Returns a ones tensor with the same shape as input tensor.
reshape
Reshapes a tensor to given target shape; total number of logical elements must remain unchanged
target_shape (Iterable[int]) – target shape, it can contain an element of -1 representing unspec_axis.
unspec_axis
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.arange(12, dtype=np.int32)) out = F.reshape(x, (3, 4)) print(out.numpy())
[[ 0 1 2 3] [ 4 5 6 7] [ 8 9 10 11]]
scatter
Writes all values from the tensor source into input tensor at the indices specified in the index tensor.
For each value in source, its output index is specified by its index in source for axis != dimension and by the corresponding value in index for axis = dimension.
axis != dimension
axis = dimension
For a 3-D tensor, input tensor is updated as:
inp[index[i][j][k]][j][k] = source[i][j][k] # if axis == 0 inp[i][index[i][j][k]][k] = source[i][j][k] # if axis == 1 inp[i][j][index[i][j][k]] = source[i][j][k] # if axis == 2
inp, index and source should have same number of dimensions.
inp
index
source
It is also required that source.shape(d) <= inp.shape(d) and index.shape(d) == source.shape(d) for all dimensions d.
source.shape(d) <= inp.shape(d)
index.shape(d) == source.shape(d)
d
Moreover, the values of index must be between 0 and inp.shape(axis) - 1 inclusive.
inp.shape(axis) - 1
Note
Please notice that, due to performance issues, the result is uncertain on the GPU device if scattering different positions from source to the same destination position regard to index tensor.
Check the following examples, the oup[0][2] is maybe from source[0][2] which value is 0.2256 or source[1][2] which value is 0.5339 if set the index[1][2] from 1 to 0.
inp (Tensor) – inp tensor which to be scattered.
axis (int) – axis along which to index.
index (Tensor) – indices of elements to scatter.
source (Tensor) – source element(s) to scatter.
import numpy as np import megengine.functional as F from megengine import tensor inp = tensor(np.zeros(shape=(3,5),dtype=np.float32)) source = tensor([[0.9935,0.9465,0.2256,0.8926,0.4396],[0.7723,0.0718,0.5939,0.357,0.4576]]) index = tensor([[0,2,0,2,1],[2,0,1,1,2]]) oup = F.scatter(inp, 0, index,source) print(oup.numpy())
[[0.9935 0.0718 0.2256 0. 0. ] [0. 0. 0.5939 0.357 0.4396] [0.7723 0.9465 0. 0.8926 0.4576]]
split
Splits the input tensor into several smaller tensors. When nsplits_or_sections is int, the last tensor may be smaller than others.
nsplits_or_sections – number of sub tensors or sections information list.
axis – which axis will be splited.
output tensor list.
import os import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.random.random((10, 20)), dtype=np.float32) y = F.split(x, 3) z = F.split(x, [6, 17], axis=1) if os.environ.get("MEGENGINE_USE_SYMBOLIC_SHAPE"): print([tuple(i.shape.numpy().tolist()) for i in y]) print([tuple(i.shape.numpy().tolist()) for i in z]) else: print([i.shape for i in y]) print([i.shape for i in z])
[(4, 20), (3, 20), (3, 20)] [(10, 6), (10, 11), (10, 3)]
squeeze
Removes dimension of shape 1.
axis (Union[int, Sequence[int], None]) – place of axis to be removed.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.array([1, 2], dtype=np.int32).reshape(1, 1, 2, 1)) out = F.squeeze(x, 3) print(out.numpy().shape)
(1, 1, 2)
stack
Concats a sequence of tensors along a new axis. The input tensors must have the same shape.
inps – input tensors.
axis – which axis will be concatenated.
device – the device output will be. Default: None
output concatenated tensor.
import numpy as np from megengine import tensor import megengine.functional as F x1 = tensor(np.arange(0, 3, dtype=np.float32).reshape((3))) x2 = tensor(np.arange(6, 9, dtype=np.float32).reshape((3))) out = F.stack([x1, x2], axis=0) print(out.numpy())
[[0. 1. 2.] [6. 7. 8.]]
transpose
Swaps shapes and strides according to given pattern.
pattern (Iterable[int]) – a list of integers including 0, 1, … , ndim-1,
ndim
and any number of 'x' char in dimensions where this tensor should be broadcasted. For examples:
'x'
('x') -> make a 0d (scalar) into a 1d vector (0, 1) -> identity for 2d vectors (1, 0) -> inverts the first and second dimensions ('x', 0) -> make a row out of a 1d vector (N to 1xN) (0, 'x') -> make a column out of a 1d vector (N to Nx1) (2, 0, 1) -> AxBxC to CxAxB (0, 'x', 1) -> AxB to Ax1xB (1, 'x', 0) -> AxB to Bx1xA (1,) -> this removes dimensions 0. It must be a broadcastable dimension (1xA to A)
('x') -> make a 0d (scalar) into a 1d vector
(0, 1) -> identity for 2d vectors
(1, 0) -> inverts the first and second dimensions
('x', 0) -> make a row out of a 1d vector (N to 1xN)
(0, 'x') -> make a column out of a 1d vector (N to Nx1)
(2, 0, 1) -> AxBxC to CxAxB
(0, 'x', 1) -> AxB to Ax1xB
(1, 'x', 0) -> AxB to Bx1xA
(1,) -> this removes dimensions 0. It must be a broadcastable dimension (1xA to A)
import numpy as np from megengine import tensor import megengine.functional as F x = tensor(np.array([[1, 1], [0, 0]], dtype=np.int32)) out = F.transpose(x, (1, 0)) print(out.numpy())
[[1 0] [1 0]]
Selects elements either from Tensor x or Tensor y, according to mask.
mask (Tensor) – a mask used for choosing x or y.
x
y
x (Tensor) – first choice.
y (Tensor) – second choice.
from megengine import tensor import megengine.functional as F mask = tensor(np.array([[True, False], [False, True]], dtype=np.bool)) x = tensor(np.array([[1, np.inf], [np.nan, 4]], dtype=np.float32)) y = tensor(np.array([[5, 6], [7, 8]], dtype=np.float32)) out = F.where(mask, x, y) print(out.numpy())
[[1. 6.] [7. 4.]]
zeros
Returns a zero tensor with given shape.
zeros_like
Returns a zero tensor with the same shape as input tensor.
import numpy as np from megengine import tensor import megengine.functional as F inp = tensor(np.arange(1, 7, dtype=np.int32).reshape(2,3)) out = F.zeros_like(inp) print(out.numpy())
[[0 0 0] [0 0 0]]
megengine.functional.types.
get_ndtuple
Converts possibly 1D tuple to n-dim tuple.
value – value will be filled in generated tuple.
n – how many elements will the tuple have.
allow_zero (bool) – whether to allow zero tuple value.
a tuple.
megengine.functional.utils.
copy
Copies tensor to another device.
device – destination device.
import numpy as np from megengine import tensor import megengine.functional as F x = tensor([1, 2, 3], np.int32) y = F.copy(x, "xpu1") print(y.numpy())
[1 2 3]
topk_accuracy
Calculates the classification accuracy given predicted logits and ground-truth labels.
logits (Tensor) – model predictions of shape [batch_size, num_classes], representing the probability (likelyhood) of each class.
target (Tensor) – ground-truth labels, 1d tensor of int32.
topk (Union[int, Iterable[int]]) – specifies the topk values, could be an int or tuple of ints. Default: 1
Union[Tensor, Iterable[Tensor]]
tensor(s) of classification accuracy between 0.0 and 1.0.
import numpy as np from megengine import tensor import megengine.functional as F logits = tensor(np.arange(80, dtype=np.int32).reshape(8,10)) target = tensor(np.arange(8, dtype=np.int32)) top1, top5 = F.topk_accuracy(logits, target, (1, 5)) print(top1.numpy(), top5.numpy())
0.0 0.375