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

[Numpy] Failed for Boolean type #18068

Closed
zheyuye opened this issue Apr 15, 2020 · 6 comments
Closed

[Numpy] Failed for Boolean type #18068

zheyuye opened this issue Apr 15, 2020 · 6 comments

Comments

@zheyuye
Copy link
Contributor

zheyuye commented Apr 15, 2020

Description

Many operations are unreliable in numpy interface but works well in mx.nd.ndarray, such as an example showcased in the reproducible code snippet. The reasons for the above problems can be boiled down to the incompatibility of Boolean type which is the return type of mx.np.equal'. Unlike the mx.np.equaloperation,mx.nd.equal` takes float32 as the resulting data type and works fine in this case.

To Reproduce

import mxnet as mx
inputs = mx.np.array([1,1,1])
fake_data = mx.np.array([1,1,0])
1 - mx.np.equal(fake_data, inputs)

inputs = mx.nd.array([1,1,1])
fake_data = mx.nd.array([1,1,0])
1 - mx.nd.equal(fake_data, inputs)

Error Message

MXNetError                                Traceback (most recent call last)
/usr/local/lib/python3.6/dist-packages/IPython/core/formatters.py in __call__(self, obj)
    700                 type_pprinters=self.type_printers,
    701                 deferred_pprinters=self.deferred_printers)
--> 702             printer.pretty(obj)
    703             printer.flush()
    704             return stream.getvalue()

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in pretty(self, obj)
    400                         if cls is not object \
    401                                 and callable(cls.__dict__.get('__repr__')):
--> 402                             return _repr_pprint(obj, self, cycle)
    403 
    404             return _default_pprint(obj, self, cycle)

/usr/local/lib/python3.6/dist-packages/IPython/lib/pretty.py in _repr_pprint(obj, p, cycle)
    695     """A pprint that just redirects to the normal repr function."""
    696     # Find newlines and replace them with p.break_()
--> 697     output = repr(obj)
    698     for idx,output_line in enumerate(output.splitlines()):
    699         if idx:

~/mxnet/python/mxnet/numpy/multiarray.py in __repr__(self)
   1163          [0.84426576 0.60276335 0.85794562]] @gpu(0)
   1164         """
-> 1165         array_str = self.asnumpy().__repr__()
   1166         dtype = self.dtype
   1167         if 'dtype=' in array_str:

~/mxnet/python/mxnet/ndarray/ndarray.py in asnumpy(self)
   2564             self.handle,
   2565             data.ctypes.data_as(ctypes.c_void_p),
-> 2566             ctypes.c_size_t(data.size)))
   2567         return data
   2568 

~/mxnet/python/mxnet/base.py in check_call(ret)
    244     """
    245     if ret != 0:
--> 246         raise get_last_ffi_error()
    247 
    248 

MXNetError: Traceback (most recent call last):
  File "../src/operator/numpy/./../tensor/elemwise_binary_scalar_op.h", line 244
MXNetError: Unknown type enum 7

Comments

@sxjscience

@zheyuye
Copy link
Contributor Author

zheyuye commented Apr 15, 2020

import mxnet as mx
inputs = mx.np.array([1,1,1])
fake_data = mx.np.array([1,1,0])
labels = mx.np.ones_like(inputs) - mx.np.equal(fake_data, inputs)

It works well.

@sxjscience
Copy link
Member

We should enhance the support of boolean type. The current problem is that operators like np.equal(a, b), a == b will return a array with boolean type. However, this array will usually be updated with operations like 1 - mask.

@sxjscience
Copy link
Member

@yzhliu
Copy link
Member

yzhliu commented Apr 29, 2020

Assignee: @cassinixu

@xidulu
Copy link
Contributor

xidulu commented May 15, 2020

Thanks for your issue,
This is indeed a very interesting use case.
I noticed that this problem also occurred in PyTorch:

>>> inputs = torch.tensor([1,1,1])
>>> fake_data = torch.tensor([1,1,0])
>>> 1 - (inputs == fake_data)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/ubuntu/anaconda3/lib/python3.7/site-packages/torch/tensor.py", line 394, in __rsub__
    return _C._VariableFunctions.rsub(self, other)
RuntimeError: Subtraction, the `-` operator, with a bool tensor is not supported. If you are trying to invert a mask, use the `~` or `logical_not()` operator instead.
>>>

If we are capable of providing a solution to this case, that would be a huge advantage over PyTorch.

@yzhliu
Copy link
Member

yzhliu commented May 22, 2020

closed by #18277

@yzhliu yzhliu closed this as completed May 22, 2020
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

4 participants