On Jun 10, 2:07 am, "alfred.morgan.al...@gmail.com"
<alfred.morgan.al...@gmail.com> wrote:
> Thanks for the advice, but at present I'm simply aiming to get the
> very basics of a neural net up and running without having to worry
> about a training algorithm at all. Here's what I have so far (again,
> very basic)
>
> ;; Net0
>
> (def nodes {})
>
> (defn insertNode [node]
> (do (def nodes (assoc nodes (:name node) node)) node) )
>
> (defn addNode [#^String name]
> (let [node {:inputs {}, :outputs {}, :activation 0, :name name}]
> (insertNode node) ))
>
> (defn connectNode [a b weight]
> (do
> (insertNode (assoc a :outputs (assoc (:outputs a) (:name b)
> weight)))
> (insertNode (assoc b :inputs (assoc (:inputs b) (:name a)
> weight))) ))
>
> (defn getWeight [a b]
> (get (:outputs a) (:name b)) )
>
> ; Now, we do some actual testing:
>
> (def n1 (addNode "Node1"))
> (def n2 (addNode "Node2"))
> (connectNode n1 n2 40)
> (println (getWeight (nodes "Node1") (nodes "Node2")))
>
> I've had a look at the concurrent ant simulation Rich Hickey
> presented, but the code's a little more advanced than my own reading-
> are 'sync' and 'alter' the sort of things I should be looking into,
> and, if so, is there any way I can abstract them away with macros?
I think refs and dosync/alter is what you want:
(def nodes (ref {}))
(defn insert-node [node]
(dosync (alter nodes assoc (:name node) node))
node)
But I would write this a bit differently, you might want to have more
than one net at a time, so maybe pass the net as an argument to the
function instead of using a global variable? I'd also pass in the
names of the nodes instead of the nodes themselves to the connect-node
and get-weight functions:
(defn insert-node [m node]
(assoc m (:name node) node)))
(defn add-node [m #^String name]
(insert-node m
{:inputs {}, :outputs {} :activation 0 :name name}))
(defn connect-node [m a b weight]
(update-in (update-in m [a :outputs]
assoc b weight)
[b :inputs] assoc a weight))
(defn get-weight [m a b]
(get (:outputs (m a)) b))
This way you can use reduce to build up your net (This may take
some effort to get your head around, but it's worth it):
user=> (let [nodes (reduce add-node {}
'("Node1" "Node2" "Node3" "Node4"))
connections '(["Node1" "Node2" 20]
["Node2" "Node3" 10])
net (reduce (fn [m [a b weight]]
(connect-node m a b weight))
nodes connections)]
(println "Node1 -> Node2 weight: "
(get-weight net "Node1" "Node2"))
(println "The net:")
(doseq [key (keys net)]
(println (net key))))
Node1 -> Node2 weight: 20
The net:
{:inputs {}, :outputs {}, :activation 0, :name Node4}
{:inputs {Node2 10}, :outputs {}, :activation 0, :name Node3}
{:inputs {Node1 20}, :outputs {Node3 10}, :activation 0, :name Node2}
{:inputs {}, :outputs {Node2 20}, :activation 0, :name Node1}
nil
--
-asbjxrn
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to clojure@googlegroups.com
Note that posts from new members are moderated - please be patient with your
first post.
To unsubscribe from this group, send email to
clojure+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en
-~----------~----~----~----~------~----~------~--~---