summaryrefslogtreecommitdiffstats
path: root/mlplib/gradient_check_test.py
blob: 688ba49d4f4b61525ad13b6f7558df1468ec8244 (plain)
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
"""
# -*- coding: utf-8 -*-
#
# Copyright 2021 Michael Büsch <m@bues.ch>
#
# Licensed under the Apache License version 2.0
# or the MIT license, at your option.
# SPDX-License-Identifier: Apache-2.0 OR MIT
#
"""

from mlplib.activation import *
from mlplib.backward import *
from mlplib.forward import *
from mlplib.gradient_check import *
from mlplib.init import *
from mlplib.loss import *
from mlplib.parameters import *

def test_gradient_check_ok():
    seed(42)

    inputs = 3
    layout = (6, 15, 5, 2)
    params = Parameters(
        weights=init_layers_weights(inputs, layout),
        biases=init_layers_biases(layout),
        actvns=[
            ReLU(),
            ReLU(),
            ReLU(),
            Sigmoid(),
        ],
    )
    x = standard_normal((10, inputs))
    y = standard_normal((10, layout[-1]))
    gradients, _ = backward_prop(x, y, params, MSE())

    ok = gradient_check(x, y, params, MSE(), gradients)
    assert ok

def test_gradient_check_notok_weight():
    seed(43)

    inputs = 3
    layout = (6, 15, 5, 2)
    params = Parameters(
        weights=init_layers_weights(inputs, layout),
        biases=init_layers_biases(layout),
        actvns=[
            ReLU(),
            ReLU(),
            ReLU(),
            Sigmoid(),
        ],
    )
    x = standard_normal((10, inputs))
    y = standard_normal((10, layout[-1]))
    gradients, _ = backward_prop(x, y, params, MSE())

    # Manipulate one weight gradient.
    gradients.dw[1][4][3] += 0.01

    ok = gradient_check(x, y, params, MSE(), gradients)
    assert not ok

def test_gradient_check_notok_bias():
    seed(44)

    inputs = 3
    layout = (6, 15, 5, 2)
    params = Parameters(
        weights=init_layers_weights(inputs, layout),
        biases=init_layers_biases(layout),
        actvns=[
            ReLU(),
            ReLU(),
            ReLU(),
            Sigmoid(),
        ],
    )
    x = standard_normal((10, inputs))
    y = standard_normal((10, layout[-1]))
    gradients, _ = backward_prop(x, y, params, MSE())

    # Manipulate one bias gradient.
    gradients.db[1][0][3] += 0.01

    ok = gradient_check(x, y, params, MSE(), gradients)
    assert not ok

# vim: ts=4 sw=4 expandtab
bues.ch cgit interface