Machine Learning in Elixir: Plot of given `profits` function does not match the expected plot (B1.0, Chapter 3, Page 65)

Hey @seanmor5,

The function given in the book

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

Results in the following plot which doesn’t match the expected plot in the book:

The accompanying Livebooks provides the following code to give the expected plot:

defn profits(trees) do
  -((trees - 1) ** 4) + trees ** 3 + trees ** 2
end

So my guess is that there is an error in the conversion to Nx functions.

Instead, the correct translation of the Livebook function should be as follows:

  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
  • Adds the missing Nx.subtract(1) that the book doesn’t have
  • Removes the unnecessary doubling of the cube in Nx.add(Nx.multiply(2, Nx.pow(trees,3)))

This gives the correct expected plot