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

Explorer <-> Nx

livebooks/explorer/with_tensor.livemd

Explorer <-> Nx

Mix.install([
  {:nx, "~> 0.9"},
  {:explorer, "~> 0.9"},
  {:kino, "~> 0.14"}
])

エイリアス

alias Explorer.DataFrame
alias Explorer.Series
require Explorer.DataFrame

データフレームの作成

sample_df =
  [
    r: [255, 127, 0, 255, 127, 0, 255, 127, 0],
    g: [255, 255, 255, 127, 127, 127, 0, 0, 0],
    b: [255, 255, 0, 0, 127, 127, 255, 127, 0]
  ]
  |> DataFrame.new()

sample_df
|> Kino.DataTable.new()

シリーズとテンソルの変換

Series.to_tensor(sample_df["r"])
[0, 0.5, 1.0]
|> Nx.tensor(type: :f64)
|> Series.from_tensor()

データフレームにテンソルを追加

alpha =
  [255]
  |> Nx.tensor()
  |> Nx.tile([9])
sample_df
|> DataFrame.put(:a, alpha)
|> Kino.DataTable.new()

defn による演算

import Nx.Defn
defmodule Helper do
  defn mean(tf) do
    ((tf[:r] + tf[:g] + tf[:b]) / 3)
    |> Nx.as_type(:f64)
  end
end
sample_df
|> DataFrame.put(:mean, Helper.mean(sample_df))
|> Kino.DataTable.new()