# Machine Learning in Elixir: Error in Chapter 9, identifying cats and dogs again

**URL:** <https://forum.devtalk.com/t/machine-learning-in-elixir-error-in-chapter-9-identifying-cats-and-dogs-again/128336>\
**Category:** PragProg Customers\
**Tags:** book-machine-learning-in-elixir, question\
**Created:** [13 October 2023 14:22 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-in-chapter-9-identifying-cats-and-dogs-again/128336 "2023-10-13T14:22:52Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![polmiro](https://forum.devtalk.com/user_avatar/forum.devtalk.com/polmiro/32/5486_2.png) [@polmiro](https://forum.devtalk.com/u/polmiro)\
**Post date:** [13 October 2023 14:22 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-in-chapter-9-identifying-cats-and-dogs-again/128336/1 "2023-10-13T14:22:52Z")

</div>

@seanmor5

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.

```auto
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

```

```auto
** (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)

```

---

<div class="post-metadata">

**Author:** ![joshprintsimple](https://forum.devtalk.com/letter_avatar_proxy/v4/letter/j/41988e/32.png) [@joshprintsimple](https://forum.devtalk.com/u/joshprintsimple)\
**Post date:** [25 October 2023 09:25 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-in-chapter-9-identifying-cats-and-dogs-again/128336/2 "2023-10-25T09:25:09Z")

</div>

@seanmor5

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:

```auto
{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)

```

---

<div class="post-metadata">

**Author:** ![polmiro](https://forum.devtalk.com/user_avatar/forum.devtalk.com/polmiro/32/5486_2.png) [@polmiro](https://forum.devtalk.com/u/polmiro)\
**Post date:** [30 October 2023 09:36 UTC](https://forum.devtalk.com/t/machine-learning-in-elixir-error-in-chapter-9-identifying-cats-and-dogs-again/128336/3 "2023-10-30T09:36:32Z")

</div>

Thanks so much Josh! That worked like a charm. I will keep that in mind next time. I did not dare to take a guess what could be going on given it was the exact book example.
