test_activation_nn_grad.py 13.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#   Copyright (c) 2019 PaddlePaddle Authors. 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.

from __future__ import print_function

import unittest
import numpy as np

import paddle.fluid as fluid
21
import paddle
22 23 24
import paddle.fluid.layers as layers
import paddle.fluid.core as core
import gradient_checker
25
import paddle.nn.functional as F
26
from paddle.fluid.framework import _test_eager_guard
27 28 29 30

from decorator_helper import prog_scope


31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
class TestSigmoidTripleGradCheck(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0005
        dtype = np.float64
        x = layers.data('x', shape, False, dtype=dtype)
        x.persistable = True
        y = layers.sigmoid(x)
        x_arr = np.random.random(shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.002
        gradient_checker.triple_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)

    def test_grad(self):
46
        paddle.enable_static()
47 48 49 50 51 52 53
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


54
class TestSigmoidDoubleGradCheck(unittest.TestCase):
55 56 57
    def sigmoid_wrapper(self, x):
        return fluid.layers.sigmoid(x[0])

58 59 60 61 62 63 64 65 66 67 68 69
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0005
        dtype = np.float64
        x = layers.data('x', shape, False, dtype=dtype)
        x.persistable = True
        y = layers.sigmoid(x)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.002
        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
70 71
        gradient_checker.double_grad_check_for_dygraph(
            self.sigmoid_wrapper, [x], y, x_init=x_arr, place=place)
72 73

    def test_grad(self):
74
        paddle.enable_static()
75 76 77 78 79 80 81
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


82
class TestTanhTripleGradCheck(unittest.TestCase):
83 84 85
    def tanh_wrapper(self, x):
        return paddle.tanh(x[0])

86 87 88 89 90 91 92 93 94 95 96 97
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0005
        dtype = np.float64
        x = layers.data('x', shape, False, dtype=dtype)
        x.persistable = True
        y = layers.tanh(x)
        x_arr = np.random.random(shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.002
        gradient_checker.triple_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
98 99
        gradient_checker.triple_grad_check_for_dygraph(
            self.tanh_wrapper, [x], y, x_init=x_arr, place=place)
100 101

    def test_grad(self):
102
        paddle.enable_static()
103 104 105 106 107 108 109
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


110
class TestTanhDoubleGradCheck(unittest.TestCase):
111 112 113
    def tanh_wrapper(self, x):
        return paddle.tanh(x[0])

114 115 116 117 118 119 120 121 122 123 124 125
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0005
        dtype = np.float64
        x = layers.data('x', shape, False, dtype=dtype)
        x.persistable = True
        y = paddle.tanh(x)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.002
        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
126 127
        gradient_checker.double_grad_check_for_dygraph(
            self.tanh_wrapper, [x], y, x_init=x_arr, place=place)
128 129

    def test_grad(self):
130
        paddle.enable_static()
131 132 133 134 135 136 137
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165
class TestAbsDoubleGradCheck(unittest.TestCase):
    def abs_wrapper(self, x):
        return paddle.abs(x[0])

    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0005
        dtype = np.float64
        x = layers.data('x', shape, False, dtype=dtype)
        x.persistable = True
        y = paddle.abs(x)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.002
        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
        gradient_checker.double_grad_check_for_dygraph(
            self.abs_wrapper, [x], y, x_init=x_arr, place=place)

    def test_grad(self):
        paddle.enable_static()
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
class TestReluDoubleGradCheck(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.005
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True
        y = layers.relu(x)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.02

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)

    def test_grad(self):
183
        paddle.enable_static()
184 185 186 187 188 189 190 191
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


class TestLeakyReluDoubleGradCheck(unittest.TestCase):
192 193 194
    def leaky_relu_wrapper(self, x):
        return paddle.nn.functional.leaky_relu(x[0], negative_slope=0.2)

195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.005
        alpha = 0.2
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True

        y = layers.leaky_relu(x, alpha=alpha)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        x_arr[np.abs(x_arr) < 0.005] = 0.02

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
211 212
        gradient_checker.double_grad_check_for_dygraph(
            self.leaky_relu_wrapper, [x], y, x_init=x_arr, place=place)
213 214

    def test_grad(self):
215
        paddle.enable_static()
216 217 218 219 220 221 222
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places = [fluid.CUDAPlace(0)]
        for p in places:
            self.func(p)


D
Double_V 已提交
223
class TestELUDoubleGradCheck(unittest.TestCase):
224 225 226
    def elu_wrapper(self, x):
        return paddle.nn.functional.elu(x[0], alpha=0.2)

D
Double_V 已提交
227 228
    @prog_scope()
    def func(self, place):
229
        shape = [2, 4, 4, 4]
D
Double_V 已提交
230
        eps = 1e-6
231
        alpha = 0.2
D
Double_V 已提交
232
        dtype = np.float64
233
        SEED = 0
D
Double_V 已提交
234 235 236 237 238

        x = layers.data('x', shape, False, dtype)
        x.persistable = True

        y = layers.elu(x, alpha=alpha)
239
        np.random.RandomState(SEED)
D
Double_V 已提交
240 241 242
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
243 244
        gradient_checker.double_grad_check_for_dygraph(
            self.elu_wrapper, [x], y, x_init=x_arr, place=place)
D
Double_V 已提交
245 246

    def test_grad(self):
247
        paddle.enable_static()
D
Double_V 已提交
248 249 250 251 252 253 254
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


255
class TestCELUDoubleGradCheck(unittest.TestCase):
256 257 258
    def celu_wrapper(self, x):
        return paddle.nn.functional.celu(x[0], alpha=0.2)

259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274
    @prog_scope()
    def func(self, place):
        shape = [2, 4, 4, 4]
        eps = 1e-6
        alpha = 0.2
        dtype = np.float64
        SEED = 0

        x = layers.data('x', shape, False, dtype)
        x.persistable = True

        y = F.celu(x, alpha=alpha)
        np.random.RandomState(SEED)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
275 276
        gradient_checker.double_grad_check_for_dygraph(
            self.celu_wrapper, [x], y, x_init=x_arr, place=place)
277 278

    def test_grad(self):
279
        paddle.enable_static()
280 281 282 283 284 285 286
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


287
class TestSqrtDoubleGradCheck(unittest.TestCase):
288 289 290
    def sqrt_wrapper(self, x):
        return paddle.sqrt(x[0])

291 292 293 294 295 296 297 298 299 300 301 302 303 304
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0001
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True

        y = layers.sqrt(x)
        x_arr = np.random.uniform(0.1, 1, shape).astype(dtype)

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
305 306
        gradient_checker.double_grad_check_for_dygraph(
            self.sqrt_wrapper, [x], y, x_init=x_arr, place=place)
307 308

    def test_grad(self):
309
        paddle.enable_static()
310 311 312 313 314 315 316
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places = [fluid.CUDAPlace(0)]
        for p in places:
            self.func(p)


W
whs 已提交
317
class TestRsqrtDoubleGradCheck(unittest.TestCase):
318 319 320
    def rsqrt_wrapper(self, x):
        return paddle.rsqrt(x[0])

W
whs 已提交
321 322 323 324 325 326 327 328 329 330 331 332 333 334
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 0.0001
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True

        y = layers.rsqrt(x)
        x_arr = np.random.uniform(0.1, 1, shape).astype(dtype)

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
335 336
        gradient_checker.double_grad_check_for_dygraph(
            self.rsqrt_wrapper, [x], y, x_init=x_arr, place=place)
W
whs 已提交
337 338

    def test_grad(self):
339
        paddle.enable_static()
W
whs 已提交
340 341 342 343 344 345 346
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places = [fluid.CUDAPlace(0)]
        for p in places:
            self.func(p)


347
class TestSquareDoubleGradCheck(unittest.TestCase):
348 349 350
    def square_wrapper(self, x):
        return paddle.square(x[0])

351 352
    @prog_scope()
    def func(self, place):
T
tianshuo78520a 已提交
353
        # the shape of input variable should be clearly specified, not inlcude -1.
354 355 356 357 358 359 360 361 362 363 364
        shape = [2, 3, 7, 9]
        eps = 0.005
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True
        y = layers.square(x)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
365 366
        gradient_checker.double_grad_check_for_dygraph(
            self.square_wrapper, [x], y, x_init=x_arr, place=place)
367 368

    def test_grad(self):
369
        paddle.enable_static()
370 371 372 373 374 375 376
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


Z
Zhong Hui 已提交
377 378 379 380 381 382 383 384 385 386 387 388
class TestAbsDoubleGradCheck(unittest.TestCase):
    @prog_scope()
    def func(self, place):
        # the shape of input variable should be clearly specified, not inlcude -1.
        shape = [2, 3, 7, 9]
        eps = 1e-6
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True
        y = layers.abs(x)
        x_arr = np.random.uniform(-1, 1, shape).astype(dtype)
Z
Zhong Hui 已提交
389 390 391 392
        # Because we set delta = 0.005 in calculating numeric gradient,
        # if x is too small, the numeric gradient is inaccurate.
        # we should avoid this
        x_arr[np.abs(x_arr) < 0.005] = 0.02
Z
Zhong Hui 已提交
393 394 395 396 397

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)

    def test_grad(self):
398
        paddle.enable_static()
Z
Zhong Hui 已提交
399 400 401 402 403 404 405
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


406
class TestLogDoubleGradCheck(unittest.TestCase):
407 408 409
    def log_wrapper(self, x):
        return paddle.log(x[0])

410 411 412 413 414 415 416 417 418 419 420 421 422 423
    @prog_scope()
    def func(self, place):
        shape = [2, 3, 7, 9]
        eps = 1e-6
        dtype = np.float64

        x = layers.data('x', shape, False, dtype)
        x.persistable = True
        y = layers.log(x)

        x_arr = np.random.uniform(0.1, 1, shape).astype(dtype)

        gradient_checker.double_grad_check(
            [x], y, x_init=x_arr, place=place, eps=eps)
424 425
        gradient_checker.double_grad_check_for_dygraph(
            self.log_wrapper, [x], y, x_init=x_arr, place=place)
426 427

    def test_grad(self):
428
        paddle.enable_static()
429 430 431 432 433 434 435
        places = [fluid.CPUPlace()]
        if core.is_compiled_with_cuda():
            places.append(fluid.CUDAPlace(0))
        for p in places:
            self.func(p)


436 437
if __name__ == "__main__":
    unittest.main()