<p>I get the following result if i increase the nodes to 5
<code>b.layer(tanh=5)</code></p>

<p>There are probably multiple answers to this question, tho. Maybe increasing the number of layers or changing the activation function. You can always use different solvers, too. Finding the best network architecture is an optimization problem of its own. Some people have tried to figure it out with genetic algorithms, for example:</p>

<p><a href="https://arxiv.org/pdf/1808.03818.pdf" rel="noreferrer">https://arxiv.org/pdf/1808.03818.pdf</a></p>

<p><a href="https://i.stack.imgur.com/hD6BD.png" rel="noreferrer"><img src="https://i.stack.imgur.com/hD6BD.png" alt="enter image description here"></a></p>
Using Gekko's brain module, how do I determine how many layers and what type of layer to use to solve a deep learning problem?
<p>I am learning to use Gekko's brain module for deep learning applications. </p>

<p>I have been setting up a neural network to learn the numpy.cos() function and then produce similar results. </p>

<p>I get a good fit when the bounds on my training are:</p>

<pre class="lang-py prettyprint-override"><code>x = np.linspace(0,2*np.pi,100)
</code></pre>

<p>But the model falls apart when I try to extend the bounds to:</p>

<pre class="lang-py prettyprint-override"><code>x = np.linspace(0,3*np.pi,100)
</code></pre>

<p>What do I need to change in my neural network to increase the flexibility of my model so that it works for other bounds?</p>

<p>This is my code:</p>

<pre class="lang-py prettyprint-override"><code>from gekko import brain
import numpy as np
import matplotlib.pyplot as plt

#Set up neural network 
b = brain.Brain()
b.input_layer(1)
b.layer(linear=2)
b.layer(tanh=2)
b.layer(linear=2)
b.output_layer(1)

#Train neural network
x = np.linspace(0,2*np.pi,100)
y = np.cos(x)
b.learn(x,y)

#Calculate using trained nueral network
xp = np.linspace(-2*np.pi,4*np.pi,100)
yp = b.think(xp)

#Plot results
plt.figure()
plt.plot(x,y,'bo')
plt.plot(xp,yp[0],'r-')
plt.show()
</code></pre>

<p>These are results to 2pi:</p>

<p><a href="https://i.stack.imgur.com/jJsQ7.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/jJsQ7.png" alt="enter image description here"></a></p>

<p>These are results to 3pi:</p>

<p><a href="https://i.stack.imgur.com/yazDz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/yazDz.png" alt="enter image description here"></a></p>