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?
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.
mx.nd.pick
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?
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.]
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
Actually, you can transform the nd to np.array with .asnumpy(), and then use numpy to do that. Slicing is not mature in mxnet.
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?
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.
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.
More than two years later, it seems like MXNet still doesn't support Boolean Index?
As part of https://github.com/apache/incubator-mxnet/issues/14253 there is support / is in progress
Most helpful comment
According to the documentation,
mx.symbol.pickchooses 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 knowyas a boolean vector, entires withy=[True,False,True], etc. Willpickwork in this case as well?