Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce operations in C# API #2698

Open
cesarsouza opened this issue Nov 28, 2017 · 3 comments
Open

Reduce operations in C# API #2698

cesarsouza opened this issue Nov 28, 2017 · 3 comments
Assignees

Comments

@cesarsouza
Copy link

Hello there,

I've been trying to use CNTK's reduce operations, but I am facing some issues when I try to reduce alongside a particular axis. I am writing below a test case with 4 assertions, 2 of them which pass and 2 which do not:

private static double[][] sum(params int[] axes)
{
    var arr = new[]
    {
        /*                            total:
        /*       */ 1.0, 2.0, 3.0, /*  6.0 */
        /*       */ 4.0, 5.0, 6.0, /* 15.0 */
        /* total:   5.0, 7.0, 9.0     21.0 */ 
    };

    var shape = NDShape.CreateNDShape(new[] { 2, 3 });
    Value vx = Value.CreateBatch(shape, arr, DeviceDescriptor.CPUDevice, readOnly: true);
    Variable x = Variable.InputVariable(shape, CNTK.DataType.Double, name: "input");

    CNTK.Function f;
    if (axes == null)
    {
        f = CNTKLib.Alias(x);
    }
    else
    {
        var axisVector = new AxisVector(axes.Select(ax => new Axis(ax)).ToArray());
        f = CNTKLib.ReduceSum(x, axis: axisVector);
    }

    var inputs = new Dictionary<Variable, Value>() { { x, vx } };
    var outputs = new Dictionary<Variable, Value>() { { f, null } };
    f.Evaluate(inputs, outputs, DeviceDescriptor.CPUDevice);
    var r = outputs[f].GetDenseData<double>((Variable)f);
    return r.Select(ri => ri.ToArray()).ToArray();
}

[Test]
public void cntk_sum_test_direct_api()
{
    double[][] r;

    r = sum(null); // first, a sanity check to verify that values are being read correctly
    double[,] d = new double[2, 3]; // result will be { 0, 1, 2, 3, 4, 5, 6 }
    Buffer.BlockCopy(r[0], 0, d, 0, sizeof(double) * d.Length);
    Assert.AreEqual(new[,] { { 1, 2, 3 }, { 4, 5, 6 } }, d); // ok

    r = sum(0, 1); // sum over all axes
    double a = r[0][0]; // result will be { 21 }
    Assert.AreEqual(21, a); // ok

    r = sum(0); // sum over first axis
    double[] b = r[0]; // result will be { 3, 7, 11 }
    Assert.AreEqual(new[] { 5.0, 7.0, 9.0 }, b); // fails

    r = sum(1); // sum over second axis
    double[] c = r[0]; // result will be { 9, 12 }
    Assert.AreEqual(new[] { 6.0, 15.0 }, b);  // fails
}

As you see, the sum method is just using CNTK to perform a ReduceSum operation over the axes it receives as a parameter, or defaults to a simple Alias operation when the vector of axes is null to just let the data pass through.

When the test function is called passing both axes to be summed, it returns the correct value. When it is called passing null, it correctly lets the data pass. However, when reducing in a particular direction, it returns wrong values albeit with correct shape.

Could someone please help shed a light on what is wrong in the above test?

Regards,
Cesar

cesarsouza added a commit to cesarsouza/keras-sharp that referenced this issue Nov 28, 2017
@bhrnjica
Copy link

This bug is still existing in the current release version!?
Is there any plan to fix it?

@akitasov
Copy link

still not fixed

@akitasov
Copy link

akitasov commented Dec 16, 2018

actually it works little other other way.
the array you providing is processed as follows
{1.0, 2.0, 3.0, 4.0, 5.0, 6.0}
processed
{
1.0 3.0 5.0
2.0 4.0 6.0
}
I've checked few other functions, same logic
CNTKLib.ReduceSum
CNTKLib.ReduceMax
CNTKLib.ReduceProd

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants