LoginSignup
19
10

More than 3 years have passed since last update.

Elixirの革新的ライブラリ「Nx」をMacでも動かしてみた

Last updated at Posted at 2021-02-19

Elixirで機械学習/ディープラーニングができるようになるライブラリ「Nx」をMacで動かしてみました。この記事は、@piacerex さんの投稿 Elixirでディープラーニング①:革新的ライブラリ「Nx」の導入 に触発されたものです。

検証環境

  • macOS Big Sur
    • MacBook Pro (13-inch, 2018, Four Thunderbolt 3 Ports)
    • CPU: 2.3 GHz クアッドコア Intel Core i5
    • Memory: 16 GB 2133 MHz LPDDR3
    • Graphics: Intel Iris Plus Graphics 655 1536 MB
  • Elixir 1.11.3
  • Erlang 23.2.5

余談ですが、私はElixir含めバージョン切り替えが必要なものはすべて asdf でインストールしています。Macユーザならhomebrewでasdfをインストールできるので、特に躓くことはないかと思います。

NXのインストール

手順は元記事とまったく同じです。まずはMixプロジェクトを作成します。

$ mix new nx_review
$ cd $_

Nxをdepsに追加します。

mix.ex
defmodule NxReview.MixProject do
  use Mix.Project

  defp deps do
    [
      {:nx, "~> 0.1.0-dev", github: "elixir-nx/nx", sparse: "nx"}
    ]
  end

依存ライブラリをインストールし、コンパイルしてみましょう。

$ mix do deps.get, compile
* Getting nx (https://github.com/elixir-nx/nx.git)
remote: Enumerating objects: 71, done.        
remote: Counting objects: 100% (71/71), done.        
remote: Compressing objects: 100% (60/60), done.        
remote: Total 6158 (delta 24), reused 27 (delta 9), pack-reused 6087        
==> nx
Compiling 17 files (.ex)
Generated nx app
==> nx_review
Compiling 1 file (.ex)
Generated nx_review app

Windowsではコンパイルに失敗するようですが、Macでは問題なくコンパイルできました 🎉

実行

iexで行列と微分を試してみます。

$ iex -S mix
iex> matrix = Nx.tensor( [ [ 0.123, 0.456 ], [ 0.789, 1.234 ] ] )
#Nx.Tensor<
  f64[2][2]
  [
    [0.123, 0.456],
    [0.789, 1.234]
  ]
>
iex> matrix
#Nx.Tensor<
  f64[2][2]
  [
    [0.123, 0.456],
    [0.789, 1.234]
  ]
>
iex> Nx.divide( Nx.exp( matrix ), Nx.sum( Nx.exp( matrix ) ) )
#Nx.Tensor<
  f64[2][2]
  [
    [0.135520130365152, 0.18907054376721744],
    [0.26378125835705013, 0.4116280675105804]
  ]
>

正常に動いて元記事と同様の結果が出力されました。

最後に

Nxを試してみたリポジトリをGitHubに置いているので、よければご覧ください。
https://github.com/mokichi/nx_review

19
10
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
19
10