Skip to content

Instantly share code, notes, and snippets.

@gamma235
Created August 10, 2015 00:40
Show Gist options
  • Save gamma235/b8db845a512c60d123af to your computer and use it in GitHub Desktop.
Save gamma235/b8db845a512c60d123af to your computer and use it in GitHub Desktop.
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))))]
(rand)))])))))
(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