Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to get index range of subarray? #240

Open
vpuri3 opened this issue Jan 23, 2024 · 4 comments
Open

How to get index range of subarray? #240

vpuri3 opened this issue Jan 23, 2024 · 4 comments

Comments

@vpuri3
Copy link
Member

vpuri3 commented Jan 23, 2024

In the case below, how do I get the range of indices spanned by x.a?

x = (; a = ones(2), b = ones(2)) |> ComponentArray

idx_a = ?? # want 1:2
@vpuri3
Copy link
Member Author

vpuri3 commented Jan 24, 2024

@jonniedie is that possible? TIA

@jonniedie
Copy link
Collaborator

It's a little clunky, but you can do

only(getaxes(x))[:a].idx

@vpuri3
Copy link
Member Author

vpuri3 commented Mar 4, 2024

Thanks, that works. Is there a similar way to get the global indices of nested arrays? eg.

ca = (; a = ones(2), b = (; x = 2 * ones(2), y = 3 * ones(2))) |> ComponentArray
idx_b_x = getindices(ca.b.x) # want 3:4

@alicedb2
Copy link

alicedb2 commented Feb 7, 2025

This would be extremely useful for my current use-case involving neural networks with Lux and DiffEqFlux. Using ComponentArrays is very useful for manipulating the parameter vector of a whole NN which for k dense layers you'd have something like

p = (layer_1=(weight=[1 3; 2 4], bias=[5, 6]), layer_2=(weight=[7 9; 8 10], bias=[11, 12]), ..., layer_k=...)

Right now I would need to iterate over the layers in an arbitrary nn and change specific columns of the weight matrix of some given layer which satisfy some condition at runtime and would need to specify using something which is not an explicit p.layer_2.weight[:, 1] .= 0, or if I create some matrix of the form p * p' I'd like to be able to index a nested submatrix and do some operations on it.

Getting the global indices of an arbitrary nested subarray would solve this.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants