Skip to content
This repository was archived by the owner on Nov 17, 2023. It is now read-only.

tf.boolean_mask equivalent in MxNet #7443

Closed
ufukcbicici opened this issue Aug 13, 2017 · 11 comments
Closed

tf.boolean_mask equivalent in MxNet #7443

ufukcbicici opened this issue Aug 13, 2017 · 11 comments

Comments

@ufukcbicici
Copy link

ufukcbicici commented Aug 13, 2017

In my architecture, I want to pick same of the samples in a minibatch at different stages of computation, based on a binary vector produced elsewhere in the network. In Tensorflow, tf.boolean_mask is generally used for that purpose. How can I achieve the similar effect in MxNet, is it possible?

@ufukcbicici
Copy link
Author

ufukcbicici commented Aug 15, 2017

To refine the question a bit more; is it possible in MxNet, ideally by using the Symbol API, to select a subset of the samples in a minibatch, based on a binary condition vector? For example given a tensor of the shape (A,B,C,D) and a binary vector of the shape (A,),I want to produce a new tensor of the shape (a,B,C,D), where a<=A and the selected samples correspond to the True entries of the binary vector and False entries are rejected.

@chinakook
Copy link
Contributor

mx.nd.pick

@ufukcbicici
Copy link
Author

According to the documentation, mx.symbol.pick chooses the given indices on a given axis. For example:

`x = [[ 1., 2.],
[ 3., 4.],
[ 5., 6.]]

// picks elements with specified indices along axis 0
pick(x, y=[0,1], 0) = [ 1., 4.]`

In my case, I don't have the knowledge of the indices in y. I only know y as a boolean vector, entires with y=[True,False,True], etc. Will pick work in this case as well?

@chinakook
Copy link
Contributor

x = mx.nd.array([1,2,3,4,5])
y = mx.nd.array([5,4,3,2,1])
mx.nd.where(x < y, x, y)

output: ...

[ 1. 2. 3. 2. 1.]
<NDArray 5 @cpu(0)>

@ufukcbicici
Copy link
Author

Is there a way to filter out the elements in mx.nd.where which do not satisfy the condition given in the first argument? I mean:

x = mx.nd.array([1,2,3,4,5])
y = mx.nd.array([5,4,3,2,1])
mx.nd.where(x < y, x, y)

Will give [1. 2.] as the output, since only the first two elements satisfy x < y

@chinakook
Copy link
Contributor

Actually, you can transform the nd to np.array with .asnumpy(), and then use numpy to do that. Slicing is not mature in mxnet.

@ufukcbicici
Copy link
Author

That is the problem; I am using a lot of glue code to implement this conditional behavior for minibatches. I keep the track of the sample indices, select ones which satisfy a certain condition and route them into separate network subsets. I am using .asnumpy() conversion and use multiple sub-networks and executors for this purpose. This costs me nearly a ten folds slower execution compared to baselines. Any chance that slicing will be improved in the near future?

@chinakook
Copy link
Contributor

You can use Anaconda Python to accelerate the calculation of numpy as it is compiled with the Intel MKL. You may got less performance loss.

@szha
Copy link
Member

szha commented Nov 22, 2017

This issue is closed due to lack of activity in the last 90 days. Feel free to ping me to reopen if this is still an active issue. Thanks!
Also, do please check out our forum (and Chinese version) for general "how-to" questions.

@szha szha closed this as completed Nov 22, 2017
@ElectronicElephant
Copy link

More than two years later, it seems like MXNet still doesn't support Boolean Index?

@leezu
Copy link
Contributor

leezu commented Jan 31, 2020

As part of #14253 there is support / is in progress

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

No branches or pull requests

5 participants