This week we came up with a second idea for feature dispatching. The earlier approach was to use macro to generate function names based on train_dense, train_string calls. This is not very automated. Also, the macros are hard to debug since it can be difficult to extract useful information from macros. We realized the problem is solvable via mixins. Idea was to have each machine implement a templated version of train_machine.

This will be called by the mixin depending on feature types. However since it is not possible to have virtual methods that are templated, we found a workaround for our case. We will write the mixins as CRTP classes. CRTP or Curiously recursive template pattern is a C++ dev magic that relies on compiler lazyness to evaluate function calls. This, for our case, means that we can pass the class name as a parameter to its own base class !. As long as a method for subclass is not called the compiler need give an error over this. We can now call the templated train_machine from the base class without making it virtual. Only issue I see is that train_Machine_templated needs to be public now.

We will have a diffrent dispatcher for each feature class. The subclasses will inherit from the dispatcher which accepts 2 template arguments. One would be the subclass itself! This is the magic of CRTP. The second would be a base class like LinearMachine for example. We implemented this for dense features and used the dispatcher in CLeastAngleRegression and CLDA

The macro version of the solution had a merit that the feature class dispatching was done in CMachine. We will combine this idea with the mixin. We added a method train_dense which will be called if Dense features are provided and the machine supports dispatching. The idea remains same forString Features as well. train_dense is implemented in our CDenseRealDispatch mixin. It checks for feature type and calls train_machine_templated with appropriate methods. We also implemented String feature dispatching in CWDSVMOcas

We also wrote a feature_name method which can generate a std::string for various feature classes.

Contributions

Feature type dispatching through recursive mixin