我试图重写为(R)cpp,一个原始的R函数,利用伽马函数(从双输入)。在原始源下面。当与'gamma(Rcpp::traits::storage_type(<14>:.type)'“进行复合时,会出现以下错误:”对sourceCpp的调用没有匹配函数
伽马函数应该放在糖中(按下面的平均值计算),所以我认为应该很容易调用。
#include <Rcpp.h>
#include <math.h>
using namespace Rcpp;
// original R function
// function (y_pred, y_true)
// {
// eps <- 1e-15
// y_pred <- pmax(y_pred, eps)
// Poisson_LogLoss <- mean(log(gamma(y_true + 1)) + y_pred -
// log(y_pred) * y_true)
// return(Poisson_LogLoss)
// }
// [[Rcpp::export]]
double poissonLogLoss(NumericVector predicted, NumericVector actual) {
NumericVector temp, y_pred_new;
double out;
const double eps=1e-15;
y_pred_new=pmax(predicted,eps);
long n = predicted.size();
for (long i = 0; i < n; ++i) {
temp[i] = log( gamma(actual[i]+1)+y_pred_new[i]-log(y_pred_new[i])*actual[i]);
}
out=mean(temp); // using sugar implementation
return out;
}发布于 2017-04-27 12:27:23
您使这太复杂了,因为Rcpp糖的工作是矢量化的。因此,以下内容也会编译:
#include <Rcpp.h>
#include <math.h>
using namespace Rcpp;
// [[Rcpp::export]]
double poissonLogLoss(NumericVector predicted, NumericVector actual) {
NumericVector temp, y_pred_new;
double out;
const double eps=1e-15;
y_pred_new=pmax(predicted,eps);
temp = log(gamma(actual + 1)) + y_pred_new - log(y_pred_new)*actual;
out=mean(temp); // using sugar implementation
return out;
}现在,您没有提供任何测试数据,因此我不知道计算是否正确。另外,因为你的R表达式已经向量化了,这不会更快。
最后,您的编译错误可能是因为Sugar函数gamma()需要一个Rcpp对象,而您提供了一个double。
https://stackoverflow.com/questions/43657305
复制相似问题