Using the Elixir Nx library from LFE
About ↟
This project aims to allow using the Elixir Nx machine learning library from LFE.
Build ↟
You will need to have Erlang, Elixir, and rebar3 installed on your system, in your PATH
.
$ mix local.hex --force
$ rebar3 compile
Make sure that you can use Elixir from LFE:
$ rebar3 lfe repl
lfe> (Elixir.IO:puts "Hello, world!")
Hello, world!
ok
Usage ↟
From the Nx README:
lfe> (set tensor (Elixir.Nx:tensor '((1 2) (3 4))))
#M(__struct__ Elixir.Nx.Tensor
data
#M(__struct__ Elixir.Nx.BinaryBackend
state
#B(1 0 0 0 0 0 0 0 2 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 4 0 0 0 ...))
names (nil nil) shape #(2 2) type #(s 64))
lfe> (Elixir.Nx:shape tensor)
#(2 2)
@rvirding is exploring support for two things that would make working with Elixir easier in LFE:
- Elixir struct support, and
- A shortcut for
(Elixir. ...)
Continuing from the Nx README:
lfe> (Elixir.Nx:divide
(Elixir.Nx:exp tensor)
(Elixir.Nx:sum (Elixir.Nx:exp tensor)))
#M(__struct__ Elixir.Nx.Tensor
data
#M(__struct__ Elixir.Nx.BinaryBackend
state
#B(226 79 3 61 185 120 178 61 105 145 114 62 144 215 36 63))
names (nil nil) shape #(2 2) type #(f 32))
For this next part, we'll use operators that override those of LFE for use in Nx. From another project:
(include-lib "lfe-nx/include/nx.lfe")
From inside the LFE nx project:
(include-lib "include/nx.lfe")
(defun softmax (t)
(/ (nx:exp t)
(nx:sum (nx:exp t))))
#M(__struct__ Elixir.Nx.Tensor
data
#M(__struct__ Elixir.Nx.BinaryBackend
state
#B(226 79 3 61 185 120 178 61 105 145 114 62 144 215 36 63))
names (nil nil) shape #(2 2) type #(f 32))
License ↟
Apache License, Version 2.0
Copyright © 2021, Duncan McGreggor [email protected].