ReduceSumSquared.cpp 2.63 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 "../../XName.h"
xiaotong committed
23 24 25 26 27 28
#include "ReduceSum.h"
#include "ReduceSumSquared.h"

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

/* 
29 30 31 32
squared sum of the items along a dimension of the tensor

For a 1-dimensional data array a, sum = \sum_i (a_i - shift)^2

xiaotong committed
33 34 35 36 37
>> input - the input tensor
>> output - the output tensor
>> dim - the dimension where the reduction is performed on
>> shift - bias on the input
*/
38
void _ReduceSumSquared(const XTensor * input, XTensor * output, int dim, const XTensor * shift)
xiaotong committed
39
{
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
    _ReduceSum(input, output, dim, shift, 2.0F);
}


/* 
squared sum of the items along a dimension of the tensor (return a XTensor structure)
make a new tensor to keep the result and return it

For a 1-dimensional data array a, sum = \sum_i (a_i - shift)^2

>> input - the input tensor
>> dim - the dimension where the reduction is performed on
>> shift - bias on the input
<< return - the squared sum of the items along a dimension of the tensor
*/
XTensor ReduceSumSquared(const XTensor &input, int dim, const XTensor &shift)
{
57
    CheckNTErrors(dim >= 0 && dim < input.order, "Illegal dimension to reduce!");
58 59 60
	
    int order = input.order - 1;
    int * dimSize = new int[order];
61
    for(int i = 0; i < order; i++){
62 63
        if(i < dim)
            dimSize[i] = input.dimSize[i];
64
        else if(i >= dim)
65 66 67
            dimSize[i] = input.dimSize[i + 1];
    }

68 69
    float dr = (!input.isSparse) ? 1.0F : input.denseRatio;
    XTensor output(order, dimSize, input.dataType, dr, input.devID, input.mem);
70 71 72 73
    output.SetTMP();

    /* call _ReduceSumSquared function */
    _ReduceSumSquared(&input, &output, dim, &shift);
74
                    
75 76
    /* tensor connection */
    XLink::MakeLink(&input, &shift, &output, REDUCE_REDUCESUMSQUARED);
77
    XLink::AddParamToHeadInt(&output, dim);
78 79

    /* destroy variables */
xiaotong committed
80
    delete[] dimSize;
81 82

    return output;
xiaotong committed
83 84
}

85
 } // namespace nts(NiuTrans.Tensor)