def test_wrong_model_type(): """ Test of predict_output() and predict_gradient() when the model type is unknown. """ input = np.random.rand(5, 1) input_scaler = MinMaxScaler() input_scaler.fit(input) model = MLPClassifier() try: predict_output(model, input) assert False except KeyError: assert True try: predict_gradient(model, input) assert False except KeyError: assert True try: predict_gradient(model, input, input_scaler=input_scaler) assert False except KeyError: assert True
def test_predict_gradient_with_output_scaler(): """ Test predict_gradient with an output scaler. """ prng = RandomState(seed=0) input = prng.rand(5, 2) def fun(input): x0, x1 = input.T return x0**2 * np.sin(x1) output_ok = fun(input) output_scaler_mm = MinMaxScaler() output_scaler_std = StandardScaler() output_scaler_mm.fit(output_ok.reshape(-1, 1)) output_scaler_std.fit(output_ok.reshape(-1, 1)) def Dfun_Dx0(input): x0, x1 = input.T return 2 * x0 * np.sin(x1) def Dfun_Dx1(input): x0, x1 = input.T return x0**2 * np.cos(x1) def Dfun(input): x0, x1 = input.T return np.column_stack((2 * x0 * np.sin(x1), x0**2 * np.cos(x1))) # Test input_scaler_mm grad_x0, _ = predict_output(Dfun_Dx0, input) grad_x1, _ = predict_output(Dfun_Dx1, input) grad_ok = np.column_stack((grad_x0, grad_x1)) / output_scaler_mm.scale_ grad_test, _ = predict_gradient(Dfun, input, output_scaler=output_scaler_mm) assert np.array_equal(grad_ok, grad_test) # Test input_scaler_std grad_x0, _ = predict_output(Dfun_Dx0, input) grad_x1, _ = predict_output(Dfun_Dx1, input) grad_ok = np.column_stack((grad_x0, grad_x1)) * output_scaler_std.scale_ grad_test, _ = predict_gradient(Dfun, input, output_scaler=output_scaler_std) assert np.array_equal(grad_ok, grad_test)
def test_predict_gradient_1(): """ Basic test of predict_gradient """ input = {'x0': [1.0, 2.0, 3.0], 'x1': [4.0, 5.0, 6.0]} def fun(input): x0, x1 = input.T return x0**2 * np.sin(x1) def Dfun_Dx0(input): x0, x1 = input.T return 2 * x0 * np.sin(x1) def Dfun_Dx1(input): x0, x1 = input.T return x0**2 * np.cos(x1) def Dfun(input): x0, x1 = input.T return np.column_stack((2 * x0 * np.sin(x1), x0**2 * np.cos(x1))) grad_x0, _ = predict_output(Dfun_Dx0, input, model_in_keys=list(input)) grad_x1, _ = predict_output(Dfun_Dx1, input, model_in_keys=list(input)) grad_ok = np.column_stack((grad_x0, grad_x1)) grad_test, _ = predict_gradient(Dfun, input, model_in_keys=['x0', 'x1']) assert np.array_equal(grad_ok, grad_test)
def test_boundary(): """ Test of predict_output() when there is a boundary. """ prng = RandomState(seed=0) input = -1 + 2 * prng.rand(10, 1) input[2] = 10 input[4] = -5 def model(input): return 2 * input def bound_fun(x): return np.abs(x) < 1 with warnings.catch_warnings(record=True) as w: _, extrapolation_sample = predict_output(model, input, boundary=bound_fun) assert np.array_equal(extrapolation_sample, [2, 4]) assert 'evaluated outside' in str(w[-1].message) with warnings.catch_warnings(record=True) as w: _, extrapolation_sample = predict_gradient(model, input, boundary=bound_fun) assert np.array_equal(extrapolation_sample, [2, 4]) assert 'evaluated outside' in str(w[-1].message)
def test_predict_gradient_with_input_scaler(): """ Test predict_gradient with an input scaler. """ prng = RandomState(seed=0) input = -100.0 + 200.0 * prng.rand(5, 2) input_scaler_mm = MinMaxScaler() input_scaled_mm = input_scaler_mm.fit_transform(input) input_scaler_std = StandardScaler() input_scaled_std = input_scaler_std.fit_transform(input) def fun(input): x0, x1 = input.T return x0**2 * np.sin(x1) def Dfun_Dx0(input): x0, x1 = input.T return 2 * x0 * np.sin(x1) def Dfun_Dx1(input): x0, x1 = input.T return x0**2 * np.cos(x1) def Dfun(input): x0, x1 = input.T return np.column_stack((2 * x0 * np.sin(x1), x0**2 * np.cos(x1))) # Test input_scaler_mm grad_x0, _ = predict_output(Dfun_Dx0, input_scaled_mm) grad_x1, _ = predict_output(Dfun_Dx1, input_scaled_mm) grad_ok = np.column_stack((grad_x0, grad_x1)) * input_scaler_mm.scale_ grad_test, _ = predict_gradient(Dfun, input, input_scaler=input_scaler_mm) assert np.array_equal(grad_ok, grad_test) # Test input_scaler_std grad_x0, _ = predict_output(Dfun_Dx0, input_scaled_std) grad_x1, _ = predict_output(Dfun_Dx1, input_scaled_std) grad_ok = np.column_stack((grad_x0, grad_x1)) / input_scaler_std.scale_ grad_test, _ = predict_gradient(Dfun, input, input_scaler=input_scaler_std) assert np.array_equal(grad_ok, grad_test)
def test_wrong_input_type(): """ Test of predict_output() and predict_gradient() when input has the wrong type. """ input = pd.DataFrame() def model(): pass try: predict_output(model, input) assert False except TypeError: assert True try: predict_gradient(model, input) assert False except TypeError: assert True