-
Notifications
You must be signed in to change notification settings - Fork 0
Demo training layout #3
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
d0dd366
Move training demo out of package
yordabayev d2ed770
Flatten demo data files
yordabayev 1f7d5da
Validate GP alphabets explicitly
yordabayev 6081905
Enforce absolute imports
yordabayev 3469783
Make demo data path configurable
yordabayev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
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
File renamed without changes.
File renamed without changes.
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -8,9 +8,9 @@ | |
| import torch | ||
| from scipy.stats import pearsonr, spearmanr | ||
|
|
||
| from .blosum50 import get_blosum50_matrix | ||
| from .utils import encode_one_hot | ||
| from .gp_wrapper import GPWrapper, LinearGP, LockGP, TanimotoGP | ||
| from demo.util import encode_one_hot | ||
| from lock_gp.blosum50 import get_blosum50_matrix | ||
| from lock_gp.gp_wrapper import GPWrapper, LinearGP, LockGP, TanimotoGP | ||
|
|
||
|
|
||
| def train_test_split( | ||
|
|
@@ -74,33 +74,37 @@ def evaluate(y_true: torch.Tensor, y_pred_mean: torch.Tensor, y_pred_var: torch. | |
| def main() -> None: | ||
| """Fit Linear, LOCK, and Tanimoto GPs to CR6261-H1 dataset.""" | ||
| parser = argparse.ArgumentParser(description="LOCK GP Demo") | ||
| parser.add_argument("--num-training", type=int, default=256, help="Number of training points.") | ||
| parser.add_argument( | ||
| "--data", | ||
| type=Path, | ||
| default=Path(__file__).parent / "cr6261_h1.csv", | ||
| help="Path to a CSV with sequence and fitness columns.", | ||
| ) | ||
| parser.add_argument("--train-size", type=int, default=256, help="Number of training points.") | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Renamed |
||
| args = parser.parse_args() | ||
|
|
||
| device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
|
|
||
| data_path = Path("data") / "cr6261_h1.csv" | ||
| df = pd.read_csv(data_path) | ||
| df = pd.read_csv(args.data) | ||
| sequences = df["sequence"].astype(str).tolist() | ||
| y_np = df["fitness"].to_numpy(dtype=np.float64) | ||
|
|
||
| alphabet, _ = get_blosum50_matrix() | ||
| x = encode_one_hot(sequences, alphabet, dtype=torch.float64) | ||
| y = torch.tensor(y_np, dtype=torch.float64) | ||
|
|
||
| x_train, y_train, x_test, y_test = train_test_split(x, y, num_train=args.num_training, device=device) | ||
| x_train, y_train, x_test, y_test = train_test_split(x, y, num_train=args.train_size, device=device) | ||
|
|
||
| print(f"Device: {device} Train size: {len(y_train)} Test size: {len(y_test)}") | ||
|
|
||
| gp_configs: list[tuple[str, type[GPWrapper]]] = [ | ||
| ("Linear GP", LinearGP), | ||
| ("Tanimoto GP", TanimotoGP), | ||
| ("LOCK GP", LockGP), | ||
| gp_models: list[tuple[str, GPWrapper]] = [ | ||
| ("Linear GP", LinearGP()), | ||
| ("Tanimoto GP", TanimotoGP(alphabet=alphabet)), | ||
| ("LOCK GP", LockGP(alphabet=alphabet)), | ||
| ] | ||
|
|
||
| results: dict[str, dict[str, float]] = {} | ||
| for name, gp_cls in gp_configs: | ||
| gp = gp_cls() | ||
| for name, gp in gp_models: | ||
| gp.fit(x_train, y_train) | ||
| mean, var = gp.predict(x_test) | ||
| results[name] = evaluate(y_test, mean, var) | ||
|
|
||
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Sequence | ||
|
|
||
| import torch | ||
| import torch.nn.functional as F | ||
|
|
||
|
|
||
| def encode_one_hot( | ||
| sequences: list[str], | ||
| alphabet: Sequence[str], | ||
| dtype: torch.dtype = torch.float64, | ||
| ) -> torch.Tensor: | ||
| """ | ||
| Convert sequences to one-hot tensor of shape (N, L, A). | ||
|
|
||
| Args: | ||
| sequences: List of equal-length sequences. | ||
| alphabet: Ordered alphabet of valid tokens. | ||
| dtype: Floating-point dtype of the returned one-hot tensor. Defaults | ||
| to ``torch.float64``. | ||
|
|
||
| Raises: | ||
| ValueError: if any sequence contains a token outside the alphabet or | ||
| if the sequences are not all the same length. | ||
| """ | ||
| seq_length = len(sequences[0]) | ||
| for seq in sequences: | ||
| if len(seq) != seq_length: | ||
| raise ValueError("All sequences must have the same length.") | ||
| alphabet_index = {tok: i for i, tok in enumerate(alphabet)} | ||
| unknown = {tok for seq in sequences for tok in seq if tok not in alphabet_index} | ||
| if unknown: | ||
| raise ValueError(f"Unknown token(s) {sorted(unknown)} encountered.") | ||
|
|
||
| indices = torch.tensor([[alphabet_index[tok] for tok in seq] for seq in sequences], dtype=torch.int64) | ||
| return F.one_hot(indices, num_classes=len(alphabet)).to(dtype=dtype) |
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
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
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can now provide custom data.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
do we really want that? this is a demo not a harness?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is a small change - maybe people will want to quickly try their own data?