pub struct MetaLearner {
pub config: MetaLearnerConfig,
pub meta_params: MetaParameters,
pub task_history: HashMap<TaskId, TaskAdaptation>,
pub meta_step: u64,
}Expand description
MAML-inspired meta-learner.
§Overview
MetaLearner::adapt_to_taskruns the inner loop: starting from the sharedmeta_paramsit takesconfig.inner_stepsgradient-descent steps on the support set of the given task and evaluates the adapted model on the query set.MetaLearner::meta_updateruns the outer loop: given a batch ofTaskAdaptations it averages the adapted weights, computes a meta-gradient (direction from current meta-weights to the average), and updatesmeta_paramswithconfig.meta_lr.MetaLearner::predictperforms linear inference.
Fields§
§config: MetaLearnerConfigHyper-parameters that control the learning algorithm.
meta_params: MetaParametersShared meta-parameters (the “good initialisation” learned by MAML).
task_history: HashMap<TaskId, TaskAdaptation>Per-task adaptation results keyed by TaskId.
meta_step: u64Count of completed outer meta-update steps.
Implementations§
Source§impl MetaLearner
impl MetaLearner
Sourcepub fn new(config: MetaLearnerConfig) -> Self
pub fn new(config: MetaLearnerConfig) -> Self
Create a new MetaLearner with weights initialised using xorshift64.
Each weight is set to xorshift64(state) as f64 / u64::MAX as f64 * 0.01
so that the initial values are small random numbers in [0, 0.01).
Sourcepub fn loss(prediction: f64, label: f64, task_type: &TaskType) -> f64
pub fn loss(prediction: f64, label: f64, task_type: &TaskType) -> f64
Compute the scalar loss for a single prediction/label pair.
task_type | loss formula |
|---|---|
| Classification | max(0, 1 - label * tanh(prediction)) |
| Regression | (prediction - label)² |
| Ranking | max(0, 1 - prediction * label) |
Sourcepub fn gradient(
features: &[f64],
prediction: f64,
label: f64,
) -> (Vec<f64>, f64)
pub fn gradient( features: &[f64], prediction: f64, label: f64, ) -> (Vec<f64>, f64)
Compute the gradient of the MSE loss with respect to the linear model parameters (weights and bias).
Returns (dL/dw, dL/db) where dL/dw[i] = 2*(prediction-label)*features[i]
and dL/db = 2*(prediction-label).
Sourcepub fn predict(
&self,
features: &[f64],
adaptation: Option<&TaskAdaptation>,
) -> Result<f64, MetaError>
pub fn predict( &self, features: &[f64], adaptation: Option<&TaskAdaptation>, ) -> Result<f64, MetaError>
Perform a linear prediction using either the meta-parameters or, if
adaptation is Some, the task-adapted parameters.
§Errors
Returns MetaError::DimensionMismatch if features.len() != config.dims.
Sourcepub fn adapt_to_task(
&mut self,
task: &MetaTask,
) -> Result<TaskAdaptation, MetaError>
pub fn adapt_to_task( &mut self, task: &MetaTask, ) -> Result<TaskAdaptation, MetaError>
Run the inner loop for task: adapt from meta_params using the
support set, then evaluate on the query set.
The resulting TaskAdaptation is stored in task_history and
returned to the caller.
§Errors
MetaError::EmptySupportSet— if the task support set is empty.MetaError::EmptyQuerySet— if the task query set is empty.MetaError::DimensionMismatch— if any example has the wrong number of features.
Sourcepub fn meta_update(
&mut self,
adaptations: &[TaskAdaptation],
) -> Result<(), MetaError>
pub fn meta_update( &mut self, adaptations: &[TaskAdaptation], ) -> Result<(), MetaError>
Run the outer loop (meta-update).
Averages adapted_weights across all provided adaptations, then
computes a meta-gradient as the direction from current meta-weights to
that average and applies a single gradient-descent step with meta_lr.
§Errors
MetaError::NoAdaptations— ifadaptationsis empty.MetaError::DimensionMismatch— if any adaptation has the wrong number of weight dimensions.
Sourcepub fn task_similarity(a: &TaskAdaptation, b: &TaskAdaptation) -> f64
pub fn task_similarity(a: &TaskAdaptation, b: &TaskAdaptation) -> f64
Compute the cosine similarity between the adapted_weights of two
task adaptations. Returns 0.0 if either weight vector is all-zero.
Sourcepub fn best_task(&self) -> Option<(&TaskId, &TaskAdaptation)>
pub fn best_task(&self) -> Option<(&TaskId, &TaskAdaptation)>
Return the task with the lowest query loss from the history, or None
if the history is empty.
Sourcepub fn reset_task(&mut self, task_id: &TaskId)
pub fn reset_task(&mut self, task_id: &TaskId)
Remove the stored adaptation for task_id from the history.
Sourcepub fn stats(&self) -> MetaLearnerStats
pub fn stats(&self) -> MetaLearnerStats
Compute aggregate statistics over the current task history.
Auto Trait Implementations§
impl Freeze for MetaLearner
impl RefUnwindSafe for MetaLearner
impl Send for MetaLearner
impl Sync for MetaLearner
impl Unpin for MetaLearner
impl UnsafeUnpin for MetaLearner
impl UnwindSafe for MetaLearner
Blanket Implementations§
impl<T> Allocation for T
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
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
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>
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>
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