-
-
Save tomjack/8d58e8f912dcaa24e7d1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(ns com.tomojack.clj-neat.nn | |
(refer-clojure)) | |
(defn sigmoid [x] | |
(/ 1 (inc (Math/exp (- x))))) | |
(defn random-weight [] | |
(dec (rand 2))) | |
(defstruct network | |
:input-nodes :hidden-nodes :output-nodes) | |
(defstruct node | |
:activation :connections :activation-fn) | |
(defstruct connection | |
:weight :incoming-node :cached-activation) | |
(defn random-connections [nodes] | |
(for [n nodes] | |
(struct connection (random-weight) n nil))) | |
(defn random-network [input hidden output] | |
(let [input-nodes (take input (repeatedly #(ref (struct node (rand) [] sigmoid)))) | |
hidden-nodes (take hidden (repeatedly #(ref (struct node nil (random-connections input-nodes) sigmoid)))) | |
output-nodes (take output (repeatedly #(ref (struct node nil (random-connections hidden-nodes) sigmoid))))] | |
(struct network input-nodes hidden-nodes output-nodes))) | |
(defn pprint-network [net] | |
(doseq [node-type [:input-nodes :hidden-nodes :output-nodes]] | |
(println (apply str (name node-type) ": " | |
(interpose ", " | |
(map #(str (:activation @%) " (" (apply str (interpose ", " (map :cached-activation (:connections @%)))) ")") | |
(node-type net))))))) | |
(defn connection-activation [conn] | |
(let [cached (:cached-activation conn)] | |
(if cached | |
(* cached (:weight conn))))) | |
(defn update-node [node] | |
(assoc node :activation | |
(let [incoming-activations (seq (remove nil? (map connection-activation (:connections node))))] | |
(if incoming-activations | |
((:activation-fn node) (reduce + incoming-activations)))))) | |
(defn update-nodes [net] | |
(doseq [node (concat (:hidden-nodes net) (:output-nodes net))] | |
(alter node update-node))) | |
(defn update-connections [net] | |
(doseq [node (concat (:hidden-nodes net) (:output-nodes net))] | |
(alter node #(assoc % :connections | |
(for [conn (:connections %)] | |
(assoc conn :cached-activation (:activation @(:incoming-node conn)))))))) | |
(defn update [net] | |
(println "updating") | |
(doto net | |
update-nodes | |
update-connections)) | |
(defn read-outputs [net] | |
(map #(:activation @%) (:output-nodes net))) | |
(defn set-inputs [net inputs] | |
) | |
(defn update-until-settled | |
([net n] | |
(update-until-settled net n n nil)) | |
([net n settled-count previous-outputs] | |
(if (= settled-count 0) | |
net | |
(let [current-outputs (read-outputs (update net))] | |
(if (= current-outputs previous-outputs) | |
(recur net n (dec settled-count) current-outputs) | |
(recur net n n current-outputs)))))) | |
(defn add-connection [from-node to-node] | |
(alter to-node #(update-in % [:connections] conj (struct connection (random-weight) from-node (:activation @from-node))))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment