pub struct MetaLearningOptimizer { /* private fields */ }Expand description
A production-quality meta-learning optimizer.
Supports MAML, Reptile, FOMAML, and ProtoNet meta-algorithms over a
linear regression model (y = w·x + b, MSE loss).
§Example
use ipfrs_tensorlogic::{
MetaLearningOptimizer, MloTaskId, MloTaskExample, MloMetaTask,
OptimizerConfig, MetaAlgorithm,
};
let config = OptimizerConfig::default_maml(2);
let mut opt = MetaLearningOptimizer::new(config);
// Build a simple task
let tid = MloTaskId::new("t1");
let ex = MloTaskExample::new(vec![1.0, 0.0], 1.0, tid.clone());
let qex = MloTaskExample::new(vec![0.0, 1.0], 0.5, tid.clone());
let task = MloMetaTask::new(tid, vec![ex], vec![qex]);
opt.add_task(task).expect("example: should succeed in docs");
let init = MetaLearningOptimizer::initialize_params(2, 42);
let steps = opt.adapt_to_task(&MloTaskId::new("t1"), &init, 3, 0.01).expect("example: should succeed in docs");
assert!(!steps.is_empty());Implementations§
Source§impl MetaLearningOptimizer
impl MetaLearningOptimizer
Sourcepub fn new(config: OptimizerConfig) -> Self
pub fn new(config: OptimizerConfig) -> Self
Create a new MetaLearningOptimizer with the given configuration.
Sourcepub fn add_task(&mut self, task: MetaTask) -> Result<(), MetaError>
pub fn add_task(&mut self, task: MetaTask) -> Result<(), MetaError>
Register a task with the optimizer.
Validates that the feature dimensionality is consistent with previously registered tasks.
Sourcepub fn adapt_to_task(
&self,
task_id: &TaskId,
init_params: &ModelParams,
steps: u8,
lr: f64,
) -> Result<Vec<AdaptationStep>, MetaError>
pub fn adapt_to_task( &self, task_id: &TaskId, init_params: &ModelParams, steps: u8, lr: f64, ) -> Result<Vec<AdaptationStep>, MetaError>
Perform gradient descent on the support set of a task for steps steps.
Returns the full adaptation history (one AdaptationStep per step).
Sourcepub fn meta_update(
&mut self,
task_ids: &[TaskId],
current_params: &ModelParams,
) -> Result<ModelParams, MetaError>
pub fn meta_update( &mut self, task_ids: &[TaskId], current_params: &ModelParams, ) -> Result<ModelParams, MetaError>
Perform one outer-loop meta-update over the specified task IDs.
Algorithm dispatch:
- MAML / FOMAML: for each task, run inner-loop adaptation; compute
meta-gradient as the mean of
(adapted − init); updatenew = current + meta_lr * meta_grad. - Reptile: move
currenttoward each task’s adapted params bystep_size. - ProtoNet: compute per-task prototypes, return params whose weights encode mean prototype and whose bias encodes the grand mean.
Sourcepub fn evaluate_task(
&self,
task_id: &TaskId,
params: &ModelParams,
) -> Result<f64, MetaError>
pub fn evaluate_task( &self, task_id: &TaskId, params: &ModelParams, ) -> Result<f64, MetaError>
Compute MSE of params on the query set of task_id.
Sourcepub fn initialize_params(dim: usize, seed: u64) -> ModelParams
pub fn initialize_params(dim: usize, seed: u64) -> ModelParams
Create a small-random ModelParams of dimensionality dim using an
XorShift-64 PRNG seeded with seed.
Sourcepub fn few_shot_predict(
&self,
task: &MetaTask,
x: &[f64],
init_params: &ModelParams,
) -> Result<f64, MetaError>
pub fn few_shot_predict( &self, task: &MetaTask, x: &[f64], init_params: &ModelParams, ) -> Result<f64, MetaError>
Adapt to the task’s support set and predict the label for x.
Auto Trait Implementations§
impl Freeze for MetaLearningOptimizer
impl RefUnwindSafe for MetaLearningOptimizer
impl Send for MetaLearningOptimizer
impl Sync for MetaLearningOptimizer
impl Unpin for MetaLearningOptimizer
impl UnsafeUnpin for MetaLearningOptimizer
impl UnwindSafe for MetaLearningOptimizer
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