""" # -*- coding: utf-8 -*- # # Copyright 2021 Michael Büsch # # 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