具有依赖于可变模板的类型的类

Con*_*ate 11 c++ templates template-meta-programming variadic-templates c++11

我最近观看了一段视频,它激励我编写自己的神经网络系统,我希望网络中的节点数量可以调整.

起初我通过解析节点数量的数组在运行时实现了这一点,但我想知道我是否可以在编译时执行此操作.这是我希望完成的事情的一个例子.

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
};
Run Code Online (Sandbox Code Playgroud)

作为更详细的示例,Net<784, 16, 16, 10> n;n.m_weight应该具有类型

tuple<Eigen::Matrix<float, 784, 16>,
    Eigen::Matrix<float, 16, 16>,
    Eigen::Matrix<float, 16, 10>>
Run Code Online (Sandbox Code Playgroud)

根据我对C++和constexpr的了解,这应该是可能的.

我应该补充说我能够做到

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());
    }
};
Run Code Online (Sandbox Code Playgroud)

但后来我只是再次使用动态matricies,这不是我所希望的.

任何建议或工作实例将不胜感激.

Vit*_*meo 6

你需要某种给定的列表类型转换的N整数返回tupleN - 1矩阵.这是一个C++ 17解决方案:

template <int A, int B, int... Is>
auto make_matrix_tuple()
{   
    if constexpr(sizeof...(Is) == 0)
    {
        return std::tuple<Eigen::Matrix<float, A, B>>{};
    }
    else
    {
        return std::tuple_cat(make_matrix_tuple<A, B>(), 
                            make_matrix_tuple<B, Is...>());
    }
}
Run Code Online (Sandbox Code Playgroud)

wandbox上的实例


在C++ 11中,您可以递归地实现此类型转换:

template <int... Is>
struct matrix_tuple_helper;

template <int A, int B, int... Rest>
struct matrix_tuple_helper<A, B, Rest...>
{
    using curr_matrix = Eigen::Matrix<float, A, B>;
    using type = 
        decltype(
            std::tuple_cat(
                std::tuple<curr_matrix>{},
                typename matrix_tuple_helper<B, Rest...>::type{}
            )
        );
};

template <int A, int B>
struct matrix_tuple_helper<A, B>
{
    using curr_matrix = Eigen::Matrix<float, A, B>;
    using type = std::tuple<curr_matrix>;
};

template <int... Is>
using matrix_tuple = typename matrix_tuple_helper<Is...>::type;
Run Code Online (Sandbox Code Playgroud)

C++ 14方法:

struct matrix_tuple_maker
{
    template <int A, int B, int C, int... Is>
    static auto get()
    {
        return std::tuple_cat(get<A, B>(), get<B, C, Is...>());
    }

    template <int A, int B>
    static auto get()
    {
        return std::tuple<Eigen::Matrix<float, A, B>>{};
    }
};

static_assert(std::is_same_v<
    decltype(matrix_tuple_maker::get<784, 16, 16, 10>()),
    std::tuple<Eigen::Matrix<float, 784, 16>,
               Eigen::Matrix<float, 16, 16>,
               Eigen::Matrix<float, 16, 10>>
    >);
Run Code Online (Sandbox Code Playgroud)