Skip to content

[WIP] More Encoding - #68

Open
kellrott wants to merge 2 commits into
developfrom
feature/more-encoding
Open

[WIP] More Encoding#68
kellrott wants to merge 2 commits into
developfrom
feature/more-encoding

Conversation

@kellrott

@kellrott kellrott commented Aug 1, 2026

Copy link
Copy Markdown
Contributor
  • Positional encoding
  • Unit testing for positional encoding dimensions

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This WIP PR extends the embkit.encoding module to support richer positional encodings (sin/cos) for ProteinOneHotEncoder, and adds a dtype option to OneHotEncoder’s precomputed one-hot tensors.

Changes:

  • Added dtype parameter support to OneHotEncoder initialization and mapping construction.
  • Added pe_dim to ProteinOneHotEncoder and introduced sinusoidal positional encoding helpers (position_sin_cos*).
  • Updated ProteinOneHotEncoder (de)serialization to include the new positional-encoding configuration and added PE helper functions.
Suppressed comments (3)

src/embkit/encoding/init.py:16

  • OneHotEncoder accepts dtype, but it isn’t stored on the instance, making it hard to apply consistently (e.g., for batch outputs).

This issue also appears on line 19 of the same file.

    def __init__(self, classes, device=None, dtype=None):
        self.classes = sorted(classes)
        self.num_classes = len(self.classes)
        self.mapping = {}
        self.class_idx = {}

src/embkit/encoding/init.py:168

  • Inside the residue loop, a scalar position value is written to one_hot_matrix[..., len(self.alphabet)], but for pe_dim>0 those channels are overwritten immediately by the sinusoidal PE assignment below. Removing this avoids redundant work and prevents accidental out-of-bounds writes if pe_dim is changed.
                if self.encode_pos:
                    if self.full_len is not None:
                        one_hot_matrix[b, i, len(self.alphabet)] = float(i) / float(self.full_len)
                    else:
                        one_hot_matrix[b, i, len(self.alphabet)] = float(i)

src/embkit/encoding/init.py:21

  • dtype is now applied to the precomputed per-class tensors (self.mapping[...]), but the batch path in __call__ still returns the default one_hot dtype (typically int64). This makes single-label and batch outputs inconsistent when dtype is provided.
        for i, n in enumerate(self.classes):
            self.mapping[n] = F.one_hot( torch.tensor(i), self.num_classes ).to(device=device, dtype=dtype)
            self.class_idx[n] = i

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +84 to 91
def __init__(self, full_len=None, encode_x=True, encode_pos=False, pe_dim=2, device=None, dtype=torch.float32, backend='torch'):
self.full_len = full_len
self.encode_x = encode_x
self.encode_pos = encode_pos
self.pe_dim = pe_dim
self.device = device
self.dtype = dtype
self.backend = backend
self.shape = (self.full_len, len(self.alphabet) + (self.pe_dim if self.encode_pos else 0))
else:
self.shape = (len(self.alphabet) + (1 if self.encode_pos else 0),) # +1 for position encoding
self.shape = (len(self.alphabet) + (1 if self.encode_pos else 0) + self.pe_dim,)
"encode_pos": self.encode_pos,
"pe_dim": self.pe_dim,
"device": self.device,
"dtype": str(self.dtype),
Comment on lines 205 to +212
return cls(
full_len=data.get("full_len"),
encode_x=data.get("encode_x", True),
encode_pos=data.get("encode_pos", False),
device=data.get("device"),
dtype=dtype,
backend=data.get("backend", 'torch')
)
)
Comment on lines +247 to +252
dim = pe_dim if pe_dim % 2 == 0 else pe_dim + 1
vec = torch.zeros(dim, device=device, dtype=dtype)

for i in range(0, dim, 2):
freq = torch.exp(torch.tensor(i * -(np.log(log_base) / dim), dtype=dtype, device=device))
vec[i] = torch.sin(pos * freq)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants