Losses¶
pytorch-widedeep accepts a number of losses and objectives that can be
passed to the Trainer class via the parameter objective
(see pytorch-widedeep.training.Trainer). For most cases the loss function
that pytorch-widedeep will use internally is already implemented in
Pytorch.
In addition, pytorch-widedeep implements a series of "custom" loss
functions. These are described below for completion since, as mentioned
before, they are used internally by the Trainer. Of course, onen could
always use them on their own and can be imported as:
from pytorch_widedeep.losses import FocalLoss
NOTE: Losses in this module expect the predictions
and ground truth to have the same dimensions for regression and binary
classification problems \((N_{samples}, 1)\). In the case of multiclass
classification problems the ground truth is expected to be a 1D tensor with
the corresponding classes. See Examples below
MSELoss ¶
Bases: Module
Mean square error loss
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions |
required |
target
|
Tensor
|
Target tensor with the actual values |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import MSELoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = MSELoss()(input, target)
Source code in pytorch_widedeep/losses.py
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | |
MSLELoss ¶
Bases: Module
Mean square log error loss
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import MSLELoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = MSLELoss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
RMSELoss ¶
Bases: Module
Root mean square error loss
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import RMSELoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = RMSELoss()(input, target)
Source code in pytorch_widedeep/losses.py
93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 | |
RMSLELoss ¶
Bases: Module
Root mean square log error loss
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import RMSLELoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = RMSLELoss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
QuantileLoss ¶
Bases: Module
Quantile loss defined as:
All credits go to the implementation at pytorch-forecasting.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
quantiles
|
List[float]
|
List of quantiles |
[0.02, 0.1, 0.25, 0.5, 0.75, 0.9, 0.98]
|
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions |
required |
target
|
Tensor
|
Target tensor with the actual values |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import QuantileLoss
>>>
>>> # REGRESSION
>>> target = torch.tensor([[0.6, 1.5]]).view(-1, 1)
>>> input = torch.tensor([[.1, .2,], [.4, .5]])
>>> qloss = QuantileLoss([0.25, 0.75])
>>> loss = qloss(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
FocalLoss ¶
Bases: Module
Implementation of the Focal loss for both binary and multiclass classification:
where, for a case of a binary classification problem
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
alpha
|
float
|
Focal Loss |
0.25
|
gamma
|
float
|
Focal Loss |
1.0
|
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import FocalLoss
>>>
>>> # BINARY
>>> target = torch.tensor([0, 1, 0, 1]).view(-1, 1)
>>> input = torch.tensor([[0.6, 0.7, 0.3, 0.8]]).t()
>>> loss = FocalLoss()(input, target)
>>>
>>> # MULTICLASS
>>> target = torch.tensor([1, 0, 2]).view(-1, 1)
>>> input = torch.tensor([[0.2, 0.5, 0.3], [0.8, 0.1, 0.1], [0.7, 0.2, 0.1]])
>>> loss = FocalLoss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
BayesianSELoss ¶
Bases: Module
Squared Loss (log Gaussian) for the case of a regression as specified in the original publication Weight Uncertainty in Neural Networks.
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import BayesianSELoss
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = BayesianSELoss()(input, target)
Source code in pytorch_widedeep/losses.py
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 | |
TweedieLoss ¶
Bases: Module
Tweedie loss for extremely unbalanced zero-inflated data
All credits go to Wenbo Shi. See this post and the original publication for details.
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target, p=1.5)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions |
required |
target
|
Tensor
|
Target tensor with the actual values |
required |
p
|
float
|
the power to be used to compute the loss. See the original publication for details |
1.5
|
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import TweedieLoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = TweedieLoss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
ZILNLoss ¶
Bases: Module
Adjusted implementation of the Zero Inflated LogNormal Loss
See A Deep Probabilistic Model for Customer Lifetime Value Prediction and the corresponding code.
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions with spape (N,3), where N is the batch size |
required |
target
|
Tensor
|
Target tensor with the actual target values |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import ZILNLoss
>>>
>>> target = torch.tensor([[0., 1.5]]).view(-1, 1)
>>> input = torch.tensor([[.1, .2, .3], [.4, .5, .6]])
>>> loss = ZILNLoss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
L1Loss ¶
Bases: Module
L1 loss
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions |
required |
target
|
Tensor
|
Target tensor with the actual values |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import L1Loss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = L1Loss()(input, target)
Source code in pytorch_widedeep/losses.py
475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 | |
FocalR_L1Loss ¶
Bases: Module
Focal-R L1 loss
Based on Delving into Deep Imbalanced Regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta
|
float
|
Focal Loss |
0.2
|
gamma
|
float
|
Focal Loss |
1.0
|
activation_fn
|
Literal[sigmoid, tanh]
|
Activation function to be used during the computation of the loss. Possible values are 'sigmoid' and 'tanh'. See the original publication for details. |
'sigmoid'
|
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import FocalR_L1Loss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = FocalR_L1Loss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
FocalR_MSELoss ¶
Bases: Module
Focal-R MSE loss
Based on Delving into Deep Imbalanced Regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta
|
float
|
Focal Loss |
0.2
|
gamma
|
float
|
Focal Loss |
1.0
|
activation_fn
|
Literal[sigmoid, tanh]
|
Activation function to be used during the computation of the loss. Possible values are 'sigmoid' and 'tanh'. See the original publication for details. |
'sigmoid'
|
Source code in pytorch_widedeep/losses.py
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 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import FocalR_MSELoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = FocalR_MSELoss()(input, target)
Source code in pytorch_widedeep/losses.py
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 | |
FocalR_RMSELoss ¶
Bases: Module
Focal-R RMSE loss
Based on Delving into Deep Imbalanced Regression.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
beta
|
float
|
Focal Loss |
0.2
|
gamma
|
float
|
Focal Loss |
1.0
|
activation_fn
|
Literal[sigmoid, tanh]
|
Activation function to be used during the computation of the loss. Possible values are 'sigmoid' and 'tanh'. See the original publication for details. |
'sigmoid'
|
Source code in pytorch_widedeep/losses.py
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import FocalR_RMSELoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = FocalR_RMSELoss()(input, target)
Source code in pytorch_widedeep/losses.py
656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 | |
HuberLoss ¶
Bases: Module
Hubbler Loss
Based on Delving into Deep Imbalanced Regression.
Source code in pytorch_widedeep/losses.py
693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
forward ¶
forward(input, target)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input
|
Tensor
|
Input tensor with predictions (not probabilities) |
required |
target
|
Tensor
|
Target tensor with the actual classes |
required |
Examples:
>>> import torch
>>>
>>> from pytorch_widedeep.losses import HuberLoss
>>>
>>> target = torch.tensor([1, 1.2, 0, 2]).view(-1, 1)
>>> input = torch.tensor([0.6, 0.7, 0.3, 0.8]).view(-1, 1)
>>> loss = HuberLoss()(input, target)
Source code in pytorch_widedeep/losses.py
703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 | |
InfoNCELoss ¶
Bases: Module
InfoNCE Loss. Loss applied during the Contrastive Denoising Self Supervised Pre-training routine available in this library
NOTE: This loss is in principle not exposed to
the user, as it is used internally in the library, but it is included
here for completion.
See SAINT: Improved Neural Networks for Tabular Data via Row Attention and Contrastive Pre-Training and references therein
Partially inspired by the code in this repo
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
temperature
|
float
|
The logits are divided by the temperature before computing the loss value |
0.1
|
reduction
|
str
|
Loss reduction method |
'mean'
|
Source code in pytorch_widedeep/losses.py
734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 | |
forward ¶
forward(g_projs)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
g_projs
|
Tuple[Tensor, Tensor]
|
Tuple with the two tensors corresponding to the output of the two projection heads, as described 'SAINT: Improved Neural Networks for Tabular Data via Row Attention and Contrastive Pre-Training'. |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import InfoNCELoss
>>> g_projs = (torch.rand(3, 5, 16), torch.rand(3, 5, 16))
>>> loss = InfoNCELoss()
>>> res = loss(g_projs)
Source code in pytorch_widedeep/losses.py
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 | |
DenoisingLoss ¶
Bases: Module
Denoising Loss. Loss applied during the Contrastive Denoising Self Supervised Pre-training routine available in this library
NOTE: This loss is in principle not exposed to
the user, as it is used internally in the library, but it is included
here for completion.
See SAINT: Improved Neural Networks for Tabular Data via Row Attention and Contrastive Pre-Training and references therein
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
lambda_cat
|
float
|
Multiplicative factor that will be applied to loss associated to the categorical features |
1.0
|
lambda_cont
|
float
|
Multiplicative factor that will be applied to loss associated to the continuous features |
1.0
|
reduction
|
str
|
Loss reduction method |
'mean'
|
Source code in pytorch_widedeep/losses.py
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 | |
forward ¶
forward(x_cat_and_cat_, x_cont_and_cont_)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_cat_and_cat_
|
Optional[Union[List[Tuple[Tensor, Tensor]], Tuple[Tensor, Tensor]]]
|
Tuple of tensors containing the raw input features and their
encodings, referred in the SAINT paper as \(x\) and \(x''\)
respectively. If one denoising MLP is used per categorical
feature |
required |
x_cont_and_cont_
|
Optional[Union[List[Tuple[Tensor, Tensor]], Tuple[Tensor, Tensor]]]
|
same as |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import DenoisingLoss
>>> x_cat_and_cat_ = (torch.empty(3).random_(3).long(), torch.randn(3, 3))
>>> x_cont_and_cont_ = (torch.randn(3, 1), torch.randn(3, 1))
>>> loss = DenoisingLoss()
>>> res = loss(x_cat_and_cat_, x_cont_and_cont_)
Source code in pytorch_widedeep/losses.py
829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 | |
EncoderDecoderLoss ¶
Bases: Module
'Standard' Encoder Decoder Loss. Loss applied during the Endoder-Decoder Self-Supervised Pre-Training routine available in this library
NOTE: This loss is in principle not exposed to
the user, as it is used internally in the library, but it is included
here for completion.
The implementation of this lost is based on that at the tabnet repo, which is in itself an adaptation of that in the original paper TabNet: Attentive Interpretable Tabular Learning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
eps
|
float
|
Simply a small number to avoid dividing by zero |
1e-09
|
Source code in pytorch_widedeep/losses.py
908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
forward ¶
forward(x_true, x_pred, mask)
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
x_true
|
Tensor
|
Embeddings of the input data |
required |
x_pred
|
Tensor
|
Reconstructed embeddings |
required |
mask
|
Tensor
|
Mask with 1s indicated that the reconstruction, and therefore the loss, is based on those features. |
required |
Examples:
>>> import torch
>>> from pytorch_widedeep.losses import EncoderDecoderLoss
>>> x_true = torch.rand(3, 3)
>>> x_pred = torch.rand(3, 3)
>>> mask = torch.empty(3, 3).random_(2)
>>> loss = EncoderDecoderLoss()
>>> res = loss(x_true, x_pred, mask)
Source code in pytorch_widedeep/losses.py
931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 | |
MultiTargetRegressionLoss ¶
Bases: Module
This class is a wrapper around the Pytorch MSELoss. It allows for multi-target regression problems. The user can provide a list of weights to apply to each target. The loss can be either the sum or the mean of the individual losses
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
weights
|
Optional[List[float]]
|
List of weights to apply to the loss associated to each target. The length of the list must match the number of targets. Alias: 'target_weights' |
None
|
reduction
|
Literal[mean, sum]
|
Specifies the reduction to apply to the loss associated to each target: 'mean' | 'sum'. Note that this is NOT the same as the reduction in the MSELoss. This reduction is applied after the loss for each target has been computed. Alias: 'target_reduction' |
'mean'
|
Examples:
>>> import torch
>>> from pytorch_widedeep.losses_multitarget import MultiTargetRegressionLoss
>>> input = torch.randn(3, 2)
>>> target = torch.randn(3, 2)
>>> loss = MultiTargetRegressionLoss(weights=[0.5, 0.5], reduction="mean")
>>> output = loss(input, target)
Source code in pytorch_widedeep/losses_multitarget.py
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 | |
MultiTargetClassificationLoss ¶
Bases: Module
This class is a wrapper around the Pytorch binary_cross_entropy_with_logits and cross_entropy losses. It allows for multi-target classification problems. The user can provide a list of weights to apply to each target. The loss can be either the sum or the mean of the individual losses
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
binary_config
|
Optional[List[Union[int, Tuple[int, float]]]]
|
List of integers with the index of the target for binary classification or tuples with two elements: the index of the targets or binary classification and the positive weight for binary classification |
None
|
multiclass_config
|
Optional[List[Union[Tuple[int, int], Tuple[int, int, List[float]]]]]
|
List of tuples with two or three elements: the index of the target and the number of classes for multiclass classification, or a tuple with the index of the target, the number of classes and a list of weights to apply to each class (i.e. the 'weight' parameter in the cross_entropy loss) |
None
|
weights
|
Optional[List[float]]
|
List of weights to apply to the loss associated to each target. The length of the list must match the number of targets. Alias: 'target_weights' |
None
|
reduction
|
Literal[mean, sum]
|
Specifies the reduction to apply to the loss associated to each target: 'mean' | 'sum'. Note that this is NOT the same as the reduction in the cross_entropy loss or the binary_cross_entropy_with_logits. This reduction is applied after the loss for each target has been computed. Alias: 'target_reduction' |
'mean'
|
binary_trick
|
bool
|
If True, each target will be considered independently and the loss will be computed as binary_cross_entropy_with_logits. This is a faster implementation. Note that the 'weights' parameter is not compatible with binary_trick=True. Also note that if binary_trick=True, the 'binary_config' must be a list of integers and the 'multiclass_config' must be a list of tuples with two integers: the index of the target and the number of classes. Finally, if binary_trick=True, the binary targets must be the first targets in the target tensor.
|
False
|
Examples:
>>> import torch
>>> from pytorch_widedeep.losses_multitarget import MultiTargetClassificationLoss
>>> input = torch.randn(5, 4)
>>> input_binary_trick = torch.randn(5, 5)
>>> target = torch.stack([torch.tensor([0, 1, 0, 1, 1]), torch.tensor([0, 1, 2, 0, 2])], 1)
>>> loss_1 = MultiTargetClassificationLoss(binary_config=[0], multiclass_config=[(1, 3)], reduction="mean")
>>> output_1 = loss_1(input, target)
>>> loss_2 = MultiTargetClassificationLoss(binary_config=[(0, 0.5)], multiclass_config=[(1, 3, [1., 2., 3.])],
... reduction="sum", weights=[0.5, 0.5])
>>> output_2 = loss_2(input, target)
>>> loss_3 = MultiTargetClassificationLoss(binary_config=[0], multiclass_config=[(1, 3)], binary_trick=True)
>>> output_3 = loss_3(input_binary_trick, target)
Source code in pytorch_widedeep/losses_multitarget.py
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 | |
MutilTargetRegressionAndClassificationLoss ¶
Bases: Module
This class is a wrapper around the MultiTargetRegressionLoss and the MultiTargetClassificationLoss. It allows for multi-target regression and classification problems. The user can provide a list of weights to apply to each target. The loss can be either the sum or the mean of the individual losses
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
regression_config
|
List[int]
|
List of integers with the indices of the regression targets |
[]
|
binary_config
|
Optional[List[Union[int, Tuple[int, float]]]]
|
List of integers with the index of the target for binary classification or tuples with two elements: the index of the targets or binary classification and the positive weight for binary classification |
None
|
multiclass_config
|
Optional[List[Union[Tuple[int, int], Tuple[int, int, List[float]]]]]
|
List of tuples with two or three elements: the index of the target and the number of classes for multiclass classification, or a tuple with the index of the target, the number of classes and a list of weights to apply to each class (i.e. the 'weight' parameter in the cross_entropy loss) |
None
|
weights
|
Optional[List[float]]
|
List of weights to apply to the loss associated to each target. The length of the list must match the number of targets. Alias: 'target_weights' |
None
|
reduction
|
Literal[mean, sum]
|
Specifies the reduction to apply to the output: 'mean' | 'sum'. Note that this is NOT the same as the reduction in the cross_entropy loss, the binary_cross_entropy_with_logits or the MSELoss. This reduction is applied after each target has been computed. Alias: 'target_reduction' |
'mean'
|
binary_trick
|
bool
|
If True, each target will be considered independently and the loss will be computed as binary_cross_entropy_with_logits. This is a faster implementation. Note that the 'weights' parameter is not compatible with binary_trick=True. Also note that if binary_trick=True, the 'binary_config' must be a list of integers and the 'multiclass_config' must be a list of tuples with two integers: the index of the target and the number of classes. Finally, if binary_trick=True, the binary targets must be the first targets in the target tensor.
|
False
|
Examples:
>>> import torch
>>> from pytorch_widedeep.losses_multitarget import MutilTargetRegressionAndClassificationLoss
>>> input = torch.randn(5, 5)
>>> target = torch.stack([torch.randn(5), torch.tensor([0, 1, 0, 1, 1]), torch.tensor([0, 1, 2, 0, 2])], 1)
>>> loss = MutilTargetRegressionAndClassificationLoss(regression_config=[0], binary_config=[2],
... multiclass_config=[(2, 3)], reduction="mean")
>>> output = loss(input, target)
Source code in pytorch_widedeep/losses_multitarget.py
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 | |