3. Gray box model - dwweiss/grayboxes GitHub Wiki
Tuning of theoretical models is almost always necessary in order to compensate lack of modelling or of understanding of process details, incompleteness of material properties or inaccuracies of numerical methods.
In the simplest form of tuning, model regression analysis is performed which often requires time consuming intervention by a human operator for selecting a-priori defined relationships between process input and tuning parameters. Such an approach has the disadvantages of:
- The accuracy demanded is dependent on a subjective factor
- If new effects are investigated, these relationships are usually unknown. Interference by a human operator is necessary for the modification in an iterative manner.
Fine-tuning of the theoretical model is implemented in method LightGray.train() as the solution of the minimization task :
λ(Xcom , xtun) is an extension of the theoretical model f(Xcom) with a set of tuning parameters xtun , Y is the target corresponding to X , and N is the number of data points. The train() method utilizes optimizers from SciPy and Modestga.
| Light gray box model | |
|---|---|
| Requirement | xtun ≠ ∅, Theoretical model extension: f(xcom) → λ(xcom , xtun) |
| Training | xtun = arg min [ λ(Xcom , xtun) - Ycom ]2 |
| Prediction | y = λ(xcom , xtun) |
| Output | y = ycom ∪ yunq |
| Meta model |
wmeta = arg min [ M(xcom , wmeta) - λ(xcom , xtun) ]2 ymeta = λ*( xcom ) = M(xcom , wmeta) |
Medium gray box models provide output sets yunq additionally to common output sets ycom. The task of finding the relationship between process and tuning parameters is indicated by the solid arrows in Fig. 3.2. The common output set ycom is the only available connection between simulation and observation when starting the model tuning.
+=============+
-->| Tuning | <---------
| +=============+ |
| | |
| v |
| Tuning Common |
| Parameter Input |
| : : : |
| : : : |
| Model Process
| Input Input
| : :
| : :
| +------------+ +-------------+
| | Simulation | | Observation |
| +------------+ +-------------+
| : :
| : :
| Common Common
| Model Process
| Output Output
| : :
| : :
| Difference of
<----------- Common Output
The train() method utilizes optimizers from scipy.optimize. The prediction employing the trained model is shown in Fig. 3.3.
Process Input
| |
v v
Unique Common
Input Input
| | |
v v |
+-------------------+ |
| Empirical Model | |
+-------------------+ |
| |
Tuning Common
Parameter Input
| |
| |
v v
Model Input
|
v
+-------------------+
| Theoretical Model |
+-------------------+
|
v
Model Output
| Medium gray box model | |
|---|---|
| Requirement | xtun ≠ ∅, Theoretical model extension: f(xcom) → λ(xcom , xtun) |
| Training | w = arg min [ λ(Xcom , β(Xprc , w)) - Ycom ]2 |
| Prediction | y = μ(xprc) = λ(xcom , β(xprc , w)) |
| Output | y = ycom ∪ yunq |
| Meta model |
wmeta = arg min [ M(xprc , wmeta) - μ(xprc) ]2 ymeta = μ*( xprc ) = M(xprc , wmeta) |
In the dark gray box model an estimate of the white box model error Δy is substracted from the white box model output ycom = f(xcom). The tuning parameter set xtun is empty and the error correction is estimated by the empirical model Δy ≈ β(xcom ∪ f(xcom), w). The latter is trained with sets of observed data points (Xprc , Ycom), see Fig. 3.4. The prediction is depicted in Fig. 3.5.
+---------------------+
| Empirical model |
|---------------------|
input (X_prc U f(X_com)) ==> | | | <-- input (x_prc U f(x_com))
| train() | predict() |
target (f(X_com) - Y_com) ==> | | | --> prediction (Delta y)
+---------------------+
Process Input
| |
v |
Commmon |
Input |
| |
v |
+-------------------+ |
| Theoretical Model | |
+-------------------+ |
| |
v |
Common Process
Output Input
| |
v v
+-------------------+
| Empirical Model |
+-------------------+
|
v
Improved Model Output
| Dark gray box model | |
|---|---|
| Requirement | xtun = ∅, yunq = ∅ |
| Training | w = arg min [ { f(Xcom) - β(Xprc ∪ f(Xcom) , w) } - Ycom ]2 |
| Prediction | y = δ(xprc) = f(xcom) - β(xprc ∪ f(xcom) , w) |
| Output | y = ycom |
| Meta model |
wmeta = arg min [ M(xprc , wmeta) - δ(xprc) ]2 ymeta = δ*(xprc) = M(xprc , wmeta) |
import numpy as np
from grayboxes.array import grid, noise
from grayboxes.black import Black
from grayboxes.lightgray import LightGray
from grayboxes.plot import plot_isomap
from grayboxes.white import White
def func(x, *c):
# initial coefficients
c0, c1 = 1., 1.
# functions returns initial coefficients if x is None
if x is None:
return c0, c1
# coefficients from positional arguments if len(c) is 2
if len(c) == 2:
c0, c1 = c
# theoretical model returns 1D array
return [c0 + x[0] + c1 * np.sin(x[1])]
# White
x = grid((32, 32), [0., 1.], [-1., 3.])
y_wht = White(f=func)(x=x, silent=True)
y_nse = noise(y_wht, relative=5e-2)
# LightGray
phi = LightGray(f=func)
phi(X=x, Y=y_nse, trials=5, goal=1e-5, n_it_max=1000, detailed=True,
trainer=('BFGS', 'L-BFGS-B', 'Nelder-Mead', 'Powell', 'CG'))
y_lgr = phi(x=x, silent=True)
print('Metrics + weights of light gray:', phi.metrics, phi.weights)
# Meta model of LightGray
M = Black()
M(X=x, Y=y_lgr, neurons=[8, 4], trials=3, goal=1e-5, trainer='rprop',
n_it_max=2000, show=100)
y_mta = M(x=x, silent=True)
print('Metrics of meta model training:', M.metrics)
axes = x[:, 0], x[:,1]
plot_isomap(*axes, y_wht[:,0], labels=['x0', 'x1', 'y_wht'])
plot_isomap(*axes, y_nse[:,0], labels=['x0', 'x1', 'y_nse'])
plot_isomap(*axes, (y_nse-y_wht)[:,0], labels=['x0', 'x1', 'y_nse - y_wht'])
plot_isomap(*axes, y_lgr[:,0], labels=['x0', 'x1', 'y_lgr'])
plot_isomap(*axes, (y_lgr-y_nse)[:,0], labels=['x0', 'x1', 'y_lgr - y_nse'])
plot_isomap(*axes, (y_lgr-y_wht)[:,0], labels=['x0', 'x1', 'y_lgr - y_wht'])
plot_isomap(*axes, y_mta[:,0], labels=['x0', 'x1', 'y_mta'])
plot_isomap(*axes, (y_mta-y_nse)[:,0], labels=['x0', 'x1', 'y_mta - y_nse'])
plot_isomap(*axes, (y_mta-y_wht)[:,0], labels=['x0', 'x1', 'y_mta - y_wht'])





