megengine.functional¶
import megengine.functional as F
注解
顾名思义,megengine.functional 模块中包含着所有与 Tensor 有关的计算接口:
- 与神经网络(Neural Network)相关的算子统一封装在 - megengine.functional.nn中;
- 分布式算子统一封装在 - megengine.functional.distributed中,方便调用;
- 其它的常见算子均可在 - megengine.functional中直接调用;
参见
用户指南中对于 使用 Functional 操作与计算 有另外一套分类逻辑,可作为参考。
General tensor operations¶
注解
该部分的 API 设计接纳了 Python 数据 API 标准联盟 中的倡导, 尽可能地向 NumPy API 靠拢。
Creation Functions¶
| 返回一个数值从 start 到 stop ,相邻间隔为 step 的张量。 | |
| 返回指定间隔的等距数列。 | |
| 返回一个二维张量,其对角线上值均为1,其他位置值为0。 | |
| 根据给定形状返回元素全为 0 的 Tensor | |
| 返回一个和参数同维度,且数值全为 0 的 Tensor | |
| 返回一个具有给定形状的全1张量。 | |
| 返回一个与输入张量形状相同的全1张量 | |
| Creates a tensor of shape  | |
| 返回一个与输入张量形状相同的且值为给定值的零张量 | 
Manipulation Functions¶
| 把张量复制到另一个设备上。 | |
| 将一个张量重塑为给定的目标形状;逻辑元素的总数必须保持不变 | |
| 通过将子张量从  | |
| 根据给定模板交换形状和步长。 | |
| 将张量广播至指定的形状。 | |
| 在给定轴之前添加维度。 | |
| 移除形状中下标为1的维度。 | |
| 拼接一些张量 | |
| 沿新轴对张量进行拼接。 | |
| 将输入张量拆分成更小的张量。 | |
| 通过重复  | |
| 重复数组的元素。 | |
| Roll the tensor along the given axis(or axes). | 
Arithmetic operations¶
| 逐元素相加。 | |
| Element-wise sub. | |
| 逐元素相乘。 | |
| 逐元素相除。 | |
| 逐元素相除并下取整。 | |
| 逐元素取相反数。 | |
| 逐元素取绝对值。 | |
| 返回一个新的张量,该张量表明每个元素的符号。 | |
| 逐元素进行指数运算。 | |
| 返回逐元素相除所得的余数。 | |
| 逐元素取平方根。 | |
| Element-wise square. | |
| 逐元素的最大数组元素。 | |
| 逐元素的最小数组元素。 | |
| 逐元素四舍五入到整数。 | |
| 逐元素上取整。 | |
| 逐元素计算下取整函数。 | |
| Clamps all elements in input tensor into the range  | |
| 逐元素计算 e 的 x 次方。 | |
| 逐元素计算 expm1 函数。 | |
| 逐元素计算以 e 为底的对数。 | |
| 逐元素计算 (x+1) 以 e 为底的对数。 | 
Trigonometric functions¶
| 逐元素计算正弦函数。 | |
| 逐元素计算余弦函数。 | |
| 逐元素计算正切函数。 | |
| 逐元素计算反正弦函数。 | |
| 逐元素计算反余弦函数。 | |
| 逐元素计算反正切函数。 | |
| 逐元素计算两个参数的反正切函数。 | 
Hyperbolic functions¶
| 逐元素计算双曲正弦函数。 | |
| 逐元素计算双曲余弦函数。 | |
| 逐元素计算双曲正切值。 | |
| 逐元素计算反双曲余弦函数。 | |
| 逐元素计算反双曲正弦函数。 | |
| 逐元素计算反双曲正切函数。 | 
Bit operations¶
| 逐元素计算 x << y。 | |
| 逐元素计算 x >> y。 | 
Logic functions¶
| 返回一个新的张量,该张量表明每个元素是否为非数值类型  | |
| 返回一个张量,它表示每个元素是否是无穷大值  | |
| 逐元素进行逻辑与运算。 | |
| 逐元素进行逻辑非运算。 | |
| 逐元素进行逻辑或运算。 | |
| 逐元素进行逻辑异或运算。 | |
| 逐元素计算 x 大于 y. | |
| 逐元素判断 x 大于或者等于 y. | |
| 逐元素的计算 x 小于 y. | |
| 逐元素计算 x 小于或等于 y. | |
| 逐元素判断是否相等。 | |
| 逐元素计算 x 不等于 y. | 
Statistical Functions¶
| 返回输入张量在给定轴上所有元素求和后的结果。 | |
| 返回输入张量在给定轴上所有元素求乘积后的结果。 | |
| 返回输入张量在给定轴上所有元素求平均后的结果。 | |
| 返回输入张量在给定轴上所有元素求最小值后的结果。 | |
| 返回输入张量在给定轴上所有元素求最大值后的结果。 | |
| 返回输入张量在给定轴上所有元素求方差后的结果。 | |
| 返回输入张量在给定轴上所有元素求标准差后的结果。 | |
| Computes the cumulative sum of elements along given axis. | 
参见
想要返回 min, max 的索引而不是元素值,请参考 Searching Functions
Linear Algebra Functions¶
| 计算两个向量  | |
| 计算一批矩阵的逆;输入必须满足 [...,n,n] 的形状。 | |
| 对矩阵  | |
| 对输入矩阵  | |
| 计算在给定轴  | |
| 返回在给定轴  | 
Indexing Functions¶
| 使用  | |
| 把  | |
| 如果在满足特定条件,则从数据中获取元素。 | |
| 根据 mask 条件选择张量  | 
Neural network operations¶
Convolution functions¶
| 一维卷积运算。 | |
| 二维卷积运算。 | |
| 三维卷积运算。 | |
| 使用 untied kernels 对图像进行二维空域卷积。 | |
| 二维转置卷积运算。 | |
| 3D transposed convolution operation. | |
| 可变形卷积 | |
| Extracts sliding local blocks from a batched input tensor. | |
| Sum over the sliding windows on the corresponding input location. | 
Pooling functions¶
| 对输入进行二维平均池化。 | |
| 对输入张量进行二维最大池化。 | |
| 对输入进行二维平均池化。 | |
| 对输入数据进行2D最大池化。 | |
| 可变形的位置敏感的感兴趣区域池化 | 
Non-linear activation functions¶
| 逐元素计算 1 / ( 1 + exp( -x ) ). | |
| 逐元素计算 relu6(x + 3) / 6. | |
| 逐元素计算 x * relu6(x + 3) / 6. | |
| 逐元素计算 max(x, 0). | |
| 逐元素计算 min(max(x, 0), 6). | |
| Elememt-wise PReLU function. | |
| Element-wose LeakyReLU function | |
| Applies the element-wise Sigmoid Linear Unit function, i.e. x * sigmoid(x). | |
| 逐元素应用函数: | |
| 逐元素应用函数: | |
| Applies a \(\text{softmax}(x)\) function. | |
| Applies the \(\log(\text{softmax}(x))\) function to an n-dimensional input tensor. | |
| 逐元素应用函数: | |
| 计算输入数据沿给定维度  | 
Normalization functions¶
| 对输入进行批标准化。 | |
| 对输入进同步批标准化。 | 
Sparse functions¶
| 对输入张量进行 one-hot 编码。 | |
| 对一些轴进行 One-hot 索引。 | |
| 应用查找表进行 embedding。 | 
Metric functions¶
| 根据给定的预测的logits和真实值标签计算分类准确率。 | 
Loss functions¶
| 计算预测值 \(x\) 和标签值 \(y\) 的每个元素之间的平均绝对误差(MAE)。 | |
| 计算预测值 \(x\) 和标签值 \(y\) 之间的均方误差(平方L2范数)。 | |
| 计算支持向量机 SVM 中经常使用的 hinge loss。 | |
| 计算 binary cross entropy loss(默认使用 logits)。 | |
| 计算 multi-class cross entropy loss(默认使用 logits)。 | 
Vision functions¶
| 将图像从一种格式转化成另一种格式。 | |
| Pad is python warpper for padding opr in megbrain, can padding in random one of the max 7 dimensions. | |
| 根据给定的大小或缩放因子将输入张量进行上/下采样。 | |
| 对按批组织的二维图像进行重映射变换。 | |
| 对按批处置的二维图像进行仿射变换。 | |
| 对按批组织的二维图像进行透视变换。 | |
| 对输入进行 roi pooling。 | |
| 对输入进行 roi align。 | |
| 根据小方框的 IoU 值进行 非最大值抑制(NMS)。 | |
| Applies correlation to inputs. | |
| NVIDIA光流SDK的实现 | 
Distributed functions¶
| Reduce tensors across the specified group by max. | |
| Reduce tensors across the specified group by min. | |
| Reduce tensors across the specified group by sum. | |
| Broadcast tensor data from root process to others. | |
| Send tensor to another process. | |
| Receive a tensor from another process. | |
| Gather tensors across the specified group and concat them at first dimension. | |
| Each process scatter input tensor to all processes and return gathered tensor. | |
| Reduce tensor data across the specified group by sum. | |
| Gather tensors across the specified group. | |
| Split tensor in root process at first dimension. | |
| Reduce tensors across the specified group by sum and split them at first dimension. |