pub struct ModelSoup { /* private fields */ }Expand description
Model Soup - Weight-space averaging for improved generalization.
From “Model soups: averaging weights of multiple fine-tuned models improves accuracy without increasing inference time” (Wortsman et al., 2022).
Model soups average the weights of multiple models (not predictions), which can:
- Improve accuracy compared to individual models
- No inference cost (single model at test time)
- Work across different hyperparameters and random seeds
- Particularly effective for models fine-tuned from same initialization
Two main recipes:
- Uniform Soup: Simple average of all model weights
- Greedy Soup: Iteratively add models that improve validation performance
§Example
use tensorlogic_train::{ModelSoup, SoupRecipe};
use std::collections::HashMap;
use scirs2_core::ndarray::Array2;
// Collect weights from multiple fine-tuned models
// let model_weights = vec![weights1, weights2, weights3];
// let soup = ModelSoup::uniform_soup(model_weights);
// let averaged_weights = soup.weights();Implementations§
Source§impl ModelSoup
impl ModelSoup
Sourcepub fn uniform_soup(
model_weights: Vec<HashMap<String, Array2<f64>>>,
) -> TrainResult<Self>
pub fn uniform_soup( model_weights: Vec<HashMap<String, Array2<f64>>>, ) -> TrainResult<Self>
Create a uniform soup by averaging all model weights equally.
§Arguments
model_weights- Weights from multiple fine-tuned models
§Returns
Model soup with uniformly averaged weights
§Example
use tensorlogic_train::ModelSoup;
use std::collections::HashMap;
use scirs2_core::ndarray::array;
let mut weights1 = HashMap::new();
weights1.insert("w".to_string(), array![[1.0, 2.0]]);
let mut weights2 = HashMap::new();
weights2.insert("w".to_string(), array![[3.0, 4.0]]);
let soup = ModelSoup::uniform_soup(vec![weights1, weights2]).unwrap();
// Averaged weights: [[2.0, 3.0]]Sourcepub fn greedy_soup(
model_weights: Vec<HashMap<String, Array2<f64>>>,
val_accuracies: Vec<f64>,
) -> TrainResult<Self>
pub fn greedy_soup( model_weights: Vec<HashMap<String, Array2<f64>>>, val_accuracies: Vec<f64>, ) -> TrainResult<Self>
Create a greedy soup by iteratively adding models that improve validation performance.
§Arguments
model_weights- Weights from multiple fine-tuned modelsval_accuracies- Validation accuracy for each model
§Returns
Model soup with greedily selected and averaged weights
§Algorithm
- Start with best single model
- Try adding each remaining model to soup
- Keep additions that improve validation performance
- Repeat until no improvement
Sourcepub fn weighted_soup(
model_weights: Vec<HashMap<String, Array2<f64>>>,
weights: Vec<f64>,
) -> TrainResult<Self>
pub fn weighted_soup( model_weights: Vec<HashMap<String, Array2<f64>>>, weights: Vec<f64>, ) -> TrainResult<Self>
Sourcepub fn num_models(&self) -> usize
pub fn num_models(&self) -> usize
Get the number of models in the soup.
Sourcepub fn recipe(&self) -> SoupRecipe
pub fn recipe(&self) -> SoupRecipe
Get the recipe used to create the soup.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for ModelSoup
impl RefUnwindSafe for ModelSoup
impl Send for ModelSoup
impl Sync for ModelSoup
impl Unpin for ModelSoup
impl UnwindSafe for ModelSoup
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
Converts
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more