Mx.nd.broadcast_equal and mx.sym.broadcast_equal?


#1

import mxnet as mx
keep_inds = mx.nd.random.uniform(0,2,shape=(20,20))
track_id_bef = mx.nd.random.uniform(0,10, shape=(20))
track_id_cur = mx.nd.random.uniform(0,10, shape=(20))
track_id_bef = track_id_bef.reshape(-1, 1)
track_id_cur = track_id_cur.reshape(-1, 1)
track_matrix = mx.nd.broadcast_equal(track_id_bef, track_id_cur.transpose())
zeros = mx.nd.zeros(shape=(20,20)) - 1
track_matrix2=mx.nd.where(keep_inds, track_matrix, zeros, name=‘track_matrix’)
print keep_inds.shape
print track_matrix.shape
print track_matrix2.shape
(20L, 20L)
(20L, 20L)
(20L, 20L)

However, when I change the code to mx.sym version.

import mxnet as mx
keep_inds = mx.sym.random.uniform(0,2,shape=(20,20))
track_id_bef = mx.sym.random.uniform(0,10, shape=(20))
track_id_cur = mx.sym.random.uniform(0,10, shape=(20))
track_id_bef = track_id_bef.reshape(-1, 1)
track_id_cur = track_id_cur.reshape(-1, 1)
track_matrix = mx.sym.broadcast_equal(track_id_bef, track_id_cur.transpose())
zeros = mx.sym.zeros(shape=(20,20)) - 1
track_matrix2 = mx.sym.where(keep_inds, track_matrix, zeros, name=‘track_matrix’)
print keep_inds.infer_shape()
print track_matrix.infer_shape()
print track_matrix2.infer_shape()
([], [(20L, 20L)], [])
([], [(20L,)], [])
(None, None, None)

A completely different result, why???
SOS!!!


#2

Hi @jiujing23333, please set the shape in a tuple for the reshape operation

track_id_bef = track_id_bef.reshape((-1, 1))
track_id_cur = track_id_cur.reshape((-1, 1))

This gives you:

import mxnet as mx
keep_inds = mx.nd.random.uniform(0,2,shape=(20,20))
track_id_bef = mx.nd.random.uniform(0,10, shape=(20))
track_id_cur = mx.nd.random.uniform(0,10, shape=(20))
track_id_bef = track_id_bef.reshape((-1, 1))
track_id_cur = track_id_cur.reshape((-1, 1))
track_matrix = mx.nd.broadcast_equal(track_id_bef, track_id_cur.transpose())
​
zeros = mx.nd.zeros(shape=(20,20)) - 1
track_matrix2=mx.nd.where(keep_inds, track_matrix, zeros, name='track_matrix')
print(keep_inds.shape)
print(track_matrix.shape)
print(track_matrix2.shape)
(20, 20)
(20, 20)
(20, 20)
import mxnet as mx
keep_inds = mx.sym.random.uniform(0,2,shape=(20,20))
track_id_bef = mx.sym.random.uniform(0,10, shape=(20))
track_id_cur = mx.sym.random.uniform(0,10, shape=(20))
track_id_bef = track_id_bef.reshape((-1, 1))
track_id_cur = track_id_cur.reshape((-1, 1))
track_matrix = mx.sym.broadcast_equal(track_id_bef, track_id_cur.transpose())
zeros = mx.sym.zeros(shape=(20,20)) - 1
track_matrix2 = mx.sym.where(keep_inds, track_matrix, zeros, name='track_matrix')
print(keep_inds.infer_shape())
print(track_matrix.infer_shape())
print(track_matrix2.infer_shape())
([], [(20, 20)], [])
([], [(20, 20)], [])
([], [(20, 20)], [])