pub struct EmbeddingMlpClassifier {
pub embedding: Vec<f32>,
pub mlp: MlpClassifier,
/* private fields */
}Expand description
A two-layer MLP classifier with a learnable token-embedding lookup.
This avoids materialising one-hot vectors for large vocabularies. The
input is built from n_context_tokens token IDs whose embeddings are
concatenated, followed by an optional continuous_dim vector of continuous
features (e.g. geometric positions).
Fields§
§embedding: Vec<f32>Token embedding table, row-major [vocab_size, embed_dim].
mlp: MlpClassifierUnderlying MLP head. Its input dimension is
n_context_tokens * embed_dim + continuous_dim.
Implementations§
Source§impl EmbeddingMlpClassifier
impl EmbeddingMlpClassifier
Sourcepub fn new(
vocab_size: usize,
embed_dim: usize,
n_context_tokens: usize,
continuous_dim: usize,
hidden_dim: usize,
output_dim: usize,
seed: u32,
rope_theta: Option<f32>,
) -> Self
pub fn new( vocab_size: usize, embed_dim: usize, n_context_tokens: usize, continuous_dim: usize, hidden_dim: usize, output_dim: usize, seed: u32, rope_theta: Option<f32>, ) -> Self
Create a fresh classifier with Xavier-like random weights.
Sourcepub fn input_dim(&self) -> usize
pub fn input_dim(&self) -> usize
Total flat input dimension after embedding lookup and concatenation.
Sourcepub fn forward(
&self,
token_ids: &[u32],
continuous: Option<&[f32]>,
) -> MlpForward
pub fn forward( &self, token_ids: &[u32], continuous: Option<&[f32]>, ) -> MlpForward
Forward pass for a single example.
Sourcepub fn predict(&self, token_ids: &[u32], continuous: Option<&[f32]>) -> usize
pub fn predict(&self, token_ids: &[u32], continuous: Option<&[f32]>) -> usize
Predicted class for a single example.
Sourcepub fn loss(
&self,
token_ids: &[u32],
continuous: Option<&[f32]>,
target: usize,
) -> f32
pub fn loss( &self, token_ids: &[u32], continuous: Option<&[f32]>, target: usize, ) -> f32
Cross-entropy loss for a single example.
Sourcepub fn forward_batch(
&self,
token_ids: &[u32],
continuous: Option<&[f32]>,
batch_size: usize,
) -> MlpForwardBatch
pub fn forward_batch( &self, token_ids: &[u32], continuous: Option<&[f32]>, batch_size: usize, ) -> MlpForwardBatch
Forward pass for a batch.
Sourcepub fn backward_batch(
&self,
token_ids: &[u32],
continuous: Option<&[f32]>,
targets: &[usize],
batch_size: usize,
) -> (EmbeddingMlpGradients, f32)
pub fn backward_batch( &self, token_ids: &[u32], continuous: Option<&[f32]>, targets: &[usize], batch_size: usize, ) -> (EmbeddingMlpGradients, f32)
Back-propagation for a batch.
Returns gradients averaged over the batch and the average cross-entropy
loss. Embedding gradients are accumulated and averaged; the returned
dembedding has the same shape as Self::embedding.
Sourcepub fn apply_sgd(&mut self, grad: &EmbeddingMlpGradients, lr: f32)
pub fn apply_sgd(&mut self, grad: &EmbeddingMlpGradients, lr: f32)
Apply a single SGD step using the supplied gradients.
Trait Implementations§
Source§impl Clone for EmbeddingMlpClassifier
impl Clone for EmbeddingMlpClassifier
Source§fn clone(&self) -> EmbeddingMlpClassifier
fn clone(&self) -> EmbeddingMlpClassifier
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for EmbeddingMlpClassifier
impl RefUnwindSafe for EmbeddingMlpClassifier
impl Send for EmbeddingMlpClassifier
impl Sync for EmbeddingMlpClassifier
impl Unpin for EmbeddingMlpClassifier
impl UnsafeUnpin for EmbeddingMlpClassifier
impl UnwindSafe for EmbeddingMlpClassifier
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
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
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