Difference between pages "Erlang and Neural Networks" and "User:AgatheGeier812"

(Created page with "Older persons use a unique set of concerns that need a unique option. Elderly people are effortlessly more vulnerable to injuries and illness, meaning they should be able to g...")

Line 1:

Line 1:

−

[[Category:AI]]

+

Older persons use a unique set of concerns that need a unique option. Elderly people are effortlessly more vulnerable to injuries and illness, meaning they should be able to get hold of someone who may help [http://www.safe-girl.co.uk Personal alarm] right away if something poor happens. That's where modern tools steps in. By getting a private alarm system that is connected to an alarm system older folks might get aid in [http://www.safe-girl.co.uk Personal alarm] touch of a button.

−

[[Category:Articles]]

+

−

by [http://webjazz.blogspot.com/ Wilhelm]

+

Sometimes, these [http://www.safe-girl.co.uk Personal alarm] methods can be purchased for free through inability charities along with other organizations that will service elderly people.

−

+

−

This is a concatenated version of a series of blog posts on my [http://webjazz.blogspot.com/ blog], which is why it's rather long. Therefore, I've done some editing to make sure it flowed nicely as one piece.

+

−

+

−

I assume that most of my audience are programmers that didn't much like math. If they did, they probably wouldn't be reading this, and would have read it themselves from a textbook. Therefore, I will go into explaining some implications of the math, but I don't actually do any proofs. If you liked math, feel free to make any corrections or notify me through the wiki's messaging system for errors.

+

−

+

−

I wrote this as I was learning Erlang. I hope this article will be informative.

+

−

+

−

== How Erlang and neural networks fit together ==

+

−

+

−

It started with an article about how the [http://www.gotw.ca/publications/concurrency-ddj.htm free lunch is over] for software engineers that a friend sent to me about two years ago. It basically stated that developers have been riding on the wave of Moore's law to save their butts, and it's not going to last forever. In addition, it's been known for a while that chip manufacturers are moving towards multi-core processors to increase performance. If developers are going to take advantage of hardware like they have been, they're going to have to learn how to program concurrent programs.

+

−

+

−

The problem is, programmers suck at it. It's well known that concurrent programming, as it stands, is [http://bc.tech.coop/blog/060105.html not easy for humans]. Even Tim Sweeney, the guy that architected the Unreal Engine (no mere programming mortal), [http://www.cs.princeton.edu/%7Edpw/popl/06/Tim-POPL.ppt thought it was hard]. It was when I started [http://webjazz.blogspot.com/2007/02/for-love-of-programmers-we-need-better.html looking beyond threads] as a concurrency abstraction that I tripped over a programming language developed specifically for concurrency.

+

−

+

−

=== Yet Another Programming Language ===

+

−

+

−

I had always thought that I should learn more about functional programming. It seemed like an odd beast to me, especially since you don't change state through side-effects. "How do you get anything done?" It's kinda like when you first learned that [http://www.acm.org/classics/oct95/ you don't need GOTO], and subsequently, when you learned that [http://notes-on-haskell.blogspot.com/2007/02/whats-wrong-with-for-loop.html FOR loops suck].

+

−

+

−

And yet, I never really found a need or a small project I could do with functional programming that might prove to be satisfying. It was only due to the search for better concurrency abstractions that I ran across [http://www.erlang.org/about.html Erlang], a functional programming language that is used explicitly because it's good at concurrency. In fact, it's pretty much the only one out there that touts concurrency as its strength.

+

−

+

−

It uses the actor model, where processes share no data and just pass messages around. Because there's nothing shared, there's no issue of synchronization or deadlocks. While not as sexy-sounding as futures or software transactional memory, the actor model falls nicely along the lines of complex and emergent systems--systems that have locally interacting parts with a global emergent behavior. Could one of these systems be good for a small side project to do in Erlang?

+

−

+

−

=== A quick detour in neural networks ===

+

−

+

−

Artificial neural networks seemed to be the perfect thing actually. A quick, quick diversion into what they are.

+

−

+

−

[[image:perceptron.gif]]

+

−

+

−

A feed-forward [http://en.wikipedia.org/wiki/Artificial_neural_network artificial neural network] is basically a network of perceptrons that can be trained to classify (recognize) patterns. You give the network a pattern as an input, it can tell you the classification of that input as an output.

+

−

+

−

You can think of a perceptron much like a neuron in your brain, where it has lots of inputs and one output. It's connected to other perceptrons through these inputs and outputs and there are weights attached to the input connections. If there is a pattern of input, and it passes a threshold, the perceptron 'fires' (i.e. outputs a value). This in turn might activate other perceptrons.

+

−

+

−

Even simpler, a perceptron is modeled as a function that takes a vector <span style="font-weight: bold;">x</span> as an input and outputs a number y. All it does is take the dot product (ie. weighted sum) of the input vector <span style="font-weight: bold;">x</span> with weights vector <span style="font-weight: bold;">w</span>, and pass it through a non-linear and continuous thresholding function, usually a [http://mathworld.wolfram.com/SigmoidFunction.html sigmoid function]. When you connect them up in layers, you get an artificial neural network that can learn to classify patterns if you train it with examples.

+

−

+

−

It has to learn patterns by adjusting the weights between perceptrons in the network after each training example, and you tell it how wrong it was in recognizing the pattern. It does this by an algorithm called [http://galaxy.agh.edu.pl/%7Evlsi/AI/backp_t_en/backprop.html back propagation]. It's the same page I lifted all these pictures from. I put all their pictures in an animated gif to illustrate:

+

−

+

−

[[Image:animate_ANN.gif]]

+

−

+

−

In the first part, the example propagates forward to an output. Then it propagates back the error. Lastly, it propagates forward the adjusted weights from the calculated error. This is not the only type of neural network that exists, but we're going to be building this type.

+

−

+

−

=== A good fit for Erlang ===

+

−

+

−

Why would this be a good fit as a subject to play with Erlang? Well, if you'll notice, each perceptron only takes input from its neighboring perceptrons, and only outputs to its neighbors. This is very much in line with the actor model of concurrency. Each process would be a perceptron, and would act as an autonomous agent that only interacts with other processes it comes into contact with--in this case, only other perceptrons it's connected to.

+

−

+

−

In addition, you'll also notice that in the animation, the perceptron values are calculated neuron by neuron. In a concurrent system, there's no reason to do this! You can actually do the calculation layer by layer, since the calculations of any individual perceptron only comes from the outputs of the perceptrons in the layers before it. Therefore, all outputs for perceptrons in a layer can be calculated in parallel.

+

−

+

−

Notice, however, that layers need to be calculated serially. I had originally thought that with the learning process propagating back and forth, maybe it could be pipelined. On closer examination, however, the best one can do is to feed-forward the next input one layer behind the adjusting of weights, to make the learning process go faster.

+

−

+

−

=== State of the Purely Functional ===

+

−

+

−

Erlang is a purely functional programming language. Functional style programming is nothing new--it's just been lurking in the more academic corners of computer science. In the transition from imperative/procedural/OO programming to functional programming, there are new concepts to grok. You'll hear this from people just [http://azaidi.blogspot.com/2007/04/zen-and-art-of-functional-programming.html learning functional programming for the first time] (myself included). The hardest thing for me to get over in a pure functional language is the absence of state. My first reaction was, "Well, how do you get anything done?"

+

−

+

−

Not having state has its advantages, and you'll hear stuff about side-effects and referential transparency. But I'd like to think of it as, <span style="font-style: italic;">things that don't have state can't be broken--they just exist</span>. However, state is useful in computation, and different languages have different ways of getting around it. With Haskell, you use monads. At first, I figured it was the same with Erlang. But in [http://tamale.net/erlang/tutorial.shtml this short tutorial on Erlang], it simply states that Erlang uses Erlang processes to keep state.

+

−

+

−

This maps pretty well with what we're trying to do. Each perceptron has state: its weights. In an object-orientated language, you may keep the weights as attributes in an object. In Erlang, each perceptron will be an Erlang process. It will "keep its state" by calling itself recursively and passing itself back the necessary state information. Any communication between perceptrons will be by send messages back and forth as they fire and stimulate each other.

+

−

+

−

== Building a perceptron ==

+

−

+

−

Enough intro on Erlang and Neural Networks. Let's get to building a perceptron.

+

−

+

−

=== The essence of a perceptron ===

+

−

+

−

[[Image:perceptron2.gif]]

+

−

+

−

So once again, this is a perceptron. It's a weighted sum (a dot product) of the inputs, which is then thresholded by f(e). So we'll write a thresholding function and a weighted sum in Erlang.

+

−

+

−

We start by declaring the name of the module, and the functions to export from the module.

+

−

+

−

<code>-module(ann).

+

−

-export([perceptron/3, sigmoid/1, dot_prod/2, feed_forward/2,

+

−

replace_input/2, convert_to_list/1]).</code>

+

−

+

−

I exported most of the functions, so I can run them from the command line. I'll remove them later on.

Since I wasn't as familiar with all the libraries in Erlang, and I wrote a dot product function, and it wasn't too bad. Erlang, for the most part, doesn't use loops. The common way is to use library functions for list processing, list comprehensions, or recursion. I just decided to try my hand at writing a recursive function. It's been a while. The first part is the base case, and the second part is what you'd do if the "recursion fairy" took care of the rest.

+

−

+

−

<code>dot_prod([], []) ->

+

−

0;

+

−

dot_prod([X_head | X_tail], [Y_head | Y_tail]) ->

+

−

X_head * Y_head + dot_prod(X_tail, Y_tail).</code>

+

−

+

−

Simple, so far, right? So to calculate the feed forward output of a perceptron, we'll do this:

+

−

+

−

<code>feed_forward(Weights, Inputs) ->

+

−

sigmoid(dot_prod(Weights, Inputs)).</code>

+

−

+

−

=== The body of a nerve ===

+

−

+

−

So far, so good. But we still need to create the actual perceptron! This is where the processes and state-keeping comes up.

+

−

+

−

<code>perceptron(Weights, Inputs, Output_PIDs) ->

+

−

receive

+

−

{stimulate, Input} ->

+

−

% add Input to Inputs to get New_Inputs...

+

−

% calculate output of perceptron...

+

−

% stimulate the perceptron my output is connected to

+

−

perceptron(Weight, New_inputs, Output_PIDs)

+

−

end.</code>

+

−

+

−

This is an Erlang process, and it receives messages from other Erlang processes. Currently, it only accepts one message, <span style="font-style: italic;">stimulate(Input)</span> from other Erlang processes. This is a message that other perceptrons will use to send its output to this perceptron's inputs. If a perceptron is stimulated, we should update our list of inputs, then calculate the new output of the perceptron, and then stimulate the perceptron my output is connected to.

+

−

+

−

Notice that at the end of the message, we call the processes again, with <span style="font-style: italic;">New_Inputs</span>. That's how we will maintain and change state.

+

−

+

−

Note this won't result in a stack overflow, because Erlang knows it doesn't have to keep the stack around. This is because no state is ever kept between messages calls and everything you need to know is passed into the function perceptron, so we can throw away the previous instances of the call to perceptron.

+

−

+

−

We do come to a snag though. How do we know which other perceptron the incoming input is from? We need to know this because we need to be able to weigh it correctly. My solution is that <span style="font-style: italic;">Input</span> is actually a tuple, consisting of {Process_ID_of_sender, Input_value}. And then I keep a list of these tuples, like a hash of PID to input values, and convert them to a list of input values when I need to calculate the output. Therefore, we end up with:

+

−

+

−

<code>perceptron(Weights, Inputs, Output_PIDs) ->

+

−

receive

+

−

{stimulate, Input} ->

+

−

% add Input to Inputs to get New_Inputs...

+

−

New_inputs = replace_input(Inputs, Input),

+

−

+

−

% calculate output of perceptron...

+

−

Output = feed_forward(Weights, convert_to_list(New_inputs)),

+

−

+

−

perceptron(Weights, New_inputs, Output_PIDs)

+

−

end.

+

−

+

−

replace_input(Inputs, Input) ->

+

−

{Input_PID, _} = Input,

+

−

lists:keyreplace(Input_PID, 1, Inputs, Input).

+

−

+

−

convert_to_list(Inputs) ->

+

−

lists:map(fun(Tup) ->

+

−

{_, Val} = Tup,

+

−

Val

+

−

end,

+

−

Inputs).</code>

+

−

+

−

If you're not familiar with the map function, check out [http://www.joelonsoftware.com/items/2006/08/01.html Joel's explanation on it]. The map function you see in <span style="font-style: italic;">convert_to_list()</span> is the same as the map function in ruby that would go:

+

−

+

−

<code>def convert_to_list(inputs)

+

−

inputs.map { |tup| tup.last }

+

−

end</code>

+

−

+

−

Now, there's one last thing that needs to be done. Once we calculate an output, we need to fire that off to other perceptrons that accept this perceptron's output as its input. And if it's not connected to another perceptron, then it should just output its value. So then we end up with:

+

−

<code>perceptron(Weights, Inputs, Output_PIDs) ->

+

−

receive

+

−

{stimulate, Input} ->

+

−

% add Input to Inputs to get New_Inputs...

+

−

New_inputs = replace_input(Inputs, Input),

+

−

+

−

% calculate output of perceptron...

+

−

Output = feed_forward(Weights, convert_to_list(New_inputs)),

+

−

+

−

% stimulate the perceptron my output is connected to

+

−

if Output_PIDs =/= [] ->

+

−

lists:foreach(fun(Output_PID) ->

+

−

Output_PID ! {stimulate, {self(), Output}}

+

−

end,

+

−

Output_PIDs);

+

−

Output_PIDs =:= [] ->

+

−

io:format("~n~w outputs: ~w", [self(), Output])

+

−

end,

+

−

perceptron(Weights, New_inputs, Output_PIDs)

+

−

end.</code>

+

−

+

−

We know which perceptrons to output to, because we keep a list of perceptron PIDs that registered with us. So if the list of Output_PIDs is not empty, then for each PID, send them a message with a tuple that contains this perceptron's PID as well as the calculated Output value. Let's try it out in a test drive

So you can see, we got an output of 0.581759. We can verify this by doing this on our TI-85 calculator:

+

−

+

−

<code>x = 0.5 * 0.3 + 0.2 * 0.9

+

−

Done

+

−

1 / (1 + e^-x)

+

−

.581759376842</code>

+

−

+

−

And so we know our perceptron is working feeding forward! Next, we have to figure out how to connect them up to each other.

+

−

+

−

== Interconnecting Perceptrons ==

+

−

+

−

We see that neural network is basically made up of interconnected perceptrons (or neurons), and they are basically modeled as a linear combination of inputs and weights with a non-linear function that modifies the output.

+

−

+

−

=== Drawing a line in the sand ===

+

−

+

−

Classifiers often do very well strictly on probabilities, such as Bayesian Spam Filters. But often times, we don't know what the underlying probabilities are for the data, and not only that, we don't have lots of training data to build accurate probability densities. One way around that is to draw a line in the data space that acts as the decision boundary between two (or more) classes. That way, you only have to find the parameters (i.e. weights) of the line, which is often fewer in number than the entire probability space.

+

−

+

−

This is exactly what a perceptron does. It creates a decision boundary in data space. If the data space is a plane (2D, or having two inputs), then it draws a line. In 3D, it draws a plane. For higher data space dimensions (4D or more), it draws a hyperplane.

+

−

+

−

=== Why not just one? ===

+

−

+

−

The problem with just using a perceptron is that it can only classify data that is linearly separable--meaning data you can separate with a line. The [http://www.generation5.org/content/1999/perceptron.asp XOR problem] is a simple illustration of how you can't draw a line that separates between on and off in an XOR. Minsky and Papert wrote a famous paper that kinda [http://www.ucs.louisiana.edu/%7Eisb9112/dept/phil341/histconn.html killed off research] in this field for about a decade because they pointed this out.

+

−

+

−

So to get around this linearity, smart people eventually figured out that they can chain perceptrons together in layers, and that gives them the ability to express ANY non-linear function, given an adequate number of hidden layers.

We would never call connect_to_output() or connect_to_input() directory. We'd just use connect(). It basically just adds the perceptron's process ID to each other, so they know who to send messages to when they have an output.

+

−

+

−

Note that the last message connect_to_input() isn't followed by a semicolon. That means every message before it in perceptron needs to end with one. So if you've been following along, the stimulate() message from part II needs a semicolon at the end of it now.

+

−

+

−

We can now connect up our perceptrons, but with the way it is, currently, we'd have to send a separate message to each perceptron connected to an input to the network. This is tedious. We are programmers and we are lazy. Let's make a perceptron also double as an source node. As source node simply passes its input to to its outputs.

+

−

+

−

<code>

+

−

perceptron(Weights, Inputs, Output_PIDs) ->

+

−

receive

+

−

% previous messages above and in part II

+

−

+

−

{pass, Input_value} ->

+

−

lists:foreach(fun(Output_PID) ->

+

−

io:format("Stimulating ~w with ~w~n", [Output_PID, Input_value]),

+

−

Output_PID ! {stimulate, {self(), Input_value}}

+

−

end,

+

−

Output_PIDs);

+

−

end.

+

−

</code>

+

−

+

−

Now we can start creating perceptrons.

+

−

+

−

<code>64> N1_pid = spawn(ann, perceptron, [[],[],[]]).

+

−

<0.325.0>

+

−

65> N2_pid = spawn(ann, perceptron, [[],[],[]]).

+

−

<0.327.0>

+

−

66> N3_pid = spawn(ann, perceptron, [[],[],[]]).

+

−

<0.329.0></code>

+

−

+

−

Note that we get back three process IDs of the three perceptrons we created. Then we start connecting them.

+

−

+

−

<code>67> ann:connect(N1_pid, N2_pid).

+

−

<0.325.0> output connected to <0.327.0>: [<0.327.0>]

+

−

<0.327.0> inputs connected to <0.325.0>: [{<0.325.0>,0.500000}]

+

−

{connect_to_input,<0.325.0>}

+

−

68> ann:connect(N1_pid, N3_pid).

+

−

<0.325.0> output connected to <0.329.0>: [<0.329.0>,<0.327.0>]

+

−

<0.329.0> inputs connected to <0.325.0>: [{<0.325.0>,0.500000}]

+

−

{connect_to_input,<0.325.0>}</code>

+

−

+

−

We used N1 as an input node connected to perceptrons 2 and 3. So if N1 is passed a value, N2 and N3 should be stimulated with that value.

+

−

+

−

<code>69> N1_pid ! {pass, 0.5}.

+

−

Stimulating <0.329.0> with 0.500000

+

−

{pass,0.500000}Stimulating <0.327.0> with 0.500000

+

−

+

−

<0.329.0> outputs: 0.562177

+

−

+

−

<0.327.0> outputs: 0.562177

+

−

</code>

+

−

+

−

Hurray! So now, the network's got tentacles, that we can connect all over the place, writhing, and wiggling with all its glee. However, this is currently a DUMB network. It can't classify anything because we haven't told it how to learn anything yet. How does it learn to classify things? It does so by adjusting the weights of the inputs of each perceptron in the network. And this, is the crux of neural networks in all its glory.

+

−

+

−

== Getting your learn on ==

+

−

+

−

In the last part, we were able to connect the perceptrons to each other. This time, we're going to look at how you'd actually learn.

+

−

+

−

=== The ways of learning ===

+

−

+

−

There are many types of neural networks. But this one that we're building is a classic feed-forward neural network. A feed-forward neural network is a linear classifier, and the way it learns is to adjust the [http://en.wikipedia.org/wiki/Hyperplane hyperplane] that separates different classes in multi-dimensional space to minimize classification error, according to what it has seen before. The way that one would adjust the hyperplane is to change the value of the weights in the neural network. But how much to adjust it?

+

−

+

−

The classic way is to use back propagation, which we'll explore here. Back propagation at its heart is just [http://en.wikipedia.org/wiki/Gradient_descent gradient descent]. The layman's term of gradient descent is "pick the direction that gives me the steepest change". Gradient descent is known to get stuck at local minima(or maxima). But doing gradient descent in a high dimensional space like neural networks, it seems like it's hard to get stuck in a local minima, because there's always another "dimension" to escape.

+

−

+

−

People since used other methods to calculate the weights, such as genetic algorithms and particle swarm optimization. You can basically use any type of optimization algorithm to adjust the weights. Since back propagation is commonly associated with feed-forward neural networks, we'll concentrate on that.

+

−

+

−

=== Carrying the Error Backwards ===

+

−

+

−

To figure out the error at the output node is easy. You simply subtract the output from what the output was suppose to be, and that's your error (not exactly, but that's the idea). Given that error, it's relatively easy to adjust the weights of the output node. The problem is, how do you adjust weights in the hidden layers when you can't directly see their output? Even if you could, how would you know which way to adjust it, since it would affect other nodes?

+

−

+

−

The basic idea of back propagation is to get the output of the network and compare its decision with the decision it should have made, and more importantly, how far off it was. That is the error rate of decision. We'll take that error and propagate it backwards towards the input so we will know how to adjust the weights, layer by layer.

+

−

+

−

I'm not going to go too much into the hows and whys back propagation, since I feel like there's a lot of tutorials out there that do it justice. If you need a book, I suggest [http://www.amazon.com/AI-Application-Programming-Tim-Jones/dp/1584502789 AI Application Programming] by Tim Jones. And I won't go into the proof either. But I will show and explain the result, since it makes understanding the code a lot easier.

+

−

+

−

=== Propagating from the output ===

+

−

+

−

Calculating the change in weights for the output node isn't too bad. Using my awesome GIMP skillz...it looks like this:

+

−

+

−

[[Image:ann_labels.png]]

+

−

+

−

We'll start from the back. I color coded it to make it easier to figure out what the equations are saying. (If a variable is bolded, that means it's a vector) The error of output of the training input is:

where t is what the output should have been, and z is what we actually got from the neural network. J(w) is basically a sum of all the errors across all output nodes. You'd want a square of the differences because you want to make all differences positive before you sum them, so the errors don't cancel each other out. The double lines stand for [http://en.wikipedia.org/wiki/Norm_%28mathematics%29 norm]. You can think of norm as "length of vector". Norm is just a convenient way to write it.

+

−

+

−

If you wanted to derive back propagation, you'd take the derivative of J(<span style="font-weight: bold;">w</span>) with respect to <span style="font-weight: bold;">w</span>, and try to minimize J. Remember what I said about going in the direction of steepest change in error? Well, to calculate change, you calculate the derivative (since derivative means change), and that's why you'd do it in the proof. If you want to follow the proof, check out page 290-293 of [http://www.amazon.com/Pattern-Classification-2nd-Richard-Duda/dp/0471056693/ref=pd_bbs_sr_1/102-9577008-0324967?ie=UTF8&s=books&amp;qid=1183961912&sr=8-1 Pattern Classification] by Duda, Hart, and Stork.

+

−

+

−

=== The hyperplane ===

+

−

+

−

Skipping all the proof, you'd get two equations by deriving from J(<b>w</b>). One for calculating the adjustment of weights in the output layer (red layer), and the other for calculating the adjustment in weights of the hidden layer (yellow), and the input layer (green).

This is the equation to adjust the <span style="color: rgb(153, 51, 153);">input weights (purple)</span> of the <span style="color: rgb(204, 0, 0);">output layer</span>. It's not too bad, and I'll go through each part.

+

−

<ul><li>ɳ - The eta (funny looking 'n') in the beginning is the learning rate. This is a variable you tweak to adjust how fast the neural network learns. I'll talk more about that some other time, but don't think that you'd want to set this as high as possible.

+

−

</li><li>(<span>t<sub style="color: rgb(204, 0, 0);">k</sub> - <span style="color: rgb(204, 0, 0);">z</span><sub style="color: rgb(204, 0, 0);">k</sub></span>) - Next, note that <span>t<sub>k</sub> - z<sub>k</sub></span> aren't bolded, so they are two numbers: 1) what the output was suppose to be 2) and the output of the neural network of the <span style="color: rgb(204, 0, 0);">kth output node</span>. For us, we only have one output node.</li><li>f'(<span style="color: rgb(153, 51, 153);">net</span><sub style="color: rgb(153, 51, 153);">k</sub>) - Remember back in [http://webjazz.blogspot.com/2007/04/erlang-and-neural-networks-part-ii.html part II], where we were talking about the [http://en.wikipedia.org/wiki/Sigmoid_function sigmoid function]? f'(<span><span><span>x</span></span></span>) is the derivative of the sigmoid function. If I haven't forgotten my calculus, it should be:

</li></ul>In equation (2) above, we'll need a part of it to send back to the <span style="color: rgb(204, 153, 51);">hidden layers</span>. We'll represent it by a lower case delta (looks like an 'o' with a squiggly on top). It is called the sensitivity. This is what we propagate back to the other layers, and where the technique gets its name.

<ul><li>As you can see, this is more of the same. The only difference is the second term, which is the dot product of all the <span style="color: rgb(153, 51, 153);">output node input weights</span> (<span style="color: rgb(153, 51, 153);">w</span><sub style="color: rgb(153, 51, 153);">kj</sub>) from a <span style="color: rgb(204, 153, 51);">hidden node</span> and the sensitivities (<span style="color: rgb(204, 0, 0);">δ</span><sub style="color: rgb(204, 0, 0);">k</sub>) across all output nodes the <span style="color: rgb(204, 153, 51);">hidden node</span> is connected to.</li><li><span style="color: rgb(51, 102, 255);">net</span><sub style="color: rgb(51, 102, 255);">j</sub> is like as before--it's the dot product of the inputs <span style="color: rgb(0, 153, 0);">x</span><sub style="color: rgb(0, 153, 0);">i</sub> with the <span style="color: rgb(51, 102, 255);">inputs weights</span> of the <span style="color: rgb(204, 153, 51);">hidden nodes</span>.

+

−

</li></ul>

+

−

+

−

=== If your eyes glazed over ===

+

−

+

−

If your eyes glazed over in the last part, <b>pay attention here, because this is the important part.</b>

+

−

+

−

From equation (5), it tells us what we'll need to code. It tells us that from the perspective a single <span style="color: rgb(204, 153, 51);">hidden node</span>, the adjustment of its <span style="color: rgb(51, 102, 255);">input weights</span> depends on:

+

−

+

−

<ol>

+

−

<li>From the 2nd term: the set of sensitivities and the <span style="color: rgb(153, 51, 153);">associated weights of the output layer</span> from the <span style="color: rgb(204, 0, 0);">next layer</span> that the <span style="color: rgb(204, 153, 51);">hidden node</span> is connected to.</li>

I know we don't have 3 output nodes and 4 input nodes. It's just to illustrate that from the perspective of the hidden node, this would be the information it needs from the layers surrounding it to adjust its input weights.

+

−

+

−

In the code base we've written so far, the input weights are contained as in the perceptron. So <span style="color: rgb(51, 102, 255);">w<sub>ji</sub></span> would belong to the <span style="color: rgb(204, 153, 51);">hidden layer</span>, and <span style="color: rgb(153, 51, 153);">w</span><sub style="color: rgb(153, 51, 153);">kj</sub> would belong to the <span style="color: rgb(204, 0, 0);">output layer</span>. Therefore, the <span style="color: rgb(204, 0, 0);">output layer</span> would need to send both the sensitivity and the <span style="color: rgb(153, 51, 153);">output layer input weights</span> back to the <span style="color: rgb(204, 153, 51);">hidden node</span>.

+

−

+

−

This perspective is important, because Erlang follows an Actor model, where you model the problem as individual agents that pass messages back and forth to each other. We have now written how each individual node adjusts its weights, and that will help us in our coding.

+

−

+

−

This also means that as the current implementation is headed, I am assuming an asynchronous model of the neural network. Each perceptron will update when any of its inputs change. That means, like a digital circuit, there will be a minimum time that it takes for the output to reach a correct steady state and for the weight adjustments to propagate back. What this minimum time will be, will probably depend on the number of hidden layers. We'll see if it'll work. I have a hunch it should be ok, as long as the inputs are throttled to wait until the minimal time passes before feeding it a new set of inputs. It might result a lot of unnecessary messages, but if we can get away with it while keeping the code simple, I think it's probably worth it.

+

−

+

−

Next time, we'll get to the code. I had intended to get to it this installment, but the code will make a lot more sense if you know what the math is saying about it.

+

−

+

−

== Refactoring code ==

+

−

+

−

I wrote this as a series over time, so I had to refactor some of the code before continuing. I decided to leave it in, since it shows off some of the features of functional style programming, for those of you coming from an imperative background.

+

−

+

−

=== Vector map ===

+

−

map() is a function that applies the same operation to all elements in an array. I needed a function that is a map for two different lists of the same length. Vector and matrix addition relies on this:

+

−

+

−

<code>

+

−

[1,2,3] + [4,5,6] = [5,10,18]

+

−

</code>

+

−

+

−

I couldn't find one in the erlang APIs, so I wrote one myself. Ends up this code is used in a couple places.

+

−

+

−

<code>% like map, but with two lists instead.

+

−

vector_map(Func, [], []) ->

+

−

[];

+

−

vector_map(Func, [Xh | Xt], [Yh | Yt]) ->

+

−

[Func(Xh, Yh) | vector_map(Func, Xt, Yt)].</code>

+

−

+

−

This looks very much like the code for the previous dot_prod(). Let's keep things DRY and make dot_prot() use vector_map.

+

−

+

−

<code>% Calculates the dot product of two lists

+

−

dot_prod(X, Y) ->

+

−

lists:foldl(fun(E, Sum) -> E + Sum end, 0,

+

−

vector_map(fun(Ex, Ey) -> Ex * Ey end, X, Y)).

+

−

</code>

+

−

+

−

So vector_map() takes the the nth element and multiplies them together. foldl() is very much like reduce() in MapReduce or inject() in ruby. It takes all the elements and sums it up.

+

−

+

−

=== f'(net<sub>j</sub>) ===

+

−

We need to calculate the feed forward, but with the derivative of the sigmoid function.

+

−

+

−

Recall that our feed_forward function looks like this:

+

−

+

−

<code>sigmoid(X) ->

+

−

1 / (1 + math:exp(-X)).

+

−

+

−

feed_forward(Weights, Inputs) ->

+

−

sigmoid(dot_prod(Weights, Inputs)).</code>

+

−

+

−

According to our calculations in the last part, we also need the derivative of the sigmoid function, but with the same stuff passed into it. So we could write another function like this:

+

−

+

−

<code>

+

−

sigmoid_deriv(X) ->

+

−

math:exp(-X) / (1 + math:exp(-2 * X)).

+

−

+

−

feed_forward_deriv(Weights, Inputs) ->

+

−

sigmoid_deriv(dot_prod(Weights, Inputs)).

+

−

</code>

+

−

+

−

This is a waste, since the two versions of feed forward are essentially doing the same thing. Here is where we can use first-class functions to get around it.

+

−

+

−

Languages that support first class functions are powerful because you can pass functions in and out of other functions as arguments. This is nice because you don't need a lot of structure to get things done--no messing around with anonymous inner classes and function pointers.

+

−

+

−

Instead, we'll write a new Sigmoid functions in perceptron and the feed_forward function outside of it.

+

−

+

−

<code>

+

−

perceptron(Weights, Inputs, [other variables]) ->

+

−

Sigmoid = fun(X) ->

+

−

1 / (1 + math:exp(-X))

+

−

end,

+

−

+

−

Sigmoid_deriv = fun(X) ->

+

−

math:exp(-X) / (1 + math:exp(-2 * X))

+

−

end,

+

−

receive

+

−

% perceptron messages...

+

−

end.

+

−

+

−

feed_forward(Func, Weights, Inputs) ->

+

−

Func(dot_prod(Weights, Inputs)).

+

−

</code>

+

−

+

−

So now if we want to feed forward with a particular non-linear activation function, we just tack it on as an argument!

+

−

+

−

<code>feed_forward(Sigmoid, [1, 2], [2, 3]). % f(net)

+

−

feed_forward(Sigmoid_deriv, [1, 2], [2, 3]). % f'(net)

+

−

</code>

+

−

+

−

=== Some helper functions to refactor ===

+

−

+

−

Remember convert_to_list?

+

−

<code>convert_to_list(Inputs) ->

+

−

lists:map(fun(Tup) ->

+

−

{_, Val} = Tup,

+

−

Val

+

−

end,

+

−

Inputs).</code>

+

−

+

−

Let's change it to convert_to_value and add a convert_to_key function:

+

−

+

−

<code>

+

−

convert_to_values(Tuple_list) ->

+

−

lists:map(fun(Tup) ->

+

−

{_, Val} = Tup,

+

−

Val

+

−

end,

+

−

Tuple_list).

+

−

+

−

convert_to_keys(Tuple_list) ->

+

−

lists:map(fun(Tup) ->

+

−

{Key, _} = Tup,

+

−

Key

+

−

end,

+

−

Tuple_list).

+

−

</code>

+

−

+

−

Again, I'm sure there's a good erlang call for this, but I couldn't find any. I decided not to use first class functions here, because calling it would have been a pain if I had to pass in the function. that just selected either the first or last element in a tuple.

+

−

+

−

=== Taking note of sensitivities ===

+

−

We also need to adjust our perceptron to keep the sensitivities from the next layer, in the same way we keep the outputs from the previous layer. That means that our perceptron will no longer keep a list of just Output_PIDs, but a list of tuples [{OutputPID_1, Sensitivity_1}, ... {OutputPID_n, Sensitivity_n}], called Sensitivities.

We'll need to do this for all occurrences of Output_PID in other messages, such as pass, connect_to_output, and connect_to_input. When we need a list of Output_PIDs, we simply call convert_to_keys(Sensitivities).

+

−

+

−

=== The actual learning ===

+

−

Now that we have all that other stuff out of the way, we can actually write the learning message of a perceptron. The steps will be:

+

−

+

−

# Update the list of sensitivities from the previous layer

+

−

# Calculate the sensitivity for this node

+

−

# Adjust all the weights based on the sensitivity

+

−

#Propagate sensitivities and associated weight back to the previous layer

Note that there are guards (the conditions that start with "when") on each of these functions. They are the different cases for how to calculate the sensitivity, given whether the node was an input node, an output node, or a hidden node.

+

−

+

−

=== Test Run ===

+

−

So as a little test, we can exercise it by writing a function called run(), and running it from the command line:

+

−

<code>run() ->

+

−

X1_pid = spawn(ann, perceptron, [[],[],[]]),

+

−

X2_pid = spawn(ann, perceptron, [[],[],[]]),

+

−

H1_pid = spawn(ann, perceptron, [[],[],[]]),

+

−

H2_pid = spawn(ann, perceptron, [[],[],[]]),

+

−

+

−

O_pid = spawn(ann, perceptron, [[],[],[]]),

+

−

+

−

% Connect input node X1 to hidden nodes H1 and H2

+

−

ann:connect(X1_pid, H1_pid),

+

−

ann:connect(X1_pid, H2_pid),

+

−

+

−

% Connect input node X2 to hidden nodes H1 and H2

+

−

ann:connect(X2_pid, H1_pid),

+

−

ann:connect(X2_pid, H2_pid),

+

−

+

−

% Connect input node H1 and H2 to output node O

+

−

ann:connect(H1_pid, O_pid),

+

−

ann:connect(H2_pid, O_pid),

+

−

+

−

X1_pid ! {status},

+

−

X2_pid ! {status},

+

−

H1_pid ! {status},

+

−

H2_pid ! {status},

+

−

O_pid ! {status},

+

−

+

−

X1_pid ! {pass, 1.8},

+

−

X2_pid ! {pass, 1.3}.</code>

+

−

+

−

<code>233> ann:run().

+

−

Output of <0.841.0> connected to Input of <0.843.0>

+

−

Output of <0.841.0> connected to Input of <0.844.0>

+

−

Output of <0.842.0> connected to Input of <0.843.0>

+

−

Output of <0.842.0> connected to Input of <0.844.0>

+

−

Output of <0.843.0> connected to Input of <0.845.0>

+

−

Output of <0.844.0> connected to Input of <0.845.0>

+

−

Status of Node(<0.841.0>)

+

−

W: []

+

−

I: []

+

−

S: [{<0.844.0>,0},{<0.843.0>,0}]

+

−

Status of Node(<0.842.0>)

+

−

W: []

+

−

I: []

+

−

S: [{<0.844.0>,0},{<0.843.0>,0}]

+

−

Status of Node(<0.843.0>)

+

−

W: [0.891633,-0.112831]

+

−

I: [{<0.842.0>,0.446080},{<0.841.0>,-0.815398}]

+

−

S: [{<0.845.0>,0}]

+

−

Status of Node(<0.844.0>)

+

−

W: [0.891633,-0.112831]

+

−

I: [{<0.842.0>,0.446080},{<0.841.0>,-0.815398}]

+

−

S: [{<0.845.0>,0}]

+

−

Status of Node(<0.845.0>)

+

−

W: [0.891633,-0.112831]

+

−

I: [{<0.844.0>,0.446080},{<0.843.0>,-0.815398}]

+

−

S: []

+

−

{pass,1.30000}Stimulating <0.844.0> with 0.200000

+

−

Stimulating <0.844.0> with 0.300000

+

−

+

−

Stimulating <0.843.0> with 0.200000

+

−

Stimulating <0.843.0> with 0.300000

+

−

<0.845.0> outputs: 0.650328

+

−

Stimulating <0.844.0> with 1.80000

+

−

Stimulating <0.844.0> with 1.30000

+

−

<0.845.0> outputs: 0.643857

+

−

Stimulating <0.843.0> with 1.80000

+

−

Stimulating <0.843.0> with 1.30000

+

−

<0.845.0> outputs: 0.606653

+

−

<0.845.0> outputs: 0.607508

+

−

(<0.845.0>) New Sensitivities: []

+

−

(<0.845.0>) Calculated Sensitivity: 0.178902

+

−

(<0.845.0>) Adjusted Weights: [0.891633,-0.112831]

+

−

<0.845.0> outputs: 0.610857

+

−

(<0.844.0>) New Sensitivities: [{<0.845.0>,0.168491}]

+

−

(<0.843.0>) New Sensitivities: [{<0.845.0>,-1.12092e-2}]

+

−

<0.845.0> outputs: 0.655916

+

−

+

−

...(clipped)...

+

−

</code>

+

−

+

−

This is an asynchronous implementation of a feed-forward network, so there will be a lot of messages flying around back and forth. It is outputting the results of the network at the same time it's adjusting its weights. One will see a finer-grain adjustment of the outputs, and will have to wait until all stimulations have passed through the system to grab the output.

+

−

+

−

One can cut down on these messages if you implement something that keeps track of which inputs or sensitivities have been updated. Wait until all inputs have been updated to stimulate the next layer, and wait until all sensitivities have been updated to propagate back to the next layer.

+

−

+

−

=== The trainer and bias ===

+

−

So now the neural network knows how to learn! All the hard part is done. However, there is one last part that I will leave as an exercise to the reader--that is to write a trainer for the network.

+

−

+

−

A trainer takes a list of examples, and feeds them to the network, one at a time, and for each valid output(remember the asynchronous network will output a series of values for any set of inputs), find the difference with the training value and send it to the learn message of the output node.

+

−

+

−

Right now, in the "stimulate" message, it simply calls the learn method directly when an output node prints its output. It always uses "1" as the default training value. This is just a place holder. The output of a network should call messages in a trainer, and do what was described in the previous paragraph.

+

−

+

−

The current implementation also does not take explicit account of biases for every node. A bias is simply a weight in a node that is not associated with any particular input, but is added to the dot product before being sent through the sigmoid function. It does affect the expressiveness of the neural network, so I will also leave that as an exercise for the reader.

+

−

+

−

=== In Conclusion ===

+

−

Hopefully this has taken the mysticism out of neural networks, and showed you some features of the Erlang programming language.

+

−

+

−

Neural networks are a deep topic, and this article hasn't even begun to scratch the surface. As a mentioned before, there are many different types of neural networks, and different ways of implementing them. Keep on learning.

+

−

+

−

<hr>

+

−

Wilhelm is at [http://www.3cglabs.com/ 3cglabs] and is working on making people expand their world by helping them discover the local adventures around them. He writes a tech blog at http://webjazz.blogspot.com/ where he highlights trends, Rails tips, and exploration of Erlang, Javascript, and Ruby.

+

Latest revision as of 06:10, 20 November 2012

Older persons use a unique set of concerns that need a unique option. Elderly people are effortlessly more vulnerable to injuries and illness, meaning they should be able to get hold of someone who may help Personal alarm right away if something poor happens. That's where modern tools steps in. By getting a private alarm system that is connected to an alarm system older folks might get aid in Personal alarm touch of a button.

Sometimes, these Personal alarm methods can be purchased for free through inability charities along with other organizations that will service elderly people.