簡體   English   中英

在循環中使用kernlab包中的rbfdot表現不佳

[英]Poor performance using rbfdot from the kernlab package in a loop

我慢慢地運行的代碼(該功能簡單的例子rbfkernlab包),需要加快:

install.packages('kernlab')       
library('kernlab')

rbf <- rbfdot(sigma=1)

test <- matrix(NaN,nrow=5,ncol=10)
for (i in 1:5) {
               for (j in 1:10) { test[i,j] <- rbf(i,j)}
               }

我已經嘗試了outer()但它不起作用,因為rbf函數沒有返回所需的長度(50)。 我需要加快這個代碼的速度,因為我有大量的數據。 我已經讀過,矢量化將是加速這一過程的聖杯,但我不知道如何。

你能指點我正確的方向嗎?

如果rbf確實是調用rbfdot的返回值,那么body(rbf)看起來像

{
    if (!is(x, "vector")) 
        stop("x must be a vector")
    if (!is(y, "vector") && !is.null(y)) 
        stop("y must a vector")
    if (is(x, "vector") && is.null(y)) {
        return(1)
    }
    if (is(x, "vector") && is(y, "vector")) {
        if (!length(x) == length(y)) 
            stop("number of dimension must be the same on both data points")
        return(exp(sigma * (2 * crossprod(x, y) - crossprod(x) - 
            crossprod(y))))
    }
}

由於大部分內容都是由檢查函數組成的,並且當你只傳入標量時crossprod簡化了,我認為你的函數簡化為

rbf <- function(x, y, sigma = 1)
{
  exp(- sigma * (x - y) ^ 2)
}

要進一步加速,請使用compiler包(需要R-2.14.0或更高版本)。

rbf_loop <- function(m, n)
{
  out <- matrix(NaN, nrow = m, ncol = n)
  for (i in seq_len(m)) 
  {
    for (j in seq_len(n)) 
    { 
      out[i,j] <- rbf(i,j)
    }
  }
  out
)

library(compiler)
rbf_loop_cmp <- cmpfun(rbf_loop)

然后將rbf_loop_cmp(m, n)的時間與rbf_loop_cmp(m, n)的時間進行比較。


簡化步驟更容易反過來看。 如果展開 (x - y) ^ 2 ,則得到x ^ 2 - 2 * x * y + y ^ 2 ,這是減去rbf函數中的東西。

在kernlab中使用函數kernelMatrix(),它應該比在內核函數上循環快幾個數量級:

library(kernlab)

rbf <- rbfdot(sigma=1)

kernelMatrix(rbf, 1:5, 1:10)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM