Skip to content
This repository has been archived by the owner on Jul 1, 2024. It is now read-only.

Bug in K.equal operator #146

Closed
roywei opened this issue Jul 27, 2018 · 3 comments
Closed

Bug in K.equal operator #146

roywei opened this issue Jul 27, 2018 · 3 comments
Labels

Comments

@roywei
Copy link

roywei commented Jul 27, 2018

The current K.equal(x, y) operator can not handle the situation when x and y have different shape with MXNet backend.

Reproducible code:

import numpy as np
from keras import backend as K

x = K.variable(np.array([[1,2],[2,3]]))
y = K.variable(np.array([[1],[3]]))
print(K.eval(K.equal(x, y)))

with TensorFlow backend output is:

[[ True False]
 [False  True]]

with MXNet backend, error msg:

Using MXNet backend
/Users/lawei/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "/Users/lawei/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1522, in simple_bind
    ctypes.byref(exe_handle)))
  File "/Users/lawei/anaconda3/lib/python3.6/site-packages/mxnet/base.py", line 235, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: Error in operator _equal0: [21:31:53] src/operator/contrib/../tensor/../elemwise_op_common.h:133: Check failed: assign(&dattr, (*vec)[i]) Incompatible attr in node _equal0 at 1-th input: expected [2,2], got [2,1]

Stack trace returned 10 entries:
[bt] (0) 0   libmxnet.so                         0x000000010ed336b4 libmxnet.so + 18100
[bt] (1) 1   libmxnet.so                         0x000000010ed3346f libmxnet.so + 17519
[bt] (2) 2   libmxnet.so                         0x000000010ed330e9 libmxnet.so + 16617
[bt] (3) 3   libmxnet.so                         0x000000010ee37245 libmxnet.so + 1081925
[bt] (4) 4   libmxnet.so                         0x000000010ee36ee3 libmxnet.so + 1081059
[bt] (5) 5   libmxnet.so                         0x000000010ef93a5b libmxnet.so + 2509403
[bt] (6) 6   libmxnet.so                         0x000000010fe956da MXNDListFree + 326538
[bt] (7) 7   libmxnet.so                         0x000000010fe8ddec MXNDListFree + 295580
[bt] (8) 8   libmxnet.so                         0x000000010fe80836 MXNDListFree + 240870
[bt] (9) 9   libmxnet.so                         0x000000010fe863aa MXNDListFree + 264282



During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/lawei/Documents/Workspace/roywei/keras/examples/test.py", line 6, in <module>
    print(K.eval(K.equal(x, y)))
  File "/Users/lawei/Documents/Workspace/roywei/keras/keras/backend/mxnet_backend.py", line 554, in eval
    executor = x.symbol.simple_bind(mx.cpu(), grad_req='null')
  File "/Users/lawei/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1528, in simple_bind
    raise RuntimeError(error_msg)
RuntimeError: simple_bind error. Arguments:
Error in operator _equal0: [21:31:53] src/operator/contrib/../tensor/../elemwise_op_common.h:133: Check failed: assign(&dattr, (*vec)[i]) Incompatible attr in node _equal0 at 1-th input: expected [2,2], got [2,1]

Stack trace returned 10 entries:
[bt] (0) 0   libmxnet.so                         0x000000010ed336b4 libmxnet.so + 18100
[bt] (1) 1   libmxnet.so                         0x000000010ed3346f libmxnet.so + 17519
[bt] (2) 2   libmxnet.so                         0x000000010ed330e9 libmxnet.so + 16617
[bt] (3) 3   libmxnet.so                         0x000000010ee37245 libmxnet.so + 1081925
[bt] (4) 4   libmxnet.so                         0x000000010ee36ee3 libmxnet.so + 1081059
[bt] (5) 5   libmxnet.so                         0x000000010ef93a5b libmxnet.so + 2509403
[bt] (6) 6   libmxnet.so                         0x000000010fe956da MXNDListFree + 326538
[bt] (7) 7   libmxnet.so                         0x000000010fe8ddec MXNDListFree + 295580
[bt] (8) 8   libmxnet.so                         0x000000010fe80836 MXNDListFree + 240870
[bt] (9) 9   libmxnet.so                         0x000000010fe863aa MXNDListFree + 264282

however, pure mxnet ndarray and symbol works fine:

x = mx.sym.Variable('x')
y = mx.sym.Variable('y')
result = mx.sym.broadcast_equal(x, y)
e = result.bind(mx.cpu(), {'x': mx.nd.array([[1,2],[2,3]]), 'y':mx.nd.array([[1],[3]])})
print(e.forward())

produce

[
[[1. 0.]
 [0. 1.]]
<NDArray 2x2 @cpu(0)>]
@roywei roywei added the bug label Jul 27, 2018
@roywei
Copy link
Author

roywei commented Jul 27, 2018

Root Cause: we should use mx.sym.broadcast_equal() when x and y have different shape.

Will fix together in pr #145

Reproducible code in mxnet:

import mxnet as mx
x = mx.sym.Variable('x')
y = mx.sym.Variable('y')
#result = mx.sym.broadcast_equal(x, y)
result = x==y
e = result.bind(mx.cpu(), {'x': mx.nd.array([[1,2],[2,3]]), 'y':mx.nd.array([[1],[2]])})
print(e.forward())

Error Msg:

Using MXNet backend
/Users/lawei/anaconda3/lib/python3.6/site-packages/h5py/__init__.py:36: FutureWarning: Conversion of the second argument of issubdtype from `float` to `np.floating` is deprecated. In future, it will be treated as `np.float64 == np.dtype(float).type`.
  from ._conv import register_converters as _register_converters
Traceback (most recent call last):
  File "/Users/lawei/Documents/Workspace/roywei/keras/examples/test.py", line 17, in <module>
    e = result.bind(mx.cpu(), {'x': mx.nd.array([[1,2],[2,3]]), 'y':mx.nd.array([[1],[2]])})
  File "/Users/lawei/anaconda3/lib/python3.6/site-packages/mxnet/symbol/symbol.py", line 1703, in bind
    ctypes.byref(handle)))
  File "/Users/lawei/anaconda3/lib/python3.6/site-packages/mxnet/base.py", line 235, in check_call
    raise MXNetError(py_str(_LIB.MXGetLastError()))
mxnet.base.MXNetError: Error in operator _equal0: [00:22:46] src/operator/contrib/../tensor/../elemwise_op_common.h:133: Check failed: assign(&dattr, (*vec)[i]) Incompatible attr in node _equal0 at 1-th input: expected [2,2], got [2,1]

Stack trace returned 10 entries:
[bt] (0) 0   libmxnet.so                         0x0000000105be56b4 libmxnet.so + 18100
[bt] (1) 1   libmxnet.so                         0x0000000105be546f libmxnet.so + 17519
[bt] (2) 2   libmxnet.so                         0x0000000105be50e9 libmxnet.so + 16617
[bt] (3) 3   libmxnet.so                         0x0000000105ce9245 libmxnet.so + 1081925
[bt] (4) 4   libmxnet.so                         0x0000000105ce8ee3 libmxnet.so + 1081059
[bt] (5) 5   libmxnet.so                         0x0000000105e45a5b libmxnet.so + 2509403
[bt] (6) 6   libmxnet.so                         0x0000000106d476da MXNDListFree + 326538
[bt] (7) 7   libmxnet.so                         0x0000000106d3fdec MXNDListFree + 295580
[bt] (8) 8   libmxnet.so                         0x0000000106d22d60 MXNDListFree + 176656
[bt] (9) 9   libmxnet.so                         0x0000000106d3858d MXNDListFree + 264765

@roywei
Copy link
Author

roywei commented Jul 27, 2018

same applies to other element wise operators

  • not_equal
  • greater
  • greater_equal
  • less
  • less_equal

@roywei
Copy link
Author

roywei commented Aug 20, 2018

closing as fixed in #145

@roywei roywei closed this as completed Aug 20, 2018
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

No branches or pull requests

1 participant