diff --git a/python/requirements.txt b/python/requirements.txt index 086b203a6d..d1a267f64f 100644 --- a/python/requirements.txt +++ b/python/requirements.txt @@ -3,4 +3,3 @@ future decorator opm numpy -tensorflow-cpu diff --git a/python/test/ml/hybrid_newton/test_hybrid_newton.py b/python/test/ml/hybrid_newton/test_hybrid_newton.py index 839e5b501c..e032683f05 100644 --- a/python/test/ml/hybrid_newton/test_hybrid_newton.py +++ b/python/test/ml/hybrid_newton/test_hybrid_newton.py @@ -4,10 +4,10 @@ from pathlib import Path import numpy as np import subprocess +from .utils import Dense, Sequential from .utils import collect_input_features, compute_output_vars, write_config, extract_newtit_from_file, extract_unrst_variables from .utils import (ABSOLUTE_CASES, RELATIVE_CASES, FEATURE_ENGINEERING_CASES, SCALING_CASES, MULTI_MODEL_CASES, ZERO_NEWTON_CASES, ALL_CASES) -import tensorflow as tf from kerasify import export_model @@ -89,9 +89,8 @@ class TestHybridNewton(unittest.TestCase): input_flat = input_flat.reshape(-1) output_flat = output_flat.reshape(-1) - model = tf.keras.Sequential([ - tf.keras.Input(shape=(input_flat.shape[0],)), - tf.keras.layers.Dense(output_flat.shape[0], use_bias=True) + model = Sequential([ + Dense(input_dim=input_flat.shape[0], output_dim=output_flat.shape[0]) ]) model.layers[0].set_weights([np.zeros((input_flat.shape[0], output_flat.shape[0])), output_flat]) apply_time = self.times[start_idx] @@ -197,3 +196,5 @@ class TestHybridNewton(unittest.TestCase): f"got {h}, expected <= {b}" ) +if __name__ == "__main__": + unittest.main() diff --git a/python/test/ml/hybrid_newton/utils.py b/python/test/ml/hybrid_newton/utils.py index 1ba59b1971..20d2468a30 100644 --- a/python/test/ml/hybrid_newton/utils.py +++ b/python/test/ml/hybrid_newton/utils.py @@ -5,6 +5,27 @@ from pathlib import Path from opm.io.ecl import ERst, ESmry +# Avoid tensorflow dependency but maintain compability with kerasify export_model method +class Dense: + def __init__(self, input_dim, output_dim, activation='linear'): + self.weights = np.zeros((input_dim, output_dim), dtype=np.float32) + self.biases = np.zeros((output_dim,), dtype=np.float32) + self.activation = activation + + def set_weights(self, w_b): + self.weights = w_b[0].astype(np.float32) + self.biases = w_b[1].astype(np.float32) + + def get_weights(self): + return [self.weights, self.biases] + + def get_config(self): + return {'activation': self.activation} + +class Sequential: + def __init__(self, layers): + self.layers = layers + def extract_unrst_variables(data_dir, deck_file): """