Is there a simple way to implement Numpy-style ndarray slicing on Mojo Tensors ?

I have a rank 4 tensor where the first dimension is an index and the last 3 represent a Voxel Grid.
# Initialize the observation space as a 4D tensor
self.observation_space = Tensor[DType.int32](num_builds, build_volume[0], build_volume[1], build_volume[2])
# Initialize the observation space as a 4D tensor
self.observation_space = Tensor[DType.int32](num_builds, build_volume[0], build_volume[1], build_volume[2])
I want to get the voxel grid given a build_id. I wrote a function to do this but I am wondering if I there's a Mojo way to do this.
1 Reply
benny
benny8mo ago
Well it depends on your use case, if your okay with allocating more memory and copying over the elements, you could simply create a new DTypePointer, then memcpy Tensor.data() offset by the index you want to slice over. If you don’t want to allocate new memory and instead want to read directly from it, I would also recommend getting the pointer out from the Tensor then offsetting that and iterating over it.
Want results from more Discord servers?
Add your server