test_cross_entropy_op.py 14.2 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
W
wanghuancoder 已提交
18
from eager_op_test import OpTest, paddle_static_guard, randomize_probability
19

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


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

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

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

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

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

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

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

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

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

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

Y
Yan Chunwei 已提交
84

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

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

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


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

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

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

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

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

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

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


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

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

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

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

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

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

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


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

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

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

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

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

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

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


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

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

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


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

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

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

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

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

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

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

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


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

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

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

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

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

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

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

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


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

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

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

    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


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

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

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


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

390
    cls_name = f"{cls_name}"
C
chengduo 已提交
391 392 393 394 395
    TestCrossEntropyFP16Op.__name__ = cls_name
    globals()[cls_name] = TestCrossEntropyFP16Op


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

409

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

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

            self.assertRaises(TypeError, test_Variable)

            def test_dtype():
W
wanghuancoder 已提交
429 430 431 432 433 434 435 436 437 438 439 440
                with paddle_static_guard():
                    # the input dtype of cross_entropy must be float16 or float32 or float64
                    # float16 only can be set on GPU place
                    x2 = paddle.static.data(
                        name='x2', shape=[-1, 3, 4, 5, 6], dtype="int32"
                    )
                    lab2 = paddle.static.data(
                        name='lab2', shape=[-1, 3, 4, 5, 6], dtype="int32"
                    )
                    paddle.nn.functional.cross_entropy(
                        x2, lab2, reduction='none', use_softmax=False
                    )
441 442 443

            self.assertRaises(TypeError, test_dtype)

444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
            def test_input_dims():
                with paddle_static_guard():
                    # "input_dims - 1 != label_dims and input_dims != label_dims" must be false.
                    x3 = paddle.static.data(
                        name='x3', shape=[-1, 3, 4, 5], dtype="int32"
                    )
                    lab3 = paddle.static.data(
                        name='lab3', shape=[-1, 3, 4, 5, 6], dtype="int32"
                    )
                    paddle.nn.functional.cross_entropy(
                        x3, lab3, reduction='none', use_softmax=False
                    )

            self.assertRaises(ValueError, test_input_dims)

459

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