ReduceMean.cpp 2.66 KB
Newer Older
xiaotong committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* NiuTrans.Tensor - an open-source tensor library
 * Copyright (C) 2017, Natural Language Processing Lab, Northestern University. 
 * 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 42 43 44
{
    CheckNTErrors((input->order > dim), "Illegal dimension specified!");

	int dimRDI = input->order - dim - 1;
    int num = input->dimSizeRDI[dimRDI];

45
    _ReduceSum(input, output, dim);
46
    _ScaleAndShiftMe(output, (DTYPE)1/num, 0);
xiaotong committed
47 48
}

49 50 51 52 53 54 55 56 57 58 59 60
/* 
get the mean value along a dimension of the tensor (return a XTensor structure)
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)
{
61
    CheckNTErrors(dim >= 0 && dim < input.order, "Illegal dimension to reduce!");
62 63 64
	
    int order = input.order - 1;
    int * dimSize = new int[order];
65
    for(int i = 0; i < order; i++){
66 67
        if(i < dim)
            dimSize[i] = input.dimSize[i];
68
        else if(i >= dim)
69 70 71
            dimSize[i] = input.dimSize[i + 1];
    }

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

    /* call _ReduceMean function */
    _ReduceMean(&input, &output, dim);
        
    /* tensor connection */
    XLink::MakeLink(&input, NULL, &output, REDUCE_REDUCEMEAN);
81
    XLink::AddParamToHeadInt(&output, dim);
82 83

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

    return output;
}

89
} // namespace nts(NiuTrans.Tensor)