Search code examples
c++modelcntk

Difference between Function::Forward and Trainer::TrainMinibatch in CNTK C++ API


I am trying to reverse engineer C++ API for CNTK (since there is no official documentations) from CNTKLibrary.h

I made a RNN network (mostly by following the unit tests source codes) and it seems working (at least no compile or runtime error for now!!!)

In the unit test source code I see they used Function::Forward and Function::Backward to compute "trainingLoss" and "predection", but also by following Python examples I found I can make "Trainer" with associated learner optimizer (FSAdaFradLearner), then call "TrainMinibatch" on that poinetr.

Now my question is, which one is the correct way to train a model?


Solution

  • There are different abstraction levels to achieve the same thing.

    Trainer is a wrapper/convenience class that remembers a few things for you, such as the root node and the learner, and provides the convenience function TrainMinibatch(), which is effectively the sequence of Forward(), Backward(), and learner.Update().

    The C++ API is very similar to the Python API. In fact, most of the Python API functions and classes are just wrappers around the corresponding C++ functions and classes. So while method signatures do not match 100% due to language and type-system differences, any logic described in the Python docs should apply directly to C++.

    In the typical use cases, calling TrainMinibatch() will end up here: [https://github.com/Microsoft/CNTK/blob/94e6582d2f63ce3bb048b9da01679abeacda877f/Source/CNTKv2LibraryDll/Trainer.cpp#L193

    It calls ExecuteForwardBackward(), which calls Forward() and Backward(). You can use it as an example of how to call those functions.