kernel-elastic-autoencoder 3.1.0__tar.gz → 3.1.2__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/PKG-INFO +1 -1
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/pyproject.toml +1 -1
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/config.py +2 -0
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/layers.py +5 -5
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/losses.py +1 -1
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/pipeline.py +6 -4
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/tokenizer.py +9 -7
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/training.py +5 -3
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/README.md +0 -0
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/__init__.py +0 -0
- {kernel_elastic_autoencoder-3.1.0 → kernel_elastic_autoencoder-3.1.2}/src/kernel_elastic_autoencoder/model.py +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
[project]
|
|
2
2
|
name = "kernel_elastic_autoencoder"
|
|
3
|
-
version = "3.1.
|
|
3
|
+
version = "3.1.2"
|
|
4
4
|
description = "Implementation of Kernel-Elastic Autoencoder for Molecular Design (https://doi.org/10.1093/pnasnexus/pgae168)"
|
|
5
5
|
authors = [
|
|
6
6
|
{ name = "Felix Rotter-McCartney", email = "felix.rotter@mail.utoronto.ca" }
|
|
@@ -217,6 +217,7 @@ class TrainingOptimizerConfig(Config):
|
|
|
217
217
|
optimizer_fn: ImportString = Field(
|
|
218
218
|
default="torch.optim.AdamW",
|
|
219
219
|
description="Optimizer function import string. Should come from torch.optim.",
|
|
220
|
+
validate_default=True,
|
|
220
221
|
)
|
|
221
222
|
optimizer_params: dict = Field(
|
|
222
223
|
default={},
|
|
@@ -225,6 +226,7 @@ class TrainingOptimizerConfig(Config):
|
|
|
225
226
|
scheduler_fn: ImportString = Field(
|
|
226
227
|
default="torch.optim.lr_scheduler.LinearLR",
|
|
227
228
|
description="Scheduler function import string. Should come from torch.optim.lr_scheduler.",
|
|
229
|
+
validate_default=True,
|
|
228
230
|
)
|
|
229
231
|
scheduler_params: dict = Field(
|
|
230
232
|
default={},
|
|
@@ -20,20 +20,18 @@ class ConditionEmbedding(nn.Module):
|
|
|
20
20
|
self,
|
|
21
21
|
condition_channels: int,
|
|
22
22
|
embedding_dim: int,
|
|
23
|
-
padding_idx: int,
|
|
24
23
|
padding_value: float,
|
|
25
24
|
):
|
|
26
25
|
super().__init__()
|
|
27
26
|
self.padding_value = padding_value
|
|
28
|
-
self.padding_idx = padding_idx
|
|
29
27
|
self.embedding = nn.Embedding(
|
|
30
|
-
condition_channels + 1, embedding_dim, padding_idx=
|
|
28
|
+
condition_channels + 1, embedding_dim, padding_idx=0
|
|
31
29
|
)
|
|
32
30
|
self.register_buffer("indices", torch.arange(1, condition_channels + 1))
|
|
33
31
|
|
|
34
32
|
def forward(self, c: torch.Tensor):
|
|
35
33
|
indices = self.indices.repeat(c.size(0), 1).to(c.device) # type: ignore
|
|
36
|
-
masked_indices = indices.masked_fill(c == self.padding_value,
|
|
34
|
+
masked_indices = indices.masked_fill(c == self.padding_value, 0)
|
|
37
35
|
embedding = self.embedding(masked_indices)
|
|
38
36
|
return embedding * c.unsqueeze(-1).repeat(1, 1, embedding.size(-1))
|
|
39
37
|
|
|
@@ -56,7 +54,9 @@ class TransformerEmbedding(nn.Module):
|
|
|
56
54
|
max_len, embedding_dim, padding_idx
|
|
57
55
|
)
|
|
58
56
|
self.conditional_embedding = ConditionEmbedding(
|
|
59
|
-
condition_channels,
|
|
57
|
+
condition_channels,
|
|
58
|
+
embedding_dim,
|
|
59
|
+
padding_value,
|
|
60
60
|
)
|
|
61
61
|
|
|
62
62
|
def forward(self, x: torch.Tensor, c: torch.Tensor | None = None):
|
|
@@ -114,7 +114,7 @@ class Loss(nn.Module):
|
|
|
114
114
|
loc=self._loc, # type: ignore
|
|
115
115
|
covariance_matrix=self._cov, # type: ignore
|
|
116
116
|
)
|
|
117
|
-
samples = mvn.rsample((self.kernel_dist_size,))
|
|
117
|
+
samples = mvn.rsample((self.kernel_dist_size,)).to(latents.device)
|
|
118
118
|
square_difference_sum = torch.cdist(latents, samples, p=2.0).pow(2)
|
|
119
119
|
kernel_pairwise_sum = torch.exp(
|
|
120
120
|
((-1 / (self.pooling_dim * self.embedding_dim)) * square_difference_sum)
|
|
@@ -69,10 +69,11 @@ class Pipeline:
|
|
|
69
69
|
Iterable[str]: List of completed sequences, stripped of special tokens.
|
|
70
70
|
"""
|
|
71
71
|
input_ids = self.tokenizer.encode(
|
|
72
|
-
|
|
73
|
-
padding=
|
|
72
|
+
text=sequences,
|
|
73
|
+
padding='do_not_pad',
|
|
74
74
|
max_length=self.model.config_typed.input.max_len,
|
|
75
75
|
add_special_tokens=False,
|
|
76
|
+
return_tensors="pt",
|
|
76
77
|
**kwargs,
|
|
77
78
|
).to(self.device)
|
|
78
79
|
conditions = torch.as_tensor(conditions, dtype=torch.float, device=device)
|
|
@@ -144,10 +145,11 @@ class Pipeline:
|
|
|
144
145
|
Iterable[str]: List of completed sequences, stripped of special tokens.
|
|
145
146
|
"""
|
|
146
147
|
input_ids = self.tokenizer.encode(
|
|
147
|
-
|
|
148
|
-
padding=
|
|
148
|
+
text=sequences,
|
|
149
|
+
padding='do_not_pad',
|
|
149
150
|
max_length=self.model.config_typed.input.max_len,
|
|
150
151
|
add_special_tokens=False,
|
|
152
|
+
return_tensors="pt",
|
|
151
153
|
**kwargs,
|
|
152
154
|
).to(self.device)
|
|
153
155
|
input_probs = torch.zeros_like(input_ids * beam_size)
|
|
@@ -11,8 +11,6 @@ class Tokenizer(Protocol):
|
|
|
11
11
|
Provides a specification for tokenizing text for model input, and recovering text from token indices.
|
|
12
12
|
"""
|
|
13
13
|
|
|
14
|
-
vocab_size: int
|
|
15
|
-
"""Number of tokens in the tokenizer's vocabulary."""
|
|
16
14
|
bos_token: str
|
|
17
15
|
"""Token marking the beginning of a sequence."""
|
|
18
16
|
bos_token_id: int
|
|
@@ -28,20 +26,22 @@ class Tokenizer(Protocol):
|
|
|
28
26
|
|
|
29
27
|
def encode(
|
|
30
28
|
self,
|
|
31
|
-
|
|
32
|
-
padding:
|
|
29
|
+
text: Iterable[str],
|
|
30
|
+
padding: str,
|
|
33
31
|
max_length: int,
|
|
34
32
|
add_special_tokens: bool,
|
|
33
|
+
return_tensors: str,
|
|
35
34
|
**kwargs,
|
|
36
35
|
) -> torch.Tensor:
|
|
37
36
|
"""Encodes an Iterable of sequences to a tensor of indices. Optionally, adds special tokens according to a
|
|
38
37
|
template and pads the outputs to a fixed length.
|
|
39
38
|
|
|
40
39
|
Args:
|
|
41
|
-
|
|
40
|
+
text: Iterable of text sequences to encode.
|
|
42
41
|
padding: Whether to pad the sequences to a fixed length.
|
|
43
42
|
max_length: Maximum length to which sequences are padded if padding is True.
|
|
44
43
|
add_special_tokens: Whether to add special tokens according to a template.
|
|
44
|
+
return_tensors: Tensor return type, must be either 'pt' or 'np'.
|
|
45
45
|
**kwargs: Keyword arguments.
|
|
46
46
|
|
|
47
47
|
Returns:
|
|
@@ -50,12 +50,12 @@ class Tokenizer(Protocol):
|
|
|
50
50
|
...
|
|
51
51
|
|
|
52
52
|
def decode(
|
|
53
|
-
self,
|
|
53
|
+
self, token_ids: torch.Tensor, skip_special_tokens: bool, **kwargs
|
|
54
54
|
) -> Iterable[str]:
|
|
55
55
|
"""Decodes a tensor of indices to an Iterable of sequences. Optionally, skips special tokens.
|
|
56
56
|
|
|
57
57
|
Args:
|
|
58
|
-
|
|
58
|
+
token_ids: Tensor of dimension (B, S) containing vocabulary indices to decode.
|
|
59
59
|
skip_special_tokens: Whether to skip decoding special tokens when constructing outputs.
|
|
60
60
|
**kwargs: Keyword arguments.
|
|
61
61
|
|
|
@@ -78,3 +78,5 @@ class Tokenizer(Protocol):
|
|
|
78
78
|
Tokenizer: Pretrained tokenizer.
|
|
79
79
|
"""
|
|
80
80
|
...
|
|
81
|
+
|
|
82
|
+
def __len__(self) -> int: ...
|
|
@@ -69,10 +69,11 @@ class Trainer:
|
|
|
69
69
|
)
|
|
70
70
|
|
|
71
71
|
input_ids = tokenizer.encode(
|
|
72
|
-
|
|
73
|
-
padding=
|
|
72
|
+
text=sequences,
|
|
73
|
+
padding="max_length",
|
|
74
74
|
max_length=model.config_typed.input.max_len,
|
|
75
75
|
add_special_tokens=True,
|
|
76
|
+
return_tensors="pt",
|
|
76
77
|
)
|
|
77
78
|
conditions = torch.as_tensor(conditions, dtype=torch.float)
|
|
78
79
|
token_mask = (input_ids != model.config_typed.common.padding_idx).to(torch.bool)
|
|
@@ -106,10 +107,11 @@ class Trainer:
|
|
|
106
107
|
curr_epoch,
|
|
107
108
|
)
|
|
108
109
|
)
|
|
109
|
-
accelerator.register_for_checkpointing(scheduler
|
|
110
|
+
accelerator.register_for_checkpointing(scheduler)
|
|
110
111
|
|
|
111
112
|
if os.path.exists(checkpoint):
|
|
112
113
|
accelerator.load_state(checkpoint)
|
|
114
|
+
curr_epoch = scheduler.scheduler.last_epoch + 1
|
|
113
115
|
|
|
114
116
|
for epoch in range(curr_epoch, self.config_typed.common.max_epochs):
|
|
115
117
|
model.train()
|
|
File without changes
|
|
File without changes
|
|
File without changes
|