sezaru

sezaru

How to create a batched input from a DataFrame when model expects multiple inputs?

My model expects multiple named inputs, that means that I need to pass the data as a map with each input name like this:

inputs = %{
  "hours_per_week" => Nx.tensor([1, 1]),
  "capital_loss" => Nx.tensor([1, 1]),
  ...
}

predict_fn.(params, inputs)

If I have an Explorer.DataFrame I can achieve the same using DataFrame.to_columns(df). The problem with this is that it is an expensive operation if the dataframe is big.

Another issue is that I want to batch the inputs to send to the trainer loop. Normally I think I would be able to use Nx.to_batched to do the job, but that function seems to only work with a single input, not multiple.

So, how can I create a batched input for my trainer loop from my data frames that is also lazy so I don’t need to have the full data in memory all the time?

Here is what I was able to make that works, but it will generate all the batches in memory which will give me a OOM depending on the dataset size:

create_batches = fn features, target, batches ->
  features_batches =
    features
    |> DF.to_columns()
    |> Enum.map(fn {key, values} ->
      values
      |> Nx.tensor()
      |> Nx.to_batched(batches)
      |> Enum.to_list()
      |> Enum.map(fn tensors -> {key, tensors} end)
    end)
    |> Enum.zip()
    |> Enum.map(fn batch -> batch |> Tuple.to_list() |> Map.new() end)

  target_batches =
    target
    |> DF.to_series(atom_keys: true)
    |> Map.fetch!(:income_bracket)
    |> Series.to_tensor()
    |> Nx.new_axis(-1)
    |> Nx.to_batched(batches)
    |> Enum.to_list()

  Enum.zip(features_batches, target_batches)
end

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
hariharasudhan94
lets say i have a sample like a = 20; b = 10; if (a > b) do {:ok, "a"} end if (a < b) do {:ok, b} end if (a == b) do {:ok, "equa...
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
alice
Hey, Just curious what are the main benefits of Elixir compared to Clojure? When is Elixir more useful than Clojure and vice versa? Th...
New
pmjoe
I have a relationship of love and hate with Elixir. Lots of things are just absolutely right, but there are some things that are kind of ...
New
SoCreat
i’m a new one to elixir which editor can i use vs code? or atom? Thanks! :smiley:
New
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New

Other popular topics Top

joaquinalcerro
Hi there, I am working with Ecto-Postgresql and I need to call all of the records from a specific table but the table has 40,000 records...
New
hariharasudhan94
I would like to know what is the best IDE for elixir development?
New
ashish173
I am using Ecto timestamps with postgres, I can see the timestamps() use the :naive_dateime but for my use case I wanted to store the ti...
New
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
dblack
I’ve got an issue with an app and I’ve no idea of how to troubleshoot it. I’m hoping someone here might have seen something similar. I p...
New
JorisKok
I have a server on AWS, and was running a load test using artillery. When looking at the Phoenix dashboard I see the Ports going to 100% ...
New

We're in Beta

About us Mission Statement