首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >两个boost::accumulators::accumulator_set相互干扰

两个boost::accumulators::accumulator_set相互干扰
EN

Stack Overflow用户
提问于 2014-04-03 17:40:38
回答 1查看 1.1K关注 0票数 0

我在我的代码中创建了一个类我的代码,作为Boost 1.54中boost::accumulators::accumulator_set的包装器。对我的问题来说,看起来很重要的是Histogram.hpp文件中的那些行:

代码语言:javascript
复制
using namespace boost::accumulators;

class Histogram {
    public:
        Histogram(int bins, size_t cache);
        accumulator_set<double,
                        features<tag::min, tag::max, tag::mean, tag::density>> acc;
};

然后在Histogram.cpp中我有一个构造函数:

代码语言:javascript
复制
Histogram::Histogram(int bins, size_t cache)
    : acc(accumulator_set<double,
          features<tag::min, tag::max, tag::mean, tag::density>>(
              tag::density::num_bins = bins,
              tag::density::cache_size = std::min(cache, MAX_CACHE_ENTRIES))) {
}

使用此直方图的代码(do_iterations() in main-metropolis.cpp)以以下代码开头:

代码语言:javascript
复制
Histogram position_histogram{settings.position_hist_bins, settings.time_sites * settings.iterations};
//Histogram action_histogram{settings.action_hist_bins, settings.iterations};

它的工作方式和我预期的一样,当我运行它时,第二行被停用。我的模拟生成了一些数据点,并将其放入Histogram::acc,然后让我提取出来:

代码语言:javascript
复制
-2.86958    0
-2.37393    0.0002
-1.87829    0.0071
-1.38265    0.06621
-0.887001   0.23902
-0.391356   0.33247
0.104288    0.2342
0.599932    0.08449
1.09558 0.02843
1.59122 0.00775
2.08687 0.00012
2.58251 1e-05
# Min -2.37393
# Max 2.58251
# Mean -0.0809983

然后我激活了行,position_histogram以一种非常奇怪的方式工作。所有的回收箱都是零,但是数据被分发到第一个和最后一个回收站的溢出桶中:

代码语言:javascript
复制
0   0.57785
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0
0   0.42215
# Min -2.37393
# Max 2.58251
# Mean -0.0809983

如果我交换行,action_histogram就会中断。所以第二个总是打破第一个。为什么第二个Histogram的初始化和第二个accumulator_set的初始化会导致第一个accumulator_set中断?

在浏览d3081a1ef7时请使用修订版代码,因为我现在已经构建了自己的直方图实现,以便继续工作。

EN

回答 1

Stack Overflow用户

发布于 2014-04-03 20:56:59

您将不得不调试这个或提供更多信息。

在我的概念的研究证明中,我使用过累加器,并且总是同时使用多个实例,而且我没有遇到这种情况。然后我意识到我从来没有并行做过density直方图,所以我对它进行了测试。

它会根据您的声明在我的测试中完成,请参阅住在Coliru

代码语言:javascript
复制
#include <boost/accumulators/statistics.hpp>
#include <boost/accumulators/accumulators.hpp>
#include <boost/random.hpp>
#include <boost/bind.hpp>

using namespace boost::accumulators;

static const size_t MAX_CACHE_ENTRIES = 32;

class Histogram {
    public:
        Histogram(int bins, size_t cache)
            : acc(accumulator_set<double,
                    features<tag::min, tag::max, tag::mean, tag::density>>(
                        tag::density::num_bins = bins,
                        tag::density::cache_size = std::min(cache, MAX_CACHE_ENTRIES))) {
            }        

        accumulator_set<double,
                        features<tag::min, tag::max, tag::mean, tag::density>> acc;
};

int main()
{
    Histogram position_histogram { 10, 32 };
    Histogram action_histogram   { 10, 32 };

    auto random = boost::bind(boost::uniform_real<double>(-100,100), boost::mt19937(42));

    size_t samples = 1<<20;
    while (samples--)
    {
        auto v = random();
        position_histogram.acc(v);
        action_histogram.acc(v);
    }

    for (auto& acc : { position_histogram.acc, action_histogram.acc })
    {
        auto hist = density(acc);

        double total = 0.0;

        for( int i = 0; i < hist.size(); i++ ) 
        {
            std::cout << "Bin lower bound: " << hist[i].first << ", Value: " << hist[i].second << std::endl; 
            total += hist[i].second;
        }

        std::cout << "Total: " << total << std::endl; //should be 1 (and it is)
    }
}

产出,如预期:

代码语言:javascript
复制
Bin lower bound: -119.673, Value: 0.000766754
Bin lower bound: -99.8442, Value: 0.099205
Bin lower bound: -80.0156, Value: 0.0987797
Bin lower bound: -60.1869, Value: 0.0990477
Bin lower bound: -40.3583, Value: 0.0991993
Bin lower bound: -20.5296, Value: 0.0989904
Bin lower bound: -0.700967, Value: 0.0993652
Bin lower bound: 19.1277, Value: 0.0993567
Bin lower bound: 38.9563, Value: 0.0993252
Bin lower bound: 58.785, Value: 0.0993109
Bin lower bound: 78.6137, Value: 0.0989342
Bin lower bound: 98.4423, Value: 0.00771904
Total: 1
Bin lower bound: -119.673, Value: 0.000766754
Bin lower bound: -99.8442, Value: 0.099205
Bin lower bound: -80.0156, Value: 0.0987797
Bin lower bound: -60.1869, Value: 0.0990477
Bin lower bound: -40.3583, Value: 0.0991993
Bin lower bound: -20.5296, Value: 0.0989904
Bin lower bound: -0.700967, Value: 0.0993652
Bin lower bound: 19.1277, Value: 0.0993567
Bin lower bound: 38.9563, Value: 0.0993252
Bin lower bound: 58.785, Value: 0.0993109
Bin lower bound: 78.6137, Value: 0.0989342
Bin lower bound: 98.4423, Value: 0.00771904
Total: 1

而且,当给两个蓄能器提供不同的样本时,我无法让它显示出任何明显的故障。

希望这能帮助你认识到你的情况有什么不同(例如,你真的给两个蓄能器提供了正确的样本吗?)

我用boost 1.53-1.55进行了测试

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22845061

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档