File size: 1,803 Bytes
705fa30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# src/models/lpe_module.py
"""

Linguistic Position Encoding (LPE) Module



Core novel contribution of ChildFluency-Net.



Encodes 6 loci-of-stuttering features into a 32-dim vector.

These features are pre-computed by build_features.py using

WhisperX transcription + spaCy linguistic parsing.



Clinical basis (Bloodstein 1960, Bernstein-Ratner 1997):

    Stuttering is 3-5x more likely on:

    - Sentence-initial positions

    - Content words (nouns, verbs) vs function words

    - Longer, rarer words

    - Near clause boundaries (cognitive planning spikes)

    - In longer, more complex utterances (higher MLU)



Input features [6 scalars, all in 0-1 range]:

    0: sentence position     (0=initial=high risk, 1=final)

    1: word class            (1=content, 0=function)

    2: syllable count        (normalized, max=6 syllables)

    3: clause boundary prox  (1=near boundary)

    4: word rarity           (1=rare=high risk, 0=common)

    5: MLU                   (sentence length / 20)

"""

import torch
import torch.nn as nn


class LPEModule(nn.Module):

    def __init__(

        self,

        input_dim  : int = 6,

        output_dim : int = 32

    ):
        super().__init__()

        # Small MLP: 6 → 16 → 32
        # Kept intentionally small — 6 features don't need
        # a large network, just a learned non-linear transformation
        self.encoder = nn.Sequential(
            nn.Linear(input_dim, 16),
            nn.ReLU(),
            nn.Linear(16, output_dim)
        )

    def forward(self, x: torch.Tensor) -> torch.Tensor:
        """

        Args:

            x : [batch, 6]   pre-computed LPE feature vectors



        Returns:

              : [batch, 32]  LPE embeddings

        """
        return self.encoder(x)