python - 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,np.polyval(pbr,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,144,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,deriv1,'m:',linewidth=2, 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,144,99)
xg = np.array([ 0.0 , 23.0 , 47.0 , 49.0 , 71.5 ,\
95.0 , 119.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,144,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,np.polyval(pbr,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,144,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,deriv1,'m:',linewidth=2, 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,144,99)
xg = np.array([ 0.0 , 23.0 , 47.0 , 49.0 , 71.5 ,\
95.0 , 119.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,144,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 等软件包。这是关于 Keras 的教程。Gekko 特别擅长处理需要额外事物的问题,例如约束、非标准激活函数和混合机器学习,其中模型是基于物理和经验元素的组合。
- Gekko 使用基于梯度的求解器,可能会陷入局部最小值。
推荐阅读
- javascript - 有没有一种方法可以像使用渐变一样将图像用作描边或填充样式?
- javascript - 从所有节点列表元素中删除样式并仅添加到单击的元素 Vanilla JS
- javascript - 如何防止用户搜索空值/空格值,因为它不断返回数据库中的所有值
- ios - iOS 13 NSURLSession websocket 不使用 TLS 1.3
- java - 如何将房间自动生成的主键作为外键添加到另一个实体
- chef-infra - MySQL Chef: NoMethodError: undefined method `set' for #
我正在尝试使用超市的 MySQL 食谱,但有一些例外
我的配置非常基本:
食谱:
mysql_service 'foo' do port '3306' version '5.5' initial_r
- c++ - C ++如何在两个类型不同的变量之间建立交换函数
- python - 为什么heroku不想上传我的项目?
- flutter - 在flutter中通过provider更新函数
- javascript - 从类方法 JavaScript 中调用构造函数