Code Monkey home page Code Monkey logo

clojure-tensorflow's Introduction

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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.