Use the function numpy.ix_
to slice a block multi-array from a multi-array numpy.ndarray
or torch.Tensor
in Python.
In julia
# Julia
#Make multiple arrays
(i, j, k) = (2, 3, 4)
x = reshape(1:i * j * k, i, j, k)
#Slice multiple arrays with multiple subscripts
(is, js, ks) = ([2, 1], [1, 3], 2:3)
x[is, js, ks] |> println
I found it difficult to find information on how to slice multiple arrays with multiple subscripts in Python, so make a note. numpy.ix_
is what you want. Note that NumPy and PyTorch default to Julia's Array in the opposite way of arranging the dimensions of a multi-array.
# Python + NumPy
import numpy
#Make multiple arrays
i, j, k = 2, 3, 4
x = (numpy.arange(k * j * i) + 1).reshape(k, j, i)
#Slice multiple arrays with multiple subscripts
# (In Python, is is like a reserved word, so is_To)
is_, js, ks = numpy.array([2, 1]) - 1, numpy.array([1, 3]) - 1, numpy.arange(2 - 1, 3)
print(x[numpy.ix_(ks, js, is_)])
# Python + PyTorch
import torch
import numpy
#Make multiple arrays
i, j, k = 2, 3, 4
x = (torch.arange(k * j * i) + 1).reshape(k, j, i)
#Slice multiple arrays with multiple subscripts
# (In Python, is is like a reserved word, so is_To)
is_, js, ks = torch.tensor([2, 1]) - 1, torch.tensor([1, 3]) - 1, torch.arange(2 - 1, 3)
print(x[numpy.ix_(ks, js, is_)])
Recommended Posts