[WIP] More Encoding - #68
Open
kellrott wants to merge 2 commits into
Open
Conversation
kellrott
commented
Aug 1, 2026
Contributor
- Positional encoding
- Unit testing for positional encoding dimensions
Contributor
There was a problem hiding this comment.
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
dtypeparameter support toOneHotEncoderinitialization and mapping construction. - Added
pe_dimtoProteinOneHotEncoderand 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
OneHotEncoderacceptsdtype, 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 forpe_dim>0those channels are overwritten immediately by the sinusoidal PE assignment below. Removing this avoids redundant work and prevents accidental out-of-bounds writes ifpe_dimis 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
dtypeis now applied to the precomputed per-class tensors (self.mapping[...]), but the batch path in__call__still returns the defaultone_hotdtype (typically int64). This makes single-label and batch outputs inconsistent whendtypeis 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) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.