如何解决GEKKO-神经网络-求解器不起作用
我正在尝试创建一个神经网络来预测变量“ miu”的行为。
由于我只有6个数据点,因此我尝试使用样条线查找更多跟随系统行为的点,然后再使用神经网络中的所有这些点。
我正在尝试使用2个输入,分别是时间和细胞浓度。预期的输出将是miu值,以微分dy / dx的形式给出,其中y是细胞浓度,x是时间。
我实现了以下代码:
from gekko import brain
import numpy as np
import matplotlib.pyplot as plt
from numpy import diff
from scipy.interpolate import CubicSpline
xm = np.array([ 0.0,23.0,47.0,49.0,\
71.5,95.0,119.0,143.0 ])
def spline(cell):
m = GEKKO()
m.options.IMODE=2
c = [m.FV(value=0) for i in range(4)]
x = m.Param(value=xm)
cell = np.array(cell)
y = m.CV(value=cell)
y.FSTATUS = 1
# polynomial model
m.Equation(y==c[0]+c[1]*x+c[2]*x**2+c[3]*x**3)
c[0].STATUS=1
m.solve(disp=False)
c[1].STATUS=1
m.solve(disp=False)
c[2].STATUS=1
c[3].STATUS=1
m.solve(disp=False)
pbr = [c[3].value[0],c[2].value[0],\
c[1].value[0],c[0].value[0]]
print(pbr)
xp = np.linspace(0,144,100)
plot1 = plt.figure(1)
if cell[0] == cell_br2[0]:
plt.plot(xm,cell_br2,'ko',label ='BR2')
plt.plot(xp,np.polyval(pbr,xp),'g:',linewidth=2)
elif cell[0] == cell_br1[0] :
plt.plot(xm,cell_br1,'mo',label ='BR1')
plt.plot(xp,'r:',linewidth=2)
plt.xlabel('time(hr)')
plt.ylabel('cells')
plt.legend()
dx = diff(xp)
dy1 = diff(np.polyval(pbr,xp))
deriv1 = dy1/dx
time =np.linspace(0,99)
plot1 = plt.figure(2)
if cell[0] == cell_br2[0]:
plt.plot(time,deriv1,'b:',linewidth=2,label ='BR2')
elif cell[0] == cell_br1[0]:
plt.plot(time,'m:',label ='BR1')
plt.xlabel('time(hr)')
plt.ylabel('miu(1/h)')
plt.legend()
plt.show()
return(deriv1)
m = GEKKO()
cell_br1 = (0.63*10**6,1.10*10**6,2.06*10**6,2.08*10**6,\
3.73*10**6,3.89*10**6,3.47*10**6,2.312*10**6)
cell_br2= (0.58*10**6,0.96*10**6,2.07*10**6,1.79*10**6,\
3.57*10**6,3.34*10**6,2.62*10**6,1.75*10**6)
b = brain.Brain()
b.input_layer(2)
b.layer(linear=5)
b.layer(tanh=5)
b.layer(linear=5)
b.output_layer(1)
x_s = np.linspace(0,99)
xg = np.array([ 0.0,71.5,\
95.0,144.0 ])
cells_spline = CubicSpline(xm,cell_br1)
y_cells = cells_spline(x_s)
miu_1 = spline(cell_br1)
miu_2 = spline(cell_br2)
x = (x_s,y_cells)#,y_glucose) #Inputs (3)
y = (miu_1) #Output (2)
b.learn(x,y) # train
xp = np.linspace(0,99)
yp = b.think(x) # validate
yyp = np.array(yp)
miu = np.reshape(yyp,(99,))
plot1 = plt.figure(3)
plt.plot(xp,miu,'r-',label = 'Predicted ')
plt.plot(x_s,miu_1,'bo',label = 'Experimental points')
plt.xlabel('Time [hr]')
plt.ylabel('miu [1/h]')
plt.legend()
plt.show()
尽管求解器找到了一个解,但它是恒定的,这表明该求解器不起作用。我的输出如下:
有人可以帮忙吗?我找不到失败的原因。谢谢
解决方法
以下是您当前使用方法的几个问题:
- 培训使用两个输入,而验证仅使用一个输入
- 数据未缩放。如果将数据缩放到-1到1,通常会有所帮助。我提供了一个简单的标量,但是有更好的方法也可以将数据零中心化。
from gekko import brain
from gekko import GEKKO
import numpy as np
import matplotlib.pyplot as plt
from numpy import diff
from scipy.interpolate import CubicSpline
xm = np.array([ 0.0,23.0,47.0,49.0,\
71.5,95.0,119.0,143.0 ])
def spline(cell):
m = GEKKO()
m.options.IMODE=2
c = [m.FV(value=0) for i in range(4)]
x = m.Param(value=xm)
cell = np.array(cell)
y = m.CV(value=cell)
y.FSTATUS = 1
# polynomial model
m.Equation(y==c[0]+c[1]*x+c[2]*x**2+c[3]*x**3)
c[0].STATUS=1
m.solve(disp=False)
c[1].STATUS=1
m.solve(disp=False)
c[2].STATUS=1
c[3].STATUS=1
m.solve(disp=False)
pbr = [c[3].value[0],c[2].value[0],\
c[1].value[0],c[0].value[0]]
print(pbr)
xp = np.linspace(0,144,100)
plot1 = plt.figure(1)
if cell[0] == cell_br2[0]:
plt.plot(xm,cell_br2,'ko',label ='BR2')
plt.plot(xp,np.polyval(pbr,xp),'g:',linewidth=2)
elif cell[0] == cell_br1[0] :
plt.plot(xm,cell_br1,'mo',label ='BR1')
plt.plot(xp,'r:',linewidth=2)
plt.xlabel('time(hr)')
plt.ylabel('cells')
plt.legend()
dx = diff(xp)
dy1 = diff(np.polyval(pbr,xp))
deriv1 = dy1/dx
time =np.linspace(0,99)
plot1 = plt.figure(2)
if cell[0] == cell_br2[0]:
plt.plot(time,deriv1,'b:',linewidth=2,label ='BR2')
elif cell[0] == cell_br1[0]:
plt.plot(time,'m:',label ='BR1')
plt.xlabel('time(hr)')
plt.ylabel('miu(1/h)')
plt.legend()
#plt.show()
return(deriv1)
cell_br1 = np.array([0.63*10**6,1.10*10**6,2.06*10**6,2.08*10**6,\
3.73*10**6,3.89*10**6,3.47*10**6,2.312*10**6])
cell_br2= np.array([0.58*10**6,0.96*10**6,2.07*10**6,1.79*10**6,\
3.57*10**6,3.34*10**6,2.62*10**6,1.75*10**6])
b = brain.Brain(remote=True)
b.input_layer(1)
b.layer(linear=1)
b.layer(tanh=4)
b.layer(linear=1)
b.output_layer(1)
x_s = np.linspace(0,99)
xg = np.array([ 0.0,71.5,\
95.0,144.0 ])
cells_spline = CubicSpline(xm,cell_br1)
y_cells = cells_spline(x_s)
miu_1 = spline(cell_br1)
miu_2 = spline(cell_br2)
scale = [1.0e6,1.0e4]
x = (y_cells/scale[0]) #,y_glucose) #Inputs (3)
y = (miu_1/scale[1]) #Output (2)
b.learn(x,y) # train
yp = b.think(x) # validate
xp = np.linspace(0,99)
yyp = np.array(yp)
miu = np.reshape(yyp,(99,))
plot1 = plt.figure(3)
plt.plot(xp,miu*scale[1],'r-',label = 'Predicted ')
plt.plot(x_s,miu_1,'bo',label = 'Experimental points')
plt.xlabel('Time [hr]')
plt.ylabel('miu [1/h]')
plt.legend()
plt.show()
建议:
- 调整节点数和层类型。
- 使用诸如Keras或PyTorch之类的软件包来解决此类问题。这是tutorial on Keras。 Gekko特别擅长解决那些需要额外条件的问题,例如约束,非标准激活函数和混合机器学习,其中模型是基于物理和经验元素的组合。
- Gekko使用的基于梯度的求解器可能会卡在局部最小值上。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。