Created
February 26, 2018 02:40
-
-
Save souenzzo/34e5797bb5f6676fff81ab512430a761 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 neural.network | |
(:require [clojure.core.matrix :as matrix] | |
[clojure.core.matrix.operators :as op]) | |
(:import (java.util Random))) | |
(def sigmoid matrix/logistic) | |
(defn sigmoid' | |
[x] | |
(op/* x (op/- 1 x))) | |
(defn new-neural-network | |
[n seed] | |
(let [gen (new Random seed)] | |
{:random gen | |
:synaptic-weights (mapv vector (repeatedly n #(.nextDouble gen)))})) | |
(defn think | |
[synaptic-weights inputs] | |
(sigmoid (matrix/dot inputs synaptic-weights))) | |
(defn single-train | |
[inputs outputs {:keys [synaptic-weights] :as this} i] | |
(let [output (think synaptic-weights inputs) | |
error (op/- outputs output) | |
adjust (matrix/dot (matrix/transpose inputs) (op/* error (sigmoid' output)))] | |
(update this :synaptic-weights op/+ adjust))) | |
(defn train | |
[this inputs outputs n] | |
(reduce (partial single-train inputs outputs) this (range n))) | |
(defn run-network | |
[] | |
(let [nn (new-neural-network 3 0) | |
inputs [[0 0 1] ;; 0 | |
[1 1 1] ;; 1 | |
[1 0 1] ;; 1 | |
[0 1 1]] ;; 0 | |
outputs (matrix/transpose [[0 1 1 0]]) | |
n5 (train nn inputs outputs 10) | |
nf (train nn inputs outputs 10000)] | |
[(think (:synaptic-weights nn) [1 0 0]) | |
(think (:synaptic-weights n5) [1 0 0]) | |
(think (:synaptic-weights nf) [1 0 0])])) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment