[英]GEKKO multiple variables deep learning function approximation
我正在尝试近似一个 function,然后在动态 GEKKO 模拟中使用它。 我找不到有关如何使用多个变量(例如 a、b、d function 参数)执行此操作的任何信息。 以下是代码示例:
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()
有人知道我该如何为此训练 model 吗?
还有什么方法可以保存 model 这样我就不需要每次都训练它,我只需将它插入到动态模拟中(GEKKO IMODE 4 或 7)。
最后,我在训练 model 后努力获得一定的价值。 我必须指定 x = np.linspace(0,0.000001,100) (x 的形式与训练相同)才能在点 x = 0 处获得 y。有没有更简单的方法只写 y = b.think(0 ) 并在 x = 0 处得到一个值? 或者 y = b.think(0,0.1,...,...) 具有多个变量。
我真的很感激能得到至少一些问题的答案:)。
尝试使用scikit-learn
训练神经网络、支持向量回归或高斯过程回归,然后将其导入 Gekko。 最新版本scikit-learn
v1.0.5
gpflow
包导入。 它目前在test.pypi.org上,应该在 2022 年 8 月之前发布到pypi.org
。文档中有示例。
对于gekko 大脑模块,下面是改编自机器学习课程的训练和预测示例。 该页面还有一个scikit-learn
和keras/tensorflow
示例。
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()
对于神经网络模型,重要的是使用 MinMax 标量或标准标量来缩放数据。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.