TNormalize.cpp 6.66 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: Lin Ye (email: linye2015@outlook.com) 2018-06-20
*/

liyinqiao committed
22
#include "../core/utilities/CheckData.h"
liyinqiao committed
23
#include "TNormalize.h"
xiaotong committed
24 25

namespace nts { // namespace nts(NiuTrans.Tensor)
liyinqiao committed
26 27 28 29 30 31

/*
case 1: normalized the data with normal distribution 
For an input x, y = a * (x-mean)/sqrt(variance+\epsilon) + b.
where a and b are the scalar and bias respectively, 
and \epsilon is the adjustment parameter.
xiaotong committed
32 33 34
*/
bool TestNormalize1()
{
liyinqiao committed
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
    /* a source tensor of size (2, 3) */
    int sOrder = 2;
    int * sDimSize = new int[sOrder];
    sDimSize[0] = 2;
    sDimSize[1] = 3;

    int sUnitNum = 1;
    for (int i = 0; i < sOrder; i++)
        sUnitNum *= sDimSize[i];

    /* a target tensor of size (2, 3) */
    int tOrder = 2;
    int * tDimSize = new int[tOrder];
    tDimSize[0] = 2;
    tDimSize[1] = 3;

    int tUnitNum = 1;
    for (int i = 0; i < tOrder; i++)
        tUnitNum *= tDimSize[i];

    /* a mean tensor of size (3) */
    int meanOrder = 1;
    int * meanDimSize = new int[meanOrder];
    meanDimSize[0] = 3;

    int meanUnitNum = 1;
    for (int i = 0; i < meanOrder; i++)
        meanUnitNum *= meanDimSize[i];

    /* a variance tensor of size (3) */
    int varOrder = 1;
    int * varDimSize = new int[varOrder];
    varDimSize[0] = 3;

    int varUnitNum = 1;
    for (int i = 0; i < varOrder; i++)
        varUnitNum *= varDimSize[i];

    /* a scale tensor of size (2, 3) */
    int aOrder = 2;
    int * aDimSize = new int[aOrder];
    aDimSize[0] = 2;
    aDimSize[1] = 3;

    int aUnitNum = 1;
    for (int i = 0; i < aOrder; i++)
        aUnitNum *= aDimSize[i];

    /* a bias tensor of size (2, 3) */
    int bOrder = 2;
    int * bDimSize = new int[bOrder];
    bDimSize[0] = 2;
    bDimSize[1] = 3;

    int bUnitNum = 1;
    for (int i = 0; i < bOrder; i++)
        bUnitNum *= bDimSize[i];

    DTYPE sData[2][3] = { {1.0F, 2.0F, 3.0F},
                          {1.5F, 2.5F, 3.5F} };
    DTYPE meanData[3] = {1.0F, 1.5F, 2.0F};
    DTYPE varData[3] = {1.0F, 1.0F, 4.0F};
liyinqiao committed
97
    DTYPE aData[2][3] = { {1.0F, 1.0F, 1.0F},
liyinqiao committed
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
                          {1.0F, 1.0F, 1.0F} };
    DTYPE answer[2][3] = { {0.0F, 0.5F, 0.5F},
                           {0.5F, 1.0F, 0.75F} };

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
    XTensor * s = NewTensorV2(sOrder, sDimSize);
    XTensor * t = NewTensorV2(tOrder, tDimSize);
    XTensor * mean = NewTensorV2(meanOrder, meanDimSize);
    XTensor * var = NewTensorV2(varOrder, varDimSize);
    XTensor * a = NewTensorV2(aOrder, aDimSize);
    XTensor * b = NewTensorV2(bOrder, bDimSize);
    XTensor * tMe = NewTensorV2(sOrder, sDimSize);
113
    XTensor tUser;
xiaotong committed
114

liyinqiao committed
115 116 117 118 119 120 121 122 123 124 125 126
    /* initialize variables */
    s->SetData(sData, sUnitNum);
    tMe->SetData(sData, sUnitNum);
    mean->SetData(meanData, meanUnitNum);
    var->SetData(varData, varUnitNum);
    a->SetData(aData, aUnitNum);
    b->SetZeroAll();
    t->SetZeroAll();

    /* call normalize function */
    _Normalize(s, t, 0, mean, var, a, b, 0.0F);
    _NormalizeMe(tMe, 0, mean, var, a, b, 0.0F);
127
    tUser = Normalize(*s, 0, *mean, *var, *a, *b, 0.0F);
liyinqiao committed
128
    
liyinqiao committed
129 130 131 132
    /* check results */
    cpuTest = _CheckData(t, answer, tUnitNum, 1e-4F) &&
              _CheckData(tMe, answer, tUnitNum, 1e-4F) &&
              _CheckData(&tUser, answer, tUnitNum, 1e-4F);
xiaotong committed
133 134

#ifdef USE_CUDA
liyinqiao committed
135 136 137 138 139 140 141 142 143 144 145
    /* GPU test */
    bool gpuTest = true;

    /* create tensors */
    XTensor * sGPU = NewTensorV2(sOrder, sDimSize, X_FLOAT, 1.0F, 0);
    XTensor * meanGPU = NewTensorV2(meanOrder, meanDimSize, X_FLOAT, 1.0F, 0);
    XTensor * varGPU = NewTensorV2(varOrder, varDimSize, X_FLOAT, 1.0F, 0);
    XTensor * aGPU = NewTensorV2(aOrder, aDimSize, X_FLOAT, 1.0F, 0);
    XTensor * bGPU = NewTensorV2(bOrder, bDimSize, X_FLOAT, 1.0F, 0);
    XTensor * tGPU = NewTensorV2(tOrder, tDimSize, X_FLOAT, 1.0F, 0);
    XTensor * tMeGPU = NewTensorV2(sOrder, sDimSize, X_FLOAT, 1.0F, 0);
146
    XTensor tUserGPU;
xiaotong committed
147

liyinqiao committed
148 149 150 151 152 153 154 155 156 157 158 159
    /* initialize variables */
    sGPU->SetData(sData, sUnitNum);
    tMeGPU->SetData(sData, sUnitNum);
    meanGPU->SetData(meanData, meanUnitNum);
    varGPU->SetData(varData, varUnitNum);
    aGPU->SetData(aData, aUnitNum);
    bGPU->SetZeroAll();
    tGPU->SetZeroAll();

    /* call Normalize function */
    _Normalize(sGPU, tGPU, 0, meanGPU, varGPU, aGPU, bGPU, 0.0F);
    _NormalizeMe(tMeGPU, 0, meanGPU, varGPU, aGPU, bGPU, 0.0F);
160
    tUserGPU = Normalize(*sGPU, 0, *meanGPU, *varGPU, *aGPU, *bGPU, 0.0F);
xiaotong committed
161

liyinqiao committed
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
    /* check results */
    gpuTest = _CheckData(tGPU, answer, tUnitNum, 1e-4F) &&
              _CheckData(tMeGPU, answer, tUnitNum, 1e-4F) &&
              _CheckData(&tUserGPU, answer, tUnitNum, 1e-4F);

    /* destroy variables */
    delete s;
    delete tMe;
    delete t;
    delete mean;
    delete var;
    delete a;
    delete b;
    delete sGPU;
    delete tMeGPU;
    delete tGPU;
    delete meanGPU;
    delete varGPU;
    delete aGPU;
    delete bGPU;
    delete[] sDimSize;
    delete[] tDimSize;
    delete[] meanDimSize;
    delete[] varDimSize;
    delete[] aDimSize;
    delete[] bDimSize;

    return cpuTest && gpuTest;
xiaotong committed
190
#else
liyinqiao committed
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206
    /* destroy variables */
    delete s;
    delete tMe;
    delete t;
    delete mean;
    delete var;
    delete a;
    delete b;
    delete[] sDimSize;
    delete[] tDimSize;
    delete[] meanDimSize;
    delete[] varDimSize;
    delete[] aDimSize;
    delete[] bDimSize;

    return cpuTest;
xiaotong committed
207 208 209 210 211 212 213 214 215 216 217
#endif // USE_CUDA
}

/* other cases */
/*
TODO!!
*/

/* test for Normalize Function */
bool TestNormalize()
{
liyinqiao committed
218 219
    XPRINT(0, stdout, "[TEST NORMALIZE] normalized the data with normal distribution \n");
    bool returnFlag = true, caseFlag = true;
xiaotong committed
220

liyinqiao committed
221 222
    /* case 1 test */
    caseFlag = TestNormalize1();
xiaotong committed
223

liyinqiao committed
224 225 226 227 228 229
    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 1 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 1 passed!\n");
xiaotong committed
230

liyinqiao committed
231 232 233 234
    /* other cases test */
    /*
    TODO!!
    */
xiaotong committed
235

liyinqiao committed
236 237 238 239 240
    if (returnFlag) {
        XPRINT(0, stdout, ">> All Passed!\n");
    }
    else
        XPRINT(0, stdout, ">> Failed!\n");
xiaotong committed
241

liyinqiao committed
242
    XPRINT(0, stdout, "\n");
xiaotong committed
243

liyinqiao committed
244
    return returnFlag;
xiaotong committed
245 246 247
}

} // namespace nts(NiuTrans.Tensor)