ReduceMean.cpp 3.87 KB
Newer Older
xiaotong committed
1
/* NiuTrans.Tensor - an open-source tensor library
liyinqiao committed
2
 * Copyright (C) 2017, Natural Language Processing Lab, Northeastern University. 
xiaotong committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
 * All rights reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/*
* $Created by: XIAO Tong (email: xiaotong@mail.neu.edu.cn) 2018-04-24
*/

22
#include "../math/ScaleAndShift.h"
23
#include "../../XName.h"
xiaotong committed
24 25 26 27 28 29
#include "ReduceSum.h"
#include "ReduceMean.h"

namespace nts{ // namespace nts(NiuTrans.Tensor)

/* 
30 31 32 33
get the mean value along a dimension of the tensor

For a 1-dimensional data array a, mean = (1/n) * sum_i input_i

xiaotong committed
34 35 36 37
>> input - the input tensor
>> output - the output tensor
>> dim - the dimension where the reduction is performed on
*/
38
void _ReduceMean(const XTensor * input, XTensor * output, int dim)
xiaotong committed
39 40 41
{
    CheckNTErrors((input->order > dim), "Illegal dimension specified!");

liyinqiao committed
42
    int num = input->dimSize[dim];
xiaotong committed
43

44
    _ReduceSum(input, output, dim);
liyinqiao committed
45
    _ScaleAndShiftMe(output, 1.0F/(DTYPE)(num), 0);
xiaotong committed
46 47
}

48
/* 
liyinqiao committed
49
get the mean value along a dimension of the tensor (return an XTensor structure)
50 51 52 53 54 55 56 57 58 59
make a new tenosr to keep the result and return it

For a 1-dimensional data array a, mean = (1/n) * sum_i input_i

>> input - the input tensor
>> dim - the dimension where the reduction is performed on
<< return - the mean value along a dimension of the tensor
*/
XTensor ReduceMean(const XTensor &input, int dim)
{
60
    CheckNTErrors(dim >= 0 && dim < input.order, "Illegal dimension to reduce!");
liyinqiao committed
61
    
62 63
    int order = input.order - 1;
    int * dimSize = new int[order];
64
    for(int i = 0; i < order; i++){
65 66
        if(i < dim)
            dimSize[i] = input.dimSize[i];
67
        else if(i >= dim)
68 69 70
            dimSize[i] = input.dimSize[i + 1];
    }

71 72
    float dr = (!input.isSparse) ? 1.0F : input.denseRatio;
    XTensor output(order, dimSize, input.dataType, dr, input.devID, input.mem);
liyinqiao committed
73
    output.SetTMPFlag();
74 75 76 77 78

    /* call _ReduceMean function */
    _ReduceMean(&input, &output, dim);
        
    /* tensor connection */
liyinqiao committed
79 80 81 82
    if (input.enableGrad) {
        XLink::MakeLink(&input, NULL, &output, REDUCE_REDUCEMEAN);
        XLink::AddParamToHeadInt(&output, dim);
    }
83 84

    /* destroy variables */
xiaotong committed
85
    delete[] dimSize;
86 87 88 89

    return output;
}

liyinqiao committed
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
/* 
get the mean value along a dimension of the tensor

For a 1-dimensional data array a, mean = (1/n) * sum_i input_i

>> input - the input tensor
>> output - the output tensor
>> dim - the dimension where the reduction is performed on
*/
void ReduceMean(const XTensor &input, XTensor &output, int dim)
{
    CheckNTErrors(dim >= 0 && dim < input.order, "Illegal dimension to reduce!");

    if (!output.isInit || !XTensor::IsReduceShaped(&input, &output, dim)) {
        int order = input.order - 1;
        int * dimSize = new int[order];
        for (int i = 0; i < order; i++) {
            if (i < dim)
                dimSize[i] = input.dimSize[i];
            else if (i >= dim)
                dimSize[i] = input.dimSize[i + 1];
        }

        float dr = (!input.isSparse) ? 1.0F : input.denseRatio;
        InitTensorV2(&output, order, dimSize, input.dataType, dr, input.devID, input.mem);

        /* destroy variables */
        delete[] dimSize;
    }

    /* call _ReduceMean function */
    _ReduceMean(&input, &output, dim);

    if (input.enableGrad) {
        /* tensor connections */
        XLink::MakeLink(&input, NULL, &output, REDUCE_REDUCEMEAN);
        XLink::AddParamToHeadInt(&output, dim);
    }
}

130
} // namespace nts(NiuTrans.Tensor)