-
Notifications
You must be signed in to change notification settings - Fork 213
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
New save or load functions for the surrogate models using pickle (#689)
* new save and load functions for the surrogate models * new save functions for the surrogates and identification of the necessary attributes for predict_values() * final implementation of an save/load option for the surrogates with pickle * better test's assertions * ruff check modifications * changes to validate the pull request
- Loading branch information
1 parent
5fa7fa9
commit 5bfec9a
Showing
8 changed files
with
140 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,87 @@ | ||
import os | ||
import unittest | ||
import numpy as np | ||
|
||
from smt.problems import Sphere | ||
from smt.sampling_methods import LHS | ||
from smt.surrogate_models import KRG, LS, KPLS, GEKPLS, KPLSK, MGP, QP, SGP, GENN | ||
|
||
|
||
class TestSaveLoad(unittest.TestCase): | ||
def test_save_load_GEKPLS(self): | ||
filename = "sm_save_test" | ||
fun = Sphere(ndim=2) | ||
|
||
sampling = LHS(xlimits=fun.xlimits, criterion="m") | ||
xt = sampling(20) | ||
yt = fun(xt) | ||
|
||
for i in range(2): | ||
yd = fun(xt, kx=i) | ||
yt = np.concatenate((yt, yd), axis=1) | ||
|
||
X = np.arange(fun.xlimits[0, 0], fun.xlimits[0, 1], 0.25) | ||
Y = np.arange(fun.xlimits[1, 0], fun.xlimits[1, 1], 0.25) | ||
X, Y = np.meshgrid(X, Y) | ||
Z1 = np.zeros((X.shape[0], X.shape[1])) | ||
Z2 = np.zeros((X.shape[0], X.shape[1])) | ||
|
||
sm = GEKPLS(print_global=False) | ||
sm.set_training_values(xt, yt[:, 0]) | ||
for i in range(2): | ||
sm.set_training_derivatives(xt, yt[:, 1 + i].reshape((yt.shape[0], 1)), i) | ||
sm.train() | ||
for i in range(X.shape[0]): | ||
for j in range(X.shape[1]): | ||
Z1[i, j] = sm.predict_values( | ||
np.hstack((X[i, j], Y[i, j])).reshape((1, 2)) | ||
).item() | ||
sm.save(filename) | ||
|
||
sm2 = GEKPLS.load(filename) | ||
self.assertIsNotNone(sm2) | ||
|
||
for i in range(X.shape[0]): | ||
for j in range(X.shape[1]): | ||
Z2[i, j] = sm2.predict_values( | ||
np.hstack((X[i, j], Y[i, j])).reshape((1, 2)) | ||
).item() | ||
|
||
np.testing.assert_allclose(Z1, Z2) | ||
|
||
os.remove(filename) | ||
|
||
def test_save_load_surrogates(self): | ||
surrogates = [KRG, KPLS, KPLSK, MGP, SGP, QP, GENN, LS] | ||
rng = np.random.RandomState(1) | ||
N_inducing = 30 | ||
num = 100 | ||
|
||
xt = np.array([0.0, 1.0, 2.0, 3.0, 4.0]) | ||
yt = np.array([0.0, 1.0, 1.5, 0.9, 1.0]) | ||
x = np.linspace(0.0, 4.0, num).reshape(-1, 1) | ||
|
||
for surrogate in surrogates: | ||
filename = "sm_save_test" | ||
|
||
sm = surrogate(print_global=False) | ||
sm.set_training_values(xt, yt) | ||
|
||
if surrogate == SGP: | ||
sm.Z = 2 * rng.rand(N_inducing, 1) - 1 | ||
sm.set_inducing_inputs(Z=sm.Z) | ||
|
||
sm.train() | ||
y1 = sm.predict_values(x) | ||
sm.save(filename) | ||
|
||
sm2 = surrogate.load(filename) | ||
y2 = sm2.predict_values(x) | ||
|
||
np.testing.assert_allclose(y1, y2) | ||
|
||
os.remove(filename) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import pickle | ||
|
||
|
||
def save(self, filename): | ||
with open(filename, "wb") as file: | ||
pickle.dump(self, file) | ||
|
||
|
||
def load(filename): | ||
sm = None | ||
with open(filename, "rb") as file: | ||
sm = pickle.load(file) | ||
return sm |