Mostafa86
Does Nx has an equivalent for JAX tree_map?
Does Nx has an equivalent for JAX tree_map jax.tree_util.tree_map — JAX documentation?
Thank you!
Marked As Solved
polvalente
Nx Core Team
Yet another possibility is to flatten each container and zip them together
2
Also Liked
polvalente
Nx Core Team
Here’s my suggestion:
def traverse([template_container | _] = list_of_arbitrary_nested_maps, fun) when is_list(list_of_arbitrary_nested_maps) do
zipped_containers =
list_of_arbitrary_nested_maps
|> Enum.map(&Nx.Defn.Composite.flatten_list[&1])
|> Enum.zip_with(fun)
{v, []} =
Nx.Defn.Composite.traverse(template_container, zipped_containers, fn _, [h | t] ->
{h, t}
end)
v
end
2
Mostafa86
Couldn’t find any so I hacked a quick one that fits my use case. Any feedback or alternatives is more than welcome as I am pretty new to Elixir.
defmodule TreeMap do
import Nx.Defn
import Nx, only: [is_tensor: 1]
defn fun({a, b, c}) do
Nx.multiply(c, Nx.add(a, b))
end
def process_list_of_arbitrary_nested_maps(list_of_arbitrary_nested_maps) do
list_of_arbitrary_nested_maps
|> Enum.map(&Map.to_list/1)
|> Enum.map(&Enum.sort/1)
|> Enum.zip()
|> Enum.map(&Tuple.to_list/1)
|> Enum.map(fn elem ->
elem |> Enum.map_reduce("", fn x, _ -> {x |> elem(1), x |> elem(0)} end)
end)
end
def traverse(list_of_arbitrary_nested_maps, fun) do
condition =
list_of_arbitrary_nested_maps
|> Enum.map(&Map.values/1)
|> Enum.map(fn x -> x |> Enum.all?(&is_tensor/1) end)
|> Enum.all?()
cond do
condition ->
list_of_arbitrary_nested_maps
|> process_list_of_arbitrary_nested_maps()
|> Enum.map(fn {l, k} -> %{"#{k}" => fun.(l |> List.to_tuple())} end)
|> Enum.reduce(&Map.merge/2)
true ->
list_of_arbitrary_nested_maps
|> process_list_of_arbitrary_nested_maps()
|> Enum.map(fn {l, k} ->
%{
"#{k}" => traverse(l, &fun/1)
}
end)
|> Enum.reduce(&Map.merge/2)
end
end
end
1
polvalente
Nx Core Team
Maybe Nx.Defn.Composite.traverse?
1
Popular in Discussions
I’d love to hear what people think about Wisp, the new Gleam web framework started by Gleam’s primary creator Louis Pilfold.
Gleam, alon...
New
A big advantage to Elixir is all the distributed goodness but for many applications running on multiple nodes having integrated Etcd, Zoo...
New
I recently came across the javascript library htmx. It reminded me a lot of liveview so I thought the community here might be interested....
New
This is an interesting article to read. Elixir’s performance, like usual, is excellent. However, it seems like the high CPU usage is co...
New
Release date is May 2017. Can’t wait for it.
New
Elixir appears to be a superior language to Python. I don’t see any advantage of Python over Elixir. Are there any?
New
What configs will make sense to put to runtime.exs?
–
A bit of how I configure apps:
I have generic configs in config/config.exs,
dev...
New
I’m looking for a host for the server part of a small (personal) side project that I’m working on. It’s currently written in Node.js and ...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
I like Umbrella projects and pretty much always use them for personal Elixir stuff, especially Nerves things.
But I don’t think this is ...
New
Other popular topics
Hello!
tl;dr Announcing Oban, an Ecto based job processing library with a focus on reliability and historical observability.
After spen...
New
Hello all!
I am typing this post from my new MacBook Pro with the M1 chip. I’m loving it so far, and will probably use it as my daily dr...
New
Anybody knows a comprehensive comparison of Django and Phoenix, thanks for the help.
Where are they similar?
Where do they differ the m...
New
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
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
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
I am trying to run a deploy with docker and I successfully runned with this command:
docker build -t romenigld/blog-prod .
but when I t...
New
Currently suffering from paralysis by [HTTP client] analysis. This is rather unusual in Elixirland as there tends to be consensus on the ...
New
I wrote this comment on r/haskell, and it’s not popular there. :wink: But I think I’m on to something…
Haskell reminds me of Java, and e...
New
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
Categories:
Sub Categories:
Forums
Popular Tags
- #ecto
- #liveview
- #troubleshooting
- #learning-elixir
- #deployment
- #library
- #erlang
- #testing
- #genserver
- #mix
- #absinthe
- #remote-other
- #otp
- #plug
- #how-to-question
- #macros
- #postgres
- #channels
- #elixirconf
- #exunit
- #discussion
- #code-sync
- #javascript
- #podcasts
- #onsite
- #dialyzer
- #docker
- #authentication
- #umbrella
- #full-time-contract
- #podcasts-by-brainlid
- #ecto-query
- #elixir-ls
- #phoenix_html
- #iex
- #blog-post
- #graphql
- #genstage
- #ai
- #websockets
- #supervisor
- #advent-of-code
- #elixirconf-us
- #distillery
- #processes
- #forms
- #api
- #metaprogramming
- #security
- #performance








