Gigasquid's libpython-clj Examples

Gigasquid put together a sweet example on how to use torch from from Clojure. To run these examples in a Nextjournal notebook, we can use tools.deps ability to depend on a git repository.

{:deps
 {org.clojure/clojure {:mvn/version "1.10.1"}
  cnuernber/libpython-clj {:mvn/version "1.32"}
  gigasquid/libpython-clj-examples
  {:git/url "https://github.com/gigasquid/libpython-clj-examples.git"
   :sha "49a631112274b2b3640dbf1efdbba3eaf5104947"}}}
deps.edn
Clojure

Install the python dependencies

pip3 install torch
pip3 install transformers
pip3 install mxnet
pip3 install opencv-python-headless
52.9s
Clj & Python env (Bash in Clojure)

And we're all set, and can experiment with the examples.

GPT2

(require '[gigasquid.gpt2 :as gpt2])
77.4s
Clojure
Clj & Python env

We can generate some text based on a given starting text

(gpt2/generate-text "Natural language processing tasks are typically approached with" 100)
5.7s
Clojure
Clj & Python env
"Natural language processing tasks are typically approached with, "What is the most important thing you want to do when you're writing a sentence?" The answer is, "I want to write a sentence that is very specific to the subject, and I want to write it in a way that is very specific to the subject." The problem with this approach is that it's not very specific. It's not very specific. It's not very specific. It's not very specific. It's not very specific. It's not very"
(gpt2/generate-text2
 "Rich Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform" 100 0.8)
5.2s
Clojure
Clj & Python env
"Rich Hickey developed Clojure because he wanted a modern Lisp for functional programming, symbiotic with the established Java platform. Jeremiah B. Pearce, who represents the University of British Columbia and the Canadian Institute of Technology in Dartmouth, said, "We've obviously got some big revenues to our credit. We've had a bit of a shortfall of some of the early-stage startups the way the real-world is, and the online is great. We've made some improvements. We've kind of made some investments in the business of integrating language, which is very valuable. There's a lot of people"

MXNet

(require '[gigasquid.mxnet :as mxnet])
(in-ns 'gigasquid.mxnet)
7.1s
Clojure
Clj & Python env
gigasquid.mxnet

Training

(dotimes [i 3]
    (println "========= Epoch " i  " ============")
    (train-epoch model train-dataset acc-metric)
  (get-metric acc-metric))
8.1s
Clojure
Clj & Python env
(test-accuracy model test-dataset acc-metric)
(get-metric acc-metric)
0.6s
Clojure
Clj & Python env
Vector(4) [libpython_clj.python.bridge$generic_python_as_list$reify__26616, "0x3bfd05c9", "('accuracy', 0.9491894736842105)", Map]

Visualization

(py. train-dataset "reset")
(def bd (next-batch train-dataset))
(def data (first (py/get-attr bd "data")))
(def image (mxnet.ndarray/slice data :begin 0 :end 1))
(def image2 (py. image "reshape" [28 28]))
(def image3 (-> (mxnet.ndarray/multiply image2 256)
                (mxnet.ndarray/cast :dtype "uint8")))
(def npimage (py. image3 "asnumpy"))
(require-python '(cv2))
3.2s
Clojure
Clj & Python env
(cv2/imwrite "/results/number.jpg" npimage)
0.0s
Clojure
Clj & Python env
true
Runtimes (2)