Skip to main content

PointerNetwork

Struct PointerNetwork 

Source
pub struct PointerNetwork {
    pub hidden_dim: usize,
    pub attn_dim: usize,
    pub input_dim: usize,
    pub w1: Vec<f64>,
    pub w2: Vec<f64>,
    pub v: Vec<f64>,
    pub enc_wx: Vec<f64>,
    pub enc_wh: Vec<f64>,
    pub enc_b: Vec<f64>,
}
Expand description

A Pointer Network attention head with optional Elman encoder.

Parameter layout (all row-major, f64):

  • w1[a * hidden_dim + h] — encoder projection (attn_dim × hidden_dim)
  • w2[a * hidden_dim + h] — decoder-query projection (attn_dim × hidden_dim)
  • v[a] — attention combination vector (attn_dim)
  • enc_wx[h * input_dim + d] — encoder input→hidden (hidden_dim × input_dim)
  • enc_wh[h * hidden_dim + h2] — encoder hidden→hidden (hidden_dim × hidden_dim)
  • enc_b[h] — encoder hidden bias (hidden_dim)

Fields§

§hidden_dim: usize

Dimensionality of encoder/decoder hidden states.

§attn_dim: usize

Attention (alignment) inner dimension.

§input_dim: usize

Input feature dimensionality for the optional Elman encoder.

§w1: Vec<f64>

Encoder-state projection W1 (attn_dim × hidden_dim).

§w2: Vec<f64>

Decoder-query projection W2 (attn_dim × hidden_dim).

§v: Vec<f64>

Attention combination vector v (attn_dim).

§enc_wx: Vec<f64>

Elman encoder input→hidden weight (hidden_dim × input_dim).

§enc_wh: Vec<f64>

Elman encoder hidden→hidden weight (hidden_dim × hidden_dim).

§enc_b: Vec<f64>

Elman encoder hidden bias (hidden_dim).

Implementations§

Source§

impl PointerNetwork

Source

pub fn zeros( hidden_dim: usize, attn_dim: usize, input_dim: usize, ) -> SeqResult<Self>

Construct a zero-initialised pointer network.

All dimensions must be positive; otherwise SeqError::InvalidConfiguration.

Source

pub fn new( hidden_dim: usize, attn_dim: usize, input_dim: usize, scale: f64, rng: &mut LcgRng, ) -> SeqResult<Self>

Construct a pointer network with small random weights from a seeded LCG.

All weight matrices and v are sampled ~ U(-scale, scale); biases start at zero. scale must be finite and positive.

Source

pub fn encode(&self, inputs: &[f64]) -> SeqResult<Vec<f64>>

Encode an input feature sequence with a minimal Elman (tanh) RNN.

inputs is n × input_dim row-major; the returned buffer is n × hidden_dim row-major encoder states e_1 … e_n. Provided as a convenience; callers may instead pass their own embeddings to the attention methods directly.

Source

pub fn attention_logits( &self, encoder_states: &[f64], query: &[f64], ) -> SeqResult<Vec<f64>>

Raw attention logits u^i_j = vᵀ tanh(W1 e_j + W2 d_i) for one query.

encoder_states is n × hidden_dim; query is length hidden_dim.

Source

pub fn pointer_distribution( &self, encoder_states: &[f64], query: &[f64], ) -> SeqResult<Vec<f64>>

Pointer distribution softmax(u^i) over the n input positions for one decoder query.

Source

pub fn forward( &self, encoder_states: &[f64], queries: &[f64], ) -> SeqResult<Vec<f64>>

Forward pass over a sequence of decoder queries.

queries is m × hidden_dim row-major. Returns an m × n row-major matrix of pointer distributions (row i is the distribution for query i).

Source

pub fn decode( &self, encoder_states: &[f64], queries: &[f64], ) -> SeqResult<Vec<usize>>

Greedy decode: emit argmax_j p^i_j for each decoder query.

Returns a sequence of m pointed input indices, each in 0..n.

Source

pub fn nll( &self, encoder_states: &[f64], queries: &[f64], targets: &[usize], ) -> SeqResult<f64>

Teacher-forced negative log-likelihood of a target index sequence.

targets[i] is the gold input position pointed to at decoder step i; NLL = − Σ_i log p^i_{targets[i]}. Targets must be in 0..n.

Source

pub fn backward( &self, encoder_states: &[f64], queries: &[f64], targets: &[usize], ) -> SeqResult<(f64, PointerGrad)>

Gradient of the teacher-forced NLL w.r.t. the attention parameters (w1, w2, v).

Uses the softmax-cross-entropy identity ∂NLL/∂u^i_j = p^i_j − 1[j = tgt_i] and back-propagates through s_{j,a} = tanh(W1 e_j + W2 d_i)_a. Returns the NLL and a PointerGrad.

Source

pub fn step( &mut self, encoder_states: &[f64], queries: &[f64], targets: &[usize], lr: f64, ) -> SeqResult<f64>

Apply one gradient-descent step on the attention parameters with learning rate lr. Returns the NLL before the update.

Trait Implementations§

Source§

impl Clone for PointerNetwork

Source§

fn clone(&self) -> PointerNetwork

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 PointerNetwork

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.