Skip to content

Commit

Permalink
Reduce allocations (#350)
Browse files Browse the repository at this point in the history
* capacity for dataset

* replace map with mapv_into

* replace map with mapv_into again

* fix clippy clone_from

* returned iter + map back
  • Loading branch information
cospectrum authored Jul 3, 2024
1 parent 2eaa686 commit 80a973b
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/composing/platt_scaling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,7 @@ mod tests {
y.len(),
"The number of data points must match the number of output targets."
);
*y = self.reg_vals.clone();
y.clone_from(&self.reg_vals);
}

fn default_target(&self, x: &Array2<f32>) -> Array1<f32> {
Expand Down
4 changes: 2 additions & 2 deletions src/dataset/impl_dataset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -603,13 +603,13 @@ where
)> {
let targets = self.as_targets();
let fold_size = targets.len() / k;
let mut res = Vec::new();

// Generates all k folds of records and targets
let mut records_chunks: Vec<_> =
self.records.axis_chunks_iter(Axis(0), fold_size).collect();
let mut targets_chunks: Vec<_> = targets.axis_chunks_iter(Axis(0), fold_size).collect();

let mut res = Vec::with_capacity(k);
// For each iteration, take the first chunk for both records and targets as the validation set and
// concatenate all the other chunks to create the training set. In the end swap the first chunk with the
// one in the next index so that it is ready for the next iteration
Expand Down Expand Up @@ -742,7 +742,7 @@ where
let targets = self.ntargets();
let tshape = self.targets.raw_dim();

let mut objs: Vec<O> = Vec::new();
let mut objs: Vec<O> = Vec::with_capacity(k);

{
let records_sl = self.records.as_slice_mut().unwrap();
Expand Down
14 changes: 7 additions & 7 deletions src/metrics_regression.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
fn mean_absolute_error(&self, compare_to: &T) -> Result<F> {
self.as_single_targets()
.sub(&compare_to.as_single_targets())
.mapv(|x| x.abs())
.mapv_into(|x| x.abs())
.mean()
.ok_or(Error::NotEnoughSamples)
}
Expand All @@ -41,7 +41,7 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
fn mean_squared_error(&self, compare_to: &T) -> Result<F> {
self.as_single_targets()
.sub(&compare_to.as_single_targets())
.mapv(|x| x * x)
.mapv_into(|x| x * x)
.mean()
.ok_or(Error::NotEnoughSamples)
}
Expand All @@ -58,7 +58,7 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
let mut abs_error = self
.as_single_targets()
.sub(&compare_to.as_single_targets())
.mapv(|x| x.abs())
.mapv_into(|x| x.abs())
.to_vec();
abs_error.sort_by(|a, b| a.partial_cmp(b).unwrap());
let mid = abs_error.len() / 2;
Expand All @@ -74,8 +74,8 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
fn mean_absolute_percentage_error(&self, compare_to: &T) -> Result<F> {
self.as_single_targets()
.sub(&compare_to.as_single_targets())
.div(&self.as_single_targets())
.mapv(|x| x.abs())
.div(self.as_single_targets())
.mapv_into(|x| x.abs())
.mean()
.ok_or(Error::NotEnoughSamples)
}
Expand All @@ -95,7 +95,7 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
- self
.as_single_targets()
.sub(&single_target_compare_to)
.mapv(|x| x * x)
.mapv_into(|x| x * x)
.sum()
/ (single_target_compare_to
.mapv(|x| (x - mean) * (x - mean))
Expand All @@ -114,7 +114,7 @@ pub trait SingleTargetRegression<F: Float, T: AsSingleTargets<Elem = F>>:
let mean_error = diff.mean().ok_or(Error::NotEnoughSamples)?;

Ok(F::one()
- (diff.mapv(|x| x * x).sum() - mean_error)
- (diff.mapv_into(|x| x * x).sum() - mean_error)
/ (single_target_compare_to
.mapv(|x| (x - mean) * (x - mean))
.sum()
Expand Down

0 comments on commit 80a973b

Please sign in to comment.