Numerical Elixir (Nx)
Mix.install([
{:nx, "~> 0.3.0"},
{:exla, "~> 0.3.0"},
{:torchx, "~> 0.3.0"}
])
Tensors
t =
Nx.tensor([
[1, 2, 3],
[4, 5, 6]
])
Nx.multiply(t, 2)
Backends
Nx.default_backend(Torchx.Backend)
t =
Nx.tensor([
[1, 2, 3],
[4, 5, 6]
])
Nx.multiply(t, 2)
Numerical definitions
defmodule NxIntro do
import Nx.Defn
defn mean(tensor) do
Nx.sum(tensor) / Nx.size(tensor)
end
end
Nx.Defn.default_options(compiler: EXLA)
t =
Nx.tensor([
[1, 2, 3],
[4, 5, 6]
])
NxIntro.mean(t)
Key points
-
Nx enables efficient numerical computation in Elixir.
-
Nx provides a single Elixir API on top of multiple low-level acceleration technologies.