poppingtonic

poppingtonic

Troubleshooting Axon: expected input to be a tensor or a map corresponding to correct input names

Following this tutorial for building a semantic search tool, I’m encountering an issue that I’m having trouble tracing properly. I have the problematic module posted here. In iex -S mix phx.server I run :ok = Pento.Model.load(), I get:

iex(2)> :ok = Pento.Model.load()                                                                                                           
** (ArgumentError) invalid input given to model, expected input expected input to be a tensor or a map corresponding to correct input names
    (axon 0.3.1) lib/axon/compiler.ex:558: Axon.Compiler.get_input/3                                                                       
    (axon 0.3.1) lib/axon/compiler.ex:285: anonymous fn/7 in Axon.Compiler.recur_model_funs/5
    (axon 0.3.1) lib/axon/compiler.ex:220: Axon.Compiler.call_init_cache/7
    (axon 0.3.1) lib/axon/compiler.ex:781: anonymous fn/6 in Axon.Compiler.layer_init_fun/11
    (elixir 1.14.2) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (axon 0.3.1) lib/axon/compiler.ex:778: Axon.Compiler.layer_init_fun/11
    (axon 0.3.1) lib/axon/compiler.ex:220: Axon.Compiler.call_init_cache/7
    (axon 0.3.1) lib/axon/compiler.ex:370: anonymous fn/6 in Axon.Compiler.recur_model_funs/5
    (elixir 1.14.2) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (nx 0.4.1) lib/nx/container.ex:79: Nx.Container.Tuple.traverse/3
    (axon 0.3.1) lib/axon/compiler.ex:367: anonymous fn/6 in Axon.Compiler.recur_model_funs/5
    (axon 0.3.1) lib/axon/compiler.ex:220: Axon.Compiler.call_init_cache/7
    (axon 0.3.1) lib/axon/compiler.ex:781: anonymous fn/6 in Axon.Compiler.layer_init_fun/11
    (elixir 1.14.2) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (axon 0.3.1) lib/axon/compiler.ex:778: Axon.Compiler.layer_init_fun/11
    (axon 0.3.1) lib/axon/compiler.ex:220: Axon.Compiler.call_init_cache/7
    (axon 0.3.1) lib/axon/compiler.ex:370: anonymous fn/6 in Axon.Compiler.recur_model_funs/5
    (elixir 1.14.2) lib/enum.ex:1780: Enum."-map_reduce/3-lists^mapfoldl/2-0-"/3
    (nx 0.4.1) lib/nx/container.ex:79: Nx.Container.Tuple.traverse/3
    (axon 0.3.1) lib/axon/compiler.ex:367: anonymous fn/6 in Axon.Compiler.recur_model_funs/5

The module in question

defmodule Pento.Model do
  @max_length 120

  def load() do
    {model, params} =
      AxonOnnx.import("priv/models/model.onnx", batch: 1, sequence: @max_length())

    {:ok, tokenizer} = Tokenizers.Tokenizer.from_pretrained("bert-base-uncased")
    {_, predict_fn} = Axon.compile(model, compiler: EXLA)

    predict_fn =
      EXLA.compile(
        fn params, inps ->
          {_, pooled} = predict_fn.(params, inps)
          Nx.squeeze(pooled)
        end,
        [params, inputs()]
      )

      :persistent_term.put({__MODULE__, :model}, {predict_fn, params})
      # load the tokenizer as well
      :persistent_term.put({__MODULE__, :tokenizer}, tokenizer)

      :ok
  end

  def max_length(), do: @max_length

  defp inputs() do
    %{
      "input_ids" => Nx.template({1, 120}, {:s, 64}),
      "token_type_ids" => Nx.template({1, 120}, {:s, 64}),
      "attention_mask" => Nx.template({1, 120}, {:s, 64})
    }
  end
end

Marked As Solved

seanmor5

seanmor5

Author of Genetic Algorithms in Elixir

When the article was written the compile API was different, now it expects you to pass the input templates. So it should be:

{params, predict_fn} = Axon.compile(model, inputs, params, compiler: EXLA)

Also Liked

seanmor5

seanmor5

Author of Genetic Algorithms in Elixir

Also, you can get rid of the EXLA compilation as axon compile now does that for you. I have plans to update the article with all of the things we’ve added in recent months!

Where Next?

Popular in Questions Top

Darmani72
If I have a post route which an argument: post /my_post_route/:my_param1, MyController.my_post_handler How would get the post params ...
New
shahryarjb
Hello, I have map which I want to convert it to string like this: the map: %{last_name: "tavakkoli", name: "shahryar"} the string I ne...
New
vac
Hi, I’m quite new in Elixir and I’m trying to format a string to a PEM format. I have the certificate value like MIIDBTCCAe2...... and I...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
joeerl
Hello again - after a longish gap I’ve decided I really must dig into Elixir and see what’s been happening here - so I have a few questio...
New
itssasanka
Hi all, Trying to get some more clarity over utc_datetime and naive_datetime for Ecto: The documentation above suggests that while ...
New
RisingFromAshes
I’ve read in another post that it may be possible with a router helper - but I couldn’t find an appropriate one, and tbh, I’m still just ...
New
bsollish-terakeet
Credo is smart enough to check for (something like) this: assert length(the_list) == 0 with this response: Checking if an enum is empt...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
komlanvi
Hi everyone, I was playing with phoenix liveView but I run into an issue. I have a form and want to validate each input text when the te...
New

Other popular topics Top

marius95
Hello everyone, I try to use an Javascript Event Handler in my root.html.leex file. Therefore I created a function in the app.js file: ...
New
New
Harrisonl
We have an ECS cluster with 4 services, where each task joins a single cluster, via discovery ECS discovery service. Currently when I de...
New
stefanchrobot
What’s the safe way to decode a JSON string into a struct? I want to avoid calling String.to_atom. Jason.decode can give me a map with st...
New
jaysoifer
Is there a way to rollback a specific migration and only that one (“skipping” all the other ones)? Would mix ecto.rollback -v 200809061...
New
aesmail
Hello guys, I have finally made it. I created an admin interface for a framework. It’s been on my todo list for years and with the curre...
New
Emily
I have VueJS GUIs with the project generated using Webpack. I have Elixir modules that will need to be used by the VueJS GUIs. I forese...
New
freewebwithme
Using vs code and installed ElixirLS: support and debugger. And I got an error popped up on start up says Failed to run ‘elixir’ comma...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Latest on Elixir Forum

Elixir Forum

We're in Beta

About us Mission Statement