std::function 使用中有性能问题,如何避免?

我有一些类,可以用来组合协方差函数(也称为核函数,请见 https://stats.stackexchange.com/questions/228552/covariance-functions-or-kernels-what-exactly-are-they),然后根据新的核函数计算协方差,例如:

auto C = GaussianKernel(50,60) + GaussianKernel(100,200);auto result = C.covarianceFunction(30.0,40.0);

但问题是我在计算协方差时调用了 std::function有没有简单的方法来避免它?
请注意,我需要计算一个大的协方差矩阵(大约50K*50K),这意味着性能非常重要。

以下是代码

class Kernel {public:     /*    Covariance function : return the covariance between two R.V. for the entire kernel's domain definition.     */    virtual double covarianceFunction(        double   X,        double   Y    )const = 0 ;    ~Kernel() = default;};class FooKernel : public Kernel {public:    FooKernel(std::function<double(double, double)> fun) : fun_(fun) {}    double covarianceFunction(        double   X,        double   Y    ) const {        return fun_(X, Y);    }    template<class T>    auto operator+(const T b) const {        return FooKernel([b, this](double X, double Y) -> double {            return this->covarianceFunction(X, Y) + b.covarianceFunction(X, Y);        });    }    FooKernel operator=(const FooKernel other) const {        return other;    }private:    std::function<double(double, double)> fun_;};class GaussianKernel : public Kernel {public:    GaussianKernel(double sigma, double scale) : m_sigma(sigma), m_scale(scale) {}    GaussianKernel(double sigma) : m_sigma(sigma), m_scale(1) {}    /*    A well known covariance function that enforces smooth deformations    Ref : Shape modeling using Gaussian process Morphable Models, Luethi et al.    */    double covarianceFunction(        double   X,        double   Y    ) const     {        //use diagonal matrix    doulbe result;    result = m_scale  *  exp(-std::norm(X - Y) / (m_sigma*m_sigma));    return result;          }    template<class T>    auto operator+(const T b) const {        return FooKernel([b, this](double X, double Y) -> double {            auto debugBval = b.covarianceFunction(X, Y);            auto debugAval = this->covarianceFunction(X, Y);            auto test = debugBval + debugAval;            return test;        });    }private:    double m_sigma;    double m_scale;};

回答:

通过对 FooKernel 使用模板化,你可以避免使用 std::function 的需要。

#include <iostream>#include <complex>#include <functional>class Kernel {public:     /*    Covariance function : return the covariance between two R.V. for the entire kernel's domain definition.     */    virtual double covarianceFunction(        double   X,        double   Y    )const = 0 ;    ~Kernel() = default;};template <typename Func>class FooKernel : public Kernel {public:    FooKernel(Func&& fun) : fun_(std::forward<Func>(fun)) {}    double covarianceFunction(        double   X,        double   Y    ) const {        return fun_(X, Y);    }    template<class T>    auto operator+(const T b) const {        return make_foo_kernel([b, this](double X, double Y) -> double {            return this->covarianceFunction(X, Y) + b.covarianceFunction(X, Y);        });    }    FooKernel operator=(const FooKernel other) const {        return other;    }private:   Func fun_;};template <typename Func>auto make_foo_kernel(Func&& fun){    return FooKernel<Func>(std::forward<Func>(fun));}class GaussianKernel : public Kernel {public:    GaussianKernel(double sigma, double scale) : m_sigma(sigma), m_scale(scale) {}    GaussianKernel(double sigma) : m_sigma(sigma), m_scale(1) {}    /*    A well known covariance function that enforces smooth deformations    Ref : Shape modeling using Gaussian process Morphable Models, Luethi et al.    */    double covarianceFunction(        double   X,        double   Y    ) const     {        //use diagonal matrix    double result;    result = m_scale  *  exp(-std::norm(X - Y) / (m_sigma*m_sigma));    return result;          }    template<class T>    auto operator+(const T b) const {        return make_foo_kernel([b, this](double X, double Y) -> double {            auto debugBval = b.covarianceFunction(X, Y);            auto debugAval = this->covarianceFunction(X, Y);            auto test = debugBval + debugAval;            return test;        });    }private:    double m_sigma;    double m_scale;};int main(){    auto C = GaussianKernel(50,60) + GaussianKernel(100,200);    auto result = C.covarianceFunction(30.0,40.0);    return 0;}

演示

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注