-
Notifications
You must be signed in to change notification settings - Fork 21.4k
/
loss.h
916 lines (836 loc) · 30.3 KB
/
loss.h
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
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
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
691
692
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
732
733
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
794
795
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
906
907
908
909
910
911
912
913
914
915
916
#pragma once
#include <ATen/ExpandUtils.h>
#include <torch/nn/functional/activation.h>
#include <torch/nn/options/loss.h>
namespace torch {
namespace nn {
namespace functional {
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor l1_loss(
const Tensor& input,
const Tensor& target,
L1LossFuncOptions::reduction_t reduction) {
return torch::l1_loss(
input,
target,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.l1_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::L1LossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::l1_loss(input, target, F::L1LossFuncOptions(torch::kNone));
/// ```
inline Tensor l1_loss(
const Tensor& input,
const Tensor& target,
const L1LossFuncOptions& options = {}) {
return detail::l1_loss(input, target, options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor kl_div(
const Tensor& input,
const Tensor& target,
KLDivFuncOptions::reduction_t reduction,
bool log_target = false) {
torch::Reduction::Reduction reduction_enum;
if (c10::get_if<enumtype::kMean>(&reduction)) {
TORCH_WARN("reduction: 'mean' divides the total loss by both the batch size and the support size."
"'batchmean' divides only by the batch size, and aligns with the KL div math definition."
"'mean' will be changed to behave the same as 'batchmean' in the next major release.");
}
// special case for batchmean
if (c10::get_if<enumtype::kBatchMean>(&reduction)) {
reduction_enum = torch::Reduction::Sum;
} else {
reduction_enum = enumtype::reduction_get_enum(reduction);
}
auto reduced = torch::kl_div(input, target, reduction_enum, log_target);
if (c10::get_if<enumtype::kBatchMean>(&reduction) && input.dim() != 0) {
reduced = reduced / input.sizes()[0];
}
return reduced;
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.kl_div
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::KLDivFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::kl_div(input, target, F::KLDivFuncOptions.reduction(torch::kNone).log_target(false));
/// ```
inline Tensor kl_div(
const Tensor& input,
const Tensor& target,
const KLDivFuncOptions& options = {}) {
return detail::kl_div(input, target, options.reduction(), options.log_target());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor mse_loss(
const Tensor& input,
const Tensor& target,
MSELossFuncOptions::reduction_t reduction) {
if (!(target.sizes() == input.sizes())) {
TORCH_WARN("Using a target size (", target.sizes(),
") that is different to the input size (", input.sizes(), "). ",
"This will likely lead to incorrect results due to broadcasting. ",
"Please ensure they have the same size.");
}
std::vector<torch::Tensor> broadcast_tensors = torch::broadcast_tensors({input, target});
auto expanded_input = broadcast_tensors[0];
auto expanded_target = broadcast_tensors[1];
return torch::mse_loss(
expanded_input,
expanded_target,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.mse_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::MSELossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::mse_loss(input, target, F::MSELossFuncOptions(torch::kNone));
/// ```
inline Tensor mse_loss(
const Tensor& input,
const Tensor& target,
const MSELossFuncOptions& options = {}) {
return detail::mse_loss(input, target, options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor binary_cross_entropy(
const Tensor& input,
const Tensor& target,
const Tensor& weight,
BinaryCrossEntropyFuncOptions::reduction_t reduction) {
auto reduction_enum = enumtype::reduction_get_enum(reduction);
if (target.sizes() != input.sizes()) {
TORCH_CHECK(
false,
"Using a target size (", target.sizes(), ") ",
"that is different to the input size (", input.sizes(), ") is deprecated. ",
"Please ensure they have the same size.");
}
auto weight_ = weight;
if (weight_.defined()) {
auto new_size = at::infer_size(target.sizes(), weight_.sizes());
weight_ = weight_.expand(new_size);
}
return torch::binary_cross_entropy(input, target, weight_, reduction_enum);
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.binary_cross_entropy
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::BinaryCrossEntropyFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::binary_cross_entropy(input, target, F::BinaryCrossEntropyFuncOptions().weight(weight));
/// ```
inline Tensor binary_cross_entropy(
const Tensor& input,
const Tensor& target,
const BinaryCrossEntropyFuncOptions& options = {}) {
return detail::binary_cross_entropy(input, target, options.weight(), options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor hinge_embedding_loss(
const Tensor& input,
const Tensor& target,
double margin,
HingeEmbeddingLossFuncOptions::reduction_t reduction) {
return torch::hinge_embedding_loss(
input,
target,
margin,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.hinge_embedding_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::HingeEmbeddingLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::hinge_embedding_loss(input, target, F::HingeEmbeddingLossFuncOptions().margin(2));
/// ```
inline Tensor hinge_embedding_loss(
const Tensor& input,
const Tensor& target,
const HingeEmbeddingLossFuncOptions& options = {}) {
return detail::hinge_embedding_loss(input, target, options.margin(), options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor multi_margin_loss(
const Tensor& input,
const Tensor& target,
int64_t p,
double margin,
const Tensor& weight,
MultiMarginLossFuncOptions::reduction_t reduction) {
TORCH_CHECK(p == 1 || p == 2, "only p == 1 and p == 2 supported");
if (weight.defined()) {
TORCH_CHECK(weight.dim() == 1, "weight must be one-dimensional");
}
return torch::multi_margin_loss(
input,
target,
p,
margin,
weight,
enumtype::reduction_get_enum(reduction)
);
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.multi_margin_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::MultiMarginLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::multi_margin_loss(input, target, F::MultiMarginLossFuncOptions().margin(2).weight(weight));
/// ```
inline Tensor multi_margin_loss(
const Tensor& input,
const Tensor& target,
const MultiMarginLossFuncOptions& options = {}) {
return detail::multi_margin_loss(input, target, options.p(), options.margin(), options.weight(), options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor cosine_embedding_loss(
const Tensor& input1,
const Tensor& input2,
const Tensor& target,
double margin,
CosineEmbeddingLossFuncOptions::reduction_t reduction) {
return torch::cosine_embedding_loss(
input1,
input2,
target,
margin,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.cosine_embedding_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::CosineEmbeddingLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::cosine_embedding_loss(input1, input2, target, F::CosineEmbeddingLossFuncOptions().margin(0.5));
/// ```
inline Tensor cosine_embedding_loss(
const Tensor& input1,
const Tensor& input2,
const Tensor& target,
const CosineEmbeddingLossFuncOptions& options = {}) {
return detail::cosine_embedding_loss(input1, input2, target, options.margin(), options.reduction());
}
// ============================================================================
inline Tensor _smooth_l1_loss(const Tensor& input, const Tensor& target, double beta = 1.) {
auto t = torch::abs(input - target);
return torch::where(t < beta, 0.5 * torch::pow(t, 2) / beta, t - 0.5 * beta);
}
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor smooth_l1_loss(
const Tensor& input,
const Tensor& target,
SmoothL1LossFuncOptions::reduction_t reduction,
double beta = 1.) {
if (target.sizes() != input.sizes()) {
TORCH_WARN("Using a target size (", target.sizes(), ") that is different to the input size (", input.sizes(), "). ",
"This will likely lead to incorrect results due to broadcasting. ",
"Please ensure they have the same size.");
}
std::vector<Tensor> expanded_tensors = torch::broadcast_tensors({input, target});
return torch::smooth_l1_loss(expanded_tensors[0], expanded_tensors[1], enumtype::reduction_get_enum(reduction), beta);
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.smooth_l1_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::SmoothL1LossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::smooth_l1_loss(input, target, F::SmoothL1LossFuncOptions(torch::kNone));
/// ```
inline Tensor smooth_l1_loss(
const Tensor& input,
const Tensor& target,
const SmoothL1LossFuncOptions& options = {},
double beta = 1.) {
return detail::smooth_l1_loss(input, target, options.reduction(), beta);
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor multilabel_margin_loss(
const Tensor& input,
const Tensor& target,
MultilabelMarginLossFuncOptions::reduction_t reduction) {
return torch::multilabel_margin_loss(
input,
target,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.multilabel_margin_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::MultilabelMarginLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::multilabel_margin_loss(input, target, F::MultilabelMarginLossFuncOptions(torch::kNone));
/// ```
inline Tensor multilabel_margin_loss(
const Tensor& input,
const Tensor& target,
const MultilabelMarginLossFuncOptions& options = {}) {
return detail::multilabel_margin_loss(input, target, options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor soft_margin_loss(
const Tensor& input,
const Tensor& target,
SoftMarginLossFuncOptions::reduction_t reduction) {
return torch::soft_margin_loss(
input,
target,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.soft_margin_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::SoftMarginLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::soft_margin_loss(input, target, F::SoftMarginLossFuncOptions(torch::kNone));
/// ```
inline Tensor soft_margin_loss(
const Tensor& input,
const Tensor& target,
const SoftMarginLossFuncOptions& options = {}) {
return detail::soft_margin_loss(input, target, options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor multilabel_soft_margin_loss(
const Tensor& input,
const Tensor& target,
const Tensor& weight,
MultilabelSoftMarginLossFuncOptions::reduction_t reduction) {
auto loss = -(target * torch::log_sigmoid(input) + (1 - target) * torch::log_sigmoid(-input));
if (weight.defined()) {
loss = loss * weight;
}
loss = loss.sum(1) / input.size(1); // only return N loss values
Tensor ret;
if (c10::get_if<enumtype::kNone>(&reduction)) {
ret = loss;
} else if (c10::get_if<enumtype::kMean>(&reduction)) {
ret = loss.mean();
} else if (c10::get_if<enumtype::kSum>(&reduction)) {
ret = loss.sum();
} else {
ret = input;
TORCH_INTERNAL_ASSERT(
false,
enumtype::get_enum_name(reduction),
" is not valid");
}
return ret;
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.multilabel_soft_margin_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::MultilabelSoftMarginLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::multilabel_soft_margin_loss(input, target, F::MultilabelSoftMarginLossFuncOptions().reduction(torch::kNone).weight(weight));
/// ```
inline Tensor multilabel_soft_margin_loss(
const Tensor& input,
const Tensor& target,
const MultilabelSoftMarginLossFuncOptions& options = {}) {
return detail::multilabel_soft_margin_loss(input, target, options.weight(), options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor triplet_margin_loss(
const Tensor& anchor,
const Tensor& positive,
const Tensor& negative,
double margin,
double p,
double eps,
bool swap,
TripletMarginLossFuncOptions::reduction_t reduction) {
return torch::triplet_margin_loss(
anchor,
positive,
negative,
margin,
p,
eps,
swap,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.triplet_margin_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::TripletMarginLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::triplet_margin_loss(anchor, positive, negative, F::TripletMarginLossFuncOptions().margin(1.0));
/// ```
inline Tensor triplet_margin_loss(
const Tensor& anchor,
const Tensor& positive,
const Tensor& negative,
const TripletMarginLossFuncOptions& options = {}) {
return detail::triplet_margin_loss(
anchor,
positive,
negative,
options.margin(),
options.p(),
options.eps(),
options.swap(),
options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor triplet_margin_with_distance_loss(
const Tensor& anchor,
const Tensor& positive,
const Tensor& negative,
c10::optional<TripletMarginWithDistanceLossFuncOptions::distance_function_t> distance_function,
double margin,
bool swap,
TripletMarginWithDistanceLossFuncOptions::reduction_t reduction) {
Tensor dist_pos, dist_neg;
if (distance_function.has_value()) {
auto distance_function_impl = distance_function.value();
dist_pos = distance_function_impl(anchor, positive);
dist_neg = distance_function_impl(anchor, negative);
} else {
dist_pos = pairwise_distance(anchor, positive);
dist_neg = pairwise_distance(anchor, negative);
}
if (swap) {
Tensor dist_swap;
if (distance_function.has_value()) {
dist_swap = distance_function.value()(positive, negative);
} else {
dist_swap = pairwise_distance(positive, negative);
}
dist_neg = torch::min(dist_neg, dist_swap);
}
auto loss = torch::clamp_min(dist_pos - dist_neg + margin, 0);
Tensor ret;
if (c10::get_if<enumtype::kNone>(&reduction)) {
ret = loss;
} else if (c10::get_if<enumtype::kMean>(&reduction)) {
ret = loss.mean();
} else if (c10::get_if<enumtype::kSum>(&reduction)) {
ret = loss.sum();
} else {
ret = anchor;
TORCH_INTERNAL_ASSERT(
false,
enumtype::get_enum_name(reduction),
" is not valid");
}
return ret;
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.triplet_margin_with_distance_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::TripletMarginWithDistanceLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::triplet_margin_with_distance_loss(anchor, positive, negative, F::TripletMarginWithDistanceLossFuncOptions().margin(1.0));
/// ```
inline Tensor triplet_margin_with_distance_loss(
const Tensor& anchor,
const Tensor& positive,
const Tensor& negative,
const TripletMarginWithDistanceLossFuncOptions& options = {}) {
return detail::triplet_margin_with_distance_loss(
anchor,
positive,
negative,
options.distance_function(),
options.margin(),
options.swap(),
options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor ctc_loss(const Tensor& log_probs,
const Tensor& targets,
const Tensor& input_lengths,
const Tensor& target_lengths,
int64_t blank,
CTCLossFuncOptions::reduction_t reduction,
bool zero_infinity) {
return torch::ctc_loss(
log_probs,
targets,
input_lengths,
target_lengths,
blank,
enumtype::reduction_get_enum(reduction),
zero_infinity);
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.ctc_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::CTCLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::ctc_loss(log_probs, targets, input_lengths, target_lengths, F::CTCLossFuncOptions().reduction(torch::kNone));
/// ```
inline Tensor ctc_loss(const Tensor& log_probs,
const Tensor& targets,
const Tensor& input_lengths,
const Tensor& target_lengths,
const CTCLossFuncOptions& options = {}) {
return detail::ctc_loss(
log_probs,
targets,
input_lengths,
target_lengths,
options.blank(),
options.reduction(),
options.zero_infinity());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor poisson_nll_loss(const Tensor& input,
const Tensor& target,
bool log_input,
bool full,
double eps,
PoissonNLLLossFuncOptions::reduction_t reduction) {
return torch::poisson_nll_loss(
input, target,
log_input, full, eps, enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.poisson_nll_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::PoissonNLLLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::poisson_nll_loss(input, target, F::PoissonNLLLossFuncOptions().reduction(torch::kNone));
/// ```
inline Tensor poisson_nll_loss(const Tensor& input, const Tensor& target,
const PoissonNLLLossFuncOptions& options = {}) {
return detail::poisson_nll_loss(
input, target,
options.log_input(), options.full(), options.eps(), options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor margin_ranking_loss(const Tensor& input1,
const Tensor& input2,
const Tensor& target,
double margin,
MarginRankingLossFuncOptions::reduction_t reduction) {
TORCH_CHECK(
input1.dim() != 0 && input2.dim() != 0 && target.dim() != 0,
"margin_ranking_loss does not support scalars, got sizes: "
"input1: ", input1.sizes(), ", input2: ", input2.sizes(),
", target: ", target.sizes());
return torch::margin_ranking_loss(input1, input2, target, margin,
enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.margin_ranking_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::MarginRankingLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::margin_ranking_loss(input1, input2, target, F::MarginRankingLossFuncOptions().margin(0.5).reduction(torch::kSum));
/// ```
inline Tensor margin_ranking_loss(const Tensor& input1, const Tensor& input2,
const Tensor& target, const MarginRankingLossFuncOptions& options = {}) {
return detail::margin_ranking_loss(input1, input2, target, options.margin(), options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor nll_loss(
const Tensor& input,
const Tensor& target,
const Tensor& weight,
int64_t ignore_index,
const NLLLossFuncOptions::reduction_t reduction) {
if (input.dim() < 2){
TORCH_CHECK(false, "Expected 2 or more dimensions (got ", input.dim(), ")");
}
if (input.sizes()[0] != target.sizes()[0]) {
TORCH_CHECK(false, "Expected input batch_size (", input.sizes()[0], ") to match target batch_size (", target.sizes()[0], ").");
}
torch::Tensor ret;
torch::Tensor input_ = input;
torch::Tensor target_ = target;
if (input_.dim() == 2) {
ret = torch::nll_loss(
input_,
target_,
weight,
enumtype::reduction_get_enum(reduction),
ignore_index);
} else if (input_.dim() == 4) {
ret = torch::nll_loss2d(
input_,
target_,
weight,
enumtype::reduction_get_enum(reduction),
ignore_index);
} else {
// dim == 3 or dim > 4
auto n = input_.sizes()[0];
auto c = input_.sizes()[1];
auto out_size = input_.sizes().slice(2).vec();
out_size.insert(out_size.begin(), n);
if (target_.sizes().slice(1) != input_.sizes().slice(2)) {
TORCH_CHECK(false, "Expected target size ", at::IntArrayRef(out_size), ", got ", target_.sizes());
}
input_ = input_.contiguous();
target_ = target_.contiguous();
// support empty batches, see #15870
if (input_.numel() > 0) {
input_ = input_.view({n, c, 1, -1});
} else {
input_ = input_.view({n, c, 0, 0});
}
if (target_.numel() > 0) {
target_ = target_.view({n, 1, -1});
} else {
target_ = target_.view({n, 0, 0});
}
auto reduction_enum = enumtype::reduction_get_enum(reduction);
if (!c10::get_if<enumtype::kNone>(&reduction)) {
ret = torch::nll_loss2d(input_, target_, weight, reduction_enum, ignore_index);
} else {
auto out = torch::nll_loss2d(input_, target_, weight, reduction_enum, ignore_index);
ret = out.view(out_size);
}
}
return ret;
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.nll_loss
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::NLLLossFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::nll_loss(input, target, F::NLLLossFuncOptions().ignore_index(-100).reduction(torch::kMean));
/// ```
inline Tensor nll_loss(
const Tensor& input,
const Tensor& target,
const NLLLossFuncOptions& options = {}) {
return detail::nll_loss(
input,
target,
options.weight(),
options.ignore_index(),
options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor cross_entropy(
const Tensor& input,
const Tensor& target,
const Tensor& weight,
int64_t ignore_index,
CrossEntropyFuncOptions::reduction_t reduction) {
NLLLossFuncOptions::reduction_t reduction_;
if (c10::get_if<enumtype::kNone>(&reduction)) {
reduction_ = torch::kNone;
} else if (c10::get_if<enumtype::kMean>(&reduction)) {
reduction_ = torch::kMean;
} else if (c10::get_if<enumtype::kSum>(&reduction)) {
reduction_ = torch::kSum;
} else {
TORCH_INTERNAL_ASSERT(
false,
enumtype::get_enum_name(reduction),
" is not valid");
}
return torch::nn::functional::detail::nll_loss(
torch::nn::functional::detail::log_softmax(input, 1, c10::nullopt),
target,
weight,
ignore_index,
reduction_);
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.cross_entropy
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::CrossEntropyFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::cross_entropy(input, target, F::CrossEntropyFuncOptions().ignore_index(-100).reduction(torch::kMean));
/// ```
inline Tensor cross_entropy(
const Tensor& input,
const Tensor& target,
const CrossEntropyFuncOptions& options = {}) {
return detail::cross_entropy(
input,
target,
options.weight(),
options.ignore_index(),
options.reduction());
}
// ============================================================================
#ifndef DOXYGEN_SHOULD_SKIP_THIS
namespace detail {
inline Tensor binary_cross_entropy_with_logits(
const Tensor& input, const Tensor& target, const Tensor& weight,
BinaryCrossEntropyWithLogitsFuncOptions::reduction_t reduction, const Tensor& pos_weight) {
TORCH_CHECK(target.sizes() == input.sizes(),
"Target size (", target.sizes(),
") must be the same as input size (",
input.sizes(), ")"
);
return torch::binary_cross_entropy_with_logits(input, target,
weight, pos_weight, enumtype::reduction_get_enum(reduction));
}
} // namespace detail
#endif /* DOXYGEN_SHOULD_SKIP_THIS */
/// See https://pytorch.org/docs/master/nn.functional.html#torch.nn.functional.binary_cross_entropy_with_logits
/// about the exact behavior of this functional.
///
/// See the documentation for `torch::nn::functional::BinaryCrossEntropyWithLogitsFuncOptions` class to learn what
/// optional arguments are supported for this functional.
///
/// Example:
/// ```
/// namespace F = torch::nn::functional;
/// F::binary_cross_entropy_with_logits(input, target, F::BinaryCrossEntropyWithLogitsFuncOptions().pos_weight(pos_weight).reduction(torch::kSum));
/// ```
inline Tensor binary_cross_entropy_with_logits(
const Tensor& input, const Tensor& target,
const BinaryCrossEntropyWithLogitsFuncOptions& options = {}) {
return detail::binary_cross_entropy_with_logits(input, target,
options.weight(), options.reduction(), options.pos_weight());
}
} // namespace functional
} // namespace nn
} // namespace torch