TSoftmax.cpp 6.25 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 22 23 24 25 26
/* 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: Xu Chen (email: hello_master1954@163.com) 2018-06-19
*/

#include "../XTensor.h"
#include "../XUtility.h"
#include "TSoftmax.h"

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

/* 
case 1: test Softmax function.
softmax function: y = e^x / \sum_{i} e^{x_i}
xiaotong committed
31 32 33
*/
bool TestSoftmax1()
{
34 35 36 37 38
    /* a tensor of size (2, 3) */
    int order = 2;
    int * dimSize = new int[order];
    dimSize[0] = 2;
    dimSize[1] = 3;
xiaotong committed
39

40 41 42
    int unitNum = 1;
    for (int i = 0; i < order; i++)
        unitNum *= dimSize[i];
xiaotong committed
43 44 45

    DTYPE xData[2][3] = { {0.0F, 1.0F, 2.0F}, 
                          {0.5F, 0.7F, 1.4F} };
46 47
    DTYPE answer[2][3] = { {0.0900F, 0.2447F, 0.6652F}, 
                           {0.2136F, 0.2609F, 0.5254F} };
xiaotong committed
48 49 50 51 52

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
53 54
    XTensor * x = NewTensor(order, dimSize);
    XTensor * y = NewTensor(order, dimSize);
55
    XTensor yUser;
xiaotong committed
56 57

    /* initialize variables */
58
    x->SetData(xData, unitNum);
xiaotong committed
59 60 61
    y->SetZeroAll();

    /* call Softmax function */
62
    _Softmax(x, y, 1);
63
    yUser = Softmax(*x, 1);
xiaotong committed
64 65
    
    /* check result */
66
	cpuTest = y->CheckData(answer, unitNum, 1e-4F) && yUser.CheckData(answer, unitNum, 1e-4F);
xiaotong committed
67 68 69 70 71 72

#ifdef USE_CUDA
    /* GPU test */
    bool gpuTest = true;

    /* create tensors */
73 74
    XTensor * xGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * yGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
75
    XTensor yUserGPU;
xiaotong committed
76 77

    /* initialize variables */
78
    xGPU->SetData(xData, unitNum);
xiaotong committed
79 80 81
    yGPU->SetZeroAll();

    /* call Softmax function */
82
    _Softmax(xGPU, yGPU, 1);
83
    yUserGPU = Softmax(*xGPU, 1);
xiaotong committed
84 85
    
    /* check result */
86
	gpuTest = yGPU->CheckData(answer, unitNum, 1e-4F) && yUserGPU.CheckData(answer, unitNum, 1e-4F);
xiaotong committed
87 88

    /* destroy variables */
liyinqiao committed
89 90 91 92
    delete x;
    delete y;
    delete xGPU;
    delete yGPU;
93
    delete[] dimSize;
xiaotong committed
94 95 96 97

    return cpuTest && gpuTest;
#else
    /* destroy variables */
98 99 100
    delete x;
    delete y;
    delete[] dimSize;
xiaotong committed
101 102 103 104 105

    return cpuTest;
#endif // USE_CUDA
}

liyinqiao committed
106 107 108
/* 
case 2: test SoftmaxBackward function.
SoftmaxBackward function: dE/dx_j = -gold_j + y_j
109
In this case, LossName=CROSSENTROPY.
xiaotong committed
110 111 112
*/
bool TestSoftmax2()
{
liyinqiao committed
113
    /* a input tensor of size (2, 3) */
114 115 116 117
    int order = 2;
    int * dimSize = new int[order];
    dimSize[0] = 1;
    dimSize[1] = 3;
xiaotong committed
118

119 120 121
    int unitNum = 1;
    for (int i = 0; i < order; i++)
        unitNum *= dimSize[i];
xiaotong committed
122 123 124

    DTYPE xData[1][3] = { {0.0F, 1.0F, 2.0F} };
    DTYPE gData[1][3] = { {0.0F, 0.0F, 1.0F} };
125 126
    DTYPE yAnswer[1][3] = { {0.0900F, 0.2447F, 0.6652F} };
    DTYPE dedxAnswer[1][3] = {0.0900F, 0.2447F, -0.3347F};
xiaotong committed
127 128 129 130 131

    /* CPU test */
    bool cpuTest = true;

    /* create tensors */
132 133 134 135 136
    XTensor * x = NewTensor(order, dimSize);
    XTensor * y = NewTensor(order, dimSize);
    XTensor * g = NewTensor(order, dimSize);
    XTensor * dedy = NewTensor(order, dimSize);
    XTensor * dedx = NewTensor(order, dimSize);
xiaotong committed
137 138

    /* initialize variables */
139 140
    x->SetData(xData, unitNum);
    g->SetData(gData, unitNum);
xiaotong committed
141 142 143 144 145
    y->SetZeroAll();
    dedx->SetZeroAll();
    dedy->SetZeroAll();

    /* call Softmax function */
146
    _Softmax(x, y, 1);
147 148
    
    /* call SoftmaxBackward function */
149
    _SoftmaxBackward(g, y, x, dedy, dedx, NULL, 1, CROSSENTROPY);
xiaotong committed
150 151
    
    /* check result */
152 153
    cpuTest = y->CheckData(yAnswer, unitNum, 1e-4F)
              && dedx->CheckData(dedxAnswer, unitNum, 1e-4F);
xiaotong committed
154 155 156 157 158 159

#ifdef USE_CUDA
    /* GPU test */
    bool gpuTest = true;

        /* create tensors */
160 161 162 163 164
    XTensor * xGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * yGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * gGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * dedyGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
    XTensor * dedxGPU = NewTensor(order, dimSize, X_FLOAT, 1.0F, 0);
xiaotong committed
165 166

    /* initialize variables */
167 168
    xGPU->SetData(xData, unitNum);
    gGPU->SetData(gData, unitNum);
xiaotong committed
169 170 171 172 173
    yGPU->SetZeroAll();
    dedxGPU->SetZeroAll();
    dedyGPU->SetZeroAll();

    /* call Softmax function */
174
    _Softmax(xGPU, yGPU, 1);
xiaotong committed
175 176

    /* call SoftmaxBackward function */
177
    _SoftmaxBackward(gGPU, yGPU, xGPU, dedyGPU, dedxGPU, NULL, 1, CROSSENTROPY);
xiaotong committed
178 179
    
    /* check result */
180 181
    gpuTest = yGPU->CheckData(yAnswer, unitNum, 1e-4F)
              && dedxGPU->CheckData(dedxAnswer, unitNum, 1e-4F);
xiaotong committed
182 183

    /* destroy variables */
liyinqiao committed
184 185 186 187 188 189 190 191 192 193
    delete x;
    delete y;
    delete g;
    delete dedx;
    delete dedy;
    delete xGPU;
    delete yGPU;
    delete gGPU;
    delete dedxGPU;
    delete dedyGPU;
194
    delete[] dimSize;
xiaotong committed
195 196 197 198

    return cpuTest && gpuTest;
#else
    /* destroy variables */
liyinqiao committed
199 200 201 202 203
    delete x;
    delete y;
    delete g;
    delete dedx;
    delete dedy;
204
    delete[] dimSize;
xiaotong committed
205 206 207 208 209 210 211 212 213 214 215 216 217

    return cpuTest;
#endif // USE_CUDA
}

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

/* test for Softmax Function */
bool TestSoftmax()
{
liyinqiao committed
218
    XPRINT(0, stdout, "[TEST SOFTMAX] softmax function and its backward computation \n");
xiaotong committed
219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257
    bool returnFlag = true, caseFlag = true;

    /* case 1 test */
    caseFlag = TestSoftmax1();

    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 1 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 1 passed!\n");

    /* case 2 test */
    caseFlag = TestSoftmax2();

    if (!caseFlag) {
        returnFlag = false;
        XPRINT(0, stdout, ">> case 2 failed!\n");
    }
    else
        XPRINT(0, stdout, ">> case 2 passed!\n");

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

    if (returnFlag) {
        XPRINT(0, stdout, ">> All Passed!\n");
    }
    else
        XPRINT(0, stdout, ">> Failed!\n");

    XPRINT(0, stdout, "\n");

    return returnFlag;
}

} // namespace nts(NiuTrans.Tensor)