ZiyueHuang commented on a change in pull request #9540: refactor regression ops 
to nnvm interface
URL: https://github.com/apache/incubator-mxnet/pull/9540#discussion_r164027751
 
 

 ##########
 File path: src/operator/regression_output.cc
 ##########
 @@ -18,86 +18,96 @@
  */
 
 /*!
- * Copyright (c) 2015 by Contributors
- * \file regression_output.cc
- * \brief regression output operator
+ * \file regression_ouput.cc
+ * \brief Regression output operator.
 */
+
 #include "./regression_output-inl.h"
-#include "./mshadow_op.h"
+
+#define MXNET_OPERATOR_REGISTER_REGRESSION_FWD(__name$, __kernel$, __bwdop$)   
\
+  NNVM_REGISTER_OP(__name$)                                                    
\
+  .set_num_inputs(2)                                                           
\
+  .set_num_outputs(1)                                                          
\
+  .set_attr<nnvm::FListInputNames>("FListInputNames",                          
\
+    [](const NodeAttrs& attrs) {                                               
\
+      return std::vector<std::string>{"data", "label"};                        
\
+    })                                                                         
\
+  .set_attr<nnvm::FInferShape>("FInferShape", RegressionOpShape)               
\
+  .set_attr<nnvm::FGradient>("FGradient", RegressionOpGrad{__bwdop$})          
\
+  .set_attr<nnvm::FInplaceOption>("FInplaceOption",                            
\
+  [](const NodeAttrs& attrs){                                                  
\
+    return std::vector<std::pair<int, int> >{{0, 0}};                          
\
+  })                                                                           
\
+  .set_attr<FCompute>("FCompute<cpu>", RegressionForward<cpu, __kernel$>)      
\
+  .add_argument("data", "NDArray-or-Symbol", "Input data to the function.")    
\
+  .add_argument("label", "NDArray-or-Symbol", "Input label to the function.")  
\
+  .add_arguments(RegressionOutputParam::__FIELDS__())
+
+#define MXNET_OPERATOR_REGISTER_REGRESSION_BWD(__name$, __kernel$)         \
+  NNVM_REGISTER_OP(__name$)                                                \
+  .set_num_inputs(2)                                                       \
+  .set_num_outputs(2)                                                      \
+  .set_attr_parser(ParamParser<RegressionOutputParam>)                     \
+  .set_attr<nnvm::TIsBackward>("TIsBackward", true)                        \
+  .set_attr<nnvm::FInplaceOption>("FInplaceOption",                        \
+  [](const NodeAttrs& attrs){                                              \
+    return std::vector<std::pair<int, int> >{{1, 0}};                      \
+  })                                                                       \
+  .set_attr<FCompute>("FCompute<cpu>", RegressionBackward<cpu, __kernel$>)
 
 namespace mxnet {
 namespace op {
 
-template<>
-Operator *CreateRegressionOutputOp<cpu>(reg_enum::RegressionOutputType type,
-                                        RegressionOutputParam param) {
-  switch (type) {
-    case reg_enum::kLinear:
-      return new RegressionOutputOp<cpu, op::mshadow_op::identity, 
op::mshadow_op::minus>(param);
-    case reg_enum::kLogistic:
-      return new RegressionOutputOp<cpu, mshadow_op::sigmoid, 
op::mshadow_op::minus>(param);
-    case reg_enum::kMAE:
-      return new RegressionOutputOp<cpu, op::mshadow_op::identity, 
mshadow_op::minus_sign>(param);
-    default:
-      LOG(FATAL) << "unknown activation type " << type;
-  }
-  return nullptr;
-}
-
-// DO_BIND_DISPATCH comes from operator_common.h
-template<reg_enum::RegressionOutputType type>
-Operator *RegressionOutputProp<type>::CreateOperator(Context ctx) const {
-  DO_BIND_DISPATCH(CreateRegressionOutputOp, type, param_);
-}
 
 DMLC_REGISTER_PARAMETER(RegressionOutputParam);
 
-MXNET_REGISTER_OP_PROPERTY(LinearRegressionOutput, 
RegressionOutputProp<reg_enum::kLinear>)
+MXNET_OPERATOR_REGISTER_REGRESSION_FWD(LinearRegressionOutput,
+  mshadow_op::identity, "_backward_linear_reg_out")
 .describe(R"code(Computes and optimizes for squared loss during backward 
propagation.
 Just outputs ``data`` during forward propagation.
 
 If :math:`\hat{y}_i` is the predicted value of the i-th sample, and 
:math:`y_i` is the corresponding target value,
 then the squared loss estimated over :math:`n` samples is defined as
 
-:math:`\text{SquaredLoss}(y, \hat{y} ) = \frac{1}{n} \sum_{i=0}^{n-1} \left( 
y_i - \hat{y}_i \right)^2`
+:math:`\text{SquaredLoss}(\textbf{Y}, \hat{\textbf{Y}} ) = \frac{1}{n} 
\sum_{i=0}^{n-1} \lVert  \textbf{y}_i - \hat{\textbf{y}}_i  \rVert_2`
 
 .. note::
    Use the LinearRegressionOutput as the final output layer of a net.
 
-By default, gradients of this loss function are scaled by factor `1/n`, where 
n is the number of training examples.
-The parameter `grad_scale` can be used to change this scale to `grad_scale/n`.
+By default, gradients of this loss function are scaled by factor `1/m`, where 
m is the number of features of a training example.
+The parameter `grad_scale` can be used to change this scale to `grad_scale/m`.
+
+)code" ADD_FILELINE);
 
-)code" ADD_FILELINE)
-.add_argument("data", "NDArray-or-Symbol", "Input data to the function.")
-.add_argument("label", "NDArray-or-Symbol", "Input label to the function.")
-.add_arguments(RegressionOutputParam::__FIELDS__());
+MXNET_OPERATOR_REGISTER_REGRESSION_BWD(_backward_linear_reg_out, 
mshadow_op::minus);
 
-MXNET_REGISTER_OP_PROPERTY(MAERegressionOutput, 
RegressionOutputProp<reg_enum::kMAE>)
+MXNET_OPERATOR_REGISTER_REGRESSION_FWD(MAERegressionOutput,
+  mshadow_op::identity, "_backward_mae_reg_out")
 .describe(R"code(Computes mean absolute error of the input.
 
 MAE is a risk metric corresponding to the expected value of the absolute error.
 
 If :math:`\hat{y}_i` is the predicted value of the i-th sample, and 
:math:`y_i` is the corresponding target value,
 then the mean absolute error (MAE) estimated over :math:`n` samples is defined 
as
 
-:math:`\text{MAE}(y, \hat{y} ) = \frac{1}{n} \sum_{i=0}^{n-1} \left| y_i - 
\hat{y}_i \right|`
+:math:`\text{MAE}(\textbf{Y}, \hat{\textbf{Y}} ) = \frac{1}{n} 
\sum_{i=0}^{n-1} \lVert \textbf{y}_i - \hat{\textbf{y}}_i \rVert_1`
 
 .. note::
    Use the MAERegressionOutput as the final output layer of a net.
 
-By default, gradients of this loss function are scaled by factor `1/n`, where 
n is the number of training examples.
-The parameter `grad_scale` can be used to change this scale to `grad_scale/n`.
+By default, gradients of this loss function are scaled by factor `1/m`, where 
m is the number of features of a training example.
 
 Review comment:
   Updated

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to