Files
FYS-STK4155/doc/pub/odenn/html/._odenn-bs024.html
T
2018-11-11 11:53:49 +01:00

446 lines
30 KiB
HTML

<!--
Automatically generated HTML file from DocOnce source
(https://github.com/hplgit/doconce/)
-->
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="generator" content="DocOnce: https://github.com/hplgit/doconce/" />
<meta name="description" content="Data Analysis and Machine Learning: Using Neural networks to solve ODEs and PDEs">
<title>Data Analysis and Machine Learning: Using Neural networks to solve ODEs and PDEs</title>
<!-- Bootstrap style: bootstrap -->
<link href="https://netdna.bootstrapcdn.com/bootstrap/3.1.1/css/bootstrap.min.css" rel="stylesheet">
<!-- not necessary
<link href="https://netdna.bootstrapcdn.com/font-awesome/4.0.3/css/font-awesome.css" rel="stylesheet">
-->
<style type="text/css">
/* Add scrollbar to dropdown menus in bootstrap navigation bar */
.dropdown-menu {
height: auto;
max-height: 400px;
overflow-x: hidden;
}
/* Adds an invisible element before each target to offset for the navigation
bar */
.anchor::before {
content:"";
display:block;
height:50px; /* fixed header height for style bootstrap */
margin:-50px 0 0; /* negative fixed header height */
}
</style>
</head>
<!-- tocinfo
{'highest level': 2,
'sections': [('Differential equations', 2, None, '___sec0'),
('Description of the equation to solve for', 2, None, '___sec1'),
('Ordinary Differential Equations', 2, None, '___sec2'),
('The trial solution', 2, None, '___sec3'),
('Minimizing the cost function using gradient descent and '
'automatic differentiation',
2,
None,
'___sec4'),
('Example: Exponential decay and setting up the network using '
'Autograd',
2,
None,
'___sec5'),
('The function to solve for', 2, None, '___sec6'),
('The trial solution', 2, None, '___sec7'),
('Reformulating the problem', 2, None, '___sec8'),
('A possible implementation of a neural network using Autograd',
2,
None,
'___sec9'),
('Backpropagation using Autograd', 2, None, '___sec10'),
('Gradient descent', 2, None, '___sec11'),
('The network with one input, hidden, and output layer',
2,
None,
'___sec12'),
('The network with one input layer, specified number of hidden '
'layers, and one output layer output layer',
2,
None,
'___sec13'),
('Example: Population growth, comparing Autograd, TensorFlow, '
"and Euler's scheme",
2,
None,
'___sec14'),
('Setting up the problem', 2, None, '___sec15'),
('The trial solution', 2, None, '___sec16'),
('The program using Autograd', 2, None, '___sec17'),
('Using forward Euler to solve the ODE', 2, None, '___sec18'),
('Using TensorFlow to model logistic population growth',
2,
None,
'___sec19'),
('The general program flow in TensorFlow', 2, None, '___sec20'),
('Program flow in TensorFlow - Construction phase',
2,
None,
'___sec21'),
('Program flow in TensorFlow - Execution phase',
2,
None,
'___sec22'),
('The full program modeling logistic population growth using '
'TensorFlow',
2,
None,
'___sec23'),
('Example: Solving the one dimensional Poisson equation using '
'Autograd and TensorFlow',
2,
None,
'___sec24'),
('The specific equation to solve for', 2, None, '___sec25'),
('Solving the equation using Autograd', 2, None, '___sec26'),
('Comparing with a numerical scheme', 2, None, '___sec27'),
('Using gradient descent in TensorFlow to solve Poisson equation',
2,
None,
'___sec28'),
('Using a different optimization algorithm implemented in '
'TensorFlow to solve Poisson equation',
2,
None,
'___sec29'),
('Partial Differential Equations', 2, None, '___sec30'),
('Example: The diffusion equation', 2, None, '___sec31'),
('Defining the problem', 2, None, '___sec32'),
('Setting up the network using Autograd', 2, None, '___sec33'),
('Setting up the network using Autograd; The trial solution',
2,
None,
'___sec34'),
('Setting up the network using Autograd; The full program',
2,
None,
'___sec35'),
('Example: Solving the wave equation using Autograd and '
'TensorFlow',
2,
None,
'___sec36'),
('The problem to solve for', 2, None, '___sec37'),
('The trial solution', 2, None, '___sec38'),
('The analytical solution', 2, None, '___sec39'),
('Solving the wave equation - the full program using Autograd',
2,
None,
'___sec40'),
('Solving the wave equation - the full program using TensorFlow',
2,
None,
'___sec41'),
('Resources', 2, None, '___sec42')]}
end of tocinfo -->
<body>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
TeX: {
equationNumbers: { autoNumber: "none" },
extensions: ["AMSmath.js", "AMSsymbols.js", "autobold.js", "color.js"]
}
});
</script>
<script type="text/javascript" async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.1/MathJax.js?config=TeX-AMS-MML_HTMLorMML">
</script>
<!-- Bootstrap navigation bar -->
<div class="navbar navbar-default navbar-fixed-top">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-responsive-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="odenn-bs.html">Data Analysis and Machine Learning: Using Neural networks to solve ODEs and PDEs</a>
</div>
<div class="navbar-collapse collapse navbar-responsive-collapse">
<ul class="nav navbar-nav navbar-right">
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown">Contents <b class="caret"></b></a>
<ul class="dropdown-menu">
<!-- navigation toc: --> <li><a href="._odenn-bs001.html#___sec0" style="font-size: 80%;">Differential equations</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs002.html#___sec1" style="font-size: 80%;">Description of the equation to solve for</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs003.html#___sec2" style="font-size: 80%;">Ordinary Differential Equations</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs004.html#___sec3" style="font-size: 80%;">The trial solution</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs005.html#___sec4" style="font-size: 80%;">Minimizing the cost function using gradient descent and automatic differentiation</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs006.html#___sec5" style="font-size: 80%;">Example: Exponential decay and setting up the network using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs007.html#___sec6" style="font-size: 80%;">The function to solve for</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs008.html#___sec7" style="font-size: 80%;">The trial solution</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs009.html#___sec8" style="font-size: 80%;">Reformulating the problem</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs010.html#___sec9" style="font-size: 80%;">A possible implementation of a neural network using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs011.html#___sec10" style="font-size: 80%;">Backpropagation using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs012.html#___sec11" style="font-size: 80%;">Gradient descent</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs013.html#___sec12" style="font-size: 80%;">The network with one input, hidden, and output layer</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs014.html#___sec13" style="font-size: 80%;">The network with one input layer, specified number of hidden layers, and one output layer output layer</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs015.html#___sec14" style="font-size: 80%;">Example: Population growth, comparing Autograd, TensorFlow, and Euler's scheme</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs016.html#___sec15" style="font-size: 80%;">Setting up the problem</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs017.html#___sec16" style="font-size: 80%;">The trial solution</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs018.html#___sec17" style="font-size: 80%;">The program using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs019.html#___sec18" style="font-size: 80%;">Using forward Euler to solve the ODE</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs020.html#___sec19" style="font-size: 80%;">Using TensorFlow to model logistic population growth</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs021.html#___sec20" style="font-size: 80%;">The general program flow in TensorFlow</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs021.html#___sec21" style="font-size: 80%;">Program flow in TensorFlow - Construction phase</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs021.html#___sec22" style="font-size: 80%;">Program flow in TensorFlow - Execution phase</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs021.html#___sec23" style="font-size: 80%;">The full program modeling logistic population growth using TensorFlow</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs022.html#___sec24" style="font-size: 80%;">Example: Solving the one dimensional Poisson equation using Autograd and TensorFlow</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs023.html#___sec25" style="font-size: 80%;">The specific equation to solve for</a></li>
<!-- navigation toc: --> <li><a href="#___sec26" style="font-size: 80%;">Solving the equation using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs025.html#___sec27" style="font-size: 80%;">Comparing with a numerical scheme</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs026.html#___sec28" style="font-size: 80%;">Using gradient descent in TensorFlow to solve Poisson equation</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs027.html#___sec29" style="font-size: 80%;">Using a different optimization algorithm implemented in TensorFlow to solve Poisson equation</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs028.html#___sec30" style="font-size: 80%;">Partial Differential Equations</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs029.html#___sec31" style="font-size: 80%;">Example: The diffusion equation</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs030.html#___sec32" style="font-size: 80%;">Defining the problem</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs031.html#___sec33" style="font-size: 80%;">Setting up the network using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs032.html#___sec34" style="font-size: 80%;">Setting up the network using Autograd; The trial solution</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs033.html#___sec35" style="font-size: 80%;">Setting up the network using Autograd; The full program</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs034.html#___sec36" style="font-size: 80%;">Example: Solving the wave equation using Autograd and TensorFlow</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs035.html#___sec37" style="font-size: 80%;">The problem to solve for</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs036.html#___sec38" style="font-size: 80%;">The trial solution</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs037.html#___sec39" style="font-size: 80%;">The analytical solution</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs038.html#___sec40" style="font-size: 80%;">Solving the wave equation - the full program using Autograd</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs039.html#___sec41" style="font-size: 80%;">Solving the wave equation - the full program using TensorFlow</a></li>
<!-- navigation toc: --> <li><a href="._odenn-bs040.html#___sec42" style="font-size: 80%;">Resources</a></li>
</ul>
</li>
</ul>
</div>
</div>
</div> <!-- end of navigation bar -->
<div class="container">
<p>&nbsp;</p><p>&nbsp;</p><p>&nbsp;</p> <!-- add vertical space -->
<a name="part0024"></a>
<!-- !split -->
<h2 id="___sec26" class="anchor">Solving the equation using Autograd </h2>
<p>
<!-- code=python (!bc pycod) typeset with pygments style "default" -->
<div class="highlight" style="background: #f8f8f8"><pre style="line-height: 125%"><span></span><span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">autograd.numpy</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">np</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">autograd</span> <span style="color: #008000; font-weight: bold">import</span> grad, elementwise_grad
<span style="color: #008000; font-weight: bold">import</span> <span style="color: #0000FF; font-weight: bold">autograd.numpy.random</span> <span style="color: #008000; font-weight: bold">as</span> <span style="color: #0000FF; font-weight: bold">npr</span>
<span style="color: #008000; font-weight: bold">from</span> <span style="color: #0000FF; font-weight: bold">matplotlib</span> <span style="color: #008000; font-weight: bold">import</span> pyplot <span style="color: #008000; font-weight: bold">as</span> plt
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">sigmoid</span>(z):
<span style="color: #008000; font-weight: bold">return</span> <span style="color: #666666">1/</span>(<span style="color: #666666">1</span> <span style="color: #666666">+</span> np<span style="color: #666666">.</span>exp(<span style="color: #666666">-</span>z))
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">deep_neural_network</span>(deep_params, x):
<span style="color: #408080; font-style: italic"># N_hidden is the number of hidden layers</span>
N_hidden <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(deep_params) <span style="color: #666666">-</span> <span style="color: #666666">1</span> <span style="color: #408080; font-style: italic"># -1 since params consist of parameters to all the hidden layers AND the output layer</span>
<span style="color: #408080; font-style: italic"># Assumes input x being an one-dimensional array</span>
num_values <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(x)
x <span style="color: #666666">=</span> x<span style="color: #666666">.</span>reshape(<span style="color: #666666">-1</span>, num_values)
<span style="color: #408080; font-style: italic"># Assume that the input layer does nothing to the input x</span>
x_input <span style="color: #666666">=</span> x
<span style="color: #408080; font-style: italic"># Due to multiple hidden layers, define a variable referencing to the</span>
<span style="color: #408080; font-style: italic"># output of the previous layer:</span>
x_prev <span style="color: #666666">=</span> x_input
<span style="color: #408080; font-style: italic">## Hidden layers:</span>
<span style="color: #008000; font-weight: bold">for</span> l <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(N_hidden):
<span style="color: #408080; font-style: italic"># From the list of parameters P; find the correct weigths and bias for this layer</span>
w_hidden <span style="color: #666666">=</span> deep_params[l]
<span style="color: #408080; font-style: italic"># Add a row of ones to include bias</span>
x_prev <span style="color: #666666">=</span> np<span style="color: #666666">.</span>concatenate((np<span style="color: #666666">.</span>ones((<span style="color: #666666">1</span>,num_values)), x_prev ), axis <span style="color: #666666">=</span> <span style="color: #666666">0</span>)
z_hidden <span style="color: #666666">=</span> np<span style="color: #666666">.</span>matmul(w_hidden, x_prev)
x_hidden <span style="color: #666666">=</span> sigmoid(z_hidden)
<span style="color: #408080; font-style: italic"># Update x_prev such that next layer can use the output from this layer</span>
x_prev <span style="color: #666666">=</span> x_hidden
<span style="color: #408080; font-style: italic">## Output layer:</span>
<span style="color: #408080; font-style: italic"># Get the weights and bias for this layer</span>
w_output <span style="color: #666666">=</span> deep_params[<span style="color: #666666">-1</span>]
<span style="color: #408080; font-style: italic"># Include bias:</span>
x_prev <span style="color: #666666">=</span> np<span style="color: #666666">.</span>concatenate((np<span style="color: #666666">.</span>ones((<span style="color: #666666">1</span>,num_values)), x_prev), axis <span style="color: #666666">=</span> <span style="color: #666666">0</span>)
z_output <span style="color: #666666">=</span> np<span style="color: #666666">.</span>matmul(w_output, x_prev)
x_output <span style="color: #666666">=</span> z_output
<span style="color: #008000; font-weight: bold">return</span> x_output
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">solve_ode_deep_neural_network</span>(x, num_neurons, num_iter, lmb):
<span style="color: #408080; font-style: italic"># num_hidden_neurons is now a list of number of neurons within each hidden layer</span>
<span style="color: #408080; font-style: italic"># Find the number of hidden layers:</span>
N_hidden <span style="color: #666666">=</span> np<span style="color: #666666">.</span>size(num_neurons)
<span style="color: #408080; font-style: italic">## Set up initial weigths and biases</span>
<span style="color: #408080; font-style: italic"># Initialize the list of parameters:</span>
P <span style="color: #666666">=</span> [<span style="color: #008000">None</span>]<span style="color: #666666">*</span>(N_hidden <span style="color: #666666">+</span> <span style="color: #666666">1</span>) <span style="color: #408080; font-style: italic"># + 1 to include the output layer</span>
P[<span style="color: #666666">0</span>] <span style="color: #666666">=</span> npr<span style="color: #666666">.</span>randn(num_neurons[<span style="color: #666666">0</span>], <span style="color: #666666">2</span> )
<span style="color: #008000; font-weight: bold">for</span> l <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(<span style="color: #666666">1</span>,N_hidden):
P[l] <span style="color: #666666">=</span> npr<span style="color: #666666">.</span>randn(num_neurons[l], num_neurons[l<span style="color: #666666">-1</span>] <span style="color: #666666">+</span> <span style="color: #666666">1</span>) <span style="color: #408080; font-style: italic"># +1 to include bias</span>
<span style="color: #408080; font-style: italic"># For the output layer</span>
P[<span style="color: #666666">-1</span>] <span style="color: #666666">=</span> npr<span style="color: #666666">.</span>randn(<span style="color: #666666">1</span>, num_neurons[<span style="color: #666666">-1</span>] <span style="color: #666666">+</span> <span style="color: #666666">1</span> ) <span style="color: #408080; font-style: italic"># +1 since bias is included</span>
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;Initial cost: </span><span style="color: #BB6688; font-weight: bold">%g</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">%</span>cost_function_deep(P, x))
<span style="color: #408080; font-style: italic">## Start finding the optimal weigths using gradient descent</span>
<span style="color: #408080; font-style: italic"># Find the Python function that represents the gradient of the cost function</span>
<span style="color: #408080; font-style: italic"># w.r.t the 0-th input argument -- that is the weights and biases in the hidden and output layer</span>
cost_function_deep_grad <span style="color: #666666">=</span> grad(cost_function_deep,<span style="color: #666666">0</span>)
<span style="color: #408080; font-style: italic"># Let the update be done num_iter times</span>
<span style="color: #008000; font-weight: bold">for</span> i <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(num_iter):
<span style="color: #408080; font-style: italic"># Evaluate the gradient at the current weights and biases in P.</span>
<span style="color: #408080; font-style: italic"># The cost_grad consist now of N_hidden + 1 arrays; the gradient w.r.t the weights and biases</span>
<span style="color: #408080; font-style: italic"># in the hidden layers and output layers evaluated at x.</span>
cost_deep_grad <span style="color: #666666">=</span> cost_function_deep_grad(P, x)
<span style="color: #008000; font-weight: bold">for</span> l <span style="color: #AA22FF; font-weight: bold">in</span> <span style="color: #008000">range</span>(N_hidden<span style="color: #666666">+1</span>):
P[l] <span style="color: #666666">=</span> P[l] <span style="color: #666666">-</span> lmb <span style="color: #666666">*</span> cost_deep_grad[l]
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&#39;Final cost: </span><span style="color: #BB6688; font-weight: bold">%g</span><span style="color: #BA2121">&#39;</span><span style="color: #666666">%</span>cost_function_deep(P, x))
<span style="color: #008000; font-weight: bold">return</span> P
<span style="color: #408080; font-style: italic">## Set up the cost function specified for this Poisson equation:</span>
<span style="color: #408080; font-style: italic"># The right side of the ODE</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">f</span>(x):
<span style="color: #008000; font-weight: bold">return</span> (<span style="color: #666666">3*</span>x <span style="color: #666666">+</span> x<span style="color: #666666">**2</span>)<span style="color: #666666">*</span>np<span style="color: #666666">.</span>exp(x)
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">cost_function_deep</span>(P, x):
<span style="color: #408080; font-style: italic"># Evaluate the trial function with the current parameters P</span>
g_t <span style="color: #666666">=</span> g_trial_deep(x,P)
<span style="color: #408080; font-style: italic"># Find the derivative w.r.t x of the trial function</span>
d2_g_t <span style="color: #666666">=</span> elementwise_grad(elementwise_grad(g_trial_deep,<span style="color: #666666">0</span>))(x,P)
right_side <span style="color: #666666">=</span> f(x)
err_sqr <span style="color: #666666">=</span> (<span style="color: #666666">-</span>d2_g_t <span style="color: #666666">-</span> right_side)<span style="color: #666666">**2</span>
cost_sum <span style="color: #666666">=</span> np<span style="color: #666666">.</span>sum(err_sqr)
<span style="color: #008000; font-weight: bold">return</span> cost_sum<span style="color: #666666">/</span>np<span style="color: #666666">.</span>size(err_sqr)
<span style="color: #408080; font-style: italic"># The trial solution:</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">g_trial_deep</span>(x,P):
<span style="color: #008000; font-weight: bold">return</span> x<span style="color: #666666">*</span>(<span style="color: #666666">1-</span>x)<span style="color: #666666">*</span>deep_neural_network(P,x)
<span style="color: #408080; font-style: italic"># The analytic solution;</span>
<span style="color: #008000; font-weight: bold">def</span> <span style="color: #0000FF">g_analytic</span>(x):
<span style="color: #008000; font-weight: bold">return</span> x<span style="color: #666666">*</span>(<span style="color: #666666">1-</span>x)<span style="color: #666666">*</span>np<span style="color: #666666">.</span>exp(x)
<span style="color: #008000; font-weight: bold">if</span> <span style="color: #19177C">__name__</span> <span style="color: #666666">==</span> <span style="color: #BA2121">&#39;__main__&#39;</span>:
npr<span style="color: #666666">.</span>seed(<span style="color: #666666">4155</span>)
<span style="color: #408080; font-style: italic">## Decide the vales of arguments to the function to solve</span>
Nx <span style="color: #666666">=</span> <span style="color: #666666">10</span>
x <span style="color: #666666">=</span> np<span style="color: #666666">.</span>linspace(<span style="color: #666666">0</span>,<span style="color: #666666">1</span>, Nx)
<span style="color: #408080; font-style: italic">## Set up the initial parameters</span>
num_hidden_neurons <span style="color: #666666">=</span> [<span style="color: #666666">200</span>,<span style="color: #666666">100</span>]
num_iter <span style="color: #666666">=</span> <span style="color: #666666">1000</span>
lmb <span style="color: #666666">=</span> <span style="color: #666666">1e-3</span>
P <span style="color: #666666">=</span> solve_ode_deep_neural_network(x, num_hidden_neurons, num_iter, lmb)
g_dnn_ag <span style="color: #666666">=</span> g_trial_deep(x,P)
g_analytical <span style="color: #666666">=</span> g_analytic(x)
<span style="color: #408080; font-style: italic"># Find the maximum absolute difference between the solutons:</span>
max_diff <span style="color: #666666">=</span> np<span style="color: #666666">.</span>max(np<span style="color: #666666">.</span>abs(g_dnn_ag <span style="color: #666666">-</span> g_analytical))
<span style="color: #008000; font-weight: bold">print</span>(<span style="color: #BA2121">&quot;The max absolute difference between the solutions is: </span><span style="color: #BB6688; font-weight: bold">%g</span><span style="color: #BA2121">&quot;</span><span style="color: #666666">%</span>max_diff)
plt<span style="color: #666666">.</span>figure(figsize<span style="color: #666666">=</span>(<span style="color: #666666">10</span>,<span style="color: #666666">10</span>))
plt<span style="color: #666666">.</span>title(<span style="color: #BA2121">&#39;Performance of neural network solving an ODE compared to the analytical solution&#39;</span>)
plt<span style="color: #666666">.</span>plot(x, g_analytical)
plt<span style="color: #666666">.</span>plot(x, g_dnn_ag[<span style="color: #666666">0</span>,:])
plt<span style="color: #666666">.</span>legend([<span style="color: #BA2121">&#39;analytical&#39;</span>,<span style="color: #BA2121">&#39;nn&#39;</span>])
plt<span style="color: #666666">.</span>xlabel(<span style="color: #BA2121">&#39;x&#39;</span>)
plt<span style="color: #666666">.</span>ylabel(<span style="color: #BA2121">&#39;g(x)&#39;</span>)
plt<span style="color: #666666">.</span>show()
</pre></div>
<p>
<p>
<!-- navigation buttons at the bottom of the page -->
<ul class="pagination">
<li><a href="._odenn-bs023.html">&laquo;</a></li>
<li><a href="._odenn-bs000.html">1</a></li>
<li><a href="">...</a></li>
<li><a href="._odenn-bs016.html">17</a></li>
<li><a href="._odenn-bs017.html">18</a></li>
<li><a href="._odenn-bs018.html">19</a></li>
<li><a href="._odenn-bs019.html">20</a></li>
<li><a href="._odenn-bs020.html">21</a></li>
<li><a href="._odenn-bs021.html">22</a></li>
<li><a href="._odenn-bs022.html">23</a></li>
<li><a href="._odenn-bs023.html">24</a></li>
<li class="active"><a href="._odenn-bs024.html">25</a></li>
<li><a href="._odenn-bs025.html">26</a></li>
<li><a href="._odenn-bs026.html">27</a></li>
<li><a href="._odenn-bs027.html">28</a></li>
<li><a href="._odenn-bs028.html">29</a></li>
<li><a href="._odenn-bs029.html">30</a></li>
<li><a href="._odenn-bs030.html">31</a></li>
<li><a href="._odenn-bs031.html">32</a></li>
<li><a href="._odenn-bs032.html">33</a></li>
<li><a href="._odenn-bs033.html">34</a></li>
<li><a href="">...</a></li>
<li><a href="._odenn-bs040.html">41</a></li>
<li><a href="._odenn-bs025.html">&raquo;</a></li>
</ul>
<!-- ------------------- end of main content --------------- -->
</div> <!-- end container -->
<!-- include javascript, jQuery *first* -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script src="https://netdna.bootstrapcdn.com/bootstrap/3.0.0/js/bootstrap.min.js"></script>
<!-- Bootstrap footer
<footer>
<a href="http://..."><img width="250" align=right src="http://..."></a>
</footer>
-->
</body>
</html>