简体   繁体   English

神经网络直线-GEKKO

[英]Neural Network straight line - GEKKO

I am new at neural networks.我是神经网络的新手。 I tried to create a neural network that predicts the values I give using GEKKO.我尝试创建一个神经网络来预测我使用 GEKKO 给出的值。 However, even though the code works I am not able to obtain an accurate prediction.但是,即使代码有效,我也无法获得准确的预测。

Additionally, are 6 data point enough to create a neural network?此外,6 个数据点是否足以创建神经网络?

Can please someone help?可以请人帮忙吗? Code can be found below代码可以在下面找到

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

x_m = 0.0,24.0,72.0,96.0,120.0,144.0
y_m = (0.023027367, 0.02636238,  0.024316255, 0.001705467, -0.004823068, -0.016863735)


x = np.array(x_m)
y = np.array(y_m)
# option for fitting function

# =============================================================================

# Size with hyperbolic tangent function
nin = 1  # inputs
n1 = 2   # hidden layer 1 (linear)
n2 = 3   # hidden layer 2 (nonlinear)
n3 = 2   # hidden layer 3 (linear)
nout = 1 # outputs
# 
# =============================================================================
# Initialize gekko
train = GEKKO()
test = GEKKO()

model = [train,test]

for m in model:
    # input(s)
    m.inpt = m.Param()

    # layer 1
    m.w1 = m.Array(m.FV, (nin,n1))
    m.l1 = [m.Intermediate(m.w1[0,i]*m.inpt) for i in range(n1)]

    # layer 2
    m.w2a = m.Array(m.FV, (n1,n2))
    m.w2b = m.Array(m.FV, (n1,n2))

    m.l2 = [m.Intermediate(sum([m.tanh(m.w2a[j,i]+m.w2b[j,i]*m.l1[j]) \
                                for j in range(n1)])) for i in range(n2)]

    # layer 3
    m.w3 = m.Array(m.FV, (n2,n3))
    m.l3 = [m.Intermediate(sum([m.w3[j,i]*m.l2[j] \
            for j in range(n2)])) for i in range(n3)]

    # output(s)
    m.outpt = m.CV()
    m.Equation(m.outpt==sum([m.l3[i] for i in range(n3)]))

    # flatten matrices
    m.w1 = m.w1.flatten()
    m.w2a = m.w2a.flatten()
    m.w2b = m.w2b.flatten()
    m.w3 = m.w3.flatten()

# Fit parameter weights
m = train
m.inpt.value=x
m.outpt.value=y
m.outpt.FSTATUS = 1

for i in range(len(m.w1)):
    m.w1[i].FSTATUS=1
    m.w1[i].STATUS=1
    m.w1[i].MEAS=1.0
for i in range(len(m.w2a)):
    m.w2a[i].STATUS=1
    m.w2b[i].STATUS=1
    m.w2a[i].FSTATUS=1
    m.w2b[i].FSTATUS=1
    m.w2a[i].MEAS=1.0
    m.w2b[i].MEAS=0.5
for i in range(len(m.w3)):
    m.w3[i].FSTATUS=1
    m.w3[i].STATUS=1
    m.w3[i].MEAS=1.0
m.options.IMODE = 2
m.options.SOLVER = 3
m.options.EV_TYPE = 2
m.solve(disp=False)

# Test sample points
m = test
for i in range(len(m.w1)):
    m.w1[i].MEAS=train.w1[i].NEWVAL
    m.w1[i].FSTATUS = 1
    print('w1['+str(i)+']: '+str(m.w1[i].MEAS))
for i in range(len(m.w2a)):
    m.w2a[i].MEAS=train.w2a[i].NEWVAL
    m.w2b[i].MEAS=train.w2b[i].NEWVAL
    m.w2a[i].FSTATUS = 1
    m.w2b[i].FSTATUS = 1
    print('w2a['+str(i)+']: '+str(m.w2a[i].MEAS))
    print('w2b['+str(i)+']: '+str(m.w2b[i].MEAS))
for i in range(len(m.w3)):
    m.w3[i].MEAS=train.w3[i].NEWVAL
    m.w3[i].FSTATUS = 1
    print('w3['+str(i)+']: '+str(m.w3[i].MEAS))
    
m.inpt.value= np.linspace(0,140)
m.options.IMODE = 2
m.options.SOLVER = 3
m.solve(disp=True)

plt.figure()
plt.plot(x,y,'bo', label = 'measured')
plt.plot(test.inpt.value,test.outpt.value,'r-', label = 'predicted')
plt.legend()
plt.show()

Here's the output:这是输出:

How about the brain module to simplify your code for neural networks in Gekko?在 Gekko 中简化神经网络代码的大脑模块怎么样?

神经网络模型

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

x_m = (0.0,24.0,72.0,96.0,120.0,144.0)
y_m = (0.023027367,0.02636238,0.024316255,\
       0.001705467,-0.004823068,-0.016863735)

x = np.array(x_m)
y = np.array(y_m)

b = brain.Brain()
b.input_layer(1)
b.layer(linear=2)
b.layer(tanh=2)
b.layer(linear=2)
b.output_layer(1)

b.learn(x,y) # train
xp = np.linspace(0,144,50) 
yp = b.think(xp) # validate

plt.figure()
plt.plot(x,y,'bo')
plt.plot(xp,yp[0],'r-')
plt.show()

Six data points aren't very many.六个数据点并不是很多。 There are more adjustable parameters than data points but this shows how to set it up for larger problems.有比数据点更多的可调参数,但这显示了如何为更大的问题设置它。 You may need to adjust the number of nodes for each layer to get a good fit.您可能需要调整每一层的节点数以获得良好的拟合。 There are additional tutorials on the Machine Learning and Dynamic Optimization web-site.机器学习和动态优化网站上还有其他教程。 You may also want to look at Keras or PyTorch.您可能还想查看 Keras 或 PyTorch。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM