ryoakg
9/24/2017 - 4:02 PM

tensor-flow-data-types.clj

(set-env! :dependencies '[[org.tensorflow/tensorflow "1.3.0"]])

(import (org.tensorflow Graph
                        Session
                        Tensor
                        TensorFlow
                        DataType
                        Output))

(with-open [^Graph   g  (Graph.)
            ^Session s  (Session. g)
            ^Tensor  ta (Tensor/create (int-array [1 2 3]))
            ^Tensor  tb (Tensor/create (int-array [4 5 6]))]
  (let [^Output a     (-> g
                          (.opBuilder "Const" "a")
                          (.setAttr "dtype" DataType/INT32)
                          (.setAttr "value" ta)
                          .build
                          (.output 0))
        ^Output b     (-> g
                          (.opBuilder "Const" "b")
                          (.setAttr "dtype" DataType/INT32)
                          (.setAttr "value" tb)
                          .build
                          (.output 0))
        ^Output c     (-> g
                          (.opBuilder "Mul" "c")
                          (.addInput a)
                          (.addInput b)
                          .build
                          (.output 0))
        ^"[I" out-buf (int-array 3)]
    (-> s
        .runner
        (.fetch "c")
        .run
        (.get 0)
        (.copyTo out-buf))
    (seq out-buf)))
;; => (4 10 18)
;;; from https://www.tensorflow.org/install/install_java
(set-env! :dependencies '[[org.tensorflow/tensorflow "1.3.0"]])

(import (org.tensorflow Graph
                        Session
                        Tensor
                        TensorFlow))

(TensorFlow/version)
;; => "1.3.0"

(let [^Graph  g (Graph.)
      ^String v (str "Hello from " (TensorFlow/version))
      ;; Construct the computation graph with a single operation, a constant
      ;; named "MyConst" with a value "value".
      ^Tensor t (Tensor/create (.getBytes v "UTF-8"))]
  ;; The Java API doesn't yet include convenience functions for adding operations.
  (-> g
      (.opBuilder "Const" "MyConst")
      (.setAttr "dtype" (.dataType t))
      (.setAttr "value" t)
      .build)
  (let [^Session s      (Session. g)
        ^Tensor  output (-> s
                            .runner
                            (.fetch "MyConst")
                            .run
                            (.get 0))]
    (String. (.bytesValue output) "UTF-8")))
(set-env! :dependencies '[[org.tensorflow/tensorflow "1.3.0"]])

(import (org.tensorflow DataType))

(require 'clojure.set
         'clojure.reflect)

(->> (clojure.reflect/reflect DataType)
     :members
     (filter #(clojure.set/superset? (:flags %)
                                     #{:static :final :enum :public}))
     (map :name)
     sort)
;; => (BOOL DOUBLE FLOAT INT32 INT64 STRING UINT8)