megengine.functional#
import megengine.functional as F
Note
顾名思义,megengine.functional
模块中包含着所有与 Tensor 有关的计算接口:
与神经网络(Neural Network)相关的算子统一封装在
megengine.functional.nn
中;分布式算子统一封装在
megengine.functional.distributed
中,方便调用;其它的常见算子均可在
megengine.functional
中直接调用;
See also
用户指南中对于 Use Functional operations and calculations 有另外一套分类逻辑,可作为参考。
General tensor operations#
Note
该部分的 API 设计接纳了 Python 数据 API 标准联盟 中的倡导, 尽可能地向 NumPy API 靠拢。
Creation Functions#
See also
Returns evenly spaced values within the half-open interval |
|
Returns evenly spaced numbers over a specified interval. |
|
Returns a two-dimensional tensor with ones on the diagonal and zeros elsewhere. |
|
Extract a diagonal or construct a diagonal tensor. |
|
Returns a new tensor having a specified shape and filled with zeros. |
|
Returns a tensor filled with zeros with the same shape and data type as input tensor. |
|
Returns a new tensor having a specified shape and filled with ones. |
|
Returns a tensor filled with ones with the same shape and data type as input tensor. |
|
Returns a new tensor having a specified shape and filled with given value. |
|
Returns a tensor filled with given value with the same shape as input tensor. |
Manipulation Functions#
See also
Copies tensor to another device. |
|
Reshapes a tensor without changing its data. |
|
Reshapes the tensor by flattening the sub-tensor from dimension |
|
Swaps shapes and strides according to given pattern. |
|
Interchange two axes of a tensor. |
|
Broadcasts a tensor to given shape. |
|
Adds dimension before given axis. |
|
Removes dimension of shape 1. |
|
Concat some tensors |
|
Concats a sequence of tensors along a new axis. |
|
Splits the input tensor into several smaller tensors. |
|
Construct an array by repeating |
|
Repeat elements of an array. |
|
Roll the tensor along the given axis(or axes). |
Arithmetic operations#
Element-wise addition. |
|
Element-wise subtraction. |
|
Element-wise multiplication. |
|
Element-wise division. |
|
Element-wise floor division. |
|
Element-wise negation. |
|
Element-wise \(\operatorname{abs}(x)\) function. |
|
Element-wise sign. |
|
Element-wise power. |
|
Element-wise \(\operatorname{mod}(x, y)\) function. |
|
Element-wise \(\operatorname{sqrt}(x)\) function. |
|
Element-wise \(x^2\) function. |
|
Element-wise maximum of tensor elements. |
|
Element-wise minimum of tensor elements. |
|
Element-wise \(\operatorname{round}(x)\) function. |
|
Element-wise \(\lceil x \rceil\) function. |
|
Element-wise \(\lfloor x \rfloor\) function. |
|
Element-wise clipping function. |
|
Element-wise \(e^x\) function. |
|
Element-wise \(e^x - 1\) function. |
|
Element-wise \(\operatorname{log}(x)\) function. |
|
Element-wise \(\log(1 + x)\) function. |
|
Element-wise \(\log(e^x + e^y)\) function. |
Trigonometric functions#
Element-wise \(\sin(x)\) function. |
|
Element-wise \(\cos(x)\) function. |
|
Element-wise \(\tan(x)\) function. |
|
Element-wise \(\arcsin(x)\) function. |
|
Element-wise \(\arccos(x)\) function. |
|
Element-wise \(\arctan(x)\) function. |
|
Element-wise \(\arctan(\frac{y}{x})\) function. |
Hyperbolic functions#
Element-wise \(\sinh(x)\) function. |
|
Element-wise \(\cosh(x)\) function. |
|
Element-wise \(\tanh(x)\) function. |
|
Element-wise \(\cosh^{-1}(x)\) function. |
|
Element-wise \(\sinh^{-1}(x)\) function. |
|
Element-wise \(\tanh^{-1}(x)\) function. |
Bit operations#
Element-wise left shift. |
|
Element-wise right shift. |
Logic functions#
Element-wise |
|
Element-wise |
|
Element-wise logical AND. |
|
Element-wise logical NOT. |
|
Element-wise logical OR. |
|
Element-wise logical XOR. |
|
Element-wise greater-than comparison. |
|
Element-wise greater-than-or-equal-to comparison. |
|
Element-wise less-than comparison. |
|
Element-wise less-than-or-equal-to comparison. |
|
Element-wise equality comparison. |
|
Element-wise inequality comparison. |
Statistical Functions#
Calculates the sum of tensor elements over a given axis (or axes). |
|
Calculates the cumulative sum of tensor elements over a given axis. |
|
Calculates the product of tensor elements over a given axis (or axes). |
|
Calculates the mean of tensor elements over a given axis (or axes). |
|
Calculates the minimum of tensor elements over a given axis (or axes). |
|
Calculates the maximum of tensor elements over a given axis (or axes). |
|
Calculates the variance of tensor elements over a given axis (or axes). |
|
Calculates the standard deviation of tensor elements over a given axis (or axes). |
|
Calculates the norm of tensor elements over a given axis. |
|
Performs \(L_p\) normalization of input tensor along given axis. |
See also
想要返回 min
, max
的索引而不是元素值,请参考 Searching Functions
Linear Algebra Functions#
Computes dot-product of two vectors |
|
Computes the inverse of a batch of matrices; input must has shape [..., n, n]. |
|
Performs a matrix multiplication of the matrices |
|
Computes the singular value decomposition of a matrix (or a stack of matrices) |
Indexing Functions#
Gathers data from input tensor on axis using index. |
|
Writes all values from the tensor source into input tensor at the indices specified in the index tensor. |
|
Takes elements from data if specific condition is satisfied on mask. |
|
Selects elements either from Tensor x or Tensor y, according to mask. |
Searching Functions#
Returns the indices of the minimum values along given axis. |
|
Returns the indices of the maximum values along given axis. |
Sorting Functions#
Returns sorted tensor and the indices would sort the input tensor. |
|
Returns the indices that would sort the input tensor. |
|
Selects the |
Metric functions#
Calculates the classification accuracy given predicted logits and ground-truth labels. |
Neural network operations#
Convolution functions#
1D convolution operation. |
|
2D convolution operation. |
|
3D convolution operation. |
|
Applies a spatial convolution with untied kernels over an groupped channeled input 4D tensor. |
|
2D transposed convolution operation. |
|
3D transposed convolution operation. |
|
Deformable Convolution. |
|
Extracts sliding local blocks from a batched input tensor. |
|
Sum over the sliding windows on the corresponding input location. |
Pooling functions#
Applies 2D average pooling over an input tensor. |
|
Applies a 2D max pooling over an input tensor. |
|
Applies a 2D average adaptive pooling over an input. |
|
Applies a 2D max adaptive pooling over an input. |
|
Deformable PSROI(Position Sensitive Region of Interest) Pooling. |
Non-linear activation functions#
Element-wise 1 / ( 1 + exp( -x ) ). |
|
Element-wise relu6(x + 3) / 6. |
|
Element-wise x * relu6(x + 3) / 6. |
|
Element-wise max(x, 0). |
|
Element-wise min(max(x, 0), 6). |
|
Element-wise max(x, 0) + y * min(x, 0). |
|
Element-wise LeakyReLU function |
|
Applies the element-wise Sigmoid Linear Unit function, i.e. x * sigmoid(x). |
|
Applies the element-wise function: |
|
Applies the element-wise function: |
|
Applies a \(\text{softmax}(x)\) function. |
|
Applies the \(\log(\text{softmax}(x))\) function to an n-dimensional input tensor. |
|
Applies the element-wise function: |
|
Calculates the logarithm of the inputs' exponential sum along the given |
Normalization functions#
Applies batch normalization to the input. |
|
Applies synchronized batch normalization to the input. |
|
Applies layer normalization to the input. |
|
Apply local response normalization to the input tensor. |
Linear functions#
Applies a linear transformation to the input tensor. |
multi-head attention functions#
Dropout functions#
Returns a new tensor where each of the elements are randomly set to zero with probability P = |
Sparse functions#
Performs one-hot encoding for the input tensor. |
|
One-hot indexing for some axes. |
|
Applies lookup table for embedding. |
Loss functions#
Calculates the mean absolute error (MAE) between each element in the pred \(x\) and label \(y\). |
|
Calculates the mean squared error (squared L2 norm) between each element in the pred \(x\) and label \(y\). |
|
Caculates the hinge loss which is often used in SVM. |
|
Computes the binary cross entropy loss (using logits by default). |
|
Computes the multi-class cross entropy loss (using logits by default). |
|
The Connectionist Temporal Classification loss. |
Vision functions#
Convert images from one format to another |
|
Pads the input tensor. |
|
Rearranges elements in a tensor of shape (..., C * r^2, H, W) to a tensor of shape (..., C, H * r, W * r), where r is an upscale factor, where ... is zero or more batch dimensions. |
|
Down/up samples the input tensor to either the given size or with the given scale_factor. |
|
Applies remap transformation to batched 2D images. |
|
Batched affine transformation on 2D images. |
|
Applies perspective transformation to batched 2D images. |
|
Applies RoI (Region of Interest) pooling on input feature, as described in Faster RCNN. |
|
Applies RoI (Region of Interest) align on input feature, as described in Mask R-CNN. |
|
Performs non-maximum suppression (NMS) on the boxes according to their intersection-over-union(IoU). |
|
Applies correlation to inputs. |
|
Implements NVIDIA Optical Flow SDK. |
Quantized functions#
Convolution bias with activation operation, only for inference. |
|
Batch convolution bias with activation operation, only for inference. |
|
|
Distributed functions#
Gather tensors across the specified group and concat them at first dimension. |
|
Reduce tensors with max operation on each value across the specified group. |
|
Reduce tensors with min operation on each value across the specified group. |
|
Reduce tensors with sum operation on each value across the specified group. |
|
Each process scatter input tensor to all processes and return gathered tensor. |
|
Broadcast tensor data from root process to others. |
|
Reduce tensors across the specified group by sum and split them at first dimension. |
|
Helper function for applying collective communication functions. |
|
Send tensor to another process. |
|
Receive a tensor from another process. |
|
Reduce tensor data across the specified group by sum. |
|
Gather tensors across the specified group. |
|
Split tensor in root process at first dimension. |
External functions#
Load a serialized extern opr subgraph and fake execute the operator. |
|
Load a serialized Cambricon model as a runtime operator in MegEngine. |
|
Load a serialized Atlas model as a runtime operator in MegEngine. |
|
Load a serialized MagicMind model as a runtime operator in MegEngine. |