t12a

t12a

Looking for help translating the examples in Programming Machine Learning into Elixir

Hi,

I’m reading the book Programming Machine Learning and trying to translate examples in Elixir using Nx.

Here is the code I exported from my Livebook.

Linear Regression

Mix.install([
  {:nx, "~> 0.4.2"},
  {:axon, "~> 0.4.1"},
  {:explorer, "~> 0.5.0"},
  {:kino, "~> 0.8.0"},
  {:vega_lite, "~> 0.1.6"},
  {:kino_vega_lite, "~> 0.1.7"}
])

Data

csv = """
Reservations,Pizzas
13,33
2,16
14,32
23,51
13,27
1,16
18,34
10,17
26,29
3,15
3,15
21,32
7,22
22,37
2,13
27,44
6,16
10,21
18,37
15,30
9,26
26,34
8,23
15,39
10,27
21,37
5,17
6,18
13,25
13,23
"""

{:ok, data} =
  csv
  |> Explorer.DataFrame.load_csv()

reserv = data["Reservations"]
pizzas = data["Pizzas"]

Let’s plot the actual values and our first attempt to model.

weight_text = Kino.Input.text("Weight", default: "1")
alias VegaLite, as: Vl
{weight, _} = Float.parse(Kino.Input.read(weight_text))
model = Explorer.DataFrame.new(iter: 1..Explorer.Series.max(reserv))

chart1 =
  Vl.new()
  |> Vl.data_from_values(data, only: ["Reservations", "Pizzas"])
  |> Vl.mark(:point)
  |> Vl.encode_field(:x, "Reservations", type: :quantitative)
  |> Vl.encode_field(:y, "Pizzas", type: :quantitative)

chart2 =
  Vl.new()
  |> Vl.data_from_values(model, only: ["iter", "weights"])
  |> Vl.transform(calculate: "datum.iter * #{weight}", as: "weights")
  |> Vl.mark(:line)
  |> Vl.encode_field(:x, "iter", type: :quantitative)
  |> Vl.encode_field(:y, "weights", type: :quantitative)

combined =
  Vl.new(width: 400, height: 300)
  |> Vl.layers([chart1, chart2])

This is how VegaLite creates a linear regression.

Vl.new(width: 400, height: 300)
|> Vl.data_from_values(
  reservations: Explorer.Series.to_list(reserv),
  pizzas: Explorer.Series.to_list(pizzas)
)
|> Vl.layers([
  Vl.new()
  |> Vl.mark(:point, filled: true)
  |> Vl.encode_field(:x, "reservations", type: :quantitative)
  |> Vl.encode_field(:y, "pizzas", type: :quantitative),
  Vl.new()
  |> Vl.mark(:line, color: :firebrick)
  |> Vl.transform(regression: "pizzas", on: "reservations")
  |> Vl.encode_field(:x, "reservations", type: :quantitative)
  |> Vl.encode_field(:y, "pizzas", type: :quantitative)
])

Predict and Loss

defmodule MachineLearning do
  def predict(x, w, b) do
    x
    |> Nx.multiply(w)
    |> Nx.add(b)
  end

  def loss(x, y, w, b) do
    # basically mean squared error
    predict(x, w, b)
    |> Nx.subtract(y)
    |> Nx.power(2)
    |> Nx.mean()
  end

  def train(x, y, iter, lr) do
    # initialize weights and biases to 0
    Enum.reduce_while(1..iter, {0.0, 0.0}, fn i, {w, b} ->
      current_loss = loss(x, y, w, b)
      IO.puts("Iter ##{i} => Loss: #{inspect(current_loss)}")

      cond do
        loss(x, y, w + lr, b) < current_loss ->
          {:cont, {w + lr, b}}

        loss(x, y, w - lr, b) < current_loss ->
          {:cont, {w - lr, b}}

        loss(x, y, w, b + lr) < current_loss ->
          {:cont, {w, b + lr}}

        loss(x, y, w, b - lr) < current_loss ->
          {:cont, {w, b - lr}}

        true ->
          {:halt, {w, b}}
      end
    end)
  end
end

reserv_t = Nx.tensor(Explorer.Series.to_list(reserv))
pizzas_t = Nx.tensor(Explorer.Series.to_list(pizzas))

MachineLearning.train(reserv_t, pizzas_t, 50, 0.1)

I could not properly make it work on the training part, it always stops the training after a few iterations. Where do you think I get it wrong?

Most Liked

polvalente

polvalente

Nx Core Team

Because you’re using def and not defn, your loss function is returning an Nx.Tensor struct ane not a number. By using the standard comparison operators, you’re comparing numbers and structs, so that will fail fast because numbers are always evaluated as less than structs in Elixir

I strongly advise you to try and rewrite everything with defn, replacing the Enum.reduce_while with while.

Otherwise, look into using Nx.to_number so that your comparisons work properly.

NickGnd

NickGnd

Hey @t12a :wave:
I’m writing you since I just published a collection of livebooks for the book you are reading (Programming Machine Learning by P. Perrotta) in case you are curious you can find them in this GH repository GitHub - nickgnd/programming-machine-learning-livebooks: Programming Machine Learning - Elixir Livebooks · GitHub

Have a great day :wave:

polvalente

polvalente

Nx Core Team

The main difference between Nx and Python is that Python has the concept of operator overloading that Elixir doesn’t. So when you have def loss returning a numpy array and compare it with < current_loss, you get numpy to do the comparison correctly.

In Nx, because your using def loss and def train, you’re actually comparing 2 Nx.Tensor structs with the default Elixir comparison operator. That will not work semantically, the same reason that comapring two DateTime structs doesn’t work properly.

You need to fix this by turning your code to defn, which replaces the default Kernel with Nx.Defn.Kernel.

Where Next?

Popular in Questions Top

_russellb
I want to try my hand at web scraping. What tools/libraries do I need to use. I’m hoping to turn this into something professional so don’...
New
vertexbuffer
Hello, can anybody help here..? I have a list of players and I what to delete an element, but every for loop the list is reverting to ori...
New
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
New
earth10
Hi, I’m just starting to build a side-project with Elixir and Phoenix and doing some basic test with Elixir alone. What strikes me is th...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
ycv005
I have followed this StackOverflow post to install the specific version of Erlang. And When I am running mix ecto.setup then getting fol...
New
vegabook
I’m brand new to Phoenix and I have stripped one of the demo applications to the bone. I just want to get an svg up on the screen. Here i...
New
baxterw3b
Hi guys, i’m new in the Elixir world, and i have to say, that i love it! i’m having some problem to understand anonymous functions with ...
New
JDanielMartinez
Hi! May someone helps me, please! I have two apps into an umbrella project: the first one is Database, which manages queries, and the se...
New

Other popular topics Top

TunkShif
This post is an instruction guide to help you setup your Neovim for Elixir development from scratch. It includes general information on h...
274 41539 114
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vonH
When I run the Plug and I recompile I wind up having to use Ctrl C to quit iex and start again. Witht the help of rlwrap I can use the cu...
New
AstonJ
Please see the new poll here: Which code editor or IDE do you use? (Poll) (2022 Edition) It’s been a while since we first asked this, I...
208 31142 143
New
romenigld
I am trying to run a deploy with docker and I successfully runned with this command: docker build -t romenigld/blog-prod . but when I t...
New
joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
openscript
Hello! Sorry for this astonishing simple question, but I’m really stuck. I try to set up the intellij-elixir plugin, but I don’t know ho...
New
dogweather
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something… Haskell reminds me of Java, and e...
New

We're in Beta

About us Mission Statement