samedi 28 octobre 2017

Class with types dependant on variadic templating

I recently watched a video which inspired me to write my own neural network system, and I wanted to have the amount of nodes in the network be adjustable.

At first I achieved this at runtime by parsing an array of the numbers of nodes but I was wondering if I could do this at compile time instead. Here's an example of the kind of thing I was hoping to accomplish.

template<int FirstNodes, int SecondNodes, int... OtherNodes>
class Net
{
    tuple<Eigen::Matrix<float, FirstNodes, SecondNodes>, ...> m_weights;
    // More matricies with the values from the OtherNodes
};

As a more detailed example, Net<784, 16, 16, 10> n; n.m_weight should have type

tuple<Eigen::Matrix<float, 784, 16>,
    Eigen::Matrix<float, 16, 16>,
    Eigen::Matrix<float, 16, 10>>

From what I know about C++ and constexpr, this should be possible.

I should add that I was able to do

template<int FirstNodes, int SecondNodes, int... OtherNodes>
class Net
{
public:
    Net()
    {
        auto nodes = {FirstNodes, SecondNodes, OtherNodes...};

        auto i = nodes.begin();
        do 
        {
            // Eigen::Matrix<float, Dynamic, Dynamic>
            Eigen::MatrixXf m(*(i++), *i);
        } while (i+1 != nodes.end());
    }
};

But then I'm just using dynamic matricies again and that isn't what I was hoping for.

Any advice or working examples would be greatly appreciated.

Aucun commentaire:

Enregistrer un commentaire