|
| 1 | +# Copyright (c) 2019-2020, RTE (https://www.rte-france.com) |
| 2 | +# See AUTHORS.txt |
| 3 | +# This Source Code Form is subject to the terms of the Mozilla Public License, version 2.0. |
| 4 | +# If a copy of the Mozilla Public License, version 2.0 was not distributed with this file, |
| 5 | +# you can obtain one at http://mozilla.org/MPL/2.0/. |
| 6 | +# SPDX-License-Identifier: MPL-2.0 |
| 7 | +# This file is part of leap_net, leap_net a keras implementation of the LEAP Net model. |
| 8 | + |
| 9 | +import logging |
| 10 | +import os |
| 11 | +import numpy as np |
| 12 | +import unittest |
| 13 | +logging.disable(logging.WARNING) |
| 14 | +os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3" |
| 15 | + |
| 16 | +try: |
| 17 | + import keras |
| 18 | + from keras.layers import Input |
| 19 | + from keras.models import Model |
| 20 | + from leap_net.keras import Ltau |
| 21 | +except ImportError: |
| 22 | + pass |
| 23 | + |
| 24 | +import pdb |
| 25 | + |
| 26 | +import sys |
| 27 | + |
| 28 | + |
| 29 | +class Test(unittest.TestCase): |
| 30 | + def setUp(self): |
| 31 | + if sys.version_info.major == 3 and sys.version_info.minor == 8: |
| 32 | + self.skipTest("Keras v3 not available on python 3.8") |
| 33 | + self.tol = 1e-5 # use to compare results that should be strictly equal, up to numerical error |
| 34 | + self.tol_learn = 1e-2 # use to compare results from a test set |
| 35 | + |
| 36 | + # to have "reproducible" results |
| 37 | + np.random.seed(1) |
| 38 | + keras.utils.set_random_seed(1) |
| 39 | + |
| 40 | + def test_ok_tau0(self): |
| 41 | + dim_x = 10 |
| 42 | + n_elem = 5 |
| 43 | + dim_tau = 1 |
| 44 | + |
| 45 | + x = Input(shape=(dim_x,), name="x") |
| 46 | + tau = Input(shape=(dim_tau,), name="tau") |
| 47 | + |
| 48 | + res_model = Ltau()((x, tau)) |
| 49 | + model = Model(inputs=[x, tau], outputs=[res_model]) |
| 50 | + |
| 51 | + X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32) |
| 52 | + TAU_train = np.zeros(shape=(n_elem, dim_tau), dtype=np.float32) |
| 53 | + res = model.predict([X_train, TAU_train]) |
| 54 | + assert np.all(res == X_train) |
| 55 | + |
| 56 | + def test_ok_tau1(self): |
| 57 | + dim_x = 10 |
| 58 | + n_elem = 100 |
| 59 | + dim_tau = 1 |
| 60 | + X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32) |
| 61 | + TAU_train = np.ones(shape=(n_elem, dim_tau), dtype=np.float32) |
| 62 | + |
| 63 | + # the keras model |
| 64 | + x = Input(shape=(dim_x,), name="x") |
| 65 | + tau = Input(shape=(dim_tau,), name="tau") |
| 66 | + res_model = Ltau(initializer='ones', use_bias=False)((x, tau)) |
| 67 | + model = Model(inputs=[x, tau], outputs=[res_model]) |
| 68 | + |
| 69 | + # make predictions |
| 70 | + res = model.predict([X_train, TAU_train]) |
| 71 | + |
| 72 | + # LEAP Net implementation in numpy in case tau is not 0 |
| 73 | + res_th = np.matmul(X_train, np.ones((dim_x, dim_tau), dtype=np.float32)) |
| 74 | + res_th = np.multiply(res_th, TAU_train) |
| 75 | + res_th = np.matmul(res_th, np.ones((dim_tau, dim_x), dtype=np.float32)) |
| 76 | + res_th += X_train |
| 77 | + assert np.mean(np.abs(res - res_th)) <= self.tol, "problem with l1" |
| 78 | + assert np.max(np.abs(res - res_th)) <= self.tol, "problem with linf" |
| 79 | + |
| 80 | + def test_ok_tau_rand(self): |
| 81 | + dim_x = 10 |
| 82 | + n_elem = 100 |
| 83 | + dim_tau = 20 |
| 84 | + |
| 85 | + X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32) |
| 86 | + TAU_train = np.random.normal(size=(n_elem, dim_tau)).astype(np.float32) |
| 87 | + |
| 88 | + # the keras model |
| 89 | + x = Input(shape=(dim_x,), name="x") |
| 90 | + tau = Input(shape=(dim_tau,), name="tau") |
| 91 | + res_model = Ltau(initializer='ones', use_bias=False)((x, tau)) |
| 92 | + model = Model(inputs=[x, tau], outputs=[res_model]) |
| 93 | + |
| 94 | + # make predictions |
| 95 | + res = model.predict([X_train, TAU_train]) |
| 96 | + |
| 97 | + # LEAP Net implementation in numpy in case tau is not 0 |
| 98 | + res_th = np.matmul(X_train, np.ones((dim_x, dim_tau), dtype=np.float32)) |
| 99 | + res_th = np.multiply(res_th, TAU_train) |
| 100 | + res_th = np.matmul(res_th, np.ones((dim_tau, dim_x), dtype=np.float32)) |
| 101 | + res_th += X_train |
| 102 | + assert np.mean(np.abs(res - res_th)) <= self.tol, "problem with l1" |
| 103 | + assert np.max(np.abs(res - res_th)) <= self.tol, "problem with linf" |
| 104 | + |
| 105 | + def test_can_learn(self): |
| 106 | + dim_x = 30 |
| 107 | + n_elem = 32*32 |
| 108 | + dim_tau = 5 |
| 109 | + |
| 110 | + X_train = np.random.normal(size=(n_elem, dim_x)).astype(np.float32) |
| 111 | + TAU_train = np.random.normal(size=(n_elem, dim_tau)).astype(np.float32) |
| 112 | + |
| 113 | + e = np.random.normal(size=(dim_x, dim_tau)).astype(np.float32) |
| 114 | + d = np.random.normal(size=(dim_tau, dim_x)).astype(np.float32) |
| 115 | + |
| 116 | + Y_train = np.matmul(X_train, e) |
| 117 | + Y_train = np.multiply(Y_train, TAU_train) |
| 118 | + Y_train = np.matmul(Y_train, d) |
| 119 | + Y_train += X_train |
| 120 | + |
| 121 | + # the keras model |
| 122 | + x = Input(shape=(dim_x,), name="x") |
| 123 | + tau = Input(shape=(dim_tau,), name="tau") |
| 124 | + res_model = Ltau()((x, tau)) |
| 125 | + model = Model(inputs=[x, tau], outputs=[res_model]) |
| 126 | + |
| 127 | + adam_ = keras.optimizers.Adam(learning_rate=1e-3) |
| 128 | + model.compile(optimizer=adam_, loss='mse') |
| 129 | + ## train it |
| 130 | + model.fit(x=[X_train, TAU_train], y=[Y_train], epochs=200, batch_size=32, verbose=False) |
| 131 | + |
| 132 | + # test it has learn something relevant |
| 133 | + X_test = np.random.normal(size=(n_elem, dim_x)).astype(np.float32) |
| 134 | + TAU_test = np.random.normal(size=(n_elem, dim_tau)).astype(np.float32) |
| 135 | + Y_test = np.matmul(X_test, e) |
| 136 | + Y_test = np.multiply(Y_test, TAU_test) |
| 137 | + Y_test = np.matmul(Y_test, d) |
| 138 | + Y_test += X_test |
| 139 | + res = model.predict([X_test, TAU_test]) |
| 140 | + assert np.mean(np.abs(res - Y_test)) <= self.tol_learn, "problem with l1" |
| 141 | + assert np.max(np.abs(res - Y_test)) <= self.tol_learn, "problem with linf" |
| 142 | + |
| 143 | +# TODO test saving / loading |
| 144 | +# TODO test name and graph visualizing |
| 145 | +# TODO test resnet too |
| 146 | + |
| 147 | + |
| 148 | +if __name__ == "__main__": |
| 149 | + unittest.main() |
0 commit comments