• This repository has been archived on 24/Jul/2020
  • Stars
    star
    112
  • Rank 312,240 (Top 7 %)
  • Language
    Clojure
  • License
    Eclipse Public Li...
  • Created over 7 years ago
  • Updated over 4 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An extremely light layer over TensorFlow's Java api

This project is achived, thanks to all who took part

There are now many better supported solutions to doing machine learning via clojure. And a library is still not necessary to use tensorflow from clojure due to its firstclass interop, see my blog post on the topic.

Clojure and TensorFlow

A light layer of wrappers over Java interop for working with TensorFlow.

Clojars Project Build Status

Getting started

Example Neural Network

(ns example.core
  (:require [clojure-tensorflow.ops :as tf]
            [clojure-tensorflow.layers :as layer]
            [clojure-tensorflow.optimizers :as optimize]
            [clojure-tensorflow.core :refer [run with-graph with-session]]))

;; Training data
(def input (tf/constant [[0. 1.] [0. 0.] [1. 1.] [1. 0.]]))
(def target (tf/constant [[0.] [0.] [1.] [1.]]))

;; Define network / model
(def network
  ;; first layer is just the training input data
  (-> input
      ;; next is a hidden layer of six neurons
      ;; we can set the activation function like so
      (layer/linear 6 :activation tf/sigmoid)
      ;; next is a hidden layer of eight neurons
      ;; tf/sigmoid is used by default when we dont
      ;; specify an activation fn
      (layer/linear 8)
      ;; our last layer needs to be the same size as
      ;; our training target data, so one neuron.
      (layer/linear 1)))


;; Cost function; we're using the squared difference
(def error (tf/square (tf/sub target network)))

;; Initialize global variables
(run (tf/global-variables-initializer))

;; Train Network 1000 epochs
(run (repeat 1000 (optimize/gradient-descent error)))

;; Initialize global variables
(run (tf/mean error))
;; => [9.304908E-5]
;; the error is now incredibly small

Requirements

TensorFlow requires at least Java 8. This will already be the default on most machines, but if it isn't for you, it's possible to force lein to use it by adding the :java-cmd "/path/to/java" key to your project.clj.

License

Copyright Β© 2017 Kieran Browne

Distributed under the Eclipse Public License either version 1.0 or (at your option) any later version.