Use of Gradient Enhanced Neural Network ModelΒΆ

from smt.sampling_methods import LHS
from smt.problems import Sphere
from smt.surrogate_models.genn import GENN, load_smt_data

import numpy as np
import otsmt
import openturns as ot
Definition of Initial data
# Construction of the DOE
fun = Sphere(ndim=2)
sampling = LHS(xlimits=fun.xlimits, criterion="m")
xt = sampling(40)
yt = fun(xt)
# Compute the gradient
for i in range(2):
    yd = fun(xt, kx=i)
    yt = np.concatenate((yt, yd), axis=1)

xv = ot.Sample([[0.1,1.],[1.,2.]])
Training of smt model for Neural Network
genn = GENN()
load_smt_data(
      genn, xt, yt[:,0], yt[:,1:])  # convenience function to read in data that is in SMT format
genn.train()

Out:

___________________________________________________________________________

                                   GENN
___________________________________________________________________________

 Problem size

      # training points.        : 40

___________________________________________________________________________

 Training

   Training ...
epoch = 0, mini-batch = 0, avg cost =  1.518
epoch = 1, mini-batch = 0, avg cost =  1.282
epoch = 2, mini-batch = 0, avg cost =  1.168
epoch = 3, mini-batch = 0, avg cost =  1.152
epoch = 4, mini-batch = 0, avg cost =  1.134
epoch = 5, mini-batch = 0, avg cost =  1.110
epoch = 6, mini-batch = 0, avg cost =  1.097
epoch = 7, mini-batch = 0, avg cost =  1.097
epoch = 8, mini-batch = 0, avg cost =  1.097
epoch = 9, mini-batch = 0, avg cost =  1.097
   Training - done. Time (sec):  2.8779452
Creation of OpenTurns PythonFunction for prediction
otgenn =  otsmt.smt2ot(genn)
otgennprediction = otgenn.getPredictionFunction()
otgenngradient = otgenn.getPredictionDerivativesFunction()


print('Predicted values by GENN:',otgennprediction(xv))
print('Prediction derivatives by GENN:',otgenngradient(xv))

Out:

___________________________________________________________________________

 Evaluation

      # eval points. : 2

   Predicting ...
   Predicting - done. Time (sec):  0.0000725

   Prediction time/pt. (sec) :  0.0000362

Predicted values by GENN:     [ y0      ]
0 : [ 34.0542 ]
1 : [ 35.4242 ]
___________________________________________________________________________

 Evaluation

      # eval points. : 2

   Predicting ...
   Predicting - done. Time (sec):  0.0002291

   Prediction time/pt. (sec) :  0.0001146

___________________________________________________________________________

 Evaluation

      # eval points. : 2

   Predicting ...
   Predicting - done. Time (sec):  0.0001798

   Prediction time/pt. (sec) :  0.0000899

Prediction derivatives by GENN:     [ y0        y1        ]
0 : [  0.28395   1.73131  ]
1 : [ -0.420781  1.15612  ]

Total running time of the script: ( 0 minutes 2.883 seconds)

Gallery generated by Sphinx-Gallery