In chapter 3 of Machine Learning in Elixir I’m putting in the “BerryFarm” module and plotting the profit graph but my output from VegaLite does not match the plot in the book even though as far as I can tell my code matches.
defmodule BerryFarm do
import Nx.Defn
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
end
trees = Nx.linspace(0, 4, n: 100)
profits = BerryFarm.profits(trees)
alias VegaLite, as: Vl
Vl.new(title: "Berry Profits", width: 800, height: 800)
|> Vl.data_from_values(%{
trees: Nx.to_flat_list(trees),
profits: Nx.to_flat_list(profits)
})
|> Vl.mark(:line, interpolate: :basis)
|> Vl.encode_field(:x, "trees", type: :quantitative)
|> Vl.encode_field(:y, "profits", type: :quantitative)
I get a graph with a max of 4.409083366394043
and not the ~20 as shown. And for a different number of trees. Looking at the Nx.linspace/3
docs it sure seems like the results should be consistent.