c++ - 快速计算一个 vector 与多个 vector 的余弦相似度

标签 c++ r performance vector rcpp

<分区>

我很想听听有关优化代码以计算 vector x(长度为 l)与 n 的余弦相似度的想法其他 vector (存储在任何结构中,例如具有 n 行和 l 列的矩阵 m)。

n 的值通常比 l 的值大得多。

我目前正在使用这个自定义 Rcpp 函数来计算 vector x 与矩阵 m 每一行的相似度:

library(Rcpp)
cppFunction('NumericVector cosine_x_to_m(NumericVector x, NumericMatrix m) {
  int nrows = m.nrow();
  NumericVector out(nrows);
  for (int i = 0; i < nrows; i++) {
    NumericVector y = m(i, _);
    out[i] = sum(x * y) / sqrt(sum(pow(x, 2.0)) * sum(pow(y, 2.0)));
  }
  return out;
}')

改变 nl,我得到以下几种计时:

enter image description here

下面是可重现的代码。


# Function to simulate data
sim_data <- function(l, n) {
  # Feature vector to be used for computing similarity
  x <- runif(l)

  # Matrix of feature vectors (1 per row) to compare against x
  m <- matrix(runif(n * l), nrow = n)

  list(x = x, m = m)
}

# Rcpp function to compute similarity of x to each row of m
library(Rcpp)
cppFunction('NumericVector cosine_x_to_m(NumericVector x, NumericMatrix m) {
  int nrows = m.nrow();
  NumericVector out(nrows);
  for (int i = 0; i < nrows; i++) {
    NumericVector y = m(i, _);
    out[i] = sum(x * y) / sqrt(sum(pow(x, 2.0)) * sum(pow(y, 2.0)));
  }
  return out;
}')    

# Timer function
library(microbenchmark)
timer <- function(l, n) {
  dat <- sim_data(l, n)
  microbenchmark(cosine_x_to_m(dat$x, dat$m))
}

# Results for grid of l and n
library(tidyverse)
results <- cross_d(list(l = seq(200, 1000, by = 200), n = seq(500, 4000, by = 500))) %>% 
  mutate(timings = map2(l, n, timer))

# Plot results
results_plot <- results %>%
  unnest(timings) %>% 
  mutate(time = time / 1000000) %>%  # Convert time to seconds
  group_by(l, n) %>% 
  summarise(mean = mean(time), ci = 1.96 * sd(time) / sqrt(n()))

pd <- position_dodge(width = 20)

results_plot %>% 
  ggplot(aes(n, mean, group= l)) +
  geom_line(aes(color = factor(l)), position = pd, size = 2) +
  geom_errorbar(aes(ymin = mean - ci, ymax = mean + ci), position = pd, width = 100) +
  geom_point(position = pd, size = 2) +
  scale_color_brewer(palette = "Blues") +
  theme_minimal() +
  labs(x = "n", y = "Seconds", color = "l") +
  ggtitle("Algorithm Runtime",
          subtitle = "Error bars represent 95% confidence intervals")

最佳答案

我使用的是 Microsoft R(带有 Intel MKL),它使矩阵乘法更快,但为了公平比较,我将其设置为单线程。

setMKLthreads(1)

在我的测试中,这个纯 R 版本 cosine_x_to_m 比你的快两倍。

cosine_x_to_m2 = function(x,m){
  x = x / sqrt(crossprod(x));
  return(  as.vector((m %*% x) / sqrt(rowSums(m^2))) );
}

用 C/C++ 重写 rowSums(m^2) 使其更快,大约是原来的四倍。

library(ramwas)
cosine_x_to_m2 = function(x,m){
  x = x / sqrt(crossprod(x));
  return(  as.vector((m %*% x) / sqrt(rowSumsSq(m))) );
}

初始性能:

Initial performance

最终版本性能:

Final version performance

关于c++ - 快速计算一个 vector 与多个 vector 的余弦相似度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43108837/

相关文章:

c++ - CPP_TEST.exe : 0xC0000005: Access violation writing location 0x00abcdef 中 0x00af7230 处出现未处理的异常

c++ - 当摆脱模偏差时,min = -upper_bound % upper_bound;//如何工作?

html - 将 Markdown 另存为交互式 HTML

r - ggplot2 中具有两种色阶的并排堆叠条形图

read.table 函数和 stdin

c# - 原始类型性能

c++ - Boost 预处理器中的迭代限制

c++ - 为什么 std::numeric_limits<float>::min() 在使用不同函数流式传输到输出时行为不同?

c - 为什么当数组大小是偶数时这段代码会变慢?

c# - 分配变量的成本