gpt4 book ai didi

python - 神经网络直线——GEKKO

转载 作者:行者123 更新时间:2023-12-05 06:58:27 30 4
gpt4 key购买 nike

我是神经网络的新手。我尝试创建一个神经网络来预测我使用 GEKKO 给出的值。然而,即使代码有效,我也无法获得准确的预测。

此外,6 个数据点是否足以创建一个神经网络?

可以请人帮忙吗?代码可以在下面找到

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()

这是输出:

最佳答案

brain module怎么样?简化您在 Gekko 中的神经网络代码?

Neural network model

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()

六个数据点并不多。可调参数比数据点多,但这显示了如何为更大的问题设置它。您可能需要调整每层的节点数以获得良好的拟合。 Machine Learning and Dynamic Optimization 上还有其他教程网站。您可能还想看看 Keras 或 PyTorch。

关于python - 神经网络直线——GEKKO,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64596467/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com