Skip to main content

SimSiam

Struct SimSiam 

Source
pub struct SimSiam { /* private fields */ }
Expand description

Struct-based SimSiam model that owns its projector and predictor weights.

All weight matrices use Kaiming (He) initialisation with scale = sqrt(2 / fan_in).

Implementations§

Source§

impl SimSiam

Source

pub fn new(config: SimSiamConfig, rng: &mut LcgRng) -> SslResult<Self>

Create a new SimSiam model with Kaiming-initialised weights.

§Errors

SslError::InvalidParameter when any dimension in config is zero.

Source

pub fn project(&self, z: &[f32]) -> SslResult<Vec<f32>>

Project a single encoder output vector.

Computes z = L2_norm(proj_w2 · ReLU(proj_w1 · x + proj_b1) + proj_b2).

§Arguments
  • z — encoder output [d_encoder].
§Errors

SslError::DimensionMismatch when z.len() != d_encoder.

Source

pub fn predict(&self, p: &[f32]) -> SslResult<Vec<f32>>

Apply the predictor to a projected representation.

Computes p = L2_norm(pred_w2 · ReLU(pred_w1 · proj + pred_b1) + pred_b2).

§Arguments
  • p — projected representation [d_out].
§Errors

SslError::DimensionMismatch when p.len() != d_out.

Source

pub fn loss(&self, z1: &[f32], z2: &[f32]) -> SslResult<f32>

Compute the symmetric SimSiam loss for two encoder outputs.

Implements L = (D(p1, sg(z2_p)) + D(p2, sg(z1_p))) / 2 where D(a, b) = -(a · b) for unit-norm vectors and sg denotes stop-gradient (a no-op in this pure-Rust implementation since there is no autograd engine).

§Arguments
  • z1 — encoder output from view 1 [d_encoder].
  • z2 — encoder output from view 2 [d_encoder].
§Errors

Propagates dimension mismatch errors from Self::project and Self::predict.

Source

pub fn d_out(&self) -> usize

Return the output dimension of the projector (= predictor I/O dim).

Source

pub fn set_identity_predictor(&mut self) -> SslResult<()>

Overwrite the predictor with an exact direction-preserving identity map.

The predictor MLP L2(W2 · ReLU(W1·p + b1) + b2) is normally a learned, randomly-initialised non-linear transform, so for a random predictor the SimSiam loss of two identical views is some arbitrary value in [-1, 1]. SimSiam’s negative-cosine loss only attains its minimum of -1 for identical views when the predictor preserves the projection’s direction.

This installs such an identity predictor. The ReLU non-linearity is bridged with the standard positive/negative split: the hidden layer computes [ReLU(p), ReLU(-p)] and the output layer reconstructs p = p⁺ - p⁻, reproducing the input exactly. The trailing L2-norm then leaves an already unit-norm projection unchanged, so predict(project(z)) == project(z) and loss(z, z) == -1.

This requires the predictor hidden dimension to be exactly twice the output dimension so the two halves can hold the positive and negative parts.

§Errors

SslError::InvalidParameter when d_predictor != 2 * d_out.

Trait Implementations§

Source§

impl Clone for SimSiam

Source§

fn clone(&self) -> SimSiam

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for SimSiam

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.