linusdm

linusdm

Elixir/Nx/Axon equivalents of PyTorch code

Hello Nx community! :wave:

I’m here as a noob in the field of AI (and python), trying to accomplish a task with my favourite language. I’d like to use an exported ONNX model (exported from a torch model, built in python, which I can load from the filesystem). Someone before me has written some python code to execute the torch model, which I would like to do in elixir. The gist boils down to the following:

import pandas as pd
import numpy as np
import torch

model = torch.load('/path/to/model.torch', map_location=torch.device('cpu'))
pred = {}

for index, data_frame in some_list_of_data_frames:
  feats = torch.from_numpy(np.array(data_frame, dtype=np.float32))
  with torch.no_grad():
    pred[index] = model.forward(feats.unsqueeze(0)).detach().cpu().numpy()[0]

result = pd.DataFrame.from_dict(pred, orient="index", columns=["some", "set", "of", "columns"])

The data frames are built somehow, but that’s for later (with explorer, I suppose). I’d like to know what a comparable elixir implementation would be, if at all possible. I manage to load the ONNX model with the axon_onnx lib, which spits out the axon model and a map of params, which is a start. But then I don’t really know how to mimic the torch.from_numpy(), torch.no_grad(), model.forward(...).detach().cpu().numpy() and pd.DataFrame.from_dict(...). If anyone has tips to share, I’d be very grateful :pray:

Most Liked

josevalim

josevalim

Creator of Elixir

Maybe this guide is a good starting point: https://github.com/elixir-nx/axon/pull/227

NickGnd

NickGnd

Hey @linusdm :wave:

A bit late, but in case you are still looking for something…

I have a similar need for creating a histogram where I can specify the bins wherein I can sum values, to get to a tensor with well structured series data. Something like this numpy histogram function: numpy.histogram — NumPy v1.23 Manual

I think Explorer might be of help here, have you seen Explorer.Series.cut/3? Is not as fancy as numpy.histogram, but for “basic” histogram creation works fine :wink:

Cheers :v:

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
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
lastday4you
I wanted to check elixir version in phoenix because i found that my elixir is 1.5 but when i use Enum.chunk_by it said the function is un...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
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
greenz1
I have a phoenix application from which a user can download multiple(5-6) files of size 1MB. I couldn’t find anything related to sending ...
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

hariharasudhan94
I would like to know what is the best IDE for elixir development?
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
dokuzbir
I want to highlight html closing tags when i click a html tag. That works in .html files but doesnt work for html.eex templates. How can...
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
Patoshizzle
After calling mix ecto.create I get this error: 17:00:32.162 [error] GenServer #PID<0.412.0> terminating ** (Postgrex.Error) FATAL...
New
sergio
Kind of like when jquery came out, it was super necessary. Existing drag and drop libraries have a bunch of baggage to support old browse...
New

We're in Beta

About us Mission Statement