<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>fs4learning &#187; Using neural networks for solving partial differential equations</title>
	<atom:link href="http://robotics.icstweb.org/fs4learning/category/projects/using-neural-networks-for-solving-partial-differential-equations/feed/" rel="self" type="application/rss+xml" />
	<link>http://robotics.icstweb.org/fs4learning</link>
	<description>Just another Robotics.icstweb.org weblog</description>
	<lastBuildDate>Thu, 16 Apr 2009 19:51:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Network improvement. Nelder-Mead</title>
		<link>http://robotics.icstweb.org/fs4learning/2008/12/27/network-improvement-nelder-mead/</link>
		<comments>http://robotics.icstweb.org/fs4learning/2008/12/27/network-improvement-nelder-mead/#comments</comments>
		<pubDate>Sat, 27 Dec 2008 15:49:09 +0000</pubDate>
		<dc:creator>xni</dc:creator>
				<category><![CDATA[Using neural networks for solving partial differential equations]]></category>
		<category><![CDATA[Nelder-Mead method]]></category>
		<category><![CDATA[speed improvement]]></category>

		<guid isPermaLink="false">http://robotics.icstweb.org/fs4learning/?p=42</guid>
		<description><![CDATA[As you could see in the previous post, neural solution is quite accurately approximates analytical. But there is a problem: it&#8217;s too slow. It takes about 5 hours to have all the calculations done. My mentor gave me an advise to use Nelder &#8211; Mead method to calculate weights. In this post I will talk [...]]]></description>
			<content:encoded><![CDATA[<p>As you could see in the previous post, neural solution is quite accurately approximates analytical. But there is a problem: it&#8217;s too slow. It takes about 5 hours to have all the calculations done.</p>
<p>My mentor gave me an advise to use <a href="http://en.wikipedia.org/wiki/Nelder-Mead_method" target="_blank">Nelder &#8211; Mead</a> method to calculate weights. In this post I will talk about implementation of this method using F#.</p>
<p>My main decision was using special class to implement this method. All of the operations are done by methods inside class and the end-user only passes a functional and gets the solution that minimizes it.</p>
<p>My class is not a classical implementation of Nelder-Mead method, it have some improvements, which guarantee that solution at the next iteration will be better than current.</p>
<p>You can download source code of this class <a href="http://cid-6656593f7ed14dcf.skydrive.live.com/self.aspx/FSharp/Atrificial%20Neural%20Network/2008.12.26/Neuler-Mead.fs" target="_self">here</a>.</p>
<p>With this method I got a solution in a few seconds, but it is not so good as previous.  I haven&#8217;t made any further researches for Nelder-Mead and gradient descent methods and their combinations, but I am going to do that and share my results.</p>
<p><a href="http://robotics.icstweb.org/fs4learning/files/2008/12/nm.jpg"><img class="alignnone size-medium wp-image-45" src="http://robotics.icstweb.org/fs4learning/files/2008/12/nm-300x300.jpg" alt="" width="300" height="300" /></a></p>
<p>Also I have an ideas:</p>
<ol>
<li>to use classification (by Kohonen maps), to sort out the worst-studied neurons</li>
<li>to research combination of methods to adjust widths and centers</li>
<li>to use asynchronism and parallel operations with lists to improve performance</li>
</ol>
<p>Complete source code of my project you can find <a href="http://cid-6656593f7ed14dcf.skydrive.live.com/self.aspx/FSharp/Atrificial%20Neural%20Network/2008.12.26/Program.fs" target="_self">here</a>.</p>
 <img src="http://robotics.icstweb.org/fs4learning/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=42" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://robotics.icstweb.org/fs4learning/2008/12/27/network-improvement-nelder-mead/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WPF &amp; F#</title>
		<link>http://robotics.icstweb.org/fs4learning/2008/12/27/wpf-f/</link>
		<comments>http://robotics.icstweb.org/fs4learning/2008/12/27/wpf-f/#comments</comments>
		<pubDate>Fri, 26 Dec 2008 22:41:53 +0000</pubDate>
		<dc:creator>xni</dc:creator>
				<category><![CDATA[Using neural networks for solving partial differential equations]]></category>
		<category><![CDATA[.NET framework 3.0]]></category>
		<category><![CDATA[GUI]]></category>

		<guid isPermaLink="false">http://robotics.icstweb.org/fs4learning/?p=36</guid>
		<description><![CDATA[In this post I would like to write a few words about creating user interfaces to F# programs. I don&#8217;t know about constructors for GUI, and I&#8217;m not sure about their existance at all, what&#8217;s why in this article we will create UI using objects of .NET Framework 3.0; At first, we need to add [...]]]></description>
			<content:encoded><![CDATA[<p>In this post I would like to write a few words about creating user interfaces to F# programs.</p>
<p>I don&#8217;t know about constructors for GUI, and I&#8217;m not sure about their existance at all, what&#8217;s why in this article we will create UI using objects of .NET Framework 3.0;</p>
<p>At first, we need to add some references to our project to make WPF available. This references are: <strong>PresentationCore</strong>, <strong>PresentationFramework</strong> and <strong>WindowsBase</strong> (all of dll&#8217;s are located at C:\Program Files\Reference Assemblies\Microsoft\Framework\v3.0\)</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">type</span> ResultWindow <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">class</span>
    <span style="color: #06c; font-weight: bold;">inherit</span> Window</pre></div></div>

<p>Here we are creating a class called ResultWindow, that inherites defined in the framework class <strong>Window</strong>.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">val</span> <span style="color: #06c; font-weight: bold;">mutable</span> <span style="color: #06c; font-weight: bold;">private</span> minX <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span></pre></div></div>

<p>I use <strong>val</strong> keyword for declaration of the field. It&#8217;s much more simple to use mutable structures instead of immutable, but I need to notice that you shouldn&#8217;t use mutable stuctures in pure functional code. All of class members declared with <strong>val</strong> keyword must be defined in constructor.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #06c; font-weight: bold;">new</span> <span style="color: #6c6;">&#40;</span> <span style="color: #5d478b; font-style: italic;">(* parameters here *)</span> <span style="color: #6c6;">&#41;</span> <span style="color: #06c; font-weight: bold;">as</span> this <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#123;</span>
        <span style="color: #5d478b; font-style: italic;">(* definition of fields *)</span>
        minX <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">0.0</span>
                                     <span style="color: #6c6;">&#125;</span> <span style="color: #06c; font-weight: bold;">then</span>
        <span style="color: #5d478b; font-style: italic;">(* some other actions *)</span></pre></div></div>

<p>Now it&#8217;s time to create a method (static methods are created without <strong>this.</strong> prefix).</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;">member this<span style="color: #a52a2a;">.</span><span style="color: #060;">DrawGrid</span> <span style="color: #a52a2a;">=</span>
        <span style="color: #06c; font-weight: bold;">let</span> gridLine <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">new</span> Line<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
        <span style="color: #5d478b; font-style: italic;">(* and many-many lines of code *)</span></pre></div></div>

<p>If you are not familiar to WPF my advise is to read some books about it. For example:</p>
<li><a href="http://blogs.msdn.com/dsyme/archive/2008/01/05/learning-wpf-through-f-and-vice-versa-by-john-liao.aspx">Don Syme&#8217;s WebLog</a></li>
<li>Practical WPF Graphics Programming &#8212; Jack Xu, Ph.D</li>
<li>WPF Recipes in C# 2008 &#8212; Sam Noble, Sam Bourton, Allen Jones</li>
<p>At the pictures presented below you can see analytic and neural solution graphics:</p>
<p><a href="http://robotics.icstweb.org/fs4learning/files/2008/12/sin2pix.jpg"></a></p>
<p><a href="http://robotics.icstweb.org/fs4learning/files/2008/12/sin2pix.jpg"><img class="alignnone size-medium wp-image-38" src="http://robotics.icstweb.org/fs4learning/files/2008/12/sin2pix.jpg" alt="" /></a><a href="http://robotics.icstweb.org/fs4learning/files/2008/12/x2-x-d183d181d0bad0bed180d0b5d0bdd0bdd18bd0b9-d0b4d0be-d0bed188d0b8d0b1d0bad0b8-1e-3.jpg"><img class="alignnone size-medium wp-image-39" src="http://robotics.icstweb.org/fs4learning/files/2008/12/x2-x-d183d181d0bad0bed180d0b5d0bdd0bdd18bd0b9-d0b4d0be-d0bed188d0b8d0b1d0bad0b8-1e-3.jpg" alt="" /></a><br />
I don&#8217;t publish my code in this post, because it is quite large. You can <a href="http://cid-6656593f7ed14dcf.skydrive.live.com/self.aspx/FSharp/Atrificial%20Neural%20Network/2008.12.26/ResultWindow.fs" target="_blank">download</a> it.</p>
 <img src="http://robotics.icstweb.org/fs4learning/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=36" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://robotics.icstweb.org/fs4learning/2008/12/27/wpf-f/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>F# and WPF</title>
		<link>http://robotics.icstweb.org/fs4learning/2008/12/01/f-and-wpf/</link>
		<comments>http://robotics.icstweb.org/fs4learning/2008/12/01/f-and-wpf/#comments</comments>
		<pubDate>Mon, 01 Dec 2008 12:40:28 +0000</pubDate>
		<dc:creator>xni</dc:creator>
				<category><![CDATA[Improve your skills]]></category>
		<category><![CDATA[Using neural networks for solving partial differential equations]]></category>

		<guid isPermaLink="false">http://robotics.icstweb.org/fs4learning/?p=33</guid>
		<description><![CDATA[By the moment I have a solution of the problem: I have the simplest artificial neural network that solves elliptic equation. For further research process I need a visualization of my solution. There are three way to have this done: Text output into file and writing application on other language to show the results. This [...]]]></description>
			<content:encoded><![CDATA[<p>By the moment I have a solution of the problem: I have the simplest artificial neural network that solves elliptic equation. For further research process I need a visualization of my solution.</p>
<p>There are three way to have this done:</p>
<ul>
<li>Text output into file and writing application on other language to show the results. This is a fast and simple solution. But my F# program returns list of weights, widths and centers. So I need to write the neural network modeler again! Oh, no&#8230; I don&#8217;t like this way.</li>
<li> Use the ability of Microsoft Visual Studio to call procedures, written on other languages inside one project. At first I wanted to use this ability, but yesterday I find the best way to do the modeler.</li>
<li>Windows Presentation Foundation (WPF). It&#8217;s a part of .NET framework 3 and gives us an opportunity to have separate code and markup written.</li>
</ul>
<p>I need to have my visualizer finished in 3 days, so results are coming soon!</p>
<p>There are a lot of materials about F# and WPF, but I liked this <a href="http://blogs.msdn.com/dsyme/archive/2008/01/05/learning-wpf-through-f-and-vice-versa-by-john-liao.aspx">one</a>.</p>
 <img src="http://robotics.icstweb.org/fs4learning/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=33" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://robotics.icstweb.org/fs4learning/2008/12/01/f-and-wpf/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Neural network overview</title>
		<link>http://robotics.icstweb.org/fs4learning/2008/11/10/neural-network-overview/</link>
		<comments>http://robotics.icstweb.org/fs4learning/2008/11/10/neural-network-overview/#comments</comments>
		<pubDate>Sun, 09 Nov 2008 23:00:25 +0000</pubDate>
		<dc:creator>xni</dc:creator>
				<category><![CDATA[Using neural networks for solving partial differential equations]]></category>

		<guid isPermaLink="false">http://robotics.icstweb.org/fs4learning/?p=18</guid>
		<description><![CDATA[If you still don&#8217;t know what the artificial neural network is -  you should read some information about it (e.g. http://en.wikipedia.org/wiki/Artificial_neuron). In this article I will use artificial neurons based on radial basis functions (RBF). The influence of a neuron located in point c upon x is calculated as: where a is also a parameter [...]]]></description>
			<content:encoded><![CDATA[<p>If you still don&#8217;t know what the artificial neural network is -  you should read some information about it (e.g. <a href="http://en.wikipedia.org/wiki/Artificial_neuron">http://en.wikipedia.org/wiki/Artificial_neuron</a>).</p>
<p>In this article I will use artificial neurons based on radial basis functions (RBF). The influence of a neuron located in point <strong>c</strong> upon <strong>x</strong> is calculated as: <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_cc3ac76084b0186de388282a8394a3ab.png" align="absmiddle" class="tex" alt="\varphi(x) = e^{\frac{||x-c||^2}{a^2}}" /> where <strong>a</strong> is also a parameter of current neuron. Let us consider that the network consists on <strong>N</strong> neurons. Then we are able to calculate total influence: <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_8ae0d7553aab075179814951399acc0d.png" align="absmiddle" class="tex" alt="u(x) = \sum\limits_{i=1}^N w_i \varphi_i(x)=\sum\limits_{i=1}^N w_i e^{\frac{||x-c_i||^2}{a_i^2}}" /></p>
<p>The first partial differential equation that I&#8217;ll solve is: <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_c168f197198a0150ee6f01b80b2f1495.png" align="absmiddle" class="tex" alt="\frac{\partial^2 U}{\partial x^2} + \frac{\partial^2 U}{\partial y^2} = 1" /> <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_a8b1ab783258c04824ccbecfd1f7d288.png" align="absmiddle" class="tex" alt="U(0) = 0" /> <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_7ff15def4a8de24ed59d481caf4b6f74.png" align="absmiddle" class="tex" alt="U(1) = 0" /></p>
<p><span id="more-18"></span><br />
At first iteration I define sets of neuron centers (<strong>funCenters</strong>), widths (<strong>funWidths</strong>), and collocation points. A collocation point is a point, where total influence of all neurons will be calculated, compared to a given value, and after that&#8217;s done, parameter corrections will be made. There are two types of collocation points: points located inside the area and points lying on it&#8217;s border.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">(* Randomly selected values *)</span>
<span style="color: #06c; font-weight: bold;">let</span> funCenters <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">0.3</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.1</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.15</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.2</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.3</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.45</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.5</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.52</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.6</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.69</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.8</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.91</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">1.03</span><span style="color: #6c6;">&#93;</span>
<span style="color: #5d478b; font-style: italic;">(* At first I suggest what width is 0.3 *)</span>
<span style="color: #06c; font-weight: bold;">let</span> funWidths <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.3</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#91;</span>1<span style="color: #a52a2a;">..</span>13<span style="color: #6c6;">&#93;</span>
<span style="color: #5d478b; font-style: italic;">(* Inner collocation points *)</span>
<span style="color: #06c; font-weight: bold;">let</span> collocationInside <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">0.01</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.2</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.25</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.41</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.56</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.61</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.78</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.8</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.95</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.99</span><span style="color: #6c6;">&#93;</span>
<span style="color: #5d478b; font-style: italic;">(* Border collocation points *)</span>
<span style="color: #06c; font-weight: bold;">let</span> collocationOutside <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#91;</span><span style="color: #c6c;">0.0</span><span style="color: #a52a2a;">;</span> <span style="color: #c6c;">1.0</span><span style="color: #6c6;">&#93;</span></pre></div></div>

<p>I declare some helpful functions:</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">(* calculate a^b *)</span>
<span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> pow <span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>b<span style="color: #a52a2a;">:</span>int<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">match</span> b <span style="color: #06c; font-weight: bold;">with</span>
    <span style="color: #a52a2a;">|</span> <span style="color: #c6c;">0</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">1.0</span>
    <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> a<span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span> pow a <span style="color: #6c6;">&#40;</span>b<span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">(* generate a RBF with specified center and width *)</span>
<span style="color: #06c; font-weight: bold;">let</span> funcGen <span style="color: #6c6;">&#40;</span>center<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>width<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">fun</span> pos <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>pow <span style="color: #6c6;">&#40;</span>pos<span style="color: #a52a2a;">-</span>center<span style="color: #6c6;">&#41;</span> <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>pow width <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">(* generate a second derivate (d^2 U / d x^2) for RBF with specified center and width *)</span>
<span style="color: #06c; font-weight: bold;">let</span> ddfuncGen <span style="color: #6c6;">&#40;</span>center<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>width<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">fun</span> pos <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #6c6;">&#40;</span> <span style="color: #c6c;">4.0</span> <span style="color: #a52a2a;">*</span> <span style="color: #6c6;">&#40;</span>pow <span style="color: #6c6;">&#40;</span>pos <span style="color: #a52a2a;">-</span> center<span style="color: #6c6;">&#41;</span> <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">/</span> <span style="color: #6c6;">&#40;</span>pow width <span style="color: #c6c;">4</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>pow width <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">*</span> <span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>pow <span style="color: #6c6;">&#40;</span>pos<span style="color: #a52a2a;">-</span>center<span style="color: #6c6;">&#41;</span> <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">/</span> <span style="color: #6c6;">&#40;</span>pow width <span style="color: #c6c;">2</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">(* right part of PDE *)</span>
<span style="color: #06c; font-weight: bold;">let</span> neodn <span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #c6c;">1.0</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">(* border values of PDE *)</span>
<span style="color: #06c; font-weight: bold;">let</span> funcBorder <span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">:</span><span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">match</span> x <span style="color: #06c; font-weight: bold;">with</span>
    <span style="color: #a52a2a;">|</span> <span style="color: #c6c;">0.0</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.0</span>
    <span style="color: #a52a2a;">|</span> <span style="color: #c6c;">1.0</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.0</span>
    <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">0.0</span></pre></div></div>

<p>Now I need to fill a list of weights. To do that I use <a href="http://en.wikipedia.org/wiki/Least_squares_method">LSM</a> and the dnAnalytics library.</p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">(* first approximation of weights using LSM *)</span>
<span style="color: #06c; font-weight: bold;">let</span> getFirstWeights <span style="color: #a52a2a;">=</span>
    <span style="color: #5d478b; font-style: italic;">(* list of RBF functions, generated for each center-width pair by funcGen function *)</span>
    <span style="color: #06c; font-weight: bold;">let</span> phiList <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map2</span> funcGen funCenters funWidths
    <span style="color: #5d478b; font-style: italic;">(* list of second derivatives for RBF for each center-width pair *)</span>
    <span style="color: #06c; font-weight: bold;">let</span> ddphiList <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map2</span> ddfuncGen funCenters funWidths
    <span style="color: #5d478b; font-style: italic;">(* LSM *)</span>
    <span style="color: #5d478b; font-style: italic;">(* every row i of H represents i-th collocation point, and every value j in i-th row is a contribution of j-th neuron. Z[i] = right part of PDE for inner collocation points i or border value for border collocation point *)</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">mutable</span> H <span style="color: #a52a2a;">=</span> dnAnalytics<span style="color: #a52a2a;">.</span><span style="color: #060;">LinearAlgebra</span><span style="color: #a52a2a;">.</span><span style="color: #060;">DenseMatrix</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#40;</span>collocationInside <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">+</span> <span style="color: #6c6;">&#40;</span>collocationOutside <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span>, funCenters <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span>List<span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">mutable</span> Z <span style="color: #a52a2a;">=</span> DenseVector<span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#40;</span>collocationInside <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">+</span> <span style="color: #6c6;">&#40;</span>collocationOutside <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">for</span> i<span style="color: #a52a2a;">=</span><span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">to</span> <span style="color: #6c6;">&#40;</span>collocationInside <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span> <span style="color: #06c; font-weight: bold;">do</span>
        <span style="color: #06c; font-weight: bold;">for</span> j<span style="color: #a52a2a;">=</span><span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">to</span> <span style="color: #6c6;">&#40;</span>funCenters <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span>List<span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span> <span style="color: #06c; font-weight: bold;">do</span>
            H<span style="color: #a52a2a;">.</span><span style="color: #060;">Item</span><span style="color: #6c6;">&#40;</span>i,j<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">&amp;</span>lt<span style="color: #a52a2a;">;-</span> ddphiList<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>j<span style="color: #6c6;">&#93;</span> collocationInside<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>i<span style="color: #6c6;">&#93;</span>
        <span style="color: #06c; font-weight: bold;">done</span>
        Z<span style="color: #a52a2a;">.</span><span style="color: #060;">Item</span><span style="color: #6c6;">&#40;</span>i<span style="color: #6c6;">&#41;</span>  <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span> <span style="color: #06c; font-weight: bold;">do</span>
        <span style="color: #06c; font-weight: bold;">for</span> j<span style="color: #a52a2a;">=</span><span style="color: #c6c;">0</span> <span style="color: #06c; font-weight: bold;">to</span> <span style="color: #6c6;">&#40;</span>funCenters <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span>List<span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">1</span> <span style="color: #06c; font-weight: bold;">do</span>
            H<span style="color: #a52a2a;">.</span><span style="color: #060;">Item</span><span style="color: #6c6;">&#40;</span>i<span style="color: #a52a2a;">+</span><span style="color: #6c6;">&#40;</span>collocationInside <span style="color: #a52a2a;">|&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">length</span><span style="color: #6c6;">&#41;</span>,j<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">&amp;</span>lt<span style="color: #a52a2a;">;-</span> phiList<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>j<span style="color: #6c6;">&#93;</span> collocationOutside<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>i<span style="color: #6c6;">&#93;</span>
        <span style="color: #06c; font-weight: bold;">done</span>
        Z<span style="color: #a52a2a;">.</span><span style="color: #060;">Item</span><span style="color: #6c6;">&#40;</span>i<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">&amp;</span>lt<span style="color: #a52a2a;">;-</span> funcBorder collocationOutside<span style="color: #a52a2a;">.</span><span style="color: #6c6;">&#91;</span>i<span style="color: #6c6;">&#93;</span>
    <span style="color: #06c; font-weight: bold;">done</span>
    <span style="color: #06c; font-weight: bold;">let</span> W <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#40;</span>H<span style="color: #a52a2a;">.</span><span style="color: #060;">Transpose</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>H<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">.</span><span style="color: #060;">Inverse</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
    <span style="color: #06c; font-weight: bold;">let</span> w <span style="color: #a52a2a;">=</span> W<span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>H<span style="color: #a52a2a;">.</span><span style="color: #060;">Transpose</span><span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>Z
    <span style="color: #5d478b; font-style: italic;">(* you can find vectorToList function into source file *)</span>
    vectorToList w <span style="color: #c6c;">0</span></pre></div></div>

<p>After a priori definition of weights I begin network training. I do this by fixing all parameters except one and minimizing residual value <em>I</em> (N &#8212; number of inner collocation points, M &#8212; number of border collocation points, K &#8212; number of neurons): <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_6a8cf3844b89f56120565ec82fd1b766.png" align="absmiddle" class="tex" alt="I = \sum\limits_{i=1}^{N} \left(\left(\sum\limits_{k=1}^{K} w_k\frac{\partial^2 \varphi_k(x_i)}{\partial x^2}\right) - f(x_i)\right)^2 + \lambda\sum\limits_{j=1}^{M}\left(\left(\sum\limits_{k=1}^{K} w_k \varphi_k(x_j)\right)-b(x_j)\right)^2" /></p>
<p>Minimisation of <em>I</em> is made by <a href="http://en.wikipedia.org/wiki/Gradient_descent">gradient descent method</a> so we need to calculate all partial derivatives <img src="http://robotics.icstweb.org/fs4learning/wp-content/cache/tex_20dd523e1d8380e00c5ad6253a0698fc.png" align="absmiddle" class="tex" alt="\frac{\partial I}{\partial a_i}\qquad \frac{\partial I}{\partial c_i}\qquad \frac{\partial I}{\partial w_i}" /></p>

<div class="wp_syntax"><div class="code"><pre class="ocaml" style="font-family:monospace;"><span style="color: #5d478b; font-style: italic;">(* function sum3 gets 3 lists and performs an operation f over heads of all lists, adding result into acc *)</span>
<span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> sum3 <span style="color: #6c6;">&#40;</span>f <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">float</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">float</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">float</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>a <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> list<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>b <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> list<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>c <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> list<span style="color: #6c6;">&#41;</span> x acc <span style="color: #a52a2a;">=</span>
    <span style="color: #06c; font-weight: bold;">match</span> a <span style="color: #06c; font-weight: bold;">with</span>
    <span style="color: #a52a2a;">|</span> <span style="color: #6c6;">&#91;</span><span style="color: #6c6;">&#93;</span> <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> acc
    <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> sum3 f <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">tl</span> a<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">tl</span> b<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">tl</span> c<span style="color: #6c6;">&#41;</span> x <span style="color: #6c6;">&#40;</span>acc<span style="color: #a52a2a;">+</span><span style="color: #6c6;">&#40;</span>f <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">hd</span> a<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">hd</span> b<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">hd</span> c<span style="color: #6c6;">&#41;</span> x<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span>
&nbsp;
<span style="color: #5d478b; font-style: italic;">(* training network *)</span>
<span style="color: #06c; font-weight: bold;">let</span> solve <span style="color: #a52a2a;">=</span>
    <span style="color: #5d478b; font-style: italic;">(* get weights for first iteration by LSM *)</span>
    <span style="color: #06c; font-weight: bold;">let</span> weights <span style="color: #a52a2a;">=</span> getFirstWeights
    <span style="color: #5d478b; font-style: italic;">(* loop *)</span>
    <span style="color: #06c; font-weight: bold;">let</span> <span style="color: #06c; font-weight: bold;">rec</span> fo a b <span style="color: #6c6;">&#40;</span>w <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> list<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>az <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> list<span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>fc <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span> list<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
        <span style="color: #06c; font-weight: bold;">match</span> a <span style="color: #06c; font-weight: bold;">with</span>
        <span style="color: #a52a2a;">|</span> _ <span style="color: #06c; font-weight: bold;">when</span> a<span style="color: #a52a2a;">&amp;</span>lt<span style="color: #a52a2a;">;</span> b <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span>
                        <span style="color: #5d478b; font-style: italic;">(* residual for some inner point x (l1 -- weights, l2 -- widths, l3 -- centers list *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> residualDerivative l1 l2 l3 x <span style="color: #a52a2a;">=</span>
                            <span style="color: #06c; font-weight: bold;">let</span> rez <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>sum3 <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> w a c x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> w <span style="color: #a52a2a;">*</span> <span style="color: #6c6;">&#40;</span><span style="color: #c6c;">4.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> l1 l2 l3 x <span style="color: #c6c;">0.0</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-</span> <span style="color: #6c6;">&#40;</span>neodn x<span style="color: #6c6;">&#41;</span>
                            rez
                        <span style="color: #5d478b; font-style: italic;">(* residual for some border point x *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> residual l1 l2 l3 x <span style="color: #a52a2a;">=</span>
                            <span style="color: #06c; font-weight: bold;">let</span> rez <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span>sum3 <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> w a c x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> w <span style="color: #a52a2a;">*</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> l1 l2 l3 x <span style="color: #c6c;">0.0</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">-</span> <span style="color: #6c6;">&#40;</span>funcBorder x<span style="color: #6c6;">&#41;</span>
                            rez
                        <span style="color: #5d478b; font-style: italic;">(* dI/dw for neuron with width a and center c *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> dIw <span style="color: #6c6;">&#40;</span>a <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>c <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
                            <span style="color: #06c; font-weight: bold;">let</span> lambda <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">100.0</span>
                            <span style="color: #06c; font-weight: bold;">let</span> rez <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">sum_by</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>residualDerivative w az fc x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #c6c;">4.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> collocationInside <span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">+</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">sum_by</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span>lambda<span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>residual w az fc x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> collocationBorder<span style="color: #6c6;">&#41;</span>
                            rez
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* generate list of derivatives for current widths az and centers fc *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> dIdw <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map2</span> dIw az fc
                        <span style="color: #5d478b; font-style: italic;">(* update widths by gradient descent method. Step is fixed (I am planning to add adaptive step) *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> weights2 <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map2</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> a b <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> a<span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>1e<span style="color: #a52a2a;">-</span>12<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>b<span style="color: #6c6;">&#41;</span> w dIdw
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* dI/da  for neuron with width a, center c and weight w *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> dIa <span style="color: #6c6;">&#40;</span>w <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>a <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>c <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
                            <span style="color: #06c; font-weight: bold;">let</span> lambda <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">100.0</span>
                            <span style="color: #06c; font-weight: bold;">let</span> rez <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">sum_by</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>residualDerivative weights2 az fc x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>w<span style="color: #a52a2a;">*</span><span style="color: #c6c;">4.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">-</span><span style="color: #c6c;">5.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">+</span><span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>pow <span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span> <span style="color: #c6c;">4</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>pow a <span style="color: #c6c;">7</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> collocationInside<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">+</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">sum_by</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span>lambda<span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>residual weights2 az fc x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>w<span style="color: #a52a2a;">*</span><span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> collocationBorder<span style="color: #6c6;">&#41;</span>
                            rez
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* generate list of derivatives for current widths2, centers fc, widths az *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> dIda <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map3</span> dIa weights2 az fc
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* update widths *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> az2 <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map2</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> a b <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> a<span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>1e<span style="color: #a52a2a;">-</span>12<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>b<span style="color: #6c6;">&#41;</span> az dIda
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* dI/dc  for neuron with width a, center c and weight w *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> dIс <span style="color: #6c6;">&#40;</span>w <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>a <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #6c6;">&#40;</span>c <span style="color: #a52a2a;">:</span> <span style="color: #06c; font-weight: bold;">float</span><span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">=</span>
                            <span style="color: #06c; font-weight: bold;">let</span> lambda <span style="color: #a52a2a;">=</span> <span style="color: #c6c;">100.0</span>
                            <span style="color: #06c; font-weight: bold;">let</span> rez <span style="color: #a52a2a;">=</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">sum_by</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>residualDerivative weights2 az2 fc x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>w<span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #c6c;">12.0</span><span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #a52a2a;">+</span><span style="color: #c6c;">8.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>pow a <span style="color: #c6c;">6</span><span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> collocationInside<span style="color: #6c6;">&#41;</span> <span style="color: #a52a2a;">+</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">sum_by</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> x <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span>lambda<span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>residual weights2 az2 fc x<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>w<span style="color: #a52a2a;">*</span><span style="color: #c6c;">2.0</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">exp</span> <span style="color: #6c6;">&#40;</span><span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span><span style="color: #6c6;">&#40;</span>x<span style="color: #a52a2a;">-</span>c<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">/</span><span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">*</span>a<span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span><span style="color: #6c6;">&#41;</span> collocationBorder<span style="color: #6c6;">&#41;</span>
                            rez
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* generate list of derivatives for current widths2, centers fc, widths az2 *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> dIdc <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map3</span> dIс weights2 az2 fc
&nbsp;
                        <span style="color: #5d478b; font-style: italic;">(* update centers *)</span>
                        <span style="color: #06c; font-weight: bold;">let</span> fc2 <span style="color: #a52a2a;">=</span> <span style="color: #06c; font-weight: bold;">List</span><span style="color: #a52a2a;">.</span><span style="color: #060;">map2</span> <span style="color: #6c6;">&#40;</span><span style="color: #06c; font-weight: bold;">fun</span> a b <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> a<span style="color: #a52a2a;">-</span><span style="color: #6c6;">&#40;</span>1e<span style="color: #a52a2a;">-</span>12<span style="color: #6c6;">&#41;</span><span style="color: #a52a2a;">*</span>b<span style="color: #6c6;">&#41;</span> fc dIdc
&nbsp;
                        fo <span style="color: #6c6;">&#40;</span>a<span style="color: #a52a2a;">+</span><span style="color: #c6c;">1</span><span style="color: #6c6;">&#41;</span> b weights2 az2 fc2
        <span style="color: #a52a2a;">|</span> _ <span style="color: #a52a2a;">-&amp;</span>gt<span style="color: #a52a2a;">;</span> <span style="color: #6c6;">&#40;</span><span style="color: #6c6;">&#41;</span>
    fo <span style="color: #c6c;">0</span> <span style="color: #c6c;">1000</span> weights funWidths funCenters
    <span style="color: #5d478b; font-style: italic;">(* now I make 1000 of iterations what is enought. But in the future I will change this condition *)</span>
    <span style="color: #5d478b; font-style: italic;">(* return true *)</span>
    <span style="color: #06c; font-weight: bold;">true</span></pre></div></div>

<p>This model is made for learning purposes and I think that now it&#8217;s very simple to analyze parameters and build a more complicated solution.</p>
<p>As always, source code is <a href="http://cid-6656593f7ed14dcf.skydrive.live.com/self.aspx/FSharp/Atrificial%20Neural%20Network/2008.11.10/Neuro%20v1.%20Nikitin%20KS.zip">available</a>.</p>
 <img src="http://robotics.icstweb.org/fs4learning/wp-content/plugins/wordpress-feed-statistics/feed-statistics.php?view=1&post_id=18" width="1" height="1" style="display: none;" />]]></content:encoded>
			<wfw:commentRss>http://robotics.icstweb.org/fs4learning/2008/11/10/neural-network-overview/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
