layers
pytorch_lattice.layers.CategoricalCalibrator
Bases: ConstrainedModule
A categorical calibrator.
This module takes an input of shape (batch_size, 1)
and calibrates it by mapping a
given category to its learned output value. The output will have the same shape as
the input.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
kernel |
|
Example:
inputs = torch.tensor(...) # shape: (batch_size, 1)
calibrator = CategoricalCalibrator(
num_categories=5,
missing_input_value=-1,
output_min=0.0
output_max=1.0,
monotonicity_pairs=[(0, 1), (1, 2)],
kernel_init=CateegoricalCalibratorInit.UNIFORM,
)
outputs = calibrator(inputs)
Source code in pytorch_lattice/layers/categorical_calibrator.py
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 |
|
__init__(num_categories, missing_input_value=None, output_min=None, output_max=None, monotonicity_pairs=None, kernel_init=CategoricalCalibratorInit.UNIFORM)
Initializes an instance of CategoricalCalibrator
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
num_categories |
int
|
The number of known categories. |
required |
missing_input_value |
Optional[float]
|
If provided, the calibrator will learn to map all
instances of this missing input value to a learned output value just
the same as it does for known categories. Note that |
None
|
output_min |
Optional[float]
|
Minimum output value. If |
None
|
output_max |
Optional[float]
|
Maximum output value. If |
None
|
monotonicity_pairs |
Optional[list[tuple[int, int]]]
|
List of pairs of indices |
None
|
kernel_init |
CategoricalCalibratorInit
|
Initialization scheme to use for the kernel. |
UNIFORM
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
ValueError
|
If |
Source code in pytorch_lattice/layers/categorical_calibrator.py
apply_constraints()
Projects kernel into desired constraints.
Source code in pytorch_lattice/layers/categorical_calibrator.py
assert_constraints(eps=1e-06)
Asserts that layer satisfies specified constraints.
This checks that weights at the indexes of monotonicity pairs are in the correct order and that the output is within bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
the margin of error allowed |
1e-06
|
Returns:
Type | Description |
---|---|
list[str]
|
A list of messages describing violated constraints including violated |
list[str]
|
monotonicity pairs. If no constraints violated, the list will be empty. |
Source code in pytorch_lattice/layers/categorical_calibrator.py
forward(x)
Calibrates categorical inputs through a learned mapping.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor of category indices of shape |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor of shape |
Source code in pytorch_lattice/layers/categorical_calibrator.py
keypoints_inputs()
Returns a tensor of keypoint inputs (category indices).
Source code in pytorch_lattice/layers/categorical_calibrator.py
pytorch_lattice.layers.Lattice
Bases: ConstrainedModule
A Lattice Module.
Layer performs interpolation using one of 'units' d-dimensional lattices with arbitrary number of keypoints per dimension. Each lattice vertex has a trainable weight, and input is considered to be a d-dimensional point within the lattice.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
kernel |
|
Example:
lattice_sizes = [2, 2, 4, 3]
inputs=torch.tensor(...) # shape: (batch_size, len(lattice_sizes))
lattice=Lattice(
lattice_sizes,
clip_inputs=True,
interpolation=Interpolation.HYPERCUBE,
units=1,
)
outputs = Lattice(inputs)
Source code in pytorch_lattice/layers/lattice.py
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 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 |
|
__init__(lattice_sizes, output_min=None, output_max=None, kernel_init=LatticeInit.LINEAR, monotonicities=None, clip_inputs=True, interpolation=Interpolation.HYPERCUBE, units=1)
Initializes an instance of 'Lattice'.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
lattice_sizes |
Union[list[int], tuple[int]]
|
List or tuple of size of lattice along each dimension. |
required |
output_min |
Optional[float]
|
Minimum output value for weights at vertices of lattice. |
None
|
output_max |
Optional[float]
|
Maximum output value for weights at vertices of lattice. |
None
|
kernel_init |
LatticeInit
|
Initialization scheme to use for the kernel. |
LINEAR
|
monotonicities |
Optional[list[Optional[Monotonicity]]]
|
|
None
|
clip_inputs |
bool
|
Whether input points should be clipped to the range of lattice. |
True
|
interpolation |
Interpolation
|
Interpolation scheme for a given input. |
HYPERCUBE
|
units |
int
|
Dimensionality of weights stored at each vertex of lattice. |
1
|
Raises:
Type | Description |
---|---|
ValueError
|
if |
NotImplementedError
|
Random monotonic initialization not yet implemented. |
Source code in pytorch_lattice/layers/lattice.py
apply_constraints()
Aggregate function for enforcing constraints of lattice.
Source code in pytorch_lattice/layers/lattice.py
assert_constraints(eps=1e-06)
Asserts that layer satisfies specified constraints.
This checks that weights follow monotonicity and bounds constraints.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
the margin of error allowed |
1e-06
|
Returns:
Type | Description |
---|---|
list[str]
|
A list of dicts describing violated constraints including indices of |
list[str]
|
monotonicity violations. If no constraints violated, the list will be empty. |
Source code in pytorch_lattice/layers/lattice.py
forward(x)
Calculates interpolation from input, using method of self.interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Union[Tensor, list[Tensor]]
|
input tensor. If |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor of shape |
Tensor
|
values. |
Raises:
Type | Description |
---|---|
ValueError
|
If the type of interpolation is unknown. |
Source code in pytorch_lattice/layers/lattice.py
pytorch_lattice.layers.Linear
Bases: ConstrainedModule
A constrained linear module.
This module takes an input of shape (batch_size, input_dim)
and applied a linear
transformation. The output will have the same shape as the input.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
kernel |
|
|
bias |
|
Example:
input_dim = 3
inputs = torch.tensor(...) # shape: (batch_size, input_dim)
linear = Linear(
input_dim,
monotonicities=[
None,
Monotonicity.INCREASING,
Monotonicity.DECREASING
],
use_bias=False,
weighted_average=True,
)
outputs = linear(inputs)
Source code in pytorch_lattice/layers/linear.py
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 |
|
__init__(input_dim, monotonicities=None, use_bias=True, weighted_average=False)
Initializes an instance of Linear
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_dim |
int
|
The number of inputs that will be combined. |
required |
monotonicities |
Optional[list[Optional[Monotonicity]]]
|
If provided, specifies the monotonicity of each input dimension. |
None
|
use_bias |
bool
|
Whether to use a bias term for the linear combination. |
True
|
weighted_average |
bool
|
Whether to make the output a weighted average i.e. all
coefficients are positive and add up to a total of 1.0. No bias term
will be used, and |
False
|
Raises:
Type | Description |
---|---|
ValueError
|
If monotonicities does not have length input_dim (if provided). |
Source code in pytorch_lattice/layers/linear.py
apply_constraints()
Projects kernel into desired constraints.
Source code in pytorch_lattice/layers/linear.py
assert_constraints(eps=1e-06)
Asserts that layer satisfies specified constraints.
This checks that decreasing monotonicity corresponds to negative weights, increasing monotonicity corresponds to positive weights, and weights sum to 1 for weighted_average=True.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
the margin of error allowed |
1e-06
|
Returns:
Type | Description |
---|---|
list[str]
|
A list of messages describing violated constraints. If no constraints |
list[str]
|
violated, the list will be empty. |
Source code in pytorch_lattice/layers/linear.py
forward(x)
Transforms inputs using a linear combination.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor of shape |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor of shape |
Source code in pytorch_lattice/layers/linear.py
pytorch_lattice.layers.NumericalCalibrator
Bases: ConstrainedModule
A numerical calibrator.
This module takes an input of shape (batch_size, 1)
and calibrates it using a
piece-wise linear function that conforms to any provided constraints. The output
will have the same shape as the input.
Attributes:
Name | Type | Description |
---|---|---|
All |
|
|
kernel |
|
|
missing_output |
|
Example:
inputs = torch.tensor(...) # shape: (batch_size, 1)
calibrator = NumericalCalibrator(
input_keypoints=np.linspace(1., 5., num=5),
output_min=0.0,
output_max=1.0,
monotonicity=Monotonicity.INCREASING,
kernel_init=NumericalCalibratorInit.EQUAL_HEIGHTS,
)
outputs = calibrator(inputs)
Source code in pytorch_lattice/layers/numerical_calibrator.py
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 422 423 424 425 426 427 428 429 430 |
|
__init__(input_keypoints, missing_input_value=None, output_min=None, output_max=None, monotonicity=None, kernel_init=NumericalCalibratorInit.EQUAL_HEIGHTS, projection_iterations=8, input_keypoints_type=InputKeypointsType.FIXED)
Initializes an instance of NumericalCalibrator
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_keypoints |
ndarray
|
Ordered list of float-valued keypoints for the underlying piece-wise linear function. |
required |
missing_input_value |
Optional[float]
|
If provided, the calibrator will learn to map all instances of this missing input value to a learned output value. |
None
|
output_min |
Optional[float]
|
Minimum output value. If |
None
|
output_max |
Optional[float]
|
Maximum output value. If |
None
|
monotonicity |
Optional[Monotonicity]
|
Monotonicity constraint for the underlying piece-wise linear function. |
None
|
kernel_init |
NumericalCalibratorInit
|
Initialization scheme to use for the kernel. |
EQUAL_HEIGHTS
|
projection_iterations |
int
|
Number of times to run Dykstra's projection algorithm when applying constraints. |
8
|
input_keypoints_type |
InputKeypointsType
|
|
FIXED
|
Raises:
Type | Description |
---|---|
ValueError
|
If |
Source code in pytorch_lattice/layers/numerical_calibrator.py
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 |
|
apply_constraints()
Jointly projects kernel into desired constraints.
Uses Dykstra's alternating projection algorithm to jointly project onto all given constraints. This algorithm projects with respect to the L2 norm, but it approached the norm from the "wrong" side. To ensure that all constraints are strictly met, we do final approximate projections that project strictly into the feasible space, but this is not an exact projection with respect to the L2 norm. Enough iterations make the impact of this approximation negligible.
Source code in pytorch_lattice/layers/numerical_calibrator.py
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 |
|
assert_constraints(eps=1e-06)
Asserts that layer satisfies specified constraints.
This checks that weights follow monotonicity constraints and that the output is within bounds.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
eps |
float
|
the margin of error allowed |
1e-06
|
Returns:
Type | Description |
---|---|
list[str]
|
A list of messages describing violated constraints including indices of |
list[str]
|
monotonicity violations. If no constraints violated, the list will be empty. |
Source code in pytorch_lattice/layers/numerical_calibrator.py
forward(x)
Calibrates numerical inputs through piece-wise linear interpolation.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x |
Tensor
|
The input tensor of shape |
required |
Returns:
Type | Description |
---|---|
Tensor
|
torch.Tensor of shape |
Source code in pytorch_lattice/layers/numerical_calibrator.py
keypoints_inputs()
Returns tensor of keypoint inputs.