如何使用Boost:serialization保存Eigen :: Matrix [英] how to use Boost:serialization to save Eigen::Matrix

查看:351
本文介绍了如何使用Boost:serialization保存Eigen :: Matrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个实现libeigen2的代码来计算特征向量。现在,我想使用boost :: serialization保存信息以供以后检索。从示例教程来看,

Hello I have a code which implements libeigen2 to calculate eigen vectors. Now I want to use boost::serialization to save the information for retrieving later. From the example tutorial I came up with the following code!

class RandomNode {
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
   ar & is_leaf_;
   ar & depth_;
   ar & num_classes_;
   ar & num_features_;
   // Split node members
   ar & random_feature_indices_;
   ar & random_feature_weights_;
   ar & threshold_;
   ar & leftChild_;
   ar & rightChild_;

 }
bool is_leaf_;
int depth_;
int num_classes_;
int num_features_;

// Split node members
VectorXi random_feature_indices_;
VectorXd random_feature_weights_;
double threshold_;
RandomNode* leftChild_;
RandomNode* rightChild_;
 // Methods and so on
}

现在当我尝试运行时这段代码我得到以下错误

Now when i tries to run this code I get the following error

/usr/include/boost/serialization/access.hpp:118:9: error: ‘class Eigen::Matrix<double, 10000, 1>’ has no member named ‘serialize’

如何序列化Eigen :: Matrix类?可能吗 ?
预先感谢。

How can I serialize the Eigen::Matrix class ? Is it possible ? Thanks in advance.

推荐答案

您应该阅读关于可序列化概念。基本上说类型必须是原始的或可序列化的。 Eigen类型与之无关,您的编译器试图告诉您。为了使本征类型可序列化,您将需要实现以下免费功能

You should read the boost::serialization documentation on the subject of serializable concept. It basically says that the types needs to be primitive or Serializable. The Eigen type is none of it, which your compiler is trying to tell you. In order to make Eigen types serializable you will need to implement the following free function

template<class Archive>
inline void serialize(
    Archive & ar, 
    my_class & t, 
    const unsigned int file_version
) {
    ...
}

为了为Eigen做到这一点,我想您可能会这样做b模板

In order to do it for Eigen, I guess you might do something like this template

这是一个适合您的示例实现:

Here is an example implementation that should work for you:

#include <fstream>
#include <Eigen/Core>
#include <boost/archive/text_oarchive.hpp>

using namespace Eigen;

struct RandomNode {
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
   ar & random_feature_indices_;
   ar & random_feature_weights_;
}
// Split node members
VectorXi random_feature_indices_;
VectorXd random_feature_weights_;
};

namespace boost
{
template<class Archive, typename _Scalar, int _Rows, int _Cols, int _Options, int _MaxRows, int _MaxCols>
inline void serialize(
    Archive & ar, 
    Matrix<_Scalar, _Rows, _Cols, _Options, _MaxRows, _MaxCols> & t, 
    const unsigned int file_version
) 
{
    size_t rows = t.rows(), cols = t.cols();
    ar & rows;
    ar & cols;
    if( rows * cols != t.size() )
    t.resize( rows, cols );

    for(size_t i=0; i<t.size(); i++)
    ar & t.data()[i];
}
}

int main()
{
    // create and open a character archive for output
    std::ofstream ofs("filename");

    RandomNode r;
    r.random_feature_indices_.resize(3,1);

    // save data to archive
    {
        boost::archive::text_oarchive oa(ofs);
        // write class instance to archive
        oa << r;
        // archive and stream closed when destructors are called
    }
}

这篇关于如何使用Boost:serialization保存Eigen :: Matrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆