注意

RAFT 中的向量搜索和聚类算法正在迁移到一个专门用于向量搜索的新库 cuVS。在此迁移过程中,我们将继续支持 RAFT 中的向量搜索算法,但不会在 RAPIDS 24.06(6 月)版本后更新它们。我们计划在 RAPIDS 24.10(10 月)版本前完成迁移,并且它们将在 24.12(12 月)版本中从 RAFT 中完全移除。

单变量随机抽样#

#include <raft/random/rng.cuh>

namespace raft::random

template<typename OutputValueType, typename IndexType>
void uniform(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType start, OutputValueType end)#

在给定范围内生成均匀分布的数字。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • Index – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • start[in] 范围的起始值

  • end[in] 范围的结束值

template<typename OutputValueType, typename IndexType>
void uniformInt(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType start, OutputValueType end)#

在给定范围内生成均匀分布的整数。

模板参数:
  • OutputValueType – 整型;输出向量的值类型

  • IndexType – 用于表示输出向量长度的类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出的随机数向量

  • start[in] 范围的起始值

  • end[in] 范围的结束值

template<typename OutputValueType, typename IndexType>
void normal(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType mu, OutputValueType sigma)#

生成具有给定均值和标准差的正态分布数。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • mu[in] 分布的均值

  • sigma[in] 分布的标准差

template<typename OutputValueType, typename IndexType>
void normalInt(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType mu, OutputValueType sigma)#

生成正态分布的整数。

模板参数:
  • OutputValueType – 整型;输出向量的值类型

  • IndexType – 输出向量长度的整型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • mu[in] 分布的均值

  • sigma[in] 分布的标准差

template<typename OutputValueType, typename IndexType>
void normalTable(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<const OutputValueType, IndexType> mu_vec, std::variant<raft::device_vector_view<const OutputValueType, IndexType>, OutputValueType> sigma, raft::device_matrix_view<OutputValueType, IndexType, raft::row_major> out)#

根据给定的均值集合和标量标准差生成正态分布表。

此表中的每一行都符合正态分布的 n 维向量,其均值为输入向量,标准差为相应的向量或标量。维度之间的相关性假定不存在。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • mu_vec[in] 均值向量(长度为 out.extent(1)

  • sigma[in] 各分量的标准差向量(长度为 out.extent(1)),或所有分量的标量标准差。

  • out[out] 输出表

template<typename OutputValueType, typename IndexType>
void fill(raft::resources const &handle, RngState &rng_state, OutputValueType val, raft::device_vector_view<OutputValueType, IndexType> out)#

使用给定值填充向量。

模板参数:
  • OutputValueType – 输出向量的值类型

  • IndexType – 用于表示输出向量长度的整型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • val[in] 用于填充输出向量的值

  • out[out] 输出向量

template<typename OutputValueType, typename IndexType, typename Type>
void bernoulli(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, Type prob)#

生成伯努利分布的布尔数组。

模板参数:
  • OutputValueType – 输出向量每个元素的类型;必须能够表示布尔值(例如,bool

  • IndexType – 输出向量长度的整型

  • Type – 用于计算概率的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出向量

  • prob[in] 正面朝上的投币概率

template<typename OutputValueType, typename IndexType>
void scaled_bernoulli(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType prob, OutputValueType scale)#

生成伯努利分布数组并应用缩放。

模板参数:
  • OutputValueType – 用于计算概率的数据类型

  • IndexType – 输出向量长度的整型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出向量

  • prob[in] 正面朝上的投币概率

  • scale[in] 缩放因子

template<typename OutputValueType, typename IndexType = int>
void gumbel(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType mu, OutputValueType beta)#

生成 Gumbel 分布的随机数。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • mu[in] 均值

  • beta[in] 尺度值

template<typename OutputValueType, typename IndexType>
void lognormal(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType mu, OutputValueType sigma)#

生成对数正态分布的数字。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • mu[in] 分布的均值

  • sigma[in] 分布的标准差

template<typename OutputValueType, typename IndexType = int>
void logistic(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType mu, OutputValueType scale)#

生成逻辑斯蒂分布的随机数。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • mu[in] 均值

  • scale[in] 尺度值

template<typename OutputValueType, typename IndexType>
void exponential(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType lambda)#

生成指数分布的随机数。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • lambda[in] 指数分布的 lambda 参数

template<typename OutputValueType, typename IndexType>
void rayleigh(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType sigma)#

生成瑞利分布的随机数。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • sigma[in] 分布的 sigma 参数

template<typename OutputValueType, typename IndexType>
void laplace(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutputValueType, IndexType> out, OutputValueType mu, OutputValueType scale)#

生成拉普拉斯分布的随机数。

模板参数:
  • OutputValueType – 输出随机数的数据类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • mu[in] 均值

  • scale[in] 尺度

template<typename OutType, typename WeightType, typename IndexType>
std::enable_if_t<std::is_integral_v<OutType>> discrete(raft::resources const &handle, RngState &rng_state, raft::device_vector_view<OutType, IndexType> out, raft::device_vector_view<const WeightType, IndexType> weights)#

生成随机整数,其中 i 的概率为 weights[i]/sum(weights)

使用示例

#include <raft/core/device_mdarray.hpp>
#include <raft/core/resources.hpp>
#include <raft/random/rng.cuh>

raft::resources handle;
...
raft::random::RngState rng(seed);
auto indices = raft::make_device_vector<int>(handle, n_samples);
raft::random::discrete(handle, rng, indices.view(), weights);

模板参数:
  • OutType – 整型输出类型

  • WeightType – 权重类型

  • IndexType – 用于表示数组长度的数据类型

参数:
  • handle[in] 用于资源管理的 raft 句柄

  • rng_state[in] 随机数生成器状态

  • out[out] 输出数组

  • weights[in] 权重数组