mxnet icon indicating copy to clipboard operation
mxnet copied to clipboard

unknown output shape before inference

Open cccorn opened this issue 8 years ago • 5 comments

I want to write a custom operation in python, the output shape is unknown before running 'forward()'. So how to write the 'infer_shape()' function? It seems I must infer the output shape from the input shape, but without the input data.

cccorn avatar Feb 10 '18 02:02 cccorn

Yes, during the phase of infer_shape we cannot access the input data. After infer_shape we can run the computation graph with input data.

If your operator's output has a maximum shape, I think there is a work around with gluon. infer_shape of your operator can return two shapes, the predefined maximum shape and a one dimension shape holding the actual size of your output,

output, size = custom_op(...)
actual_size = size.asscalar()
output = output.reshape(-1)[ : actual_size]

ZiyueHuang avatar Feb 10 '18 14:02 ZiyueHuang

Here is a relevant issue with dynamic shape, https://github.com/apache/incubator-mxnet/issues/8822.

ZiyueHuang avatar Feb 10 '18 14:02 ZiyueHuang

@ZiyueHuang Thank you for your response. There is a maximum shape of my operator's output. Did you mean the output and actual size output, size are two output symbols of my operator? If it is, I got an error from asscalar(): NotImplementedForSymbol: Function asscalar is not implemented for Symbol and only available in NDArray. So is there any way to deal with the symbolic operator?

cccorn avatar Feb 14 '18 03:02 cccorn

@cccorn - Asking usability questions at discuss - https://discuss.mxnet.io/ will yield faster response.

sandeep-krishnamurthy avatar Feb 15 '18 00:02 sandeep-krishnamurthy

@sandeep-krishnamurthy : Tag it to label "question" and can be closed.

vdantu avatar Feb 27 '18 21:02 vdantu