From 2c9f6059760c2c6bb62c6ceac3fd283f52d39fe8 Mon Sep 17 00:00:00 2001 From: Laurent Mazare Date: Sun, 6 Aug 2023 22:51:08 +0200 Subject: [PATCH] Add rand-like/randn-like. (#333) --- candle-core/src/tensor.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/candle-core/src/tensor.rs b/candle-core/src/tensor.rs index 170b3fda..ff381620 100644 --- a/candle-core/src/tensor.rs +++ b/candle-core/src/tensor.rs @@ -269,6 +269,10 @@ impl Tensor { Self::rand_impl(lo, up, s, device, false) } + pub fn rand_like(&self, lo: f64, up: f64) -> Result { + Tensor::rand_f64_impl(lo, up, self.shape(), self.dtype(), self.device(), false) + } + pub(crate) fn randn_impl, T: crate::FloatDType>( mean: T, std: T, @@ -296,6 +300,17 @@ impl Tensor { Ok(from_storage(storage, s, none, is_variable)) } + pub fn randn_like(&self, mean: f64, stdev: f64) -> Result { + Tensor::randn_f64_impl( + mean, + stdev, + self.shape(), + self.dtype(), + self.device(), + false, + ) + } + /// Creates a new tensor initialized with values sampled from a normal distribution with the /// specified `mean` and standard deviation `std`. pub fn randn, T: crate::FloatDType>(