-
-
Notifications
You must be signed in to change notification settings - Fork 95
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
masked_select: Filtering tensors with a boolean mask tensor #400
Comments
I have come up with this:
which seems reasonable. not sure if there's a simpler way. |
So what's happening is that Numpy accepts a Tensor of booleans as a mask for selecting things. Your solution works for your case because you don't need to discard/filter the values, however implementing filtering with a Tensor of bool would also be very useful for dataframes/analysis related stuff. For filtering, I see the following difficulties:
|
Thanks for considering it. For my additional 0.02... To that end, it'd be nice if the broadcasting was done auto-magically (as in numpy) as well. |
Broadcast is done auto-magically with the Arraymancer/src/tensor/operators_broadcasted.nim Lines 25 to 28 in 94efff3
|
I am also having trouble just using the map/apply stuff. for example, I can't understand how to change this (which either segfaults or runs endlessly): import arraymancer
var T = randomTensor[float32](250, 17384, 1'f32)
let m = T.map_inline():
if x < 0: 1'f32 else: 0'f32
echo m.shape
echo m
echo m.mean(axis=0) |
@brentp It's been a while since your last post, so maybe you've noticed this after posting at some point (or you encountered a real bug): I believe the reason you're seeing the code run endlessly, is simply that the tensor you create is huge and arraymancer's printing is pretty slow (and doesn't just cut off after a fixed N elements). |
Note that masked_select implementation is planned soon. |
Tentative implementation and names at #429 If you have suggestion on proc name and description to limit confusion especially for proc masked_axis_fill(t: var Tensor[T], mask: Tensor[bool], axis: int, value: T)
## Take a 1D-mask
## iterate on t along the axis and fill the slice of t with `value`
## if the mask[current_iteration_index] is true
proc masked_fill_along_axis(t: var Tensor[T], mask: Tensor[bool], axis: int, value: T)
## Take a N-D mask. Dimension along the axis must be 1
## iterate on t along the axis
## On the slice of t, apply masked_fill I'm taking them (let's have the name discussion in the PR) |
* index_select should use SomeInteger not SOmeNumber * Overload index_select for arrays and sequences * Masked Selector overload for openarrays * Add masked overload for regular arrays and sequences * Initial support of Numpy fancy indexing: index select * Fix broadcast operators from #429 using deprecated syntax * Stash dispatcher, working with types in macros is a minefield nim-lang/Nim#14021 * Masked indexing: closes #400, workaround nim-lang/Nim#14021 * Test for full masked fancy indexing * Add index_fill * Tensor mutation via fancy indexing * Add tests for index mutation via fancy indexing * Fancy indexing: supports broadcasting a value to a masked assignation * Detect wrong mask or tensor axis length * masked axis assign value test * Add masked assign of broadcastable tensor * Tag for changelog [skip ci]
given a 2d array/tensor, how would i do the arraymancer equivalent of this numpy expression:
I think it's probably some combination of the
map/apply/fold_inline
, but it's not obvious how to do that.Once I understand, I can open a PR with some common examples like this if it's helpful.
The text was updated successfully, but these errors were encountered: