Powered by AppSignal & Oban Pro

Ch 6.1: Deep Learning

Ch6.1-DL simple.livemd

Ch 6.1: Deep Learning

Mix.install([{:nx, "~> 0.5"}])

Simple NN

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

  @doc """
  Same as hidden but separated out here for illustrative clarity
  """
  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

To keep things simple, you’ll only input scalar shapes to your neural network, so your parameters can all be scalars as well:

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)
Nx.Random.uniform_split(new_key, 0 , 1, shape: {})   
|> IO.inspect()
|> NeuralNetwork.predict(w1, b1, w2, b2)