Nx Deep Learning
Mix.install([
{:nx, "~> 0.5"}
])
Main
# Implementing a Neural Network
defmodule NeuralNetwork do
import Nx.Defn
defn dense(input, weight, bias) do
input
|> Nx.dot(weight)
|> Nx.ad(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
# Generate Intermediate Layers
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)
# Generate Predictions
# Nx.Random.uniform_split(new_key, shape: {}) # ???
# |> NeuralNetwork.predict(w1, b1, w2, b2)