views:

98

answers:

3

I'm newbie in Functional Programming.

I have a huge neural network with thousands of neurons and every connection between neurons has its weight. I have to update these weights very often, several thousand times per learning session.

Is FP still applicable here? I mean in fp we can't modify variables and only able to return new variables not changing previous values. Does this mean I have to recreate whole network on every weight update?

A: 

Look into Haskell arrays which include mutable variants in a monad.

Doug Currie
A: 

You should not need to recreate the entire network every time a weight update occurs. Presumably, your neurons are modeled as individual objects - this means that to "update" an individual neuron, you would actually be creating a new neuron with the updated weight. Then this neuron would be inserted into the network in place of the old one, which would in turn be free for reclamation by the garbage collector.

I do not agree with the idea of using mutable state. Functional languages know that they are functional, so they make optimizations for functional programming. If a functional language really is the best tool for the job, then take advantage of its benefits.

danben
Let's say I've got simple tree: Root | \Node1 Node2So if I create Node3 and replace Node1 with Node3, doesnt that mean I'm changing the whole tree?
Denis Gorodetskiy
There are two things wrong with your example. Firstly, your post asks whether you need to recreate the whole network for every weight update, not whether it "changes". I addressed this issue in my answer. Second, you made it small enough such that there is some change to every node. Imagine instead you have a network of 1000 nodes, and just one of them needs to be replaced. Are you still changing the whole tree?
danben
Don't your words "Then this neuron would be inserted into the network in place of the old one" mean that neural network actually changes? I mean replacing part of a whole - isn't it a change of variable (neural net in this case)?
Denis Gorodetskiy
Try thinking of the neural net as a set of entities (nodes) rather than a single entity.
danben
+1  A: 

If you structure your data in such a way that you can use a persistent data structure to model your neural network, functional updates to the neural network will be cheap (at least compared to copying the whole thing).

If it is still not fast enough, your language may allow other techniques (such as careful use of mutation) to speed it up; for example, if you were using Clojure, you could use transients to some additional speed.

Brian