shawn_leong
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
Marked As Solved
seanmor5
Author of Genetic Algorithms in Elixir
Thank you for pointing this out! I’ve fixed the issue in the next beta release!
1
Popular Pragmatic Bookshelf topics
Following the steps described in Chapter 6 of the book, I’m stuck with running the migration as described on page 84:
bundle exec sequel...
New
I can’t setup the Rails source code. This happens in a working directory containing multiple (postgres) Rails apps.
With:
ruby-3.0.0
s...
New
Dear Sophie.
I tried to do the “Authorization” exercise and have two questions:
When trying to plug in an email-service, I found the ...
New
I ran this command after installing the sample application:
$ cards add do something --owner Brian
And got a file not found error:
Fil...
New
When installing Cards as an editable package, I get the following error:
ERROR: File “setup.py” not found. Directory cannot be installe...
New
Is the book’s epub format available to read on Google Play Books?
New
Is there any place where we can discuss the solutions to some of the exercises? I can figure most of them out, but am having trouble with...
New
Hi,
I am getting an error I cannot figure out on my test.
I have what I think is the exact code from the book, other than I changed “us...
New
When running the program in chapter 8, “Implementing Combat”, the printout Health before attack was never printed so I assumed something ...
New
Docker-Machine became part of the Docker Toolbox, which was deprecated in 2020, long after Docker Desktop supported Docker Engine nativel...
New
Other popular topics
Algorithms and data structures are much more than abstract concepts. Mastering them enables you to write code that runs faster and more e...
New
Curious to know which languages and frameworks you’re all thinking about learning next :upside_down_face:
Perhaps if there’s enough peop...
New
New
Learn different ways of writing concurrent code in Elixir and increase your application's performance, without sacrificing scalability or...
New
Author Spotlight
Jamis Buck
@jamis
This month, we have the pleasure of spotlighting author Jamis Buck, who has written Mazes for Prog...
New
Author Spotlight
Erin Dees
@undees
Welcome to our new author spotlight! We had the pleasure of chatting with Erin Dees, co-author of ...
New
Author Spotlight:
VM Brasseur
@vmbrasseur
We have a treat for you today! We turn the spotlight onto Open Source as we sit down with V...
New
Big O Notation can make your code faster by orders of magnitude. Get the hands-on info you need to master data structures and algorithms ...
New
Get the comprehensive, insider information you need for Rails 8 with the new edition of this award-winning classic.
Sam Ruby @rubys
...
New
A concise guide to MySQL 9 database administration, covering fundamental concepts, techniques, and best practices.
Neil Smyth
MySQL...
New
Categories:
Sub Categories:
Popular Portals
- /elixir
- /rust
- /ruby
- /wasm
- /erlang
- /phoenix
- /keyboards
- /python
- /js
- /rails
- /security
- /go
- /swift
- /vim
- /clojure
- /emacs
- /haskell
- /java
- /svelte
- /onivim
- /typescript
- /kotlin
- /crystal
- /c-plus-plus
- /tailwind
- /react
- /gleam
- /ocaml
- /elm
- /flutter
- /vscode
- /ash
- /opensuse
- /html
- /centos
- /php
- /zig
- /deepseek
- /scala
- /sublime-text
- /lisp
- /textmate
- /react-native
- /nixos
- /debian
- /agda
- /kubuntu
- /arch-linux
- /deno
- /django
- /revery
- /ubuntu
- /nodejs
- /spring
- /manjaro
- /diversity
- /lua
- /julia
- /c
- /slackware









