Cntk: Custom Loss function in C#

Created on 5 Jan 2018  路  2Comments  路  Source: microsoft/CNTK

Hi CNTK team,

First, thanks for building this awesome ML library.

We're trying to build a regression model and it's working great for big values, but badly for the smaller ones.

We think the problem is that we use SquareError as the loss / evalError function, and we would like to use Mean Absolute Percentage Error (https://en.wikipedia.org/wiki/Mean_absolute_percentage_error) instead.

The C# API provides only three Error functions: (ClassificationError, EditDistanceError and SquaredError).

It looks like we can implement our own (https://docs.microsoft.com/en-us/cognitive-toolkit/Reduction-Operations) but we have problems with the mandatory Axis parameter in C#:

```C#
public static Function MeanAbsoluteError(Variable prediction, Variable targets)
{
var absolute = CNTKLib.Abs(CNTKLib.Minus(targets, prediction));

        return CNTKLib.ReduceMean(absolute, /*axis required here*/);
    }

    public static Function MeanAbsolutePercentageError(Variable prediction, Variable targets)
    {
        var absolute = CNTKLib.Abs(CNTKLib.Minus(targets, prediction));
        var absolutePercentage = CNTKLib.ElementDivide(absolute, targets);
        return CNTKLib.ReduceMean(absolutePercentage, /*axis required here*/);
    }

```

What is the value that should be provided there?

Thanks in advance.

Most helpful comment

Hi all ,

the following should do the trick:

```C#
public static Function MeanAbsoluteError(Variable prediction, Variable targets)
{
var absolute = CNTKLib.Abs(CNTKLib.Minus(targets, prediction));

        return CNTKLib.ReduceMean(absolute, new Axis(-1));
    }

    public static Function MeanAbsolutePercentageError(Variable prediction, Variable targets)
    {
        var absolute = CNTKLib.Abs(CNTKLib.Minus(targets, prediction));
        var absolutePercentage = CNTKLib.ElementDivide(absolute, targets);
        return CNTKLib.ReduceMean(absolutePercentage, new Axis(-1));
    }

```

It would be great, if common loss/error functions like MAP, MAPE, etc would be exposed by the CNTK C# bindings in a future version.

All 2 comments

Hi all ,

the following should do the trick:

```C#
public static Function MeanAbsoluteError(Variable prediction, Variable targets)
{
var absolute = CNTKLib.Abs(CNTKLib.Minus(targets, prediction));

        return CNTKLib.ReduceMean(absolute, new Axis(-1));
    }

    public static Function MeanAbsolutePercentageError(Variable prediction, Variable targets)
    {
        var absolute = CNTKLib.Abs(CNTKLib.Minus(targets, prediction));
        var absolutePercentage = CNTKLib.ElementDivide(absolute, targets);
        return CNTKLib.ReduceMean(absolutePercentage, new Axis(-1));
    }

```

It would be great, if common loss/error functions like MAP, MAPE, etc would be exposed by the CNTK C# bindings in a future version.

What exactly are actually the semantics of Axes and their indices? The wikis/comments explanations vary a little bit and are very short.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

hellosaumil picture hellosaumil  路  16Comments

Madgeeno picture Madgeeno  路  31Comments

cha-zhang picture cha-zhang  路  49Comments

haryngod picture haryngod  路  17Comments

shyamalschandra picture shyamalschandra  路  17Comments