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

MLIE Chapter 6

books/ai/mlie/chapter6.livemd

MLIE Chapter 6

# 138 
Mix.install([
  {:nx, "~> 0.6"}
])

Section

# 138 
defmodule NeuralNetwork do
  import Nx.Defn

  defn dense(input, weight, bias) do
    input
    |> Nx.dot(weight)
    |> Nx.add(bias)
  end

  defn activation(input) do
    Nx.sigmoid(input)
  end

  defn hidden(input, weight, bias) do
    input
    |> dense(weight, bias)
    |> activation()
  end

  defn output(input, weight, bias) do
    input
    |> dense(weight, bias)
    |> activation()
  end

  defn predict(input, w1, b1, w2, b2) do
    input
    |> hidden(w1, b1)
    |> output(w2, b2)
  end
end
# 140 
key = Nx.Random.key(42)
{w1, new_key} = Nx.Random.uniform(key)
{b1, new_key} = Nx.Random.uniform(new_key)
{w2, new_key} = Nx.Random.uniform(new_key)
{b2, new_key} = Nx.Random.uniform(new_key)
# 140 
# Nx.Random.uniform_split(new_key, shape: {})
# |> NeuralNetwork.predict(w1, b1, w2, b2)