
polmiro
Machine Learning in Elixir: Error in Chapter 9, identifying cats and dogs again
the code provided I am running into an error when training the first model that uses transfer learning with ONNX model mobilenetv2-7. I am a bit stuck here, not really sure what may be wrong.
warning: Axon.Optimizers.adam/1 is deprecated. Use Polaris.Optimizers.adam/1 instead
machine-learning-in-elixir-src/StopReinventingTheWheel.livemd#cell:s4yqqx5n4j6oyja2b2c57iqes4fvcbny:2
12:22:35.339 [warning] found unexpected key in the initial parameters map: "mobilenetv20_output_pred_fwd"
Epoch: 0, Batch: 700, accuracy: 0.7747430 loss: 0.4431267
Batch: 6, accuracy: 0.9151786 loss: 0.1636844
** (ArgumentError) argument at position 1 is not compatible with compiled function template.
{
<<<<< Expected <<<<<
#Nx.Tensor<
f32[32][channels: 3][height: 160][width: 160]
>
==========
#Nx.Tensor<
f32[26][channels: 3][height: 160][width: 160]
>
>>>>> Argument >>>>>
,
<<<<< Expected <<<<<
#Nx.Tensor<
s64[32][1]
>
==========
#Nx.Tensor<
s64[26][1]
>
>>>>> Argument >>>>>
}
(nx 0.6.2) lib/nx/defn.ex:323: anonymous fn/7 in Nx.Defn.compile_flatten/5
(elixir 1.15.2) lib/enum.ex:1819: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
(nx 0.6.2) lib/nx/lazy_container.ex:61: Nx.LazyContainer.Tuple.traverse/3
(nx 0.6.2) lib/nx/defn.ex:320: Nx.Defn.compile_flatten/5
(nx 0.6.2) lib/nx/defn.ex:312: anonymous fn/4 in Nx.Defn.compile/3
(stdlib 5.0.2) timer.erl:270: :timer.tc/2
(axon 0.6.0) lib/axon/loop.ex:1805: anonymous fn/4 in Axon.Loop.run_epoch/5
/data/machine-learning-in-elixir-src/StopReinventingTheWheel.livemd#cell:s4yqqx5n4j6oyja2b2c57iqes4fvcbny:10: (file)
Marked As Solved

joshprintsimple
The length of the val_paths has to be divisible by the batch number so that the tensors come out the right shape.
You can just add this line
val_paths = Enum.take(val_paths, 224)
after setting the val_paths. I also removed the Enum.take line at the end because the length of the train_paths is 24000 and that is also divisible by a batch size of 32.
so that cell would look like this now:
{test_paths, train_paths} =
Path.wildcard("train/*.jpg")
|> Enum.shuffle()
|> Enum.split(1000)
{test_paths, val_paths} = test_paths |> Enum.split(750)
val_paths = Enum.take(val_paths, 224)
batch_size = 32
target_height = 160
target_width = 160
train_pipeline =
CatsAndDogs.pipeline_with_augmentations(
train_paths,
batch_size,
target_height,
target_width
)
val_pipeline =
CatsAndDogs.pipeline(
val_paths,
batch_size,
target_height,
target_width
)
test_pipeline =
CatsAndDogs.pipeline(
test_paths,
batch_size,
target_height,
target_width
)
# Enum.take(train_pipeline, 1)
1
Popular Pragmatic topics

Some minor things in the paper edition that says “3 2020” on the title page verso, not mentioned in the book’s errata online:
p. 186 But...
New

Hi,
build fails on:
bracket-lib = “~0.8.1”
when running on Mac Mini M1 Rust version 1.5.0:
Compiling winit v0.22.2
error[E0308]: mi...
New

On the page xv there is an instruction to run bin/setup from the main folder. I downloaded the source code today (12/03/21) and can’t see...
New

Title: Intuitive Python: docker run… denied error (page 2)
Attempted to run the docker command in both CLI and Powershell
PS C:\Users\r...
New

I’m under the impression that when the reader gets to page 136 (“View Data with the Database Inspector”), the code SHOULD be able to buil...
New

Title: Build a Weather Station with Elixir and Nerves: Problem connecting to Postgres with Grafana on (page 64)
If you follow the defau...
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

Skimming ahead, much of the following is explained in Chapter 3, but new readers (like me!) will hit a roadblock in Chapter 2 with their ...
New

@mfazio23
Android Studio will not accept anything I do when trying to use the Transformations class, as described on pp. 140-141. Googl...
New

I’ve got to the end of Ch. 11, and the app runs, with all tabs displaying what they should – at first. After switching around between St...
New
Other popular topics

If it’s a mechanical keyboard, which switches do you have?
Would you recommend it? Why?
What will your next keyboard be?
Pics always w...
New

Any thoughts on Svelte?
Svelte is a radical new approach to building user interfaces. Whereas traditional frameworks like React and Vue...
New

I know that -t flag is used along with -i flag for getting an interactive shell. But I cannot digest what the man page for docker run com...
New

Why, if your answer is yes?
New

I ended up cancelling my Moonlander order as I think it’s just going to be a bit too bulky for me.
I think the Planck and the Preonic (o...
New

Hello content creators! Happy new year. What tech topics do you think will be the focus of 2021? My vote for one topic is ethics in tech...
New

In case anyone else is wondering why Ruby 3 doesn’t show when you do asdf list-all ruby :man_facepalming: do this first:
asdf plugin-upd...
New

I am trying to crate a game for the Nintendo switch, I wanted to use Java as I am comfortable with that programming language. Can you use...
New

Author Spotlight:
David Bryant Copeland
@davetron5000
We’re so happy to bring you another Author Spotlight, a series where we sit dow...
New

This is a very quick guide, you just need to:
Download LM Studio: https://lmstudio.ai/
Click on search
Type DeepSeek, then select the o...
New
Latest in PragProg
Latest (all)
Categories:
Popular Portals
- /elixir
- /rust
- /wasm
- /ruby
- /erlang
- /phoenix
- /keyboards
- /js
- /rails
- /python
- /security
- /go
- /swift
- /vim
- /clojure
- /java
- /haskell
- /emacs
- /svelte
- /onivim
- /typescript
- /crystal
- /c-plus-plus
- /tailwind
- /kotlin
- /gleam
- /react
- /flutter
- /elm
- /ocaml
- /vscode
- /opensuse
- /ash
- /centos
- /php
- /deepseek
- /zig
- /scala
- /html
- /debian
- /nixos
- /lisp
- /agda
- /textmate
- /react-native
- /sublime-text
- /kubuntu
- /arch-linux
- /ubuntu
- /revery
- /manjaro
- /spring
- /django
- /diversity
- /nodejs
- /lua
- /julia
- /c
- /slackware
- /neovim