pieteeken

pieteeken

Machine Learning in Elixir: comments on chapter 11, version B 3.0

Comments to Chapter 11 version B 3.0

@seanmor5

The dependencies from chapter 11 gave errors on 10 march 2024.
I followed the advices in the errors, but at the end it still didn’t work.
So I loaded this versions (They work with cuda120!)

 Mix.install([  
  {:bumblebee, "> 0.0.0"},  
  {:axon, "> 0.0.0"},  
  {:exla, "> 0.0.0"},  
  {:nx, "> 0.0.0"},  
  {:kino, "~> 0.8"},  
  {:kino_bumblebee, "> 0.0.0"}  
 ])  

Nx.global_default_backend(EXLA.Backend)  

Section Generating Text

Bumblebee.Text.conversation does not exist in BumbleBee version 0.5
So I used Bumblebee.Text.generation()
Also the Kino part had to be changed!

With Bumblebee.Text.generation() the model with “microsoft/DialoGPT-medium” only finishes sentences. So it is not really a chatbot.

If you change the model to “gpt2” you get a real chatbot (but it is a weird bot!)
Sentences are repeated. Don’t know how to handle it.


# The weird bot  
{:ok, model} = Bumblebee.load_model({:hf, "gpt2"})  
{:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "gpt2"})  
{:ok, generation_config} = Bumblebee.load_generation_config({:hf, "gpt2"})  

# This model "microsoft/DialoGPT-medium"  only finishes sentences 
# {:ok, model} = Bumblebee.load_model({:hf, "microsoft/DialoGPT-medium"})  
# {:ok, tokenizer} = Bumblebee.load_tokenizer({:hf, "gpt2"})  
# {:ok, generation_config} = Bumblebee.load_generation_config({:hf, "microsoft/DialoGPT-medium"})  

generation_config = Bumblebee.configure(generation_config, max_new_tokens: 100)  

Also the serving statement mus be changed

serving =  
  Bumblebee.Text.generation(model, tokenizer, generation_config,  
    compile: [batch_size: 1, sequence_length: 1000],  
    defn_options: [compiler: EXLA]  
  )  

The Kino part must be changed a bit.
In the form watch the “%{results…” line

frame = Kino.Frame.new()  
controls = [message: Kino.Input.text("New Message")]  
form = Kino.Control.form(controls, submit: "Send Message", reset_on_submit: [:message])  

form  
|> Kino.Control.stream()  
|> Kino.listen(nil, fn %{data: %{message: message}}, _token_summary ->  
  Kino.Frame.append(frame, Kino.Markdown.new("**Me:** #{message}"))  

  %{results: [%{text: text, token_summary: token_summary}]}= Nx.Serving.run(serving, %{text: message})  

  Kino.Frame.append(frame, Kino.Markdown.new("**Bot:** #{text}"))  
  {:cont, token_summary}  
end)  

Kino.Layout.grid([frame, form], gap: 16)

Section Classifying Images

In the first cell:
defn_options: [compiler: EXLA] gives an error, maybe because of use cuda120?
So I removed it

{:ok, model_info} = Bumblebee.load_model({:hf, "google/vit-base-patch16-224"})
{:ok, featurizer} = Bumblebee.load_featurizer({:hf, "google/vit-base-patch16-224"})
serving =
  Bumblebee.Vision.image_classification(model_info, featurizer,
    top_k: 1,
    compile: [batch_size: 1]
    # defn_options: [compiler: EXLA] #gives error
  )

The Kino part doesn’t work at all.
Dragging the image removes all the Evalute/Reevaluate knobs
Open camera and Upload work correct.
But pushing the Run button gives an error. I could not repair that.
So I replaced the Kino part by:


image =  
  "/home/piet/livebooks/ML_Elixir/Cat.jpg"  
  |> StbImage.read_file!()  
  |> StbImage.resize(224, 224)  
  |> StbImage.to_nx()  
  |> Nx.as_type({:u, 8})  
  
  output = Nx.Serving.run(serving, image)  

output.predictions  
|> Enum.map(&{&1.label, &1.score})  
|> Kino.Bumblebee.ScoredList.new()  

Section Fine-tuning Pre-trained Models

In the beginning training seemed to go well, although the accuracy after batch 861 was 0.0920101!
And at that batch I got an error!
The error after Epoch: 0, Batch: 861:

  ** (MatchError) no match of right hand side value: ["\"2", "This must be the place that .....  
  ............  
  ..........."]  
     
   (stdlib 5.1.1) erl_eval.erl:498: :erl_eval.expr/6  
    #cell:33sgnufmubmhq4l6:11: (file)  
    #cell:33sgnufmubmhq4l6:10: (file)  
    #cell:33sgnufmubmhq4l6:14: (file)  

Probably there is some error in the Yelp zip file.
I downloaded it twice but still an error.

Also every adding to the output gave the annoying warning:

warning: passing options to Bumblebee.apply_tokenizer/3 is deprecated, please use Bumblebee.  configure/2 to set tokenizer options  
  (bumblebee 0.5.3) lib/bumblebee.ex:856: Bumblebee.apply_tokenizer/3  
  (elixir 1.15.7) src/elixir.erl:396: :elixir.eval_external_handler/3  
  (stdlib 5.1.1) erl_eval.erl:750: :erl_eval.do_apply/7  
  (stdlib 5.1.1) erl_eval.erl:494: :erl_eval.expr/6  
  (stdlib 5.1.1) erl_eval.erl:136: :erl_eval.exprs/6  
  (elixir 1.15.7) lib/stream.ex:613: anonymous fn/4 in Stream.map/2  

Since this is not the end version (I assume) and the dependencies are not right, the chapter, besides the first section, needs a rewrite.
If it is necessary I can send you my livebook, But I don`t know how to attach.

First Post!

jshprentz

jshprentz

@seanmor5

As Piet reported, the code on page 260 fails at

    image
    |> Nx.from_binary(:u8)

Based on the example at Examples — Bumblebee v0.5.3, I replaced the image line with this pipeline:

    image.file_ref
    |> Kino.Input.file_path()
    |> File.read!()

Now the Run button successfully ingests the image, runs the model, and reports the top predictions. The serving = ... code on page 259 contained the option top_k: 1, so only one prediction is shown. After changing 1 to 5 and uploading the book’s cat image, the five predictions match those shown on page 261, albeit with different probabilities.

Here is the complete modified code from page 260:

image_input = Kino.Input.image("Image", size: {224, 224})
form = Kino.Control.form([image: image_input], submit: "Run")
frame = Kino.Frame.new()

form
|> Kino.Control.stream()
|> Stream.filter(& &1.data.image)
|> Kino.listen(fn %{data: %{image: image}} ->
  Kino.Frame.render(frame, Kino.Markdown.new("Running..."))

  image =
    image.file_ref
    |> Kino.Input.file_path()
    |> File.read!()
    |> Nx.from_binary(:u8)
    |> Nx.reshape({image.height, image.width, 3})

  output = Nx.Serving.run(serving, image)

  output.predictions
  |> Enum.map(&{&1.label, &1.score})
  |> Kino.Bumblebee.ScoredList.new()
  |> then(&Kino.Frame.render(frame, &1))
end)

Kino.Layout.grid([form, frame], boxed: true, gap: 16)

Where Next?

Popular Pragmatic Bookshelf topics Top

Alexandr
Hi everyone! There is an error on the page 71 in the book “Programming machine learning from coding to depp learning” P. Perrotta. You c...
New
mikecargal
Title: Hands-On Rust (Chapter 11: prefab) Just played a couple of amulet-less games. With a bit of debugging, I believe that your can_p...
New
herminiotorres
Hi @Margaret , On page VII the book tells us the example and snippets will be all using Elixir version 1.11 But on page 3 almost the en...
New
fynn
This is as much a suggestion as a question, as a note for others. Locally the SGP30 wasn’t available, so I ordered a SGP40. On page 53, ...
New
jskubick
I found an issue in Chapter 7 regarding android:backgroundTint vs app:backgroundTint. How to replicate: load chapter-7 from zipfile i...
New
dsmith42
Hey there, I’m enjoying this book and have learned a few things alredayd. However, in Chapter 4 I believe we are meant to see the “>...
New
taguniversalmachine
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
jonmac
The allprojects block listed on page 245 produces the following error when syncing gradle: “org.gradle.api.GradleScriptException: A prob...
New
mert
AWDWR 7, page 152, page 153: Hello everyone, I’m a little bit lost on the hotwire part. I didn’t fully understand it. On page 152 @rub...
New
redconfetti
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 Top

ohm
Which, if any, games do you play? On what platform? I just bought (and completed) Minecraft Dungeons for my Nintendo Switch. Other than ...
New
axelson
I’ve been really enjoying obsidian.md: It is very snappy (even though it is based on Electron). I love that it is all local by defaul...
New
Exadra37
I am thinking in building or buy a desktop computer for programing, both professionally and on my free time, and my choice of OS is Linux...
New
AstonJ
I’ve been hearing quite a lot of comments relating to the sound of a keyboard, with one of the most desirable of these called ‘thock’, he...
New
DevotionGeo
The V Programming Language Simple language for building maintainable programs V is already mentioned couple of times in the forum, but I...
New
AstonJ
Continuing the discussion from Thinking about learning Crystal, let’s discuss - I was wondering which languages don’t GC - maybe we can c...
New
AstonJ
Biggest jackpot ever apparently! :upside_down_face: I don’t (usually) gamble/play the lottery, but working on a program to predict the...
New
mafinar
This is going to be a long an frequently posted thread. While talking to a friend of mine who has taken data structure and algorithm cou...
New
PragmaticBookshelf
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
New

Sub Categories: