1 #pragma once 2 3 #include <torch/arg.h> 4 #include <torch/csrc/Export.h> 5 #include <torch/enum.h> 6 #include <torch/types.h> 7 8 namespace torch { 9 namespace nn { 10 11 /// Options for the `L1Loss` module. 12 /// 13 /// Example: 14 /// ``` 15 /// L1Loss model(L1LossOptions(torch::kNone)); 16 /// ``` 17 struct TORCH_API L1LossOptions { 18 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 19 reduction_t; 20 21 TORCH_OPTIONS_CTOR_VARIANT_ARG3(L1LossOptions, reduction, kNone, kMean, kSum) 22 23 /// Specifies the reduction to apply to the output. 24 TORCH_ARG(reduction_t, reduction) = torch::kMean; 25 }; 26 27 namespace functional { 28 /// Options for `torch::nn::functional::l1_loss`. 29 /// 30 /// See the documentation for `torch::nn::L1LossOptions` class to learn what 31 /// arguments are supported. 32 /// 33 /// Example: 34 /// ``` 35 /// namespace F = torch::nn::functional; 36 /// F::l1_loss(input, target, F::L1LossFuncOptions(torch::kNone)); 37 /// ``` 38 using L1LossFuncOptions = L1LossOptions; 39 } // namespace functional 40 41 // ============================================================================ 42 43 /// Options for the `KLDivLoss` module. 44 /// 45 /// Example: 46 /// ``` 47 /// KLDivLoss 48 /// model(KLDivLossOptions().reduction(torch::kNone).log_target(false)); 49 /// ``` 50 struct TORCH_API KLDivLossOptions { 51 typedef std::variant< 52 enumtype::kNone, 53 enumtype::kBatchMean, 54 enumtype::kSum, 55 enumtype::kMean> 56 reduction_t; 57 58 TORCH_OPTIONS_CTOR_VARIANT_ARG4( 59 KLDivLossOptions, 60 reduction, 61 kNone, 62 kBatchMean, 63 kSum, 64 kMean) 65 66 /// Specifies the reduction to apply to the output. 67 /// ``'none'`` | ``'batchmean'`` | ``'sum'`` | ``'mean'``. Default: ``'mean'`` 68 TORCH_ARG(reduction_t, reduction) = torch::kMean; 69 70 /// Specifies whether `target` is accepted in the log space. Default: False 71 TORCH_ARG(bool, log_target) = false; 72 }; 73 74 namespace functional { 75 /// Options for `torch::nn::functional::kl_div`. 76 /// 77 /// See the documentation for `torch::nn::KLDivLossOptions` class to learn what 78 /// arguments are supported. 79 /// 80 /// Example: 81 /// ``` 82 /// namespace F = torch::nn::functional; 83 /// F::kl_div(input, target, 84 /// F::KLDivFuncOptions().reduction(torch::kNone).log_target(false)); 85 /// ``` 86 using KLDivFuncOptions = KLDivLossOptions; 87 } // namespace functional 88 89 // ============================================================================ 90 91 /// Options for the `MSELoss` module. 92 /// 93 /// Example: 94 /// ``` 95 /// MSELoss model(MSELossOptions(torch::kNone)); 96 /// ``` 97 struct TORCH_API MSELossOptions { 98 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 99 reduction_t; 100 101 TORCH_OPTIONS_CTOR_VARIANT_ARG3(MSELossOptions, reduction, kNone, kMean, kSum) 102 103 /// Specifies the reduction to apply to the output. 104 /// ``'none'`` | ``'mean'`` | ``'sum'``. Default: ``'mean'`` 105 TORCH_ARG(reduction_t, reduction) = torch::kMean; 106 }; 107 108 namespace functional { 109 /// Options for `torch::nn::functional::mse_loss`. 110 /// 111 /// See the documentation for `torch::nn::MSELossOptions` class to learn what 112 /// arguments are supported. 113 /// 114 /// Example: 115 /// ``` 116 /// namespace F = torch::nn::functional; 117 /// F::mse_loss(input, target, F::MSELossFuncOptions(torch::kNone)); 118 /// ``` 119 using MSELossFuncOptions = MSELossOptions; 120 } // namespace functional 121 122 // ============================================================================ 123 124 /// Options for the `BCELoss` module. 125 /// 126 /// Example: 127 /// ``` 128 /// BCELoss model(BCELossOptions().reduction(torch::kNone).weight(weight)); 129 /// ``` 130 struct TORCH_API BCELossOptions { 131 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 132 reduction_t; 133 134 /// A manual rescaling weight given to the loss of each batch element. 135 TORCH_ARG(Tensor, weight) = {}; 136 /// Specifies the reduction to apply to the output. 137 /// ``'none'`` | ``'mean'`` | ``'sum'``. Default: ``'mean'`` 138 TORCH_ARG(reduction_t, reduction) = torch::kMean; 139 }; 140 141 namespace functional { 142 /// Options for `torch::nn::functional::binary_cross_entropy`. 143 /// 144 /// See the documentation for `torch::nn::BCELossOptions` class to learn what 145 /// arguments are supported. 146 /// 147 /// Example: 148 /// ``` 149 /// namespace F = torch::nn::functional; 150 /// F::binary_cross_entropy(input, target, 151 /// F::BinaryCrossEntropyFuncOptions().weight(weight)); 152 /// ``` 153 using BinaryCrossEntropyFuncOptions = BCELossOptions; 154 } // namespace functional 155 156 // ============================================================================ 157 158 /// Options for the `HingeEmbeddingLoss` module. 159 /// 160 /// Example: 161 /// ``` 162 /// HingeEmbeddingLoss 163 /// model(HingeEmbeddingLossOptions().margin(4).reduction(torch::kNone)); 164 /// ``` 165 struct TORCH_API HingeEmbeddingLossOptions { 166 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 167 reduction_t; 168 169 /// Specifies the threshold for which the distance of a negative sample must 170 /// reach in order to incur zero loss. Default: 1 171 TORCH_ARG(double, margin) = 1.0; 172 /// Specifies the reduction to apply to the output. Default: Mean 173 TORCH_ARG(reduction_t, reduction) = torch::kMean; 174 }; 175 176 namespace functional { 177 /// Options for `torch::nn::functional::hinge_embedding_loss`. 178 /// 179 /// See the documentation for `torch::nn::HingeEmbeddingLossOptions` class to 180 /// learn what arguments are supported. 181 /// 182 /// Example: 183 /// ``` 184 /// namespace F = torch::nn::functional; 185 /// F::hinge_embedding_loss(input, target, 186 /// F::HingeEmbeddingLossFuncOptions().margin(2)); 187 /// ``` 188 using HingeEmbeddingLossFuncOptions = HingeEmbeddingLossOptions; 189 } // namespace functional 190 191 // ============================================================================ 192 193 /// Options for the `MultiMarginLoss` module. 194 /// 195 /// Example: 196 /// ``` 197 /// MultiMarginLoss model(MultiMarginLossOptions().margin(2).weight(weight)); 198 /// ``` 199 struct TORCH_API MultiMarginLossOptions { 200 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 201 reduction_t; 202 203 /// Has a default value of :math:`1`. :math:`1` and :math:`2` 204 /// are the only supported values. 205 TORCH_ARG(int64_t, p) = 1; 206 /// Has a default value of :math:`1`. 207 TORCH_ARG(double, margin) = 1.0; 208 /// A manual rescaling weight given to each 209 /// class. If given, it has to be a Tensor of size `C`. Otherwise, it is 210 /// treated as if having all ones. 211 TORCH_ARG(Tensor, weight) = Tensor(); 212 /// Specifies the reduction to apply to the output: 213 /// ``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be 214 /// applied, 215 /// ``'mean'``: the sum of the output will be divided by the number of 216 /// elements in the output, ``'sum'``: the output will be summed. Default: 217 /// ``'mean'`` 218 TORCH_ARG(reduction_t, reduction) = torch::kMean; 219 }; 220 221 namespace functional { 222 /// Options for `torch::nn::functional::multi_margin_loss`. 223 /// 224 /// See the documentation for `torch::nn::MultiMarginLossOptions` class to learn 225 /// what arguments are supported. 226 /// 227 /// Example: 228 /// ``` 229 /// namespace F = torch::nn::functional; 230 /// F::multi_margin_loss(input, target, 231 /// F::MultiMarginLossFuncOptions().margin(2).weight(weight)); 232 /// ``` 233 using MultiMarginLossFuncOptions = MultiMarginLossOptions; 234 } // namespace functional 235 236 // ============================================================================ 237 238 /// Options for the `CosineEmbeddingLoss` module. 239 /// 240 /// Example: 241 /// ``` 242 /// CosineEmbeddingLoss model(CosineEmbeddingLossOptions().margin(0.5)); 243 /// ``` 244 struct TORCH_API CosineEmbeddingLossOptions { 245 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 246 reduction_t; 247 248 /// Specifies the threshold for which the distance of a negative sample must 249 /// reach in order to incur zero loss. Should be a number from -1 to 1, 0 250 /// to 0.5 is suggested. Default: 0.0 251 TORCH_ARG(double, margin) = 0.0; 252 /// Specifies the reduction to apply to the output. Default: Mean 253 TORCH_ARG(reduction_t, reduction) = torch::kMean; 254 }; 255 256 namespace functional { 257 /// Options for `torch::nn::functional::cosine_embedding_loss`. 258 /// 259 /// See the documentation for `torch::nn::CosineEmbeddingLossOptions` class to 260 /// learn what arguments are supported. 261 /// 262 /// Example: 263 /// ``` 264 /// namespace F = torch::nn::functional; 265 /// F::cosine_embedding_loss(input1, input2, target, 266 /// F::CosineEmbeddingLossFuncOptions().margin(0.5)); 267 /// ``` 268 using CosineEmbeddingLossFuncOptions = CosineEmbeddingLossOptions; 269 } // namespace functional 270 271 // ============================================================================ 272 273 /// Options for the `MultiLabelMarginLoss` module. 274 /// 275 /// Example: 276 /// ``` 277 /// MultiLabelMarginLoss model(MultiLabelMarginLossOptions(torch::kNone)); 278 /// ``` 279 struct TORCH_API MultiLabelMarginLossOptions { 280 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 281 reduction_t; 282 283 TORCH_OPTIONS_CTOR_VARIANT_ARG3( 284 MultiLabelMarginLossOptions, 285 reduction, 286 kNone, 287 kMean, 288 kSum) 289 290 /// Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 291 /// 'none': no reduction will be applied, 'mean': the sum of the output will 292 /// be divided by the number of elements in the output, 'sum': the output will 293 /// be summed. Default: 'mean' 294 TORCH_ARG(reduction_t, reduction) = torch::kMean; 295 }; 296 297 namespace functional { 298 /// Options for `torch::nn::functional::multilabel_margin_loss`. 299 /// 300 /// See the documentation for `torch::nn::MultiLabelMarginLossOptions` class to 301 /// learn what arguments are supported. 302 /// 303 /// Example: 304 /// ``` 305 /// namespace F = torch::nn::functional; 306 /// F::multilabel_margin_loss(input, target, 307 /// F::MultilabelMarginLossFuncOptions(torch::kNone)); 308 /// ``` 309 using MultilabelMarginLossFuncOptions = MultiLabelMarginLossOptions; 310 } // namespace functional 311 312 // ============================================================================ 313 314 /// Options for the `SoftMarginLoss` module. 315 /// 316 /// Example: 317 /// ``` 318 /// SoftMarginLoss model(SoftMarginLossOptions(torch::kNone)); 319 /// ``` 320 struct TORCH_API SoftMarginLossOptions { 321 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 322 reduction_t; 323 324 TORCH_OPTIONS_CTOR_VARIANT_ARG3( 325 SoftMarginLossOptions, 326 reduction, 327 kNone, 328 kMean, 329 kSum) 330 331 /// Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 332 /// 'none': no reduction will be applied, 'mean': the sum of the output will 333 /// be divided by the number of elements in the output, 'sum': the output will 334 /// be summed. Default: 'mean' 335 TORCH_ARG(reduction_t, reduction) = torch::kMean; 336 }; 337 338 namespace functional { 339 /// Options for `torch::nn::functional::soft_margin_loss`. 340 /// 341 /// See the documentation for `torch::nn::SoftMarginLossOptions` class to learn 342 /// what arguments are supported. 343 /// 344 /// Example: 345 /// ``` 346 /// namespace F = torch::nn::functional; 347 /// F::soft_margin_loss(input, target, 348 /// F::SoftMarginLossFuncOptions(torch::kNone)); 349 /// ``` 350 using SoftMarginLossFuncOptions = SoftMarginLossOptions; 351 } // namespace functional 352 353 // ============================================================================ 354 355 /// Options for the `MultiLabelSoftMarginLoss` module. 356 /// 357 /// Example: 358 /// ``` 359 /// MultiLabelSoftMarginLoss 360 /// model(MultiLabelSoftMarginLossOptions().reduction(torch::kNone).weight(weight)); 361 /// ``` 362 struct TORCH_API MultiLabelSoftMarginLossOptions { 363 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 364 reduction_t; 365 366 /// A manual rescaling weight given to each 367 /// class. If given, it has to be a Tensor of size `C`. Otherwise, it is 368 /// treated as if having all ones. 369 TORCH_ARG(Tensor, weight) = Tensor(); 370 371 /// Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 372 /// 'none': no reduction will be applied, 'mean': the sum of the output will 373 /// be divided by the number of elements in the output, 'sum': the output will 374 /// be summed. Default: 'mean' 375 TORCH_ARG(reduction_t, reduction) = torch::kMean; 376 }; 377 378 namespace functional { 379 /// Options for `torch::nn::functional::multilabel_soft_margin_loss`. 380 /// 381 /// See the documentation for `torch::nn::MultiLabelSoftMarginLossOptions` class 382 /// to learn what arguments are supported. 383 /// 384 /// Example: 385 /// ``` 386 /// namespace F = torch::nn::functional; 387 /// F::multilabel_soft_margin_loss(input, target, 388 /// F::MultilabelSoftMarginLossFuncOptions().reduction(torch::kNone).weight(weight)); 389 /// ``` 390 using MultilabelSoftMarginLossFuncOptions = MultiLabelSoftMarginLossOptions; 391 } // namespace functional 392 393 // ============================================================================ 394 395 /// Options for the `TripletMarginLoss` module. 396 /// 397 /// Example: 398 /// ``` 399 /// TripletMarginLoss 400 /// model(TripletMarginLossOptions().margin(3).p(2).eps(1e-06).swap(false)); 401 /// ``` 402 struct TORCH_API TripletMarginLossOptions { 403 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 404 reduction_t; 405 406 /// Specifies the threshold for which the distance of a negative sample must 407 /// reach in order to incur zero loss. Default: 1 408 TORCH_ARG(double, margin) = 1.0; 409 /// Specifies the norm degree for pairwise distance. Default: 2 410 TORCH_ARG(double, p) = 2.0; 411 TORCH_ARG(double, eps) = 1e-6; 412 /// The distance swap is described in detail in the paper Learning shallow 413 /// convolutional feature descriptors with triplet losses by V. Balntas, 414 /// E. Riba et al. Default: False 415 TORCH_ARG(bool, swap) = false; 416 /// Specifies the reduction to apply to the output. Default: Mean 417 TORCH_ARG(reduction_t, reduction) = torch::kMean; 418 }; 419 420 namespace functional { 421 /// Options for `torch::nn::functional::triplet_margin_loss`. 422 /// 423 /// See the documentation for `torch::nn::TripletMarginLossOptions` class to 424 /// learn what arguments are supported. 425 /// 426 /// Example: 427 /// ``` 428 /// namespace F = torch::nn::functional; 429 /// F::triplet_margin_loss(anchor, positive, negative, 430 /// F::TripletMarginLossFuncOptions().margin(1.0)); 431 /// ``` 432 using TripletMarginLossFuncOptions = TripletMarginLossOptions; 433 } // namespace functional 434 435 // ============================================================================ 436 437 /// Options for the `TripletMarginWithDistanceLoss` module. 438 /// 439 /// Example: 440 /// ``` 441 /// TripletMarginWithDistanceLoss 442 /// model(TripletMarginWithDistanceLossOptions().margin(3).swap(false)); 443 /// ``` 444 struct TORCH_API TripletMarginWithDistanceLossOptions { 445 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 446 reduction_t; 447 typedef std::function<Tensor(const Tensor&, const Tensor&)> 448 distance_function_t; 449 450 /// Specifies a nonnegative, real-valued function that quantifies the 451 /// closeness of two tensors. If not specified, `F::pairwise_distance` will 452 /// be used. Default: nullopt 453 TORCH_ARG(std::optional<distance_function_t>, distance_function) = 454 std::nullopt; 455 /// Specifies a nonnegative margin representing the minimum difference 456 /// between the positive and negative distances required for the loss to be 0. 457 /// Larger margins penalize cases where the negative examples are not distance 458 /// enough from the anchors, relative to the positives. Default: 1 459 TORCH_ARG(double, margin) = 1.0; 460 /// Whether to use the distance swap described in the paper Learning shallow 461 /// convolutional feature descriptors with triplet losses by V. Balntas, 462 /// E. Riba et al. If True, and if the positive example is closer to the 463 /// negative example than the anchor is, swaps the positive example and the 464 /// anchor in the loss computation. Default: False 465 TORCH_ARG(bool, swap) = false; 466 /// Specifies the reduction to apply to the output. Default: Mean 467 TORCH_ARG(reduction_t, reduction) = torch::kMean; 468 }; 469 470 namespace functional { 471 /// Options for `torch::nn::functional::triplet_margin_with_distance_loss`. 472 /// 473 /// See the documentation for `torch::nn::TripletMarginWithDistanceLossOptions` 474 /// class to learn what arguments are supported. 475 /// 476 /// Example: 477 /// ``` 478 /// namespace F = torch::nn::functional; 479 /// F::triplet_margin_with_distance_loss(anchor, positive, negative, 480 /// F::TripletMarginWithDistanceLossFuncOptions().margin(1.0)); 481 /// ``` 482 using TripletMarginWithDistanceLossFuncOptions = 483 TripletMarginWithDistanceLossOptions; 484 } // namespace functional 485 486 // ============================================================================ 487 488 /// Options for the `CTCLoss` module. 489 /// 490 /// Example: 491 /// ``` 492 /// CTCLoss 493 /// model(CTCLossOptions().blank(42).zero_infinity(false).reduction(torch::kSum)); 494 /// ``` 495 struct TORCH_API CTCLossOptions { 496 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 497 reduction_t; 498 499 /// blank label. Default `0`. 500 TORCH_ARG(int64_t, blank) = 0; 501 /// Specifies the reduction to apply to the output. Default: Mean 502 TORCH_ARG(reduction_t, reduction) = torch::kMean; 503 /// Whether to zero infinite losses and the associated gradients. 504 /// Default: `false`. Infinite losses mainly occur when the inputs are 505 /// too short to be aligned to the targets. 506 TORCH_ARG(bool, zero_infinity) = false; 507 }; 508 509 namespace functional { 510 /// Options for `torch::nn::functional::ctc_loss`. 511 /// 512 /// See the documentation for `torch::nn::CTCLossOptions` class to learn what 513 /// arguments are supported. 514 /// 515 /// Example: 516 /// ``` 517 /// namespace F = torch::nn::functional; 518 /// F::ctc_loss(log_probs, targets, input_lengths, target_lengths, 519 /// F::CTCLossFuncOptions().reduction(torch::kNone)); 520 /// ``` 521 using CTCLossFuncOptions = CTCLossOptions; 522 } // namespace functional 523 524 // ============================================================================ 525 526 /// Options for the `SmoothL1Loss` module. 527 /// 528 /// Example: 529 /// ``` 530 /// SmoothL1Loss model(SmoothL1LossOptions().reduction(torch::kNone).beta(0.5)); 531 /// ``` 532 struct TORCH_API SmoothL1LossOptions { 533 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 534 reduction_t; 535 536 TORCH_OPTIONS_CTOR_VARIANT_ARG3( 537 SmoothL1LossOptions, 538 reduction, 539 kNone, 540 kMean, 541 kSum) 542 543 /// Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 544 /// 'none': no reduction will be applied, 'mean': the sum of the output will 545 /// be divided by the number of elements in the output, 'sum': the output will 546 /// be summed. Default: 'mean' 547 TORCH_ARG(reduction_t, reduction) = torch::kMean; 548 /// Specifies the threshold at which to change between L1 and L2 loss. 549 /// If beta is not specified, a value of 1.0 will be used. 550 /// Default: nullopt 551 TORCH_ARG(std::optional<double>, beta) = std::nullopt; 552 }; 553 554 namespace functional { 555 /// Options for `torch::nn::functional::smooth_l1_loss`. 556 /// 557 /// See the documentation for `torch::nn::SmoothL1LossOptions` class to learn 558 /// what arguments are supported. 559 /// 560 /// Example: 561 /// ``` 562 /// namespace F = torch::nn::functional; 563 /// F::smooth_l1_loss(input, target, F::SmoothL1LossFuncOptions(torch::kNone)); 564 /// ``` 565 using SmoothL1LossFuncOptions = SmoothL1LossOptions; 566 } // namespace functional 567 568 // ============================================================================ 569 570 /// Options for the `HuberLoss` module. 571 /// 572 /// Example: 573 /// ``` 574 /// HuberLoss model(HuberLossOptions().reduction(torch::kNone).delta(0.5)); 575 /// ``` 576 struct TORCH_API HuberLossOptions { 577 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 578 reduction_t; 579 580 TORCH_OPTIONS_CTOR_VARIANT_ARG3( 581 HuberLossOptions, 582 reduction, 583 kNone, 584 kMean, 585 kSum) 586 587 /// Specifies the reduction to apply to the output: 'none' | 'mean' | 'sum'. 588 /// 'none': no reduction will be applied, 'mean': the sum of the output will 589 /// be divided by the number of elements in the output, 'sum': the output will 590 /// be summed. Default: 'mean' 591 TORCH_ARG(reduction_t, reduction) = torch::kMean; 592 /// Specifies the threshold at which to change between L1 and L2 loss. 593 /// Default: 1.0 594 TORCH_ARG(double, delta) = 1.0; 595 }; 596 597 namespace functional { 598 /// Options for `torch::nn::functional::huber_loss`. 599 /// 600 /// See the documentation for `torch::nn::HuberLossOptions` class to learn what 601 /// arguments are supported. 602 /// 603 /// Example: 604 /// ``` 605 /// namespace F = torch::nn::functional; 606 /// F::huber_loss(input, target, F::HuberLossFuncOptions(torch::kNone)); 607 /// ``` 608 using HuberLossFuncOptions = HuberLossOptions; 609 } // namespace functional 610 611 // ============================================================================ 612 613 /// Options for the `PoissonNLLLoss` module. 614 /// 615 /// Example: 616 /// ``` 617 /// PoissonNLLLoss 618 /// model(PoissonNLLLossOptions().log_input(false).full(true).eps(0.42).reduction(torch::kSum)); 619 /// ``` 620 struct TORCH_API PoissonNLLLossOptions { 621 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 622 reduction_t; 623 624 /// if true the loss is computed as `exp(input) - target * input`, 625 /// if false the loss is `input - target * log(input + eps)`. 626 TORCH_ARG(bool, log_input) = true; 627 /// whether to compute full loss, i.e. to add the Stirling approximation term 628 /// target * log(target) - target + 0.5 * log(2 * pi * target). 629 TORCH_ARG(bool, full) = false; 630 /// Small value to avoid evaluation of `log(0)` when `log_input = false`. 631 /// Default: 1e-8 632 TORCH_ARG(double, eps) = 1e-8; 633 /// Specifies the reduction to apply to the output. Default: Mean 634 TORCH_ARG(reduction_t, reduction) = torch::kMean; 635 }; 636 637 namespace functional { 638 /// Options for `torch::nn::functional::poisson_nll_loss`. 639 /// 640 /// See the documentation for `torch::nn::PoissonNLLLossOptions` class to learn 641 /// what arguments are supported. 642 /// 643 /// Example: 644 /// ``` 645 /// namespace F = torch::nn::functional; 646 /// F::poisson_nll_loss(input, target, 647 /// F::PoissonNLLLossFuncOptions().reduction(torch::kNone)); 648 /// ``` 649 using PoissonNLLLossFuncOptions = PoissonNLLLossOptions; 650 } // namespace functional 651 652 // ============================================================================ 653 654 /// Options for the `MarginRankingLoss` module. 655 /// 656 /// Example: 657 /// ``` 658 /// MarginRankingLoss 659 /// model(MarginRankingLossOptions().margin(0.5).reduction(torch::kSum)); 660 /// ``` 661 struct TORCH_API MarginRankingLossOptions { 662 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 663 reduction_t; 664 665 /// Has a default value of `0`. 666 TORCH_ARG(double, margin) = 0; 667 /// Specifies the reduction to apply to the output. Default: Mean 668 TORCH_ARG(reduction_t, reduction) = torch::kMean; 669 }; 670 671 namespace functional { 672 /// Options for `torch::nn::functional::margin_ranking_loss`. 673 /// 674 /// See the documentation for `torch::nn::MarginRankingLossOptions` class to 675 /// learn what arguments are supported. 676 /// 677 /// Example: 678 /// ``` 679 /// namespace F = torch::nn::functional; 680 /// F::margin_ranking_loss(input1, input2, target, 681 /// F::MarginRankingLossFuncOptions().margin(0.5).reduction(torch::kSum)); 682 /// ``` 683 using MarginRankingLossFuncOptions = MarginRankingLossOptions; 684 } // namespace functional 685 686 // ============================================================================ 687 688 /// Options for the `NLLLoss` module. 689 /// 690 /// Example: 691 /// ``` 692 /// NLLLoss model(NLLLossOptions().ignore_index(-100).reduction(torch::kMean)); 693 /// ``` 694 struct TORCH_API NLLLossOptions { 695 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 696 reduction_t; 697 698 /// A manual rescaling weight given to each 699 /// class. If given, it has to be a Tensor of size `C`. Otherwise, it is 700 /// treated as if having all ones. 701 TORCH_ARG(Tensor, weight) = {}; 702 /// Specifies a target value that is ignored 703 /// and does not contribute to the input gradient. 704 TORCH_ARG(int64_t, ignore_index) = -100; 705 /// Specifies the reduction to apply to the output. Default: Mean 706 TORCH_ARG(reduction_t, reduction) = torch::kMean; 707 }; 708 709 namespace functional { 710 /// Options for `torch::nn::functional::nll_loss`. 711 /// 712 /// See the documentation for `torch::nn::NLLLossOptions` class to learn what 713 /// arguments are supported. 714 /// 715 /// Example: 716 /// ``` 717 /// namespace F = torch::nn::functional; 718 /// F::nll_loss(input, target, 719 /// F::NLLLossFuncOptions().ignore_index(-100).reduction(torch::kMean)); 720 /// ``` 721 using NLLLossFuncOptions = NLLLossOptions; 722 } // namespace functional 723 724 // ============================================================================ 725 726 /// Options for the `CrossEntropyLoss` module. 727 /// 728 /// Example: 729 /// ``` 730 /// CrossEntropyLoss 731 /// model(CrossEntropyLossOptions().ignore_index(-100).reduction(torch::kMean)); 732 /// ``` 733 struct TORCH_API CrossEntropyLossOptions { 734 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 735 reduction_t; 736 737 /// A manual rescaling weight given to each class. If given, has to be a 738 /// Tensor of size C 739 TORCH_ARG(Tensor, weight) = {}; 740 /// Specifies a target value that is ignored 741 /// and does not contribute to the input gradient. 742 TORCH_ARG(int64_t, ignore_index) = -100; 743 /// Specifies the reduction to apply to the output. Default: Mean 744 TORCH_ARG(reduction_t, reduction) = torch::kMean; 745 /// Specifies the amount of smoothing when computing the loss. Default: 0.0 746 TORCH_ARG(double, label_smoothing) = 0.0; 747 }; 748 749 namespace functional { 750 /// Options for `torch::nn::functional::cross_entropy`. 751 /// 752 /// See the documentation for `torch::nn::CrossEntropyLossOptions` class to 753 /// learn what arguments are supported. 754 /// 755 /// Example: 756 /// ``` 757 /// namespace F = torch::nn::functional; 758 /// F::cross_entropy(input, target, 759 /// F::CrossEntropyFuncOptions().ignore_index(-100).reduction(torch::kMean)); 760 /// ``` 761 using CrossEntropyFuncOptions = CrossEntropyLossOptions; 762 } // namespace functional 763 764 // ============================================================================ 765 766 /// Options for the `BCEWithLogitsLoss` module. 767 /// 768 /// Example: 769 /// ``` 770 /// BCEWithLogitsLoss 771 /// model(BCEWithLogitsLossOptions().reduction(torch::kNone).weight(weight)); 772 /// ``` 773 struct TORCH_API BCEWithLogitsLossOptions { 774 typedef std::variant<enumtype::kNone, enumtype::kMean, enumtype::kSum> 775 reduction_t; 776 /// A manual rescaling weight given to the loss of each batch element. 777 /// If given, has to be a Tensor of size `nbatch`. 778 TORCH_ARG(Tensor, weight) = {}; 779 /// Specifies the reduction to apply to the output. Default: Mean 780 TORCH_ARG(reduction_t, reduction) = torch::kMean; 781 /// A weight of positive examples. 782 /// Must be a vector with length equal to the number of classes. 783 TORCH_ARG(Tensor, pos_weight) = {}; 784 }; 785 786 namespace functional { 787 /// Options for `torch::nn::functional::binary_cross_entropy_with_logits`. 788 /// 789 /// See the documentation for `torch::nn::BCEWithLogitsLossOptions` class to 790 /// learn what arguments are supported. 791 /// 792 /// Example: 793 /// ``` 794 /// namespace F = torch::nn::functional; 795 /// F::binary_cross_entropy_with_logits(input, target, 796 /// F::BinaryCrossEntropyWithLogitsFuncOptions().pos_weight(pos_weight).reduction(torch::kSum)); 797 /// ``` 798 using BinaryCrossEntropyWithLogitsFuncOptions = BCEWithLogitsLossOptions; 799 } // namespace functional 800 801 } // namespace nn 802 } // namespace torch 803