Skip to content

Instantly share code, notes, and snippets.



Created Aug 10, 2015
What would you like to do?
neural network exploration
(ns machine-learning.ann
(:use clojure.core.matrix)
(:require [clojure.core.matrix.impl.mathsops :as m]
[clojure.core.matrix.operators :as o]))
(defn make-layers [coll]
"Takes a coll like [3 4 2] and uses it to make an architecture of 3 layers with an input layer of 3 nodes, hidden layer of 4, etc. "
(let [make-layer (fn [number-of-nodes] (vec (take number-of-nodes (repeat 0))))
input-layer (make-layer (first coll))
output-layer [(make-layer (last coll))]
hidden-layers (vec (for [n (rest (butlast coll))] (make-layer n)))]
(into (vec (cons input-layer hidden-layers)) output-layer)))
(defn initialize-weights [layers]
(loop [head (first layers), tail (rest layers), weights []]
(if (empty? tail) weights
(recur (first tail)
(rest tail)
(into weights [(vec (for [i (range (* (count head) (count (first tail))))]
(defn make-neural-network [layers weights]
(into (vec (interleave layers weights)) [(last layers)]))
(defn propogate-forward [inputs nn]
(let [weights (vec (for [i nn :when (not (even? (.indexOf nn i)))] i))]
(mapv #(m/tanh %)
(mapv #(reduce + %) (o/* inputs (transpose weights))))))
(defn propogate-back [output expected nn] ;;TODO)
(defn pprint [coll]
(println (reverse (into [] (interpose "\n" coll)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment