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
use burn_core as burn;
use burn::config::Config;
use burn::module::{Content, DisplaySettings, Module, ModuleDisplay};
use burn::tensor::{Int, Tensor};
use super::Reduction;
/// Configuration to create a [Multi Margin loss](MultiMarginLoss) using the
/// [init function](MultiMarginLossConfig::init).
#[derive(Config, Debug)]
pub struct MultiMarginLossConfig {
/// The margin between the correct class and the others. Default: `1.0`.
#[config(default = 1.0)]
pub margin: f64,
/// The norm degree for the loss (`1` or `2`). Default: `1`.
#[config(default = 1)]
pub p: i32,
}
impl MultiMarginLossConfig {
/// Initialize [Multi Margin loss](MultiMarginLoss).
pub fn init(&self) -> MultiMarginLoss {
self.assertions();
MultiMarginLoss {
margin: self.margin,
p: self.p,
}
}
fn assertions(&self) {
assert!(
self.margin >= 0.0,
"Margin for multi margin loss must be non-negative, got {}",
self.margin
);
assert!(
self.p == 1 || self.p == 2,
"Multi margin loss only supports p = 1 or p = 2, got {}",
self.p
);
}
}
/// Multi-class classification margin (hinge) loss between input `x` and target class indices
/// `y`, following
/// [`torch.nn.MultiMarginLoss`](https://pytorch.org/docs/stable/generated/torch.nn.MultiMarginLoss.html).
///
/// For each sample the loss is
///
/// ```text
/// L = (1 / C) * sum over i != y of max(0, margin - x[y] + x[i]) ^ p
/// ```
///
/// where `C` is the number of classes and `y` is the target class index.
#[derive(Module, Debug)]
#[module(custom_display)]
pub struct MultiMarginLoss {
/// The margin between the correct class and the others.
pub margin: f64,
/// The norm degree for the loss (`1` or `2`).
pub p: i32,
}
impl ModuleDisplay for MultiMarginLoss {
fn custom_settings(&self) -> Option<DisplaySettings> {
DisplaySettings::new()
.with_new_line_after_attribute(false)
.optional()
}
fn custom_content(&self, content: Content) -> Option<Content> {
content
.add("margin", &self.margin)
.add("p", &self.p)
.optional()
}
}
impl MultiMarginLoss {
/// Compute the loss for each sample, then reduce to a single value.
///
/// `Reduction::Auto` behaves as `Reduction::Mean`.
///
/// # Shapes
///
/// - input: `[batch_size, num_classes]`
/// - target: `[batch_size]` (class indices in `0..num_classes`)
/// - output: `[1]`
pub fn forward(
&self,
input: Tensor<2>,
target: Tensor<1, Int>,
reduction: Reduction,
) -> Tensor<1> {
let loss = self.forward_no_reduction(input, target);
match reduction {
Reduction::Mean | Reduction::Auto => loss.mean(),
Reduction::Sum => loss.sum(),
other => panic!("{other:?} reduction is not supported"),
}
}
/// Compute the loss for each sample, without reducing.
///
/// # Shapes
///
/// - input: `[batch_size, num_classes]`
/// - target: `[batch_size]` (class indices in `0..num_classes`)
/// - output: `[batch_size]`
pub fn forward_no_reduction(&self, input: Tensor<2>, target: Tensor<1, Int>) -> Tensor<1> {
let [batch_size, num_classes] = input.dims();
let target_indices = target.reshape([batch_size, 1]);
// Score of the correct class per sample: [batch_size, 1].
let correct = input.clone().gather(1, target_indices);
// Sum over ALL classes of max(0, margin - x[y] + x[i]) ^ p: [batch_size, 1].
let summed = input
.sub(correct)
.add_scalar(self.margin)
.clamp_min(0.0)
.powi_scalar(self.p)
.sum_dim(1);
// The correct class (i == y) always contributes clamp(margin)^p = margin^p (margin >= 0),
// so subtract it once to exclude it, then average over the classes.
// p is restricted to {1, 2}, so compute margin^p without `f64::powi` (std-only).
let margin_pow_p = if self.p == 2 {
self.margin * self.margin
} else {
self.margin
};
let per_sample: Tensor<1> = summed.sub_scalar(margin_pow_p).squeeze_dim(1);
per_sample.mul_scalar(1.0 / num_classes as f64)
}
}
#[cfg(test)]
mod tests {
use super::*;
use burn::tensor::TensorData;
use burn::tensor::Tolerance;
type FT = f32;
#[test]
fn test_multi_margin_loss() {
let device = Default::default();
let input = Tensor::<2>::from_data(
TensorData::from([[0.1, 0.2, 0.7], [0.9, 0.05, 0.05]]),
&device,
);
let target = Tensor::<1, Int>::from_data(TensorData::from([2, 0]), &device);
let loss = MultiMarginLossConfig::new().init();
let no_reduction = loss.forward_no_reduction(input.clone(), target.clone());
let mean = loss.forward(input.clone(), target.clone(), Reduction::Mean);
let sum = loss.forward(input, target, Reduction::Sum);
// (1/C) * sum_{i != y} max(0, margin - x[y] + x[i]); margin=1, p=1. Reference from PyTorch.
let expected = TensorData::from([0.3, 0.1]);
no_reduction
.into_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
mean.into_data()
.assert_approx_eq::<FT>(&TensorData::from([0.2]), Tolerance::default());
sum.into_data()
.assert_approx_eq::<FT>(&TensorData::from([0.4]), Tolerance::default());
}
#[test]
fn test_multi_margin_loss_p2() {
let device = Default::default();
let input = Tensor::<2>::from_data(
TensorData::from([[0.1, 0.2, 0.7], [0.9, 0.05, 0.05]]),
&device,
);
let target = Tensor::<1, Int>::from_data(TensorData::from([2, 0]), &device);
let loss = MultiMarginLossConfig::new().with_p(2).init();
let no_reduction = loss.forward_no_reduction(input, target);
// squared hinge (p = 2); reference from PyTorch.
let expected = TensorData::from([0.136_667, 0.015]);
no_reduction
.into_data()
.assert_approx_eq::<FT>(&expected, Tolerance::default());
}
#[test]
fn display() {
let config = MultiMarginLossConfig::new().with_margin(0.5);
let loss = config.init();
assert_eq!(
alloc::format!("{loss}"),
"MultiMarginLoss {margin: 0.5, p: 1}"
);
}
}