├── .gitignore ├── LICENSE ├── README.md ├── project.clj └── src └── clojure_tensorflow_interop ├── core.clj ├── helpers.clj └── utils.clj /.gitignore: -------------------------------------------------------------------------------- 1 | /target 2 | /classes 3 | /checkouts 4 | pom.xml 5 | pom.xml.asc 6 | *.jar 7 | *.class 8 | /.lein-* 9 | /.nrepl-port 10 | .hgignore 11 | .hg/ 12 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | THE ACCOMPANYING PROGRAM IS PROVIDED UNDER THE TERMS OF THIS ECLIPSE PUBLIC 2 | LICENSE ("AGREEMENT"). ANY USE, REPRODUCTION OR DISTRIBUTION OF THE PROGRAM 3 | CONSTITUTES RECIPIENT'S ACCEPTANCE OF THIS AGREEMENT. 4 | 5 | 1. DEFINITIONS 6 | 7 | "Contribution" means: 8 | 9 | a) in the case of the initial Contributor, the initial code and 10 | documentation distributed under this Agreement, and 11 | 12 | b) in the case of each subsequent Contributor: 13 | 14 | i) changes to the Program, and 15 | 16 | ii) additions to the Program; 17 | 18 | where such changes and/or additions to the Program originate from and are 19 | distributed by that particular Contributor. A Contribution 'originates' from 20 | a Contributor if it was added to the Program by such Contributor itself or 21 | anyone acting on such Contributor's behalf. Contributions do not include 22 | additions to the Program which: (i) are separate modules of software 23 | distributed in conjunction with the Program under their own license 24 | agreement, and (ii) are not derivative works of the Program. 25 | 26 | "Contributor" means any person or entity that distributes the Program. 27 | 28 | "Licensed Patents" mean patent claims licensable by a Contributor which are 29 | necessarily infringed by the use or sale of its Contribution alone or when 30 | combined with the Program. 31 | 32 | "Program" means the Contributions distributed in accordance with this 33 | Agreement. 34 | 35 | "Recipient" means anyone who receives the Program under this Agreement, 36 | including all Contributors. 37 | 38 | 2. GRANT OF RIGHTS 39 | 40 | a) Subject to the terms of this Agreement, each Contributor hereby grants 41 | Recipient a non-exclusive, worldwide, royalty-free copyright license to 42 | reproduce, prepare derivative works of, publicly display, publicly perform, 43 | distribute and sublicense the Contribution of such Contributor, if any, and 44 | such derivative works, in source code and object code form. 45 | 46 | b) Subject to the terms of this Agreement, each Contributor hereby grants 47 | Recipient a non-exclusive, worldwide, royalty-free patent license under 48 | Licensed Patents to make, use, sell, offer to sell, import and otherwise 49 | transfer the Contribution of such Contributor, if any, in source code and 50 | object code form. This patent license shall apply to the combination of the 51 | Contribution and the Program if, at the time the Contribution is added by the 52 | Contributor, such addition of the Contribution causes such combination to be 53 | covered by the Licensed Patents. The patent license shall not apply to any 54 | other combinations which include the Contribution. No hardware per se is 55 | licensed hereunder. 56 | 57 | c) Recipient understands that although each Contributor grants the licenses 58 | to its Contributions set forth herein, no assurances are provided by any 59 | Contributor that the Program does not infringe the patent or other 60 | intellectual property rights of any other entity. Each Contributor disclaims 61 | any liability to Recipient for claims brought by any other entity based on 62 | infringement of intellectual property rights or otherwise. As a condition to 63 | exercising the rights and licenses granted hereunder, each Recipient hereby 64 | assumes sole responsibility to secure any other intellectual property rights 65 | needed, if any. For example, if a third party patent license is required to 66 | allow Recipient to distribute the Program, it is Recipient's responsibility 67 | to acquire that license before distributing the Program. 68 | 69 | d) Each Contributor represents that to its knowledge it has sufficient 70 | copyright rights in its Contribution, if any, to grant the copyright license 71 | set forth in this Agreement. 72 | 73 | 3. REQUIREMENTS 74 | 75 | A Contributor may choose to distribute the Program in object code form under 76 | its own license agreement, provided that: 77 | 78 | a) it complies with the terms and conditions of this Agreement; and 79 | 80 | b) its license agreement: 81 | 82 | i) effectively disclaims on behalf of all Contributors all warranties and 83 | conditions, express and implied, including warranties or conditions of title 84 | and non-infringement, and implied warranties or conditions of merchantability 85 | and fitness for a particular purpose; 86 | 87 | ii) effectively excludes on behalf of all Contributors all liability for 88 | damages, including direct, indirect, special, incidental and consequential 89 | damages, such as lost profits; 90 | 91 | iii) states that any provisions which differ from this Agreement are offered 92 | by that Contributor alone and not by any other party; and 93 | 94 | iv) states that source code for the Program is available from such 95 | Contributor, and informs licensees how to obtain it in a reasonable manner on 96 | or through a medium customarily used for software exchange. 97 | 98 | When the Program is made available in source code form: 99 | 100 | a) it must be made available under this Agreement; and 101 | 102 | b) a copy of this Agreement must be included with each copy of the Program. 103 | 104 | Contributors may not remove or alter any copyright notices contained within 105 | the Program. 106 | 107 | Each Contributor must identify itself as the originator of its Contribution, 108 | if any, in a manner that reasonably allows subsequent Recipients to identify 109 | the originator of the Contribution. 110 | 111 | 4. COMMERCIAL DISTRIBUTION 112 | 113 | Commercial distributors of software may accept certain responsibilities with 114 | respect to end users, business partners and the like. While this license is 115 | intended to facilitate the commercial use of the Program, the Contributor who 116 | includes the Program in a commercial product offering should do so in a 117 | manner which does not create potential liability for other Contributors. 118 | Therefore, if a Contributor includes the Program in a commercial product 119 | offering, such Contributor ("Commercial Contributor") hereby agrees to defend 120 | and indemnify every other Contributor ("Indemnified Contributor") against any 121 | losses, damages and costs (collectively "Losses") arising from claims, 122 | lawsuits and other legal actions brought by a third party against the 123 | Indemnified Contributor to the extent caused by the acts or omissions of such 124 | Commercial Contributor in connection with its distribution of the Program in 125 | a commercial product offering. The obligations in this section do not apply 126 | to any claims or Losses relating to any actual or alleged intellectual 127 | property infringement. In order to qualify, an Indemnified Contributor must: 128 | a) promptly notify the Commercial Contributor in writing of such claim, and 129 | b) allow the Commercial Contributor tocontrol, and cooperate with the 130 | Commercial Contributor in, the defense and any related settlement 131 | negotiations. The Indemnified Contributor may participate in any such claim 132 | at its own expense. 133 | 134 | For example, a Contributor might include the Program in a commercial product 135 | offering, Product X. That Contributor is then a Commercial Contributor. If 136 | that Commercial Contributor then makes performance claims, or offers 137 | warranties related to Product X, those performance claims and warranties are 138 | such Commercial Contributor's responsibility alone. Under this section, the 139 | Commercial Contributor would have to defend claims against the other 140 | Contributors related to those performance claims and warranties, and if a 141 | court requires any other Contributor to pay any damages as a result, the 142 | Commercial Contributor must pay those damages. 143 | 144 | 5. NO WARRANTY 145 | 146 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, THE PROGRAM IS PROVIDED ON 147 | AN "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, EITHER 148 | EXPRESS OR IMPLIED INCLUDING, WITHOUT LIMITATION, ANY WARRANTIES OR 149 | CONDITIONS OF TITLE, NON-INFRINGEMENT, MERCHANTABILITY OR FITNESS FOR A 150 | PARTICULAR PURPOSE. Each Recipient is solely responsible for determining the 151 | appropriateness of using and distributing the Program and assumes all risks 152 | associated with its exercise of rights under this Agreement , including but 153 | not limited to the risks and costs of program errors, compliance with 154 | applicable laws, damage to or loss of data, programs or equipment, and 155 | unavailability or interruption of operations. 156 | 157 | 6. DISCLAIMER OF LIABILITY 158 | 159 | EXCEPT AS EXPRESSLY SET FORTH IN THIS AGREEMENT, NEITHER RECIPIENT NOR ANY 160 | CONTRIBUTORS SHALL HAVE ANY LIABILITY FOR ANY DIRECT, INDIRECT, INCIDENTAL, 161 | SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING WITHOUT LIMITATION 162 | LOST PROFITS), HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 163 | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 164 | ARISING IN ANY WAY OUT OF THE USE OR DISTRIBUTION OF THE PROGRAM OR THE 165 | EXERCISE OF ANY RIGHTS GRANTED HEREUNDER, EVEN IF ADVISED OF THE POSSIBILITY 166 | OF SUCH DAMAGES. 167 | 168 | 7. GENERAL 169 | 170 | If any provision of this Agreement is invalid or unenforceable under 171 | applicable law, it shall not affect the validity or enforceability of the 172 | remainder of the terms of this Agreement, and without further action by the 173 | parties hereto, such provision shall be reformed to the minimum extent 174 | necessary to make such provision valid and enforceable. 175 | 176 | If Recipient institutes patent litigation against any entity (including a 177 | cross-claim or counterclaim in a lawsuit) alleging that the Program itself 178 | (excluding combinations of the Program with other software or hardware) 179 | infringes such Recipient's patent(s), then such Recipient's rights granted 180 | under Section 2(b) shall terminate as of the date such litigation is filed. 181 | 182 | All Recipient's rights under this Agreement shall terminate if it fails to 183 | comply with any of the material terms or conditions of this Agreement and 184 | does not cure such failure in a reasonable period of time after becoming 185 | aware of such noncompliance. If all Recipient's rights under this Agreement 186 | terminate, Recipient agrees to cease use and distribution of the Program as 187 | soon as reasonably practicable. However, Recipient's obligations under this 188 | Agreement and any licenses granted by Recipient relating to the Program shall 189 | continue and survive. 190 | 191 | Everyone is permitted to copy and distribute copies of this Agreement, but in 192 | order to avoid inconsistency the Agreement is copyrighted and may only be 193 | modified in the following manner. The Agreement Steward reserves the right to 194 | publish new versions (including revisions) of this Agreement from time to 195 | time. No one other than the Agreement Steward has the right to modify this 196 | Agreement. The Eclipse Foundation is the initial Agreement Steward. The 197 | Eclipse Foundation may assign the responsibility to serve as the Agreement 198 | Steward to a suitable separate entity. Each new version of the Agreement will 199 | be given a distinguishing version number. The Program (including 200 | Contributions) may always be distributed subject to the version of the 201 | Agreement under which it was received. In addition, after a new version of 202 | the Agreement is published, Contributor may elect to distribute the Program 203 | (including its Contributions) under the new version. Except as expressly 204 | stated in Sections 2(a) and 2(b) above, Recipient receives no rights or 205 | licenses to the intellectual property of any Contributor under this 206 | Agreement, whether expressly, by implication, estoppel or otherwise. All 207 | rights in the Program not expressly granted under this Agreement are 208 | reserved. 209 | 210 | This Agreement is governed by the laws of the State of New York and the 211 | intellectual property laws of the United States of America. No party to this 212 | Agreement will bring a legal action under this Agreement more than one year 213 | after the cause of action arose. Each party waives its rights to a jury trial 214 | in any resulting litigation. 215 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # How to run TensorFlow in Clojure 2 | 3 | TensorFlow now has a (very alpha) Java api which means clojure gets one for free. For now, Java's api is very sparse but don’t let that stop you getting your hands dirty, it already provides everything we need to work with TensorFlow in Clojure. With just java interop and a couple of helper functions we can start writing great idiomatic Clojure. 4 | 5 | To get started, read [Running TensorFlow in Clojure](http://kieranbrowne.com/research/clojure-tensorflow-interop) which explains the code and the concepts. 6 | 7 | You can also read more about TensorFlow's java api [here](https://www.tensorflow.org/versions/master/install/install_java). 8 | 9 | ## Method 1: Add the maven dependency 10 | 11 | ### Just add the dependency to `project.clj` 12 | [org.tensorflow/tensorflow "1.1.0-rc1"] 13 | 14 | > Note: This dependency requires java 8. If that isn't your version by default, you can force lein to use it by adding the `:java-cmd "/path/to/java"` key to your `project.clj`. 15 | 16 | ## Method 2: Build from source 17 | 18 | TensorFlow's java api is in active development with changes and improvments added every other day. By building from source, you have access to the latest changes to TensorFlow as they are added. This method of course takes much longer. 19 | 20 | ### 1. Install cli tools 21 | 1. [Install Bazel](https://www.bazel.build/versions/master/docs/install.html) 22 | 2. [Install Maven](https://maven.apache.org/install.html) 23 | 3. [Install Swig](http://www.swig.org/Doc3.0/Preface.html) 24 | 25 | If you use homebrew, just `brew install maven bazel swig`. I also had to `brew upgrade bazel` due to compatibility issues. 26 | 27 | ### 2. Clone TensorFlow 28 | 29 | ```sh 30 | $ git clone git@github.com:tensorflow/tensorflow.git 31 | $ cd tensorflow 32 | ``` 33 | 34 | ### 3. Configure the build 35 | 36 | ```sh 37 | $ ./configure 38 | ``` 39 | You will be prompted with various questions about your build. If you have a CUDA graphics card definitely say yes to the gpu options. 40 | 41 | ### 4. Build TensorFlow 42 | 43 | It's probably a good idea to put the kettle on for this one. It took about 20 minutes on my MacBook pro. 44 | 45 | ```sh 46 | $ bazel build -c opt //tensorflow/java:pom 47 | ``` 48 | 49 | ### 5. Add to your local maven repository 50 | ```sh 51 | $ mvn install:install-file \ 52 | -Dfile=bazel-bin/tensorflow/java/libtensorflow.jar \ 53 | -DpomFile=bazel-bin/tensorflow/java/pom.xml 54 | ``` 55 | -------------------------------------------------------------------------------- /project.clj: -------------------------------------------------------------------------------- 1 | (defproject clojure-tensorflow-interop "0.1.0-SNAPSHOT" 2 | :description "How to run TensorFlow native library in Clojure" 3 | :url "http://kieranbrowne.com/clojure-tensorflow-interop" 4 | :license {:name "Eclipse Public License" 5 | :url "http://www.eclipse.org/legal/epl-v10.html"} 6 | :dependencies [[org.clojure/clojure "1.8.0"] 7 | ; [org.tensorflow/libtensorflow "1.0.1" 8 | ; :native-prefix ""] 9 | [org.tensorflow/tensorflow "1.1.0-rc1"] 10 | [org.clojure/data.csv "0.1.3"]] 11 | :native-path "/users/kieran/Downloads/jni" 12 | ) 13 | -------------------------------------------------------------------------------- /src/clojure_tensorflow_interop/core.clj: -------------------------------------------------------------------------------- 1 | (ns clojure-tensorflow-interop.core 2 | (:import [org.tensorflow 3 | TensorFlow 4 | Tensor 5 | Session 6 | Output 7 | Operation 8 | OperationBuilder 9 | Graph 10 | DataType]) 11 | (:require 12 | [clojure-tensorflow-interop.helpers :as tf] 13 | [clojure-tensorflow-interop.utils :as utils 14 | :refer [tensor->clj clj->tensor]])) 15 | 16 | ;; We can test our installation by running the version method 17 | ;; on the TensorFlow class. 18 | (. TensorFlow version) 19 | ;; => "1.x.x-rc2" 20 | 21 | ;; Before we get started with the actual code, there are a few concepts 22 | ;; I need to explain otherwise none of this is going to make sense. 23 | 24 | ;; The main object of computation in TensorFlow is the *tensor*. 25 | ;; A Tensor is just a typed multi-dimensional array. Nothing scary here. 26 | 27 | ;; When we write code for TensorFlow, we're not actually running 28 | ;; computations. Instead we're composing a data structure which 29 | ;; describes the flow of data. In TensorFlow this is called a *graph*. 30 | ;; The graph will describe the flow of our data through a series of 31 | ;; operations (*ops* for short). Nothing will actually be computed 32 | ;; until we launch our graph in a *session*. The session handles the 33 | ;; execution of our graph on the CPU or GPU and returns the resulting 34 | ;; tensors. 35 | 36 | ;; In short, our clojure code will assemble a graph and fire off commands 37 | ;; to the C code using a Session object. 38 | ;; Let's get started. 39 | 40 | ;; For demonstration purposes I'm going to do the first example without 41 | ;; abstracting my code. It ain't pretty, but it should make the process 42 | ;; clearer. 43 | 44 | ;; First we need to initialise a new Graph object 45 | (def graph (new Graph)) 46 | 47 | ;; Next we're going to need some example tensors to work with. 48 | ;; Because the computation isn't running in clojure we can't just define 49 | ;; our values. Instead we're defining an operation node in the graph 50 | ;; that generates a constant. 51 | ;; First I'm creating a tensor object using class' the create method. 52 | ;; Because we're interopping with the java class we first need to turn 53 | ;; our clojure persistant vector into an array of 32bit Integers. 54 | ;; Using the arrow macro for clarity; we call the .opBuilder method on 55 | ;; our graph, the first argument is the binary operation which will run 56 | ;; in this case, its "Const". This is one of a set of possible binary ops 57 | ;; that have been implemented in native code. 58 | ;; The second argument is a name for the operation. I went with 59 | ;; "tensor-1" for simplicity, but "Joaquin Phoenix" would 60 | ;; have also worked. The only requirement is that it is unique to 61 | ;; the graph. Next we set the value and datatype attributes 62 | ;; that are required for the Const operation. Finally we build our 63 | ;; operation based on the attributes and use the output method to return 64 | ;; it. It is this returned operation that gets saved in clojure. 65 | 66 | (def tensor-1 67 | (let [tensor 68 | (Tensor/create 69 | (int-array 70 | [360 909 216 108 777 132 256 174 999 228 324 800 264] 71 | ))] 72 | (-> graph 73 | (.opBuilder "Const" "tensor-1") 74 | (.setAttr "dtype" (.dataType tensor)) 75 | (.setAttr "value" tensor) 76 | .build 77 | (.output 0)))) 78 | 79 | (def tensor-2 80 | (let [tensor 81 | (Tensor/create 82 | (int-array [5 9 2 1 7 3 8 2 9 2 3 8 8]))] 83 | (-> graph 84 | (.opBuilder "Const" "tensor-2") 85 | (.setAttr "dtype" (.dataType tensor)) 86 | (.setAttr "value" tensor) 87 | .build 88 | (.output 0)))) 89 | 90 | ;; Now lets add a more exciting operation to our graph. 91 | ;; Again we will call the .opBuilder method on our graph object. 92 | ;; I'm going to use the "Div" (division) operation this time. 93 | ;; Next we call the .addInput method to add our two example tensors 94 | ;; as input to the operation. 95 | ;; Again we build and output our operation object, saving it as "divide". 96 | (def divide 97 | (-> 98 | (.opBuilder graph "Div" "my-dividing-operation") 99 | (.addInput tensor-1) 100 | (.addInput tensor-2) 101 | .build 102 | (.output 0) 103 | )) 104 | 105 | 106 | ;; To run our newly built operations, we need to create a session object 107 | ;; based on our graph. 108 | (def session (new Session graph)) 109 | 110 | 111 | ;; We'll call the .runner method on our session to get the engine running. 112 | ;; We use the .fetch method to retrieve the divide operation by name; 113 | ;; in this case we want to pass it the name we gave to the divide 114 | ;; operation just before ("my-dividing-operation"). The .get method 115 | ;; gets our result from the returned array, this gives us a Tensor object 116 | ;; which has all the data but cannot be read easily, so finally to 117 | ;; read our results, we call the .copyTo method on the Tensor to 118 | ;; copy the contents to an integer array. 119 | (def result 120 | (-> session 121 | .runner 122 | (.fetch "my-dividing-operation") 123 | .run 124 | (.get 0) 125 | (.copyTo (int-array 13)) 126 | )) 127 | 128 | ;; Finally we can read our results. 129 | (apply str (map char result)) 130 | ;; => "Hello, World!" 131 | 132 | ;; So we successfully ran a basic TensorFlow graph, but that code made my 133 | ;; eyes bleed. This is partially because the TensorFlow Java api is so 134 | ;; new and doesn't have the multitudes of helper functions that python 135 | ;; has yet. 136 | 137 | ;; TensorFlow's Java API is still extremely barebones and isn't covered by 138 | ;; the TensorFlow API stability guarantees. That's likely why we don't yet 139 | ;; have a clojure TensorFlow api, although there's at least one in the 140 | ;; works. 141 | 142 | ;; We have all that we need from the Java api already. We can already 143 | ;; work with all binary operations, run sessions and even load existing 144 | ;; and/or pre-trained models from TensorFlow in any other language. 145 | 146 | ;; There's a real opportunity here to make a properly clojurian api. 147 | ;; There are a couple of things that I think make TensorFlow and Clojure 148 | ;; a great match. 149 | ;; TensorFlow's graph concept maps well to data structure 150 | ;; structure, and no programming language has a better story for working 151 | ;; with data structures than clojure. 152 | 153 | ;; Right, lets actually do some machine learning 154 | ;; For simplicity's sake, I'm going to write a very shallow neural network. 155 | ;; From here on, I'm going to start using a very light layer on interop 156 | ;; that I defined in `helpers.clj`. 157 | 158 | ;; First, we'll need some training data. 159 | (def training-data 160 | ;; input => output 161 | [ [0. 0. 1.] [0.] 162 | [0. 1. 1.] [1.] 163 | [1. 1. 1.] [1.] 164 | [1. 0. 1.] [0.] ]) 165 | 166 | ;; We can split out training data into inputs and outputs like so. 167 | ;; Note the use of tf/constant. This simply wraps the operationBuilder 168 | ;; and takes care of adding the Const operation to the default graph. 169 | (def inputs (tf/constant (take-nth 2 training-data))) 170 | (def outputs (tf/constant (take-nth 2 (rest training-data)))) 171 | 172 | ;; We want to initialise our weights as a random value between -1 and 1. 173 | ;; Here we use tf/variable which creates a variable node on the graph. 174 | (def weights 175 | (tf/variable 176 | (repeatedly 3 (fn [] (repeatedly 1 #(dec (rand 2))))))) 177 | 178 | ;; Even though we're defining nodes for the tf graph, we can still define 179 | ;; our code as functions. This is particularly nice because we can still 180 | ;; use a higher order functions like comp and partial in our code. 181 | (defn network [x] 182 | (tf/sigmoid (tf/matmul x weights))) 183 | 184 | ;; For our network to learn we need to measure the difference between 185 | ;; the training outputs and our network's outputs. 186 | (defn error [network-output] 187 | (tf/div (tf/pow (tf/sub outputs network-output) (tf/constant 2.)) (tf/constant 2.0))) 188 | 189 | ;; For back propagation, we need the derivative of our error and sigmoid 190 | ;; functions. 191 | (defn error' [network-output] 192 | (tf/sub network-output outputs)) 193 | 194 | (defn sigmoid' [x] 195 | (tf/mult x (tf/sub (tf/constant 1.) x))) 196 | 197 | (defn deltas [network-output] 198 | (tf/matmul 199 | (tf/transpose inputs) 200 | (tf/mult 201 | (error' (network inputs)) 202 | (sigmoid' (network inputs))))) 203 | 204 | (def train-network 205 | (tf/assign weights (tf/sub weights (deltas (network inputs))))) 206 | 207 | ;; So far we seem to have used a whole bunch of functions to build our 208 | ;; operations. But really we've only been using one. 209 | ;; The function `op-builder` which is defined in `helpers.clj` simply 210 | ;; wraps up a bit of object-oriented code from the java api which adds 211 | ;; operations to the graph. All the other operations we have used, just 212 | ;; pass arguments to `op-builder`. This is why we can safely wrap so much 213 | ;; functionality without worrying that the Java api will change on us. 214 | 215 | ;; The other thing that our `helpers.clj` file defines is a couple of 216 | ;; functions to make running operations a bit easier. 217 | 218 | ;; Running TensorFlow code on the CPU or GPU 219 | 220 | ;; Pattern 1: op-run 221 | 222 | ;; For running a single operation (though this can be nested) we have the 223 | ;; op-run function. 224 | ;; This will return a Tensor object. 225 | (tf/op-run (tf/sub (tf/constant [1.]) (tf/constant [3.]))) 226 | ;; => #object[org.tensorflow.Tensor 0x4cab913d "FLOAT tensor with shape [1]"] 227 | ;; To read this value back into clojure values we can use tensor->clj 228 | (tensor->clj 229 | (tf/op-run (tf/sub (tf/constant [1.]) (tf/constant [3.])))) 230 | ;; => [-2.0] 231 | 232 | ;; Pattern 2: session-run 233 | 234 | ;; Converting data from TensorFlow back into clojure is important for our 235 | ;; results, but its a bottleneck in our model, where as much as possible 236 | ;; we should try to keep computation running on the CPU or GPU. 237 | ;; For this we have the tf/session-run function which takes a list of 238 | ;; operations, which can change the state of the model (such as when 239 | ;; we are training) and the takes care of converting the final result back 240 | ;; to clojure values so we can read it in the repl. 241 | (tf/session-run 242 | [(tf/global-variables-initializer) 243 | (network inputs)]) 244 | ;; This returns the results of the network before training. 245 | ;; Note also the use of tf/global-variables-initializer; this is needed 246 | ;; when we are using one or more variables in our graph. There are other 247 | ;; ways of approaching the variable initialisation problem for TensorFlow 248 | ;; graphs, but for now I've just gone with the standard solution from 249 | ;; the main TF api. Note that despite the "global" in the function name 250 | ;; this is more of a naming convention. The variable initialisation is 251 | ;; scoped to the tf/session run function and won't affect other sessions. 252 | 253 | 254 | ;; Pattern 3: global session object 255 | 256 | ;; Patterns 1 and 2 are great for testing small parts of your graph or 257 | ;; a couple of operations here and there. But when we train our network 258 | ;; we want it's trained weights to be preserved so we can actually use 259 | ;; the trained network to get shit done. 260 | 261 | ;; For this we want to create a session object at the global level. 262 | (def sess (tf/session)) 263 | ;; We also might want to make a partial of the session-run function 264 | ;; to get the best of pattern 2 as well. 265 | (def sess-run (partial tf/session-run tf/default-graph sess)) 266 | 267 | ;; Now we can break up our operations steps into logical breaks 268 | ;; initialise variables and run the untrained network 269 | (sess-run [(tf/global-variables-initializer) 270 | (network inputs)]) 271 | 272 | ;; Run the train-network operation 10000 times and then check the error. 273 | (sess-run 274 | [(repeat 10000 train-network) 275 | (tf/mean (error (network inputs)))]) 276 | 277 | ;; Run the network on a new example 278 | (sess-run [(network (tf/constant [[1. 1. 1.]]))]) 279 | ;; => [[0.99740285]] 280 | 281 | ;; And that's about it. 282 | ;; We've converted our eyesore object-oriented interop code to something 283 | ;; perfectly readable with just a couple of functions. The code base is 284 | ;; tiny enough to allow immediate changes if the Java api changes on us 285 | ;; and the system is flexible enough that we don't need to wait for the 286 | ;; Java api to get fleshed out to jump in and get our hands dirty. 287 | -------------------------------------------------------------------------------- /src/clojure_tensorflow_interop/helpers.clj: -------------------------------------------------------------------------------- 1 | (ns clojure-tensorflow-interop.helpers 2 | (:refer-clojure :exclude [get + * -]) 3 | (:require [clojure-tensorflow-interop.utils 4 | :as utils :refer [tensor->clj clj->tensor]]) 5 | (:import [org.tensorflow 6 | TensorFlow 7 | Tensor 8 | Session 9 | Shape 10 | Output 11 | Operation 12 | OperationBuilder 13 | Graph 14 | DataType] 15 | )) 16 | 17 | 18 | (def default-graph (new Graph)) 19 | ;; We need some stateful code to make this work like classic tf you 20 | ;; don't have to use it in this way though; and there are plenty of 21 | ;; benefits to writing in a more functional style. 22 | (def global-variables (atom [])) 23 | (defn global-variables-initializer [] 24 | @global-variables) 25 | 26 | (defn session 27 | "Create a session" 28 | ([graph] (new Session graph)) 29 | ([] (session default-graph))) 30 | 31 | (defn op-builder 32 | "Returns a function which creates an operation for the graph" 33 | ([op-profile] (op-builder op-profile default-graph)) 34 | ([op-profile graph] 35 | (let [{:keys [operation node-name attrs inputs] 36 | :or {node-name (str (gensym operation)) attrs {} inputs []} 37 | } op-profile] 38 | ((fn [graph] 39 | (utils/thread graph 40 | (flatten 41 | [#(.opBuilder % operation node-name) 42 | ;; set attributes if any 43 | (map 44 | (fn [attr] 45 | #(.setAttr % (name (first attr)) (second attr))) 46 | attrs) 47 | ;; add inputs if any 48 | (map (fn [input] 49 | #(.addInput % 50 | (if (fn? input) (input graph) input) 51 | )) inputs) 52 | #(.build %) 53 | #(.output % 0)]))) graph)))) 54 | 55 | 56 | 57 | (defn constant [val] 58 | (let [tensor (clj->tensor val)] 59 | (op-builder 60 | {:operation "Const" 61 | :attrs {:dtype (.dataType tensor) 62 | :value tensor 63 | }}))) 64 | 65 | (defn assign [var val] 66 | (op-builder 67 | {:operation "Assign" 68 | :inputs [var (if (utils/tf-obj? val) val (constant val))] 69 | })) 70 | 71 | (defn variable 72 | ([val] (variable val {})) 73 | ([val bits] 74 | (let [tensor (clj->tensor val) 75 | var (op-builder 76 | (merge 77 | {:operation "Variable" 78 | :attrs {:shape (utils/tensor->shape tensor) 79 | :dtype (.dataType tensor)} 80 | } bits))] 81 | (swap! global-variables conj (assign var val)) 82 | var))) 83 | 84 | (defn placeholder [datatype] 85 | (op-builder 86 | {:operation "Placeholder" 87 | :attrs {:dtype datatype} 88 | })) 89 | 90 | (defn get [val] 91 | #(let [tensor (clj->tensor val)] 92 | ((op-builder 93 | {:operation "get" 94 | :input [val] 95 | }) %))) 96 | 97 | 98 | (defn mult [a b] 99 | (op-builder 100 | {:operation "Mul" 101 | :inputs [a b]})) 102 | 103 | (defn div [a b] 104 | (op-builder 105 | {:operation "Div" 106 | :inputs [a b]})) 107 | 108 | (defn add [a b] 109 | (op-builder 110 | {:operation "Add" 111 | :inputs [a b]})) 112 | 113 | (defn sub [a b] 114 | (op-builder 115 | {:operation "Sub" 116 | :inputs [a b]})) 117 | 118 | (defn sum 119 | ([t] (sum t (constant 0))) 120 | ([t dims] 121 | (op-builder 122 | {:operation "Sum" 123 | :inputs [t dims]}))) 124 | 125 | (defn tanh [a] 126 | (op-builder 127 | {:operation "Tanh" 128 | :inputs [a]})) 129 | 130 | (defn sigmoid [a] 131 | (op-builder 132 | {:operation "Sigmoid" 133 | :inputs [a]})) 134 | 135 | (defn pow [a b] 136 | (op-builder 137 | {:operation "Pow" 138 | :inputs [a b]})) 139 | 140 | (defn size [a] 141 | (op-builder 142 | {:operation "Size" 143 | :inputs [a]})) 144 | 145 | (defn abs [a] 146 | (op-builder 147 | {:operation "Abs" 148 | :inputs [a]})) 149 | 150 | (defn mean [a] 151 | (op-builder 152 | {:operation "Mean" 153 | :inputs [a (constant 0)]})) 154 | 155 | (defn transpose [a] 156 | (op-builder 157 | {:operation "Transpose" 158 | :inputs [a (constant [1 0])]})) 159 | 160 | (defn matmul [a b] 161 | (op-builder 162 | {:operation "MatMul" 163 | :inputs [a b]})) 164 | ;; alias 165 | (def dot matmul) 166 | 167 | (defn n-args 168 | "This function takes a two argument operation like mult and add and 169 | returns a version which can take 2 -> infinity arguments like normal 170 | clojure functions. 171 | TODO: Still causes stackoverflow for many args" 172 | [func] 173 | (fn [& args] (reduce func args))) 174 | 175 | (def * (n-args mult)) 176 | (def + (n-args add)) 177 | (def - (n-args sub)) 178 | 179 | (defn feed 180 | "Feed value to placeholder 181 | Pass a map of locations to values" 182 | ([runner feed-map] 183 | (utils/thread 184 | runner 185 | (map (fn [[key val]] 186 | #(.feed % key val)) feed-map)))) 187 | 188 | (defn run 189 | [runner op] 190 | (.run (.fetch runner op))) 191 | 192 | (defn op-run 193 | "Call session runner on single op. 194 | Returns tensor object" 195 | ([op] (op-run default-graph op)) 196 | ([graph op] (op-run graph (Session. graph) op {})) 197 | ([graph session op] (op-run graph session op {})) 198 | ([graph session op feed-map] 199 | (-> session 200 | .runner 201 | (feed feed-map) 202 | (.fetch (.name (.op (if (fn? op) (op graph) op)))) 203 | .run 204 | (.get 0) 205 | ))) 206 | 207 | (defn session-run 208 | "Run list of ops, return last" 209 | ([ops] (session-run default-graph ops)) 210 | ([graph ops] (session-run graph (Session. graph) ops)) 211 | ([graph session ops] 212 | (let [ops (flatten ops) 213 | op-run (partial op-run graph session)] 214 | 215 | ;; initialise global variables 216 | (map op-run @global-variables) 217 | 218 | ;; run first n ops to set up state 219 | (doseq [op (butlast ops)] 220 | (op-run op)) 221 | 222 | ;; run final op and return value 223 | (tensor->clj 224 | (op-run (last ops)))))) 225 | 226 | (defn with-session [& ops] 227 | (session-run ops)) 228 | -------------------------------------------------------------------------------- /src/clojure_tensorflow_interop/utils.clj: -------------------------------------------------------------------------------- 1 | (ns clojure-tensorflow-interop.utils 2 | (:import [org.tensorflow 3 | Tensor 4 | Shape 5 | DataType 6 | ])) 7 | 8 | (defn recursively 9 | "Apply function to all items in nested data structure if 10 | condition function is met." 11 | [apply-if-fn func data] 12 | (if (apply-if-fn data) 13 | (func (map (partial recursively apply-if-fn func) data)) 14 | data)) 15 | 16 | (defn make-coll 17 | "Make a collection of x,y,z... dimensions" 18 | [fill & dims] 19 | (case (count dims) 20 | 0 fill 21 | 1 (repeat (first dims) fill) 22 | (repeat (first dims) (apply (partial make-coll fill) (rest dims))) 23 | )) 24 | 25 | (def array? 26 | "Works like coll? but returns true if argument is array" 27 | #(= \[ (first (.getName (.getClass %))))) 28 | 29 | (def tensor->shape 30 | #(let [arr (.shape %)] 31 | (if (> (count arr) 0) 32 | (Shape/make 33 | (aget arr 0) 34 | (java.util.Arrays/copyOfRange arr 1 (count arr))) 35 | (Shape/scalar)))) 36 | 37 | (defn tf-vals [v] 38 | "Convert value into type acceptable to TensorFlow 39 | Persistent data structures become arrays 40 | Longs become 32bit integers 41 | Doubles become floats" 42 | (cond 43 | (coll? v) 44 | (if (coll? (first v)) 45 | (to-array (map tf-vals v)) 46 | (case (.getName (type (first v))) 47 | "java.lang.Long" (int-array v) 48 | "java.lang.Double" (float-array v))) 49 | (= (.getName (type v)) "java.lang.Long") (int v) 50 | (= (.getName (type v)) "java.lang.Double") (float v) 51 | ;; anything else 52 | true v)) 53 | 54 | 55 | (defn output-shape [tensor] 56 | (let [shape (tensor->shape tensor) 57 | dims (map #(.size shape %) 58 | (range (.numDimensions shape))) 59 | d (case (.name (.dataType tensor)) 60 | "INT32" 0 61 | "FLOAT" 0.0)] 62 | (tf-vals 63 | (apply (partial make-coll d) dims)))) 64 | 65 | (defn get-tensor-val [tensor] 66 | (let [copy-to (output-shape tensor)] 67 | (cond 68 | (array? copy-to) (.copyTo tensor copy-to) 69 | (float? copy-to) (.floatValue tensor) 70 | ((complement float?) copy-to) (.intValue tensor)))) 71 | 72 | (def tensor->clj (comp (partial recursively array? vec) get-tensor-val)) 73 | 74 | (def clj->tensor #(Tensor/create (tf-vals %))) 75 | 76 | (defn tf-obj? [x] 77 | (if (re-find #"org.tensorflow" (.getName (class x))) 78 | true false)) 79 | 80 | 81 | (defn thread 82 | "Approximately equivalent to -> macro. 83 | Required because -> must run at compile time" 84 | [val functions] (reduce #(%2 %1) val functions)) 85 | 86 | --------------------------------------------------------------------------------