pub struct MixtureOfExperts<E, G>where
E: Estimator,
G: GatingNetwork,{ /* private fields */ }Expand description
Mixture of Experts ensemble
Implementations§
Source§impl<E, G> MixtureOfExperts<E, G>where
E: Estimator,
G: GatingNetwork,
impl<E, G> MixtureOfExperts<E, G>where
E: Estimator,
G: GatingNetwork,
pub fn builder() -> MoeBuilder<E, G>
pub fn n_experts(&self) -> usize
pub fn config(&self) -> &MoeConfig
pub fn predict(&self, input: &[f32]) -> f32
Sourcepub fn compute_load_balance_loss(&self, inputs: &Matrix<f32>) -> f32
pub fn compute_load_balance_loss(&self, inputs: &Matrix<f32>) -> f32
Compute load balancing auxiliary loss.
Encourages even distribution of inputs across experts to prevent expert collapse (all inputs routed to single expert).
Switch Transformer (Fedus et al. 2021, Eq. 4-6) auxiliary loss:
Loss = alpha * N * sum_i(f_i * P_i) where:
N= number of expertsalpha=load_balance_weightf_i= fraction of tokens dispatched to expert i (hard top-k)P_i= MEAN router softmax probability for expert i over ALL tokens (full softmax, every expert, every token — NOT top-k-only)
§Arguments
inputs- Matrix of shape [n_samples,n_features]
§Returns
Load balance loss value (lower is more balanced)
Source§impl<E, G> MixtureOfExperts<E, G>
impl<E, G> MixtureOfExperts<E, G>
Sourcepub fn fit(
&mut self,
_x: &Matrix<f32>,
_y: &Vector<f32>,
) -> Result<(), AprenderError>
pub fn fit( &mut self, _x: &Matrix<f32>, _y: &Vector<f32>, ) -> Result<(), AprenderError>
Fit MoE using pre-trained experts.
This is a simple two-stage training approach:
- Experts are assumed to be pre-trained (passed in via builder)
- No gating training is performed (uses initial weights)
For more sophisticated training, use separate expert training
followed by MoE construction.
§Arguments
_x- Training features (unused in this simple implementation)_y- Training labels (unused in this simple implementation)
Source§impl<E, G> MixtureOfExperts<E, G>
impl<E, G> MixtureOfExperts<E, G>
Trait Implementations§
Auto Trait Implementations§
impl<E, G> Freeze for MixtureOfExperts<E, G>
impl<E, G> RefUnwindSafe for MixtureOfExperts<E, G>
impl<E, G> Send for MixtureOfExperts<E, G>
impl<E, G> Sync for MixtureOfExperts<E, G>
impl<E, G> Unpin for MixtureOfExperts<E, G>
impl<E, G> UnsafeUnpin for MixtureOfExperts<E, G>
impl<E, G> UnwindSafe for MixtureOfExperts<E, G>
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
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