-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathloss.py
More file actions
421 lines (358 loc) · 15.1 KB
/
Copy pathloss.py
File metadata and controls
421 lines (358 loc) · 15.1 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
"""Loss components.
Members are explicitly re-exported in pyprobound.
"""
import abc
import dataclasses
import itertools
from collections.abc import Iterable, Iterator
from typing import Generic, Literal, NamedTuple, TypeVar, cast
import torch
from torch import Tensor
from typing_extensions import override
from .base import Component, Transform
from .containers import TModuleList
from .experiment import Experiment
from .table import Batch, CountBatch, Table
from .utils import get_split_size
T = TypeVar("T", bound=Batch)
class Loss(NamedTuple):
"""A loss value, interpreted as the sum of both elements.
Attributes:
negloglik: The negative log likelihood.
regularization: The regularization value.
"""
negloglik: Tensor
regularization: Tensor
class BaseLoss(Component, Generic[T]):
"""Transform that calculates loss.
Attributes:
transforms (TModuleList[Transform]): The components to be jointly
optimized.
"""
def __init__(
self,
components: Iterable[Transform],
weights: Iterable[float] | None = None,
lambda_l2: float = 1e-6,
lambda_l1: float = 0,
exponential_bound: float = 40,
dilute_regularization: bool = False,
exclude_regularization: Iterable[str] = tuple(),
equalize_contribution: bool = False,
max_split: int | None = None,
) -> None:
"""Initializes the multitask loss from an iterable of components.
Args:
transforms: The components to be jointly optimized.
weights: Multiplier to the NLL of each corresponding component.
lambda_l2: L2 regularization hyperparameter.
lambda_l1: L1 regularization hyperparameter.
exponential_bound: Value of exponential barrier.
dilute_regularization: Whether to keep hyperparameters fixed as the
number of components increases.
exclude_regularization: Keywords used to exclude parameters from
regularization.
equalize_contribution: Whether to update the weights so that the
rescaled losses are constant relative to each other.
max_split: Maximum number of sequences scored at a time.
"""
super().__init__(name="")
# Store loss attributes
self.transforms = TModuleList(components)
self.lambda_l2 = lambda_l2
self.lambda_l1 = lambda_l1
self.exponential_bound = exponential_bound
self.dilute_regularization = dilute_regularization
self.exclude_regularization = tuple(exclude_regularization)
self.equalize_contribution = equalize_contribution
self.max_split = max_split
# Update names
for transform_idx, transform in enumerate(self.transforms):
if transform.name == "":
transform.name = str(transform_idx)
# Store scaling factor for loss of each experiment
if weights is None:
weights = [1 / len(self.transforms)] * len(self.transforms)
else:
weights = list(weights)
self.weights = weights
if len(self.weights) != len(self.transforms):
raise ValueError(
f"Length of weights {len(self.weights)} does not match"
f" number of components {len(self.transforms)}"
)
@override
def components(self) -> Iterator[Transform]:
return iter(self.transforms)
@abc.abstractmethod
def negloglik(
self, transform: Transform, batch: T
) -> tuple[Tensor, Tensor | float]:
"""Calculates the negative log-likelihood plus a normalization factor.
Args:
transform: The component used for scoring.
batch: The batch to be scored.
Returns:
A tuple of scalar tensors (negloglik, norm), where negloglik is the
negative log-likelihood of the batch and norm is a scaling factor.
A running sum of each is kept, and the loss is the ratio.
"""
def get_setup_string(self) -> str:
"""A description used when printing the output of an optimizer."""
out = [
"### Regularization:",
f"\t L1 Lambda: {self.lambda_l1}",
f"\t L2 Lambda: {self.lambda_l2}",
f"\t Exponential Bound: {self.exponential_bound}",
f"\t Excluded Reg.: {self.exclude_regularization}",
f"\t Eq. Contribution: {self.equalize_contribution}",
f"\t Weights: {self.weights}",
]
out.append("\n### Transforms:")
for transform in self.transforms:
out.append(f"\t{str(transform)}")
out.append("\n### Binding:")
for binding_idx, (binding, _) in enumerate(
self.optim_procedure().items()
):
binding_str = "-".join(str(i) for i in binding)
binding_str = binding_str.replace("\n", "\n\t\t")
out.extend([f"\t Mode {binding_idx}: {binding_str}"])
return "\n".join(out)
def regularization(self, component: Component) -> Tensor:
"""Calculates parameter regularization.
Args:
component: The component containing parameters to be regularized.
Returns:
The regularization value as a scalar tensor.
"""
# Get flattened parameter vector
param_list = []
for name, param in component.named_parameters():
if torch.any(torch.isneginf(param)):
continue
if any(exclude in name for exclude in self.exclude_regularization):
continue
param_list.append(param.flatten())
param_vec = torch.cat(param_list)
regularization = torch.tensor(0.0, device=param_vec.device)
# L2 regularization
if self.lambda_l2 > 0:
regularization += self.lambda_l2 * param_vec.square().sum()
# L1 regularization
if self.lambda_l1 > 0:
regularization += self.lambda_l1 * param_vec.abs().sum()
# Exponential barrier
if self.exponential_bound != float("inf"):
regularization += torch.sum(
torch.exp(param_vec - self.exponential_bound)
+ torch.exp(-param_vec - self.exponential_bound)
)
return regularization
@override
def forward(self, batches: Iterable[T]) -> Loss:
"""Calculates the multitask weighted loss and regularization.
Args:
batches: Iterable of batches to calculate the loss against.
Returns:
A NamedTuple with attributes `negloglik` and `regularization`,
both as scalar tensors.
"""
for param in self.parameters():
device = param.device
break
neglogliks: list[Tensor] = []
try:
# Calculate loss for each component
for transform, batch in zip(self.transforms, batches, strict=True):
split_size = get_split_size(
self.max_embedding_size(),
(
batch.batchlen()
if self.max_split is None
else min(self.max_split, batch.batchlen())
),
device,
)
# Split calculation into minibatches of split_size
curr_nll = torch.tensor(0.0, device=device)
curr_norm = torch.tensor(0.0, device=device)
for elements in zip(
*(
( # Split tensors according to split_size
torch.split(i, split_size)
if isinstance(i, Tensor)
else itertools.repeat(i)
)
for i in ( # Generator of fields in a dataclass
getattr(batch, field.name)
for field in dataclasses.fields(batch)
)
)
):
# Get batch type (Table could inherit from Batch)
batch_type = type(batch)
if issubclass(batch_type, Table):
for base_type in batch_type.__bases__: # type: ignore[unreachable]
if issubclass(base_type, Batch):
batch_type = base_type
# Create new batch, moving fields to device
batch = batch_type(
*(
i.to(device) if isinstance(i, Tensor) else i
for i in elements
)
)
# Update nll and norm
negloglik, norm = self.negloglik(transform, batch)
curr_nll += negloglik
curr_norm += norm
neglogliks.append(curr_nll / curr_norm)
except ValueError as e:
if str(e).startswith("zip"):
raise ValueError(
"Length of components and batches may not match"
) from e
raise e
# Get scaling factors for each component
weights = self.weights
if self.equalize_contribution:
with torch.inference_mode():
old_sum = sum(
w * nll.item() for w, nll in zip(weights, neglogliks)
)
weights = [
w / nll.item() for w, nll in zip(weights, neglogliks)
]
new_sum = sum(
w * nll.item() for w, nll in zip(weights, neglogliks)
)
weights = [w * old_sum / new_sum for w in weights]
# Get regularization
if self.dilute_regularization:
regularization: Tensor | Literal[0] = self.regularization(self)
else:
regularization = sum(
w * self.regularization(transform)
for w, transform in zip(weights, self.transforms)
)
# Multiply losses by weights
final_nll = sum(w * nll for w, nll in zip(weights, neglogliks))
return Loss(cast(Tensor, final_nll), cast(Tensor, regularization))
@override
def __call__(self, batches: Iterable[T]) -> Loss:
"""See https://github.com/pytorch/pytorch/issues/45414."""
return cast(Loss, super().__call__(batches))
class MultiExperimentLoss(BaseLoss[CountBatch]):
"""Multitask optimization of multiple count tables with a Poisson loss.
Attributes:
transforms (TModuleList[Experiment]): The experiments to be jointly
optimized.
"""
def __init__(
self,
components: Iterable[Experiment],
weights: Iterable[float] | None = None,
lambda_l2: float = 1e-6,
lambda_l1: float = 0,
pseudocount: float = 0,
exponential_bound: float = 40,
full_loss: bool = False,
dilute_regularization: bool = False,
exclude_regularization: Iterable[str] = tuple(),
equalize_contribution: bool = False,
max_split: int | None = None,
) -> None:
"""Initializes the multitask loss from an iterable of experiments.
Args:
components: The experiments to be jointly optimized.
weights: Multiplier to the NLL of each corresponding experiment.
lambda_l2: L2 regularization hyperparameter.
lambda_l1: L1 regularization hyperparameter.
pseudocount: Scaling factor for Dirichlet-inspired regularization.
exponential_bound: Value of exponential barrier.
full_loss: Whether to compute the constant terms of the NLL.
dilute_regularization: Whether to keep hyperparameters fixed as the
number of experiments increases.
exclude_regularization: Keywords used to exclude parameters from
regularization.
equalize_contribution: Whether to update the weights so that the
rescaled losses are constant relative to each other.
max_split: Maximum number of sequences scored at a time.
"""
super().__init__(
components=components,
weights=weights,
lambda_l2=lambda_l2,
lambda_l1=lambda_l1,
exponential_bound=exponential_bound,
dilute_regularization=dilute_regularization,
exclude_regularization=exclude_regularization,
equalize_contribution=equalize_contribution,
max_split=max_split,
)
self.full_loss = full_loss
self.pseudocount = pseudocount
# Maintain compatability with older fits where `transforms` attribute
# was called `experiments` while avoiding registering as a submodule
object.__setattr__( # Equiv. to `self.experiments = self.transforms`
self, "experiments", self.transforms
)
@override
def regularization(self, component: Component) -> Tensor:
"""Calculates parameter regularization.
Args:
transform: The component containing parameters to be regularized.
Returns:
The regularization value as a scalar tensor.
"""
regularization = super().regularization(component)
# Dirichlet regularization
if self.pseudocount > 0:
# Get normalization value
if component is self:
norm = sum(
self.pseudocount / sum(expt.counts_per_round)
for expt in self.transforms
) / len(self.transforms)
else:
norm = self.pseudocount / sum(component.counts_per_round)
# Calculate PDF
log_pdf = torch.tensor(0.0, device=regularization.device)
for module in self.modules():
if hasattr(module, "get_dirichlet"):
log_pdf += module.get_dirichlet()
regularization -= log_pdf * norm
return regularization
@override
def negloglik(
self, transform: Transform, batch: CountBatch
) -> tuple[Tensor, Tensor]:
if self.full_loss:
loglik = (
(batch.target * transform(batch.seqs))
+ (
batch.target
* torch.log(batch.target.sum(dim=1, keepdim=True))
)
- batch.target
- torch.lgamma(batch.target + 1)
)
else:
loglik = batch.target * transform(batch.seqs)
return -torch.sum(loglik), torch.sum(batch.target)
class MultiRoundMSLELoss(BaseLoss[CountBatch]):
"""Multitask optimization of intensity experiments with a MSLE loss.
Attributes:
transforms (TModuleList[BaseRound]): The rounds to be jointly
optimized.
"""
@override
def negloglik(
self, transform: Transform, batch: CountBatch
) -> tuple[Tensor, int]:
assert batch.target.ndim == 2 and batch.target.shape[-1] == 1
assert transform.reference_round is None
return torch.sum(
torch.square(torch.log(batch.target[:, 0]) - transform(batch.seqs))
), len(batch.target)