test_cross_entropy_op.py 13.3 KB
Newer Older
1
#   Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved.
D
dzhwinter 已提交
2
#
D
dzhwinter 已提交
3 4 5
# 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
D
dzhwinter 已提交
6
#
D
dzhwinter 已提交
7
#     http://www.apache.org/licenses/LICENSE-2.0
D
dzhwinter 已提交
8
#
D
dzhwinter 已提交
9 10 11 12 13 14
# 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.

Q
Qiao Longfei 已提交
15
import unittest
16
import numpy as np
C
chengduo 已提交
17
import paddle.fluid.core as core
18
from op_test import OpTest, randomize_probability
19 20
import paddle.fluid as fluid
from paddle.fluid import Program, program_guard
Q
Qiao Longfei 已提交
21 22


C
chengduo 已提交
23
class TestCrossEntropyOp(OpTest):
24
    """Test cross-entropy with discrete one-hot labels."""
25

Q
Qiao Longfei 已提交
26
    def setUp(self):
27
        self.op_type = "cross_entropy"
C
chengduo 已提交
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
        self.soft_label = False
        self.ignore_index = -100
        self.dtype = np.float64
        self.batch_size = 30
        self.class_num = 10

        self.init_dtype_type()
        self.init_attr_type()
        self.init_bs_class_num()
        self.init_x()
        self.init_label()
        self.get_cross_entropy()

        self.inputs = {"X": self.x, "Label": self.label}
        self.outputs = {"Y": self.cross_entropy}
        self.attrs = {
            "soft_label": self.soft_label,
45
            "ignore_index": self.ignore_index,
C
chengduo 已提交
46 47 48
        }

    def init_x(self):
49 50 51
        self.x = randomize_probability(
            self.batch_size, self.class_num, dtype=self.dtype
        )
C
chengduo 已提交
52 53

    def init_label(self):
54 55 56
        self.label = np.random.randint(
            0, self.class_num, (self.batch_size, 1), dtype="int64"
        )
C
chengduo 已提交
57 58 59

    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
60 61 62 63 64 65
            [
                [-np.log(self.x[i][self.label[i][0]])]
                for i in range(self.x.shape[0])
            ],
            dtype="float64",
        )
C
caoying03 已提交
66

C
chengduo 已提交
67 68
    def init_attr_type(self):
        pass
69

C
chengduo 已提交
70 71
    def init_dtype_type(self):
        pass
C
caoying03 已提交
72

C
chengduo 已提交
73 74
    def init_bs_class_num(self):
        pass
Q
Qiao Longfei 已提交
75

76
    def test_check_output(self):
Q
qijun 已提交
77
        self.check_output()
Q
Qiao Longfei 已提交
78

79
    def test_check_grad(self):
80
        self.check_grad(["X"], "Y", numeric_grad_delta=0.001)
81

Y
Yan Chunwei 已提交
82

83
class TestCrossEntropyOpRemoveLastDim(TestCrossEntropyOp):
84
    """Test cross-entropy with discrete one-hot labels with shape [batch_size]"""
85 86

    def init_label(self):
87 88 89
        self.label = np.random.randint(
            0, self.class_num, (self.batch_size), dtype="int64"
        )
90 91 92

    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
93
            [-np.log(self.x[i][self.label[i]]) for i in range(self.x.shape[0])],
94 95
            dtype="float64",
        )
96 97


C
chengduo 已提交
98
class TestCrossEntropyOp2(TestCrossEntropyOp):
99
    """Test cross-entropy with vectorized soft labels."""
100

C
chengduo 已提交
101 102
    def init_label(self):
        self.label = np.random.uniform(
103 104
            0.1, 1.0, [self.batch_size, self.class_num]
        ).astype(self.dtype)
C
chengduo 已提交
105
        self.label /= self.label.sum(axis=1, keepdims=True)
C
caoying03 已提交
106

C
chengduo 已提交
107
    def get_cross_entropy(self):
108 109 110 111 112
        self.cross_entropy = (
            (-self.label * np.log(self.x))
            .sum(axis=1, keepdims=True)
            .astype(self.dtype)
        )
C
caoying03 已提交
113

C
chengduo 已提交
114 115
    def init_attr_type(self):
        self.soft_label = True
116

C
chengduo 已提交
117
    def init_dtype_type(self):
118
        self.dtype = np.float64
C
chengduo 已提交
119 120 121 122

    def init_bs_class_num(self):
        self.batch_size = 5
        self.class_num = 37
123 124

    def test_check_grad(self):
125 126 127
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
128 129


C
chengduo 已提交
130
class TestCrossEntropyOp3(TestCrossEntropyOp):
131
    """Test cross-entropy with vectorized one-hot representation of labels."""
132

C
chengduo 已提交
133
    def init_label(self):
134 135 136
        self.label_index = np.random.randint(
            0, self.class_num, (self.batch_size)
        )
C
chengduo 已提交
137 138
        self.label = np.zeros(self.x.shape).astype(self.dtype)
        self.label[np.arange(self.batch_size), self.label_index] = 1
C
caoying03 已提交
139

C
chengduo 已提交
140 141
    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
142 143 144 145 146
            [
                [-np.log(self.x[i][self.label_index[i]])]
                for i in range(self.x.shape[0])
            ]
        ).astype(self.dtype)
C
caoying03 已提交
147

C
chengduo 已提交
148 149
    def init_attr_type(self):
        self.soft_label = True
C
caoying03 已提交
150

C
chengduo 已提交
151
    def init_dtype_type(self):
152
        self.dtype = np.float64
153

C
chengduo 已提交
154 155
    def init_bs_class_num(self):
        self.batch_size = 5
Z
zhupengyang 已提交
156
        self.class_num = 27
157 158

    def test_check_grad(self):
159 160 161
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
162 163


C
chengduo 已提交
164
class TestCrossEntropyOp4(TestCrossEntropyOp):
165
    """Test high rank tensor cross-entropy with discrete one-hot labels."""
166

C
chengduo 已提交
167 168 169
    def init_x(self):
        self.shape = [10, 2, 4]
        self.ins_num = np.prod(np.array(self.shape))
170 171 172
        self.X_2d = randomize_probability(self.ins_num, self.class_num).astype(
            self.dtype
        )
C
chengduo 已提交
173
        self.x = self.X_2d.reshape(self.shape + [self.class_num])
174

C
chengduo 已提交
175
    def init_label(self):
176 177 178
        self.label_2d = np.random.randint(
            0, self.class_num, (self.ins_num, 1), dtype="int64"
        )
C
chengduo 已提交
179
        self.label = self.label_2d.reshape(self.shape + [1])
180

C
chengduo 已提交
181
    def get_cross_entropy(self):
182
        cross_entropy_2d = np.asmatrix(
183 184 185 186 187 188 189 190
            [
                [-np.log(self.X_2d[i][self.label_2d[i][0]])]
                for i in range(self.X_2d.shape[0])
            ]
        ).astype(self.dtype)
        self.cross_entropy = np.array(cross_entropy_2d).reshape(
            self.shape + [1]
        )
191

C
chengduo 已提交
192 193
    def init_attr_type(self):
        self.soft_label = False
194

C
chengduo 已提交
195 196
    def init_dtype_type(self):
        self.dtype = np.float64
197

C
chengduo 已提交
198 199
    def init_bs_class_num(self):
        self.class_num = 10
200 201


202
class TestCrossEntropyOp4RemoveLastDim(TestCrossEntropyOp4):
203
    """Test high rank tensor cross-entropy with discrete one-hot labels with shape [batch_size]"""
204 205

    def init_label(self):
206 207 208
        self.label_2d = np.random.randint(
            0, self.class_num, (self.ins_num, 1), dtype="int64"
        )
209 210 211 212
        self.label = self.label_2d.reshape(self.shape)

    def get_cross_entropy(self):
        cross_entropy_2d = np.asmatrix(
213 214 215 216 217
            [
                [-np.log(self.X_2d[i][self.label_2d[i][0]])]
                for i in range(self.X_2d.shape[0])
            ]
        ).astype(self.dtype)
218 219 220
        self.cross_entropy = np.array(cross_entropy_2d).reshape(self.shape)


C
chengduo 已提交
221
class TestCrossEntropyOp5(TestCrossEntropyOp):
222
    """Test high rank tensor cross-entropy with vectorized soft labels."""
223

C
chengduo 已提交
224 225 226
    def init_x(self):
        self.shape = [4, 3]
        self.ins_num = np.prod(np.array(self.shape))
227 228 229
        self.X_2d = randomize_probability(self.ins_num, self.class_num).astype(
            self.dtype
        )
C
chengduo 已提交
230
        self.x = self.X_2d.reshape(self.shape + [self.class_num])
231

C
chengduo 已提交
232 233
    def init_label(self):
        self.label_2d = np.random.uniform(
234 235
            0.1, 1.0, [self.ins_num, self.class_num]
        ).astype(self.dtype)
C
chengduo 已提交
236 237
        self.label_2d /= self.label_2d.sum(axis=1, keepdims=True)
        self.label = self.label_2d.reshape(self.shape + [self.class_num])
238

C
chengduo 已提交
239
    def get_cross_entropy(self):
240 241 242 243 244 245 246 247
        cross_entropy_2d = (
            (-self.label_2d * np.log(self.X_2d))
            .sum(axis=1, keepdims=True)
            .astype(self.dtype)
        )
        self.cross_entropy = np.array(cross_entropy_2d).reshape(
            self.shape + [1]
        )
248

C
chengduo 已提交
249 250
    def init_attr_type(self):
        self.soft_label = True
251

C
chengduo 已提交
252
    def init_dtype_type(self):
253
        self.dtype = np.float64
C
chengduo 已提交
254 255 256

    def init_bs_class_num(self):
        self.class_num = 37
257 258

    def test_check_grad(self):
259 260 261
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
262 263


C
chengduo 已提交
264
class TestCrossEntropyOp6(TestCrossEntropyOp):
265
    """Test high rank tensor cross-entropy with vectorized one-hot representation of labels."""
266

C
chengduo 已提交
267 268 269
    def init_x(self):
        self.shape = [4, 3, 2]
        self.ins_num = np.prod(np.array(self.shape))
270 271 272
        self.X_2d = randomize_probability(self.ins_num, self.class_num).astype(
            self.dtype
        )
C
chengduo 已提交
273 274 275
        self.x = self.X_2d.reshape(self.shape + [self.class_num])

    def init_label(self):
276 277 278
        self.label_index_2d = np.random.randint(
            0, self.class_num, (self.ins_num), dtype="int64"
        )
C
chengduo 已提交
279 280 281
        label_2d = np.zeros(self.X_2d.shape)
        label_2d[np.arange(self.ins_num), self.label_index_2d] = 1
        self.label = label_2d.reshape(self.shape + [self.class_num]).astype(
282 283
            self.dtype
        )
C
chengduo 已提交
284 285

    def get_cross_entropy(self):
286
        cross_entropy_2d = np.asmatrix(
287 288 289 290 291 292 293 294 295 296
            [
                [-np.log(self.X_2d[i][self.label_index_2d[i]])]
                for i in range(self.X_2d.shape[0])
            ]
        )
        self.cross_entropy = (
            np.array(cross_entropy_2d)
            .reshape(self.shape + [1])
            .astype(self.dtype)
        )
297

C
chengduo 已提交
298 299
    def init_attr_type(self):
        self.soft_label = True
300

C
chengduo 已提交
301
    def init_dtype_type(self):
302
        self.dtype = np.float64
303

C
chengduo 已提交
304 305
    def init_bs_class_num(self):
        self.class_num = 17
306 307

    def test_check_grad(self):
308 309 310
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
311 312


C
chengduo 已提交
313
class TestCrossEntropyOp7(TestCrossEntropyOp):
314
    """Test cross-entropy with ignore index."""
315

C
chengduo 已提交
316
    def init_label(self):
317 318 319
        self.label = np.random.randint(
            0, self.class_num, (self.batch_size, 1), dtype="int64"
        )
C
chengduo 已提交
320 321 322

    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
323 324 325 326 327 328 329
            [
                [-np.log(self.x[i][self.label[i][0]])]
                if self.label[i][0] != self.ignore_index
                else [0]
                for i in range(self.x.shape[0])
            ]
        ).astype(self.dtype)
C
chengduo 已提交
330 331 332 333 334 335 336 337 338 339 340 341 342

    def init_attr_type(self):
        self.soft_label = False
        self.ignore_index = 3

    def init_dtype_type(self):
        self.dtype = np.float64

    def init_bs_class_num(self):
        self.batch_size = 30
        self.class_num = 10


343
class TestCrossEntropyOp7RemoveLastDim(TestCrossEntropyOp7):
344
    """Test cross-entropy with ignore index with shape [batch_size]."""
345 346

    def init_label(self):
347 348 349
        self.label = np.random.randint(
            0, self.class_num, (self.batch_size), dtype="int64"
        )
350 351 352

    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
353 354 355 356 357 358 359 360 361 362 363 364
            [
                [-np.log(self.x[i][self.label[i]])]
                if self.label[i] != self.ignore_index
                else [0]
                for i in range(self.x.shape[0])
            ]
        ).astype(self.dtype)
        self.cross_entropy = (
            np.array(self.cross_entropy)
            .reshape([self.batch_size])
            .astype(self.dtype)
        )
365 366


C
chengduo 已提交
367 368
# Add Fp16 test
def create_test_class(parent, cls_name):
369 370 371
    @unittest.skipIf(
        not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
    )
C
chengduo 已提交
372 373 374 375 376 377 378 379 380 381 382 383
    class TestCrossEntropyFP16Op(parent):
        def init_dtype_type(self):
            return np.float16

        def test_check_output(self):
            place = core.CUDAPlace(0)
            if core.is_float16_supported(place):
                self.check_output_with_place(place, atol=2e-1)

        def test_check_grad(self):
            place = core.CUDAPlace(0)
            if core.is_float16_supported(place):
384 385 386
                self.check_grad_with_place(
                    place, ['X'], 'Y', max_relative_error=0.9
                )
C
chengduo 已提交
387 388 389 390 391 392 393

    cls_name = "{0}".format(cls_name)
    TestCrossEntropyFP16Op.__name__ = cls_name
    globals()[cls_name] = TestCrossEntropyFP16Op


create_test_class(TestCrossEntropyOp, "TestCrossEntropyF16Op")
394
# create_test_class(TestCrossEntropyOp2, "TestCrossEntropyF16Op2")
C
chengduo 已提交
395 396
create_test_class(TestCrossEntropyOp3, "TestCrossEntropyF16Op3")
create_test_class(TestCrossEntropyOp4, "TestCrossEntropyF16Op4")
397 398 399 400
create_test_class(
    TestCrossEntropyOp4RemoveLastDim, "TestCrossEntropyF16Op4RemoveLastDim"
)
# create_test_class(TestCrossEntropyOp5, "TestCrossEntropyF16Op5")
C
chengduo 已提交
401 402
create_test_class(TestCrossEntropyOp6, "TestCrossEntropyF16Op6")
create_test_class(TestCrossEntropyOp7, "TestCrossEntropyF16Op7")
403 404 405
create_test_class(
    TestCrossEntropyOp7RemoveLastDim, "TestCrossEntropyF16Op7RemoveLastDim"
)
406

407

408
class TestCrossEntropyOpError(unittest.TestCase):
409 410 411 412 413
    def test_errors(self):
        with program_guard(Program(), Program()):

            def test_Variable():
                # the input of cross_entropy must be Variable.
414 415 416 417 418 419
                x1 = fluid.create_lod_tensor(
                    np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace()
                )
                lab1 = fluid.create_lod_tensor(
                    np.array([-1, 3, 5, 5]), [[1, 1, 1, 1]], fluid.CPUPlace()
                )
420 421 422 423 424 425 426
                fluid.layers.cross_entropy(x1, lab1)

            self.assertRaises(TypeError, test_Variable)

            def test_dtype():
                # the input dtype of cross_entropy must be float16 or float32 or float64
                # float16 only can be set on GPU place
427 428 429 430 431 432
                x2 = fluid.layers.data(
                    name='x2', shape=[3, 4, 5, 6], dtype="int32"
                )
                lab2 = fluid.layers.data(
                    name='lab2', shape=[3, 4, 5, 6], dtype="int32"
                )
433 434 435 436 437
                fluid.layers.cross_entropy(x2, lab2)

            self.assertRaises(TypeError, test_dtype)


Q
Qiao Longfei 已提交
438 439
if __name__ == "__main__":
    unittest.main()