summaryrefslogtreecommitdiffstats
path: root/mlplib/backward_test.py
blob: f3b595e9790963ed45141a9b2376d0dd039d7abc (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
"""
# -*- 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 *
import numpy as np

def test_backward_prop():
    seed(42)

    inputs = 4
    layout = (6, 6, 9, 2)
    params = Parameters(
        weights=init_layers_weights(inputs, layout),
        biases=init_layers_biases(layout),
        actvns=[
            ReLU(),
            LReLU(0.1),
            Tanh(),
            Sigmoid(),
        ],
    )
    x = standard_normal((20, inputs))
    y = standard_normal((20, layout[-1]))
    gradients, yh = backward_prop(x, y, params, MSE())

    yh2 = forward_prop(x, params)
    assert np.all(yh == yh2)

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

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