Is there a simple function to predict results?


I used Module.predict() before I use Gluon. But now when I migrate everything to Gluon I can’t find a simple API to do prediction.
How do you make prediction with Gluon package?


I use the following for prediction with Gluon: Say you have defined a model mynet somehow, that takes as input some datum, say data. Predictions takes place - on a single batch - with:

with autograd.predict_mode():
    predictions = mynet(data)

this takes care of layers that have different behaviour in train/predict mode, although by default

predictions = mynet(data)

is on predict mode. You need a for loop if you want prediction for a set of points larger than a single batch.

Hope this helps.


Thanks. I currently used similar method too. I didn’t notice this autograd.predict_mode(). I used something like:

for data in data_iter:
    predictions = mynet(data)

But wouldn’t it be cumbersome?


Personally, I prefer the for loop solution instead of just having a predict_on_whole_data solution. The reason is that my models/data are almost never small enough so I can fit everything inside memory easily, so I need to write custom data generators etc. From that point and on, it is trivial to write a function that will include the data generator in the for loop.

This is just personal opinion and I am not involved in any way in mxnet development. Just a (very happy) user.

I am using autograd.predict_mode() for clear code reading (for other people) and peace of mind, just in case I missed something from the documentation.