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

Chapt 3

chapt3.livemd

Chapt 3

Section

  Mix.install([
      {:axon, "~> 0.6.1"},
      {:nx, "~> 0.7.3"},
      {:exla, "~> 0.7.3"},
      {:bumblebee, "~> 0.5.3"},
      {:explorer, "~> 0.9.1"},
      {:kino, "~> 0.13.2"},
      {:kino_vega_lite, "~> 0.1.13"},
      {:vega_lite, "~> 0.1.9"},
      {:stb_image, "~> 0.6.9"},
      {:benchee, "~> 1.3"}
  ])
Nx.default_backend(EXLA.Backend)
a = Nx.tensor([1, 2, 3])
b = Nx.tensor([4.0, 5.0, 6.0])
c = Nx.tensor([1, 0, 1], type: {:u, 8})

for t <- [a, b, c], do: dbg(t)

goog_current_price = 2677.32
goog_pe = 23.86
goog_mkt_cap = 1760

m_current_price = 133.93
m_pe = 11.10
m_mkt_cap = 360

stonks = Nx.tensor([
  [goog_current_price, goog_pe, goog_mkt_cap],
  [m_current_price, m_pe, m_mkt_cap]
])
invert_color_channels = Nx.tensor([
  [-1, 0, 0, 0],
  [0, -1, 0, 0],
  [0, 0, -1, 0],
  [0, 0, 0, 1]
])

"/Users/toddpickell/code/learn_nx_elixir/ml_nx_book/files/silly_cat.png"
|> StbImage.read_file!()
|> StbImage.resize(256, 256)
|> StbImage.to_nx()
|> Nx.dot(invert_color_channels)
|> Nx.as_type({:u, 8})
|> Kino.Image.new()
vector = Nx.dot(Nx.tensor([1,2,3]), Nx.tensor([1,2,3]))
vector_matrix = Nx.dot(Nx.tensor([1,2]), Nx.tensor([[1],[2]]))
matrix_matrix = Nx.dot(Nx.tensor([[1,2]]), Nx.tensor([[3],[4]]))
  [vector, vector_matrix, matrix_matrix]
|> Enum.each(&amp;IO.inspect/1)
simulation = fn key ->
  {v, k} = Nx.Random.uniform(key)
  if Nx.to_number(v) < 0.5, do: {0, k}, else: {1, k}
end
key = Nx.Random.key(42)

for n <- [10,100,1000,10000] do
  Enum.map_reduce(1..n, key, fn _, key -> simulation.(key) end)
  |> elem(0)
  |> Enum.sum()
  |> IO.inspect(label: "#{n} run")
end
defmodule BerryFarm do
  import Nx.Defn

  defn profits(trees) do
    trees
    |> Nx.subtract(1)
    |> Nx.pow(4)
    |> Nx.negate()
    |> Nx.add(Nx.pow(trees, 3))
    |> Nx.add(Nx.pow(trees, 2))
  end

  defn profits_derivative(trees) do
    grad(trees, &amp;profits/1)
  end
end
trees = Nx.linspace(0, 3, n: 100)
profits = BerryFarm.profits(trees)
profits_derivative = BerryFarm.profits_derivative(trees)

alias VegaLite, as: Vl

Vl.new(title: "Berry Profits", width: 1440, height: 1000)
|> Vl.data_from_values(%{
  trees: Nx.to_flat_list(trees),
  profits: Nx.to_flat_list(profits),
  profits_derivative: Nx.to_flat_list(profits_derivative)
})
|> Vl.layers([
  Vl.new()
  |> Vl.mark(:line, interpolate: :basis)
  |> Vl.encode_field(:x, "trees", type: :quantitative)
  |> Vl.encode_field(:y, "profits", type: :quantitative),

  Vl.new()
  |> Vl.mark(:line, interpolate: :basis)
  |> Vl.encode_field(:x, "trees", type: :quantitative)
  |> Vl.encode_field(:y, "profits_derivative", type: :quantitative)
  |> Vl.encode(:color, value: "#ff0000")
]
)
defmodule GradFun do
  @moduledoc "Calculate the gradient of a simple function"
  import Nx.Defn

  defn my_function(x) do
    x
    |> Nx.cos()
    |> Nx.exp()
    |> Nx.sum()
    |> print_expr()
  end

  defn grad_my_function(x) do
    grad(x, &amp;my_function/1) |> print_expr()
  end
end
GradFun.grad_my_function(Nx.tensor([1.0,2.0,3.0]))