zacksiri

zacksiri

Expected data structure for training a Multi Input / Multi Output Axon model

Hey everyone.

So i’ve been developing a model and it started as a straightforward logistic regression model and has evolved into a multi input / multi output model.

Here is what the model looks like

def model do
    # Create three input tensors for CPU, Memory, and Disk
    input_cpu = Axon.input("cpu", shape: {nil, 2})
    input_memory = Axon.input("memory", shape: {nil, 3})
    input_disk = Axon.input("disk", shape: {nil, 2})

    # Create separate prediction paths for each resource
    cpu_prediction =
      Axon.dense(input_cpu, 2, activation: :sigmoid, name: "cpu")

    memory_prediction =
      input_memory
      |> Axon.dense(8, activation: :relu)
      |> Axon.dense(2, activation: :sigmoid, name: "memory")

    disk_prediction =
      Axon.dense(input_disk, 2, activation: :sigmoid, name: "disk")

    # Combine outputs into a single model with multiple outputs
    Axon.container(
      %{
        cpu: cpu_prediction,
        memory: memory_prediction,
        disk: disk_prediction
      },
      name: "results"
    )
end

Here is what the training loop looks like:

 def train(data, opts \\ []) do
    save? = Keyword.get(opts, :save, false)
    model = model()

    state = Keyword.get(opts, :state) || Axon.ModelState.empty()
    iterations = Keyword.get(opts, :iterations, 100)
    epochs = Keyword.get(opts, :epochs, 100)

    # Losses and weights for each output cpu, memory, disk
    losses = [binary_cross_entropy: 0.2, binary_cross_entropy: 0.4, binary_cross_entropy: 0.4]

    state =
      model
      |> Axon.Loop.trainer(losses, Polaris.Optimizers.adamw(learning_rate: 0.01))
      |> Axon.Loop.run(data, state, iterations: iterations, epochs: epochs)

    if save? do
      dump_state(state)
    end

    state
end

I’ve tried the following data structures:

training_data = [
  # Example 1: Good placement (plenty of resources)
  {
    %{
      "cpu" => Nx.tensor([[0.05, 0.825]]),    # [requested, available]
      "memory" => Nx.tensor([[0.0625, 0.65, 0.10]]),
      "disk" => Nx.tensor([[0.004, 0.55]])
    },
    %{
      cpu: Nx.tensor([[1.0, 0.0]]),      # Good placement
      memory: Nx.tensor([[1.0, 0.0]]),   # Good placement
      disk: Nx.tensor([[1.0, 0.0]])      # Good placement
    }
  },
  
  # Example 2: Bad placement (scarce resources)
  {
    %{
      "cpu" => Nx.tensor([[0.05, 0.12]]),     # Low available CPU
      "memory" => Nx.tensor([[0.0625, 0.15, 0.010]]), # Low available memory
      "disk" => Nx.tensor([[0.004, 0.10]])     # Low available disk
    },
    %{
      cpu: Nx.tensor([[0.0, 1.0]]),      # Bad placement
      memory: Nx.tensor([[0.0, 1.0]]),   # Bad placement
      disk: Nx.tensor([[0.0, 1.0]])      # Bad placement
    }
  }
]
training_data = [
  # Example 1: Good placement
  {
    {
      Nx.tensor([[0.05, 0.825]]),
      Nx.tensor([[0.0625, 0.65, 0.010]]),
      Nx.tensor([[0.004, 0.55]])
    },
    {
      Nx.tensor([[1.0, 0.0]]),
      Nx.tensor([[1.0, 0.0]]),
      Nx.tensor([[1.0, 0.0]])
    }
  },
  # Example 2: Bad placement
  {
    {
      Nx.tensor([[0.05, 0.12]]),
      Nx.tensor([[0.0625, 0.15, 0.010]]),
      Nx.tensor([[0.004, 0.10]])
    },
    {
      Nx.tensor([[0.0, 1.0]]),
      Nx.tensor([[0.0, 1.0]]),
      Nx.tensor([[0.0, 1.0]])
    }
  }
]
# Correct training data structure with properly shaped tensors
training_data = [
  # Each training example
  {
    # Inputs tuple
    {
      Nx.tensor([0.05, 0.825]),    # cpu - shape {2}
      Nx.tensor([0.0625, 0.65, 0.010]),   # memory - shape {2}
      Nx.tensor([0.004, 0.55])     # disk - shape {2}
    },
    # Targets tuple
    {
      Nx.tensor([1.0, 0.0]),    # cpu target - shape {2}
      Nx.tensor([1.0, 0.0]),    # memory target - shape {2}
      Nx.tensor([1.0, 0.0])     # disk target - shape {2}
    }
  }
]

None of the above examples seem to work. Any suggestions?

Marked As Solved

zacksiri

zacksiri

I managed to figure out where I went wrong.

The Axon.container is wrong. The output format needs to be a tuple not a map

So this is invalid

# Change
Axon.container(
      %{
        cpu: cpu_prediction,
        memory: memory_prediction,
        disk: disk_prediction
      },
      name: "results"
    )

# to

 Axon.container(
   {cpu_prediction, memory_prediction, disk_prediction},
   name: "results"
  )

Then the training data set should look like the following:

training_data = [
  {
    # Input map with string keys matching the Axon.input names
    %{
      "cpu" => Nx.tensor([0.05, 0.825]),
      "memory" => Nx.tensor([0.0625, 0.65, 0.75]),
      "disk" => Nx.tensor([0.004, 0.55])
    },
    # Target outputs still as a tuple
    {
      Nx.tensor([1.0, 0.0]),    # cpu target
      Nx.tensor([1.0, 0.0]),    # memory target
      Nx.tensor([1.0, 0.0])     # disk target
    }
  }
]

I am now able to start the training loop. I have no one but myself to blame. I followed Claude down a rabbit hole. I was very happy with my simple logistic regression model.

Then I went to ask it how split the output into cpu, memory, disk instead of just having a single output, and it gave me the model you see above. In it’s defense most of the model was correct the only part that’s wrong is the Axon.container bit. But my lack of experience naively thought it would work.

Where Next?

Popular in Questions Top

minhajuddin
I have seen a lot of code which picks the first element from a list using Enum.at(0) instead of List.first. Is there a reason why people ...
New
jononomo
I am trying to figure out how Mix knows whether the environment is test, dev, or prod – where is this set? Thanks.
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
aalberti333
As the title describes, I’m trying to run Enum.map() over a list of key/value pairs, where the value is a map. My data looks like this: ...
New
siddhant3030
Hi, I have to write a raw query for one of my project. But till now I have used ecto queries and don’t have much experience writing raw ...
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
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

Other popular topics Top

Qqwy
Original source of discussion: This topic on the Pragmatic Programmers’ Functional Web Development with Elixir, OTP, and Phoenix forum. ...
New
jononomo
For some reason my phoenix channels are working for me in my local dev environment, but as soon as I deploy via Docker, I get a 403 error...
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
sen
Hi All, I set a environment variables in dev.exs , like below code. when i start server, how can i set the ${enable} value? thanks. d...
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
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

We're in Beta

About us Mission Statement