Powered by AppSignal & Oban Pro
Would you like to see your link here? Contact us

Numerical Elixir (Nx)

talks/2022_10_meetup/1_nx.livemd

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

  1. Nx enables efficient numerical computation in Elixir.

  2. Nx provides a single Elixir API on top of multiple low-level acceleration technologies.

Evolutionary algorithms ->