Ascend C提供一组Conv3DBackpropFilter高阶API,便于用户快速实现卷积的反向运算,求解反向传播的误差。
Conv3dBackpropFilter的计算公式为:

实现Conv3DBackpropFilter求解反向传播误差运算的具体步骤如下:
1 2 3 4 5 6 7 | #include "lib/conv_backprop/conv3d_bp_filter_api.h" using inputType = ConvBackpropApi::ConvType <ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::NDC1HWC0, inputType>; using weightSizeType = ConvBackpropApi::ConvType<ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::ND, int32_t>; using gradOutputType = ConvBackpropApi::ConvType<ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::NDC1HWC0, gradOutputType>; using gradWeightType = ConvBackpropApi::ConvType <ConvCommonApi::TPosition::GM, ConvCommonApi::ConvFormat::FRACTAL_Z_3D, gradWeightType>; ConvBackpropApi::Conv3DBackpropFilter <inputType, weightSizeType, gradOutputType, gradWeightType> gradWeight_; |
创建对象时需要传入特征矩阵Input、权重矩阵Weight的shape信息WeightSize、GradOutput和GradWeight的参数类型信息,类型信息通过ConvType来定义,包括:内存逻辑位置、数据格式、数据类型。
1 2 3 4 5 6 | template <TPosition POSITION, ConvFormat FORMAT, typename T> struct ConvType { constexpr static TPosition pos = POSITION; // Convolution输入或输出的逻辑位置 constexpr static ConvFormat format = FORMAT; // Convolution输入或输出的数据格式 using Type = T; // Convolution输入或输出的数据类型 }; |
参数 |
说明 |
|---|---|
POSITION |
内存逻辑位置。
|
ConvFormat |
数据格式。
|
TYPE |
数据类型。
注意:Input、GradOutput数据类型需要一致,具体数据类型组合关系请参考表2。 |
Input |
WeightSize |
GradOutput |
GradWeight |
支持平台 |
|---|---|---|---|---|
half |
int32_t |
half |
float |
|
bfloat16_t |
int32_t |
bfloat16_t |
float |
|
1 | gradWeight_.Init(&(tilingData->dwTiling)); // 初始化gradWeight_相关参数 |
1 2 3 4 | gradWeight_.SetGradOutput(gradOutputGm_[offsetA_]); // 设置矩阵gradOutput gradWeight_.SetInput(inputGm_[offsetB_]); // 设置矩阵Input gradWeight_.SetSingleShape(singleShapeM, singleShapeN, singleShapeK); // 设置需要计算要形状 gradWeight_.SetStartPosition(hoStartIdx_); // 设置初始位置 |
1 2 3 | while (gradWeight_.Iterate()) { gradWeight_.GetTensorC(gradWeightGm_[offsetC_]); } |
1 | gradWeight_.End(); |