megengine.functional¶
import megengine.functional as F
注解
顾名思义,megengine.functional
模块中包含着所有与 Tensor 有关的计算接口:
与神经网络(Neural Network)相关的算子统一封装在
megengine.functional.nn
中;分布式算子统一封装在
megengine.functional.distributed
中,方便调用;其它的常见算子均可在
megengine.functional
中直接调用;
参见
用户指南中对于 使用 Functional 操作与计算 有另外一套分类逻辑,可作为参考。
全局tensor操作¶
注解
该部分的 API 设计接纳了 Python 数据 API 标准联盟 中的倡导, 尽可能地向 NumPy API 靠拢。
Creation Functions¶
返回半开区间 [start, stop) 内均匀间隔的值组成的1维Tensor。 |
|
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. |
|
返回一个特定形状的元素全为 0 的张量。 |
|
返回一个跟输入tensor拥有相同shape和dtype的全零tensor。 |
|
返回一个新创建的拥有指定shape且被1填充的tensor。 |
|
使用输入 Tensor 来填充输出 Tensor,保持相同的形状和数据类型。 |
|
Returns a new tensor having a specified shape and filled with given value. |
|
返回一个与输入张量形状相同的且值为给定值的零张量 |
Manipulation Functions¶
把张量复制到另一个设备上。 |
|
在不改变数据的情况下更改 Tensor 的形状。 |
|
通过将子张量从 |
|
根据给定模板交换形状和步长。 |
|
交换一个 tensor 的两个坐标轴。 |
|
将张量广播至指定的形状。 |
|
在给定轴之前添加维度。 |
|
移除形状中下标为1的维度。 |
|
拼接一些张量 |
|
沿新轴对张量进行拼接。 |
|
将输入张量拆分成更小的张量。 |
|
通过重复 |
|
重复数组的元素。 |
|
沿给定轴axis循环滚动张量 |
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. |
|
返回在给定轴 |
参见
想要返回 min
, max
的索引而不是元素值,请参考 Searching Functions
Linear Algebra Functions¶
计算两个向量 |
|
计算一批矩阵的逆;输入必须满足 [...,n,n] 的形状。 |
|
对矩阵 |
|
计算矩阵(或一组矩阵) |
Indexing Functions¶
使用 |
|
把 |
|
如果在满足特定条件,则从数据中获取元素。 |
|
根据 mask 条件选择张量 |
Searching Functions¶
返回沿给定轴的最小值的索引。 |
|
返回沿给定轴的最大值的索引。 |
Sorting Functions¶
返回经过排序后的张量。 |
|
返回经过排序后的张量。 |
|
按行排序,选出二维矩阵中 Top-K (默认情况下)个最小元素。 |
Metric functions¶
根据给定的预测的logits和真实值标签计算分类准确率。 |
神经网络运算¶
卷积运算¶
一维卷积运算。 |
|
二维卷积运算。 |
|
三维卷积运算。 |
|
对4D输入tensor进行通道分组并应用非统一kernel的空间卷积操作。 |
|
二维转置卷积运算。 |
|
3D transposed convolution operation. |
|
可变形卷积 |
|
从多batch输入tensor中提取滑窗大小的局部分块。 |
|
对滑动窗口对应的输入数据求和。 |
池化函数¶
对输入进行二维平均池化。 |
|
对输入张量进行二维最大池化。 |
|
对输入进行二维平均池化。 |
|
对输入数据进行2D最大池化。 |
|
可变形的位置敏感的感兴趣区域池化 |
非线性激活函数¶
逐元素计算 1 / ( 1 + exp( -x ) ). |
|
逐元素计算 relu6(x + 3) / 6. |
|
逐元素计算 x * relu6(x + 3) / 6. |
|
逐元素计算 max(x, 0). |
|
逐元素计算 min(max(x, 0), 6). |
|
Element-wise max(x, 0) + y * min(x, 0). |
|
LeakyReLU 函数。 |
|
逐元素应用 SiLU 函数: x * sigmoid(x) 。 |
|
逐元素应用函数: |
|
逐元素应用函数: |
|
应用 \(\text{softmax}(x)\) 函数 |
|
将 \(\log(\text{softmax}(x))\) 函数应用于一个 n 维 Tensor 输入上 |
|
逐元素应用函数: |
|
计算输入数据沿给定维度 |
标准化函数¶
对输入进行批标准化。 |
|
对输入进同步批标准化。 |
|
Applies layer normalization to the input. |
|
对输入 Tensor 进行局部响应归一化。 |
线性函数¶
对输入张量进行线性变换。 |
Dropout functions¶
返回一个新的张量,其中每个元素以 P = |
Sparse functions¶
对输入张量进行 one-hot 编码。 |
|
对一些轴进行 One-hot 索引。 |
|
应用查找表进行 embedding。 |
Loss functions¶
计算预测值 \(x\) 和标签值 \(y\) 的每个元素之间的平均绝对误差(MAE)。 |
|
计算预测值 \(x\) 和标签值 \(y\) 之间的均方误差(平方L2范数)。 |
|
计算支持向量机 SVM 中经常使用的 hinge loss。 |
|
计算 binary cross entropy loss(默认使用 logits)。 |
|
计算 multi-class cross entropy loss(默认使用 logits)。 |
|
计算 Connectionist Temporal Classification loss 。 |
视觉运算¶
将图像从一种格式转化成另一种格式。 |
|
填充输入张量。 |
|
将形状为 (..., C * r^2, H, W) 的张量的元素重排为一个形状为 (..., C, H * r, W * r) 的张量,其中 r 是一个放大系数, ... 是零个或更多的 batch 维度。 |
|
根据给定的大小或缩放因子将输入张量进行上/下采样。 |
|
对按批组织的二维图像进行重映射变换。 |
|
Batched affine transformation on 2D images. |
|
对按批组织的二维图像进行透视变换。 |
|
在指定输入的感兴趣区域上执行池化,并获得固定大小的特征图, 如Faster RCNN中所述 |
|
RoI Align 是在指定输入的感兴趣区域上获得特征图,如 Mask R-CNN 论文中所述。 |
|
根据小方框的 IoU 值进行 非最大值抑制(NMS)。 |
|
对输入进行相关性计算。 |
|
NVIDIA光流SDK的实现 |
Quantized functions¶
带有激活函数的卷积偏置操作,仅用于推理 |
|
带有激活函数的批量卷积偏置,仅用于推理 |
|
|
分布式算子¶
在指定的组中收集张量,并沿第一维对其进行拼接。 |
|
在指定组中以 max 操作来对张量进行规约操作。 |
|
在指定组中以 min 操作来对张量进行规约操作。 |
|
在指定组中以求和操作来对张量进行规约操作。 |
|
每个进程将输入张量分散到所有进程,并返回收集的张量。 |
|
从根进程向其他进程广播张量数据。 |
|
通过求和规约指定组中的张量,并在第一维度将其拆分。 |
|
用于集合通信的辅助函数。 |
|
发送张量到另一个进程。 |
|
从另一个进程接收张量。 |
|
在指定组中以 sum操作来对张量进行规约操作。 |
|
在指定的组中收集张量。 |
|
在根进程中沿第一维度切分张量。 |
External functions¶
加载序列化的extern opr子图并假执行运算符。 |
|
Load a serialized Cambricon model as a runtime operator in MegEngine. |
|
加载 |
|
加载序列化的MagicMind模型作为MegEngine中的运行时操作符。 |