简体   繁体   中英

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. 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?

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?

神经网络模型

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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