dmlc/mshadow

Can't get correct answer from reduce_with_axis

WarBean opened this issue · 1 comments

I'm trying to implement a tensorflow-like reduce_sum operator for mxnet, based on mshadow::expr::reduce_with_axis. Firstly I wrote some testing code but got the wrong answer. Not sure whether my usage is inappropriate. Here's the test code:

#define MSHADOW_STAND_ALONE 1
#include <iostream>
#include "../mshadow/tensor.h"
#include "../mshadow/extension/reduce_with_axis.h"
using namespace mshadow;
using namespace mshadow::expr;
using namespace std;

int main() {
    Tensor<cpu, 3, float> t3(Shape3(2, 2, 5));
    AllocSpace(&t3);
    t3 = 1.0f;

    Tensor<cpu, 2, float> t2(Shape2(2, 2));
    AllocSpace(&t2);
    t2 = reduce_with_axis<red::sum, false>(t3, 2);
    for (index_t i = 0; i < t2.size(0); ++i) {
        for (index_t j = 0; j < t2.size(1); ++j) {
            cout << t2[i][j] << ' ';
        }
        cout << endl;
    }
}

The output varied from

5 5
7.3787e+19 0

to

5 5
4 0

or something else. However, if I reduce dimension 0 or 1, rather than 2, then the result is constant and correct.

I also read the source code of reduce_with_axis.h but failed to catch the idea.

@piiswrong

Related to #116 and no bug after applying the patch.