Search code examples
c++mxnetloss-function

Output value with Mxnet C++ API


I am trying to implement a custom loss function in Mxnet, using its C++ API. The question of loss function has already been risen in python (how to use customized loss function with mxnet?) even though it doesn't address the specific question of the output.

Let say I want to create my own softmax function, I could do the following:

         Symbol expFc2 = exp(fc2);
         Symbol sumExp = sum("sumExp", expFc2, dmlc::optional<Shape>(Shape(1)));
         Symbol expandedSumExp = expand_dims("expandedSumExp", sumExp, 1);
         Symbol customSoftmax = broadcast_div(expFc2, expandedSumExp);
         Symbol cross_entropy = (-1) * (one_hot("OneHotDataLabel", data_label, 10) * log(customSoftmax) + (1 - one_hot("OneHotDataLabel", data_label, 10)) * log(1 - customSoftmax));
         Symbol lenet = MakeLoss(cross_entropy);

However, whenever I get the output, auto curOutput = exe->outputs;, I seem to get the value after the computation of the loss function, which would be the cross_entropy.

How to get the result of the customSoftmax computation?


Solution

  • There is no API to access intermediate outputs in a computation graph. This is for optimization reasons. Any output you need must be returned as output of the graph. You can use mx.symbol.Group to return multiple symbols as output.

    Here is a Python example. You should be able to do the same thing in C++.