对大型犰狳矩阵或向量进行子采样

问题描述

我一直在浏览 Armadillo documentation 和示例,但似乎没有真正有效的方法来对大型向量或矩阵进行子采样(或重新采样),因此如果您最初有 N 个元素,则结束最多 N / k 个元素。有几种方法可以进行 shuffle 和 shift,但仅此而已。

所以我只是按顺序循环遍历所有元素,但除了对可用内核进行矢量化之外,肯定还有更好的方法吗?

bool subsample(config& cfg,arma::mat& data,int skippCount)
{
    const auto processor_count = 1; // currently not using threading because 'inplace'

    const size_t cols = data.n_cols;
    const size_t period = skippCount + 1 ;
    size_t newCols = cols / period;
    newCols += (0 == (cols % period)) ? 0 : 1;
       
    const size_t blockSize = 256;
    std::vector<thread> workers;

    for (size_t blockID = 0; blockID < newCols / blockSize; ++blockID) {
        workers.push_back(std::thread([&data,blockID,newCols,period]() { 
            // copy blockSize elements inplace (overwrites other entries))
            size_t c = blockID * blockSize;
            for (size_t b = 0; (c < newCols) && (b < blockSize); c++,b++) {
                arma::vec v = data.col(period * c); 
                data.col(c) = v;
            }
        }));

        if (workers.size()==processor_count) {
            for (auto& thread : workers) thread.join();
            workers.clear();
        }
    }
    for (auto& thread : workers) thread.join(); // make sure all threads finish
    data.resize(data.n_rows,newCols);
    return true;
}

如果您对此有任何改进建议,我们将不胜感激。此外,最好在“就地”执行此操作以节省内存。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...