0

Consider the followoing example from np.meshgrid docs:

nx, ny = (3, 2)
x = np.linspace(0, 1, nx)
y = np.linspace(0, 1, ny)
xv, yv = np.meshgrid(x, y)

In my application, instead of x and y, I've 25 variables. To create a grid out of the 25 variables, one way would be:

v1 = np.linspace(0, 1, 10)
v2 = np.linspace(0, 1, 10)
...
v25 = np.linspace(0, 1, 10)

z_grid = np.meshgrid(v1, v2, ..., v25)

However, the code will look ugly and not modular w.r.t. the number of variables (since each variable is hard-coded). Therefore, I am interested in something like the following:

n_variables = 25
z = np.array([np.linspace(0, 1, 10)] * n_variables)
z_grid = np.dstack(np.meshgrid(z))

However, I am guessing meshgrid(z) is not the correct call, and I should expand z to n_variables arrays. Any thoughts on how I can expand the 2D array into multiple 1D arrays?

8
  • Are you sure you want a meshgrid that large? 10**25 is probably larger than your memory! Try something smaller, say with 3 variables, and make sure the result are what you need. Maybe you need to tell us what the bigger task is. Commented Jan 27, 2021 at 21:15
  • I need to walk in a 25 variable latent space; however, 25 is experimental, hence my method should be flexible on that. Commented Jan 27, 2021 at 21:38
  • What's a latent space? meshgrid creates n arrays, each (n1,n2,...) in shape. Beyond 3 variables memory uses gets out of hand. Commented Jan 27, 2021 at 21:42
  • Please see the example here, I would like to create a mesh grid of the size of the latent space of the VAE I'm creating. In other words, I'm trying to nodge the latent variables and see their impact on the decoder's output. Commented Jan 27, 2021 at 21:45
  • 1
    meshgrid(*alist_of_arrays) Commented Jan 27, 2021 at 21:52

1 Answer 1

0

this should do it.

n_variables = 25
z = np.array([np.linspace(0, 1, 10)] * n_variables)
z_grid = np.dstack(np.meshgrid(*z))

the * operator before list, unpacks list elements. consider following:

v1 = [1,2,3]
v2 = [4,5,6]
list_of_v = [v1,v2]
some_fucntion(v1,v2) == some_function(*list_ov_v)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.