test_cross_entropy_op.py 13.4 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

17
import numpy as np
18
from op_test import OpTest, randomize_probability
19

20
import paddle
21
import paddle.fluid as fluid
22
import paddle.fluid.core as core
23
from paddle.fluid import Program, program_guard
Q
Qiao Longfei 已提交
24 25


C
chengduo 已提交
26
class TestCrossEntropyOp(OpTest):
27
    """Test cross-entropy with discrete one-hot labels."""
28

Q
Qiao Longfei 已提交
29
    def setUp(self):
30
        self.op_type = "cross_entropy"
C
chengduo 已提交
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
        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,
48
            "ignore_index": self.ignore_index,
C
chengduo 已提交
49 50 51
        }

    def init_x(self):
52 53 54
        self.x = randomize_probability(
            self.batch_size, self.class_num, dtype=self.dtype
        )
C
chengduo 已提交
55 56

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

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

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

C
chengduo 已提交
73 74
    def init_dtype_type(self):
        pass
C
caoying03 已提交
75

C
chengduo 已提交
76 77
    def init_bs_class_num(self):
        pass
Q
Qiao Longfei 已提交
78

79
    def test_check_output(self):
Q
qijun 已提交
80
        self.check_output()
Q
Qiao Longfei 已提交
81

82
    def test_check_grad(self):
83
        self.check_grad(["X"], "Y", numeric_grad_delta=0.001)
84

Y
Yan Chunwei 已提交
85

86
class TestCrossEntropyOpRemoveLastDim(TestCrossEntropyOp):
87
    """Test cross-entropy with discrete one-hot labels with shape [batch_size]"""
88 89

    def init_label(self):
90 91 92
        self.label = np.random.randint(
            0, self.class_num, (self.batch_size), dtype="int64"
        )
93 94 95

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


C
chengduo 已提交
101
class TestCrossEntropyOp2(TestCrossEntropyOp):
102
    """Test cross-entropy with vectorized soft labels."""
103

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

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

C
chengduo 已提交
117 118
    def init_attr_type(self):
        self.soft_label = True
119

C
chengduo 已提交
120
    def init_dtype_type(self):
121
        self.dtype = np.float64
C
chengduo 已提交
122 123 124 125

    def init_bs_class_num(self):
        self.batch_size = 5
        self.class_num = 37
126 127

    def test_check_grad(self):
128 129 130
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
131 132


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

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

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

C
chengduo 已提交
151 152
    def init_attr_type(self):
        self.soft_label = True
C
caoying03 已提交
153

C
chengduo 已提交
154
    def init_dtype_type(self):
155
        self.dtype = np.float64
156

C
chengduo 已提交
157 158
    def init_bs_class_num(self):
        self.batch_size = 5
Z
zhupengyang 已提交
159
        self.class_num = 27
160 161

    def test_check_grad(self):
162 163 164
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
165 166


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

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

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

C
chengduo 已提交
184
    def get_cross_entropy(self):
185
        cross_entropy_2d = np.asmatrix(
186 187 188 189 190 191 192 193
            [
                [-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]
        )
194

C
chengduo 已提交
195 196
    def init_attr_type(self):
        self.soft_label = False
197

C
chengduo 已提交
198 199
    def init_dtype_type(self):
        self.dtype = np.float64
200

C
chengduo 已提交
201 202
    def init_bs_class_num(self):
        self.class_num = 10
203 204


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

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

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


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

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

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

C
chengduo 已提交
242
    def get_cross_entropy(self):
243 244 245 246 247 248 249 250
        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]
        )
251

C
chengduo 已提交
252 253
    def init_attr_type(self):
        self.soft_label = True
254

C
chengduo 已提交
255
    def init_dtype_type(self):
256
        self.dtype = np.float64
C
chengduo 已提交
257 258 259

    def init_bs_class_num(self):
        self.class_num = 37
260 261

    def test_check_grad(self):
262 263 264
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
265 266


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

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

    def init_label(self):
279 280 281
        self.label_index_2d = np.random.randint(
            0, self.class_num, (self.ins_num), dtype="int64"
        )
C
chengduo 已提交
282 283 284
        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(
285 286
            self.dtype
        )
C
chengduo 已提交
287 288

    def get_cross_entropy(self):
289
        cross_entropy_2d = np.asmatrix(
290 291 292 293 294 295 296 297 298 299
            [
                [-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)
        )
300

C
chengduo 已提交
301 302
    def init_attr_type(self):
        self.soft_label = True
303

C
chengduo 已提交
304
    def init_dtype_type(self):
305
        self.dtype = np.float64
306

C
chengduo 已提交
307 308
    def init_bs_class_num(self):
        self.class_num = 17
309 310

    def test_check_grad(self):
311 312 313
        self.check_grad(
            ["X"], "Y", max_relative_error=0.05, numeric_grad_delta=0.001
        )
314 315


C
chengduo 已提交
316
class TestCrossEntropyOp7(TestCrossEntropyOp):
317
    """Test cross-entropy with ignore index."""
318

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

    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
326 327 328 329 330 331 332
            [
                [-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 已提交
333 334 335 336 337 338 339 340 341 342 343 344 345

    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


346
class TestCrossEntropyOp7RemoveLastDim(TestCrossEntropyOp7):
347
    """Test cross-entropy with ignore index with shape [batch_size]."""
348 349

    def init_label(self):
350 351 352
        self.label = np.random.randint(
            0, self.class_num, (self.batch_size), dtype="int64"
        )
353 354 355

    def get_cross_entropy(self):
        self.cross_entropy = np.asmatrix(
356 357 358 359 360 361 362 363 364 365 366 367
            [
                [-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)
        )
368 369


C
chengduo 已提交
370 371
# Add Fp16 test
def create_test_class(parent, cls_name):
372 373 374
    @unittest.skipIf(
        not core.is_compiled_with_cuda(), "core is not compiled with CUDA"
    )
C
chengduo 已提交
375 376 377 378 379 380 381 382 383 384 385 386
    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):
387 388 389
                self.check_grad_with_place(
                    place, ['X'], 'Y', max_relative_error=0.9
                )
C
chengduo 已提交
390 391 392 393 394 395 396

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


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

410

411
class TestCrossEntropyOpError(unittest.TestCase):
412 413 414 415 416
    def test_errors(self):
        with program_guard(Program(), Program()):

            def test_Variable():
                # the input of cross_entropy must be Variable.
417 418 419 420 421 422
                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()
                )
423 424 425
                paddle.nn.functional.cross_entropy(
                    x1, lab1, reduction='none', use_softmax=False
                )
426 427 428 429 430 431

            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
432 433 434 435 436 437
                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"
                )
438 439 440
                paddle.nn.functional.cross_entropy(
                    x2, lab2, reduction='none', use_softmax=False
                )
441 442 443 444

            self.assertRaises(TypeError, test_dtype)


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