Skip to content

Commit

Permalink
dummy
Browse files Browse the repository at this point in the history
  • Loading branch information
fractalmanifold committed Aug 27, 2024
1 parent 0eb5e3d commit fdc8a27
Show file tree
Hide file tree
Showing 32 changed files with 70 additions and 110 deletions.
11 changes: 10 additions & 1 deletion CMakeLists_files.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -475,7 +475,7 @@ list (APPEND TEST_SOURCE_FILES
tests/material/test_spline.cpp
tests/material/test_tabulation.cpp
tests/test_Visitor.cpp
opm/ml/keras_model_test.cpp
tests/ml/keras_model_test.cpp
)

# tests that need to be linked to dune-common
Expand Down Expand Up @@ -648,6 +648,15 @@ list (APPEND TEST_DATA_FILES
tests/material/co2_unittest_below_sat.json
tests/material/h2o_unittest.json
tests/material/h2_unittest.json
tests/ml/ml_tools/models/test_dense_1x1.model
tests/ml/ml_tools/models/test_dense_2x2.model
tests/ml/ml_tools/models/test_dense_10x1.model
tests/ml/ml_tools/models/test_dense_10x10.model
tests/ml/ml_tools/models/test_dense_10x10x10.model
tests/ml/ml_tools/models/test_dense_relu_10.model
tests/ml/ml_tools/models/test_dense_tanh_10.model
tests/ml/ml_tools/models/test_relu_10.model
tests/ml/ml_tools/models/test_scalingdense_10x1.model
)
if(ENABLE_ECL_OUTPUT)
list (APPEND TEST_DATA_FILES
Expand Down
Empty file added opm/ml/ml_tools/__init__.py
Empty file.
Binary file removed opm/ml/ml_tools/models/test_dense_10x1.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_10x10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_10x10x10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_1x1.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_2x2.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_relu_10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_dense_tanh_10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_relu_10.model
Binary file not shown.
Binary file removed opm/ml/ml_tools/models/test_scalingdense_1x1.model
Binary file not shown.
46 changes: 0 additions & 46 deletions opm/ml/ml_tools/scalertest.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@
# You should have received a copy of the GNU General Public License
# along with OPM. If not, see <http://www.gnu.org/licenses/>.



from __future__ import annotations

import pathlib
Expand All @@ -39,58 +37,14 @@

data: np.ndarray = np.random.uniform(-500, 500, (5, 1))

# model: keras.Model = keras.Sequential(
#
# [
#
# keras.layers.Input([10]),
#
# MinMaxScalerLayer(feature_range=feature_ranges[0]),
#
# keras.layers.Dense(units=10),
#
# MinMaxUnScalerLayer(feature_range=feature_ranges[1]),
#
# ]
#
# )

model = Sequential()
model.add(keras.layers.Input([1]))
model.add(MinMaxScalerLayer(feature_range=(0.0, 1.0)))
# model.add(Flatten())
model.add(Dense(1, input_dim=1))
model.add(Dense(1, input_dim=1))
model.add(Dense(1, input_dim=1))
model.add(Dense(1, input_dim=1))

model.add(MinMaxUnScalerLayer(feature_range=(-3.7, -1.0)))
#
# model.get_layer(model.layers[0].name).adapt(data=data)
# model.get_layer(model.layers[-1].name).adapt(data=data)

# model.add(Dense(1, input_dim=1))

# model: keras.Model = keras.Sequential(
#
# [
#
# keras.layers.Input([1]),
#
# MinMaxScalerLayer(feature_range=(0.0, 1.0)),
#
# # keras.layers.Dense(1, input_dim=1),
#
# # MinMaxUnScalerLayer(feature_range=(0.0, 1.0)),
#
# ]
#
# )


#
# model.get_layer(model.layers[0].name).adapt(data=data)
# #
# model.get_layer(model.layers[-1].name).adapt(data=data)

export_model(model, str(savepath))
24 changes: 12 additions & 12 deletions opm/ml/keras_model_test.cpp → tests/ml/keras_model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,18 +24,18 @@
along with OPM. If not, see <http://www.gnu.org/licenses/>.
*/

#include "keras_model.hpp"
#include <opm/ml/keras_model.hpp>
#include <iostream>
#include <stdio.h>
#include "ml_tools/include/test_dense_10x1.h"
#include "ml_tools/include/test_dense_10x10.h"
#include "ml_tools/include/test_dense_10x10x10.h"
#include "ml_tools/include/test_dense_1x1.h"
#include "ml_tools/include/test_dense_2x2.h"
#include "ml_tools/include/test_relu_10.h"
#include "ml_tools/include/test_dense_relu_10.h"
#include "ml_tools/include/test_dense_tanh_10.h"
#include "ml_tools/include/test_scalingdense_1x1.h"
#include <tests/ml/ml_tools/include/test_dense_10x1.hpp>
#include <tests/ml/ml_tools/include/test_dense_10x10.hpp>
#include <tests/ml/ml_tools/include/test_dense_10x10x10.hpp>
#include <tests/ml/ml_tools/include/test_dense_1x1.hpp>
#include <tests/ml/ml_tools/include/test_dense_2x2.hpp>
#include <tests/ml/ml_tools/include/test_relu_10.hpp>
#include <tests/ml/ml_tools/include/test_dense_relu_10.hpp>
#include <tests/ml/ml_tools/include/test_dense_tanh_10.hpp>
#include <tests/ml/ml_tools/include/test_scalingdense_10x1.hpp>


namespace Opm {
Expand Down Expand Up @@ -148,9 +148,9 @@ bool tensor_test() {
return true;
}


}


int main() {
typedef Opm::DenseAd::Evaluation<double, 1> Evaluation;

Expand Down Expand Up @@ -193,7 +193,7 @@ int main() {
return 1;
}

if (!test_scalingdense_1x1<Evaluation>(&load_time, &apply_time)) {
if (!test_scalingdense_10x1<Evaluation>(&load_time, &apply_time)) {
return 1;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,16 @@
# You should have received a copy of the GNU General Public License
# along with OPM. If not, see <http://www.gnu.org/licenses/>.


import numpy as np
import pprint
import os
import os, sys
from tensorflow import keras

from keras.models import Sequential
from keras.layers import Conv2D, Dense, Flatten, Activation, MaxPooling2D, Dropout, BatchNormalization, ELU, Embedding, LSTM

sys.path.append('../../../opm/ml/ml_tools')

from kerasify import export_model
from scaler_layers import MinMaxScalerLayer, MinMaxUnScalerLayer

Expand Down Expand Up @@ -110,20 +112,17 @@ def to_cpp(ndarray):
}
'''


directory = os.getcwd()
directory1 = "models"
directory2 = "include"

# path1=os.path.abspath(directory)
if os.path.isdir(directory1):
print(f"{directory1} exists.")
else:
print(f"{directory1} does not exist.")
path1 = os.path.join(directory, directory1)
os.makedirs(path1)


if os.path.isdir(directory2):
print(f"{directory2} exists.")
else:
Expand All @@ -139,17 +138,16 @@ def output_testcase(model, test_x, test_y, name, eps):
print(model.summary())

export_model(model, 'models/test_%s.model' % name)
path = os.path.abspath(f'../../opm-common/opm/ml/ml_tools/models/test_{name}.model')
path = os.path.abspath(f'tests/ml/ml_tools/models/test_{name}.model')
relative_path = os.path.relpath(path)
with open('include/test_%s.h' % name, 'w') as f:
with open('include/test_%s.hpp' % name, 'w') as f:
x_shape, x_data = c_array(test_x[0])
y_shape, y_data = c_array(predict_y[0])

f.write(TEST_CASE % (name, name, x_shape, x_data, y_shape, y_data, relative_path, eps))



# scaling 1x1
# scaling 10x1
data: np.ndarray = np.random.uniform(-500, 500, (5, 1))
feature_ranges: list[tuple[float, float]] = [(0.0, 1.0), (-3.7, 0.0)]
test_x = np.random.rand(10, 10).astype('f')
Expand All @@ -166,7 +164,7 @@ def output_testcase(model, test_x, test_y, name, eps):
# #
model.get_layer(model.layers[0].name).adapt(data=data)
model.get_layer(model.layers[-1].name).adapt(data=data)
output_testcase(model, test_x, test_y, 'scalingdense_1x1', '1e-3')
output_testcase(model, test_x, test_y, 'scalingdense_10x1', '1e-3')

# Dense 1x1
test_x = np.arange(10)
Expand Down Expand Up @@ -230,5 +228,4 @@ def output_testcase(model, test_x, test_y, name, eps):
model.add(Dense(10, input_dim=10, activation='tanh'))
model.add(Dense(10, input_dim=10, activation='tanh'))
model.add(Dense(10, input_dim=10, activation='tanh'))
output_testcase(model, test_x, test_y, 'dense_tanh_10', '1e-6')

output_testcase(model, test_x, test_y, 'dense_tanh_10', '1e-6')
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ bool test_dense_10x1(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.74998796,0.21858868,0.23070294,0.070500664,0.46532258,0.49146336,
0.3717498,0.45293888,0.6332608,0.43785256};
in.data_ = {0.17291813,0.55951893,0.55021375,0.8738541,0.2227031,0.90520567,
0.32658377,0.9591912,0.81037205,0.9161704};

Opm::Tensor<Evaluation> out{1};
out.data_ = {0.49467176};
out.data_ = {-0.19687384};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_10x1.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_10x1.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,17 +39,17 @@ bool test_dense_10x10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.46586692,0.9036544,0.5005982,0.5969117,0.09755054,0.60282475,
0.9040883,0.6297268,0.12081735,0.42354217};
in.data_ = {0.7425694,0.8231955,0.43356442,0.1050322,0.68590504,0.004866237,
0.2682057,0.2863745,0.51028156,0.4160977};

Opm::Tensor<Evaluation> out{1};
out.data_ = {0.04306573};
out.data_ = {0.42263064};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_10x10.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_10x10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ bool test_dense_10x10x10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.5250269,0.20885888,0.2868855,0.11718734,0.56410795,0.3253708,
0.63131493,0.44084042,0.6677203,0.29525173};
in.data_ = {0.19410305,0.7003953,0.8654097,0.52044004,0.56675416,0.3896138,
0.39529598,0.28999075,0.8540118,0.2849137};

Opm::Tensor<Evaluation> out{10};
out.data_ = {0.054961562,-0.45561615,-0.2112424,0.0062360223,0.3164827,
0.746866,-0.41519523,0.004653876,0.58843577,-0.3870317};
out.data_ = {0.17738305,0.66652817,-0.06525068,0.71002805,0.044267435,
0.5738672,0.44073245,-0.22361669,0.49488887,-0.064859115};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_10x10x10.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_10x10x10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ bool test_dense_1x1(Evaluation* load_time, Evaluation* apply_time)
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_1x1.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_1x1.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ bool test_dense_2x2(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{2};
in.data_ = {0.31118634,0.7765746};
in.data_ = {0.401759,0.21791744};

Opm::Tensor<Evaluation> out{1};
out.data_ = {-0.18889335};
out.data_ = {0.40147796};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_2x2.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_2x2.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ bool test_dense_relu_10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.8129924,0.75172913,0.5933609,0.15881199,0.6235366,0.87480235,
0.7488489,0.11598817,0.17794482,0.53804207};
in.data_ = {0.26160437,0.12646836,0.738134,0.21118933,0.8889372,0.5010538,
0.9388751,0.34989306,0.13648356,0.076768965};

Opm::Tensor<Evaluation> out{10};
out.data_ = {0.13795315,0.,0.074093476,0.3255115,0.64427626,0.,
0.014938377,0.,0.,0.};
out.data_ = {0.4369418,1.300816,0.7456924,0.,0.,0.21714777,
0.062389035,0.,0.,0.};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_relu_10.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_relu_10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ bool test_dense_tanh_10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.31477565,0.54144824,0.679132,0.85510814,0.96523064,0.32442212,
0.25378492,0.45629957,0.75501525,0.3410805};
in.data_ = {0.94085747,0.1746581,0.09927705,0.012826765,0.040825576,0.14897323,
0.6822367,0.91748506,0.67433727,0.5048738};

Opm::Tensor<Evaluation> out{10};
out.data_ = {-0.24370436,-0.15176499,-0.48301288,-0.071210645,0.31381485,
0.20028076,-0.4273302,0.20151882,0.26926264,-0.2015895};
out.data_ = {-0.08283218,0.022714693,-0.15881601,-0.31616235,-0.20223989,
0.4720518,-0.13530512,-0.18132511,-0.51301044,0.8691121};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_dense_tanh_10.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_dense_tanh_10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,18 +39,18 @@ bool test_relu_10(Evaluation* load_time, Evaluation* apply_time)
KASSERT(apply_time, "Invalid Evaluation");

Opm::Tensor<Evaluation> in{10};
in.data_ = {0.52176595,0.39513543,0.9489158,0.8337155,0.21538404,0.9417168,
0.20235209,0.06845642,0.97452354,0.67260426};
in.data_ = {0.9424436,0.8319943,0.91191685,0.74209344,0.40372875,0.7391152,
0.02710398,0.4579188,0.45213553,0.97500265};

Opm::Tensor<Evaluation> out{10};
out.data_ = {0.,0.38307053,0.27412605,0.,0.,0.8103977,
0.6584701,0.,0.62901723,0.};
out.data_ = {0.255042,0.303974,0.32974565,0.,0.,0.18417864,
0.,0.,0.,0.};

KerasTimer load_timer;
load_timer.Start();

KerasModel<Evaluation> model;
KASSERT(model.LoadModel("../../opm-common/opm/ml/ml_tools/models/test_relu_10.model"), "Failed to load model");
KASSERT(model.LoadModel("tests/ml/ml_tools/models/test_relu_10.model"), "Failed to load model");

*load_time = load_timer.Stop();

Expand Down
Loading

0 comments on commit fdc8a27

Please sign in to comment.