Skip to content

Commit

Permalink
generate random from uniform distribution
Browse files Browse the repository at this point in the history
Signed-off-by: Keming <[email protected]>
  • Loading branch information
kemingy committed Sep 25, 2024
1 parent 86f0532 commit 8f3cbec
Showing 1 changed file with 8 additions and 7 deletions.
15 changes: 8 additions & 7 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
//! Utility functions for the project.
use std::cmp::min;
use std::fs::File;
use std::io::{BufReader, BufWriter, Read, Write};
use std::path::Path;

use faer::{Col, ColRef, Mat, MatRef};
use num_traits::{FromBytes, ToBytes};
use rand::{thread_rng, Rng};
use rand::distributions::{Distribution, Uniform};

use crate::consts::THETA_LOG_DIM;

/// Generate a random orthogonal matrix from QR decomposition.
pub fn gen_random_qr_orthogonal(dim: usize) -> Mat<f32> {
let mut rng = thread_rng();
let random = Mat::from_fn(dim, dim, |_, _| rng.gen::<f32>());
let mut rng = rand::thread_rng();
let uniform = Uniform::<f32>::new(0.0, 1.0);
let random = Mat::from_fn(dim, dim, |_, _| uniform.sample(&mut rng));
random.qr().compute_q()
}

Expand All @@ -34,8 +34,9 @@ pub fn gen_fixed_bias(dim: usize) -> Mat<f32> {

/// Generate a random bias vector.
pub fn gen_random_bias(dim: usize) -> Vec<f32> {
let mut rng = thread_rng();
(0..dim).map(|_| rng.gen::<f32>()).collect()
let mut rng = rand::thread_rng();
let uniform = Uniform::<f32>::new(0.0, 1.0);
(0..dim).map(|_| uniform.sample(&mut rng)).collect()
}

/// Convert a vector to a dynamic vector.
Expand Down Expand Up @@ -351,7 +352,7 @@ where
/// Calculate the recall.
pub fn calculate_recall(truth: &[i32], res: &[i32], topk: usize) -> f32 {
let mut count = 0;
let length = min(topk, truth.len());
let length = topk.min(truth.len());
for t in truth.iter().take(length) {
for y in res.iter().take(length.min(res.len())) {
if *t == *y {
Expand Down

0 comments on commit 8f3cbec

Please sign in to comment.