GEKKO multiple variables deep learning function approximation

Viewed 39

I am trying to approximate one function and then use it in the dynamic GEKKO simulation. I could not find any information on how to do this with multiple variables (such as a,b,d function parameters). Here is the example of the code:

from gekko import brain
import numpy as np
import matplotlib.pyplot as plt
b = brain.Brain(remote = False)
b.input_layer(1)
b.layer(linear=2)
b.layer(tanh=5)
b.layer(linear=2)
b.output_layer(1)

x = np.linspace(-2,2,100)
a = np.linspace(0,1,10)
b = np.linspace(0,0.5,10)
d = np.linspace(-1,0,10)

def function(x,a,b,d):
    y = 0.0001*x + a*b*x + d
    return y

b.learn(x,function(x)) # Not sure here how to implement, does not allow multiple variables in x
xp = np.linspace(-2,2,100)
yp = b.think(xp)
plt.figure()
# plt.plot(x,y,'bo')
plt.plot(xp,yp[0],'r-')
plt.show()

Does anybody know how can I train the model for this?

Also is there any method to save the model so that I dont need to train it every time, and I would simply insert it into the dynamic simulation (GEKKO IMODE 4 or 7).

Finally I am struggling to get a certain value after training the model. I have to specify x = np.linspace(0,0.000001,100) (the same form of x as for training) to get y at point x = 0. Is there any simpler method just to write y = b.think(0) and get one value at x = 0? Or y = b.think(0,0.1,...,...) with multiple variables .

I would really appreciate to get an answer for at least of some questions :).

1 Answers

Try using scikit-learn to train a neural network, support vector regressor, or gaussian process regression and then import it into Gekko. The latest version v1.0.5 supports import from scikit-learn and gpflow packages. It is currently on test.pypi.org and should be released to pypi.org by August 2022. There are examples in the documentation.

For the gekko brain module, below is an example of training and prediction adapted from the Machine Learning course. That page also has a scikit-learn and keras/tensorflow example.

nn result

from gekko import brain
import numpy as np
import matplotlib.pyplot as plt  

# set random seed
np.random.seed(0)

# generate training data
n = 20 # samples
x = np.random.rand(n)*2 # between 0 and 2
a = np.random.rand(n)   # between 0 and 1
c = np.random.rand(n)*0.5 # between 0 and 0.5
d = np.random.rand(n)*-1  # between 0 and -1
def function(x,a,c,d):
    y = 0.0001*x + a*c*x + d
    return y
z = function(x,a,c,d)

# consolidate inputs
inputs = np.vstack((x,a,c,d))
output = z

b = brain.Brain(remote = False)
b.input_layer(4)
b.layer(linear=2)
b.layer(tanh=5)
b.layer(linear=2)
b.output_layer(1)
# train
b.learn(inputs,output)      

# validate with new numbers
x = np.random.rand(n)*2 # between 0 and 2
a = np.random.rand(n)   # between 0 and 1
c = np.random.rand(n)*0.5 # between 0 and 0.5
d = np.random.rand(n)*-1  # between 0 and -1
z = function(x,a,c,d)
inputs = np.vstack((x,a,c,d))
output = z

output_pred = b.think(inputs)  

plt.figure()
plt.plot(output,output_pred[0],'ro')
plt.plot([-1,1],[-1,1],'k-')
plt.show()

For neural network models, it is important to scale the data with either a MinMax scalar or else a Standard scalar.

Related