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

MXPredReshape bug: need to reshape softmax_label #12176

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/c_api/c_predict_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,8 @@ int MXPredReshape(mx_uint num_input_nodes,
for (size_t i=0; i < arg_names.size(); ++i) {
TShape newShape = arg_shapes[i];
NDArray &arr = p->arg_arrays[i];
if (new_shape.count(arg_names[i]) != 0) {
if (new_shape.count(arg_names[i]) != 0 ||
strcmp("softmax_label", arg_names[i].c_str()) == 0) {
Copy link
Contributor

@apeforest apeforest Sep 28, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still trying to understand the problem. But this hack is not acceptable purely from coding practice.

ret->arg_arrays[i].ReshapeAndAlloc(newShape);
} else {
CHECK_EQ(newShape.Size(), arr.shape().Size())
Expand Down
34 changes: 34 additions & 0 deletions tests/python/unittest/test_predictor.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,40 @@ def test_load_ndarray():
for k in nd_data.keys():
assert_almost_equal(nd_data[k].asnumpy(), nd_load[k], rtol=1e-5, atol=1e-6)

def test_reshape_softmax_label():
prefix = 'test_predictor_reshape_softmax'
symbol_file = "%s-symbol.json" % prefix
param_file = "%s-0000.params" % prefix

# define and save simple net using softmax
net = mx.sym.Variable('data')
net = mx.sym.FullyConnected(net, name='fc1', num_hidden=5)
net = mx.sym.SoftmaxOutput(net, name='softmax')
net.save(symbol_file);

# init and save params
mod = mx.mod.Module(symbol=net, context=mx.cpu())
data_shape = [('data', (1, 3))]
mod.bind(data_shapes = data_shape,
for_training = False,
inputs_need_grad = False
)
mod.init_params()
mod.save_params(param_file)

# inputs with diffferent batch sizes
input1 = np.random.uniform(size=(3,3))
input2 = np.random.uniform(size=(1,3))

predictor = Predictor(open(symbol_file, "r").read(),
open(param_file, "rb").read(),
{'data':input1.shape})
predictor.forward(data=input1)
predictor_out1 = predictor.get_output(0)

predictor.reshape({'data':input2.shape})
predictor.forward(data=input2)
predictor_out2 = predictor.get_output(0)

if __name__ == '__main__':
import nose
Expand Down