-
Notifications
You must be signed in to change notification settings - Fork 0
/
loss_lc.py
339 lines (276 loc) · 18.1 KB
/
loss_lc.py
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
from torch.nn.modules.module import Module
from torch.nn import functional as F
from torch.nn import _reduction as _Reduction
from torch import Tensor
from typing import Optional
import warnings
import torch
from torch.nn import grad # noqa: F401
class _Loss(Module):
reduction: str
def __init__(self, size_average=None, reduce=None, reduction: str = 'mean') -> None:
super(_Loss, self).__init__()
if size_average is not None or reduce is not None:
self.reduction = _Reduction.legacy_get_string(size_average, reduce)
else:
self.reduction = reduction
class _WeightedLoss(_Loss):
def __init__(self, weight: Optional[Tensor] = None, size_average=None, reduce=None, reduction: str = 'mean') -> None:
super(_WeightedLoss, self).__init__(size_average, reduce, reduction)
self.register_buffer('weight', weight)
def log_softmax_lc(input_n: Tensor, input_b: Tensor, logits_calibraion_degree: float, dim: Optional[int] = None, _stacklevel: int = 3, dtype: Optional[int] = None) -> Tensor:
r"""Applies a softmax followed by a logarithm.
While mathematically equivalent to log(softmax(x)), doing these two
operations separately is slower, and numerically unstable. This function
uses an alternative formulation to compute the output and gradient correctly.
See :class:`~torch.nn.LogSoftmax` for more details.
Args:
input_n (Tensor): input_n
input_b (Tensor): input_b
logits_calibraion_degree (float): logits_calibraion_degree
dim (int): A dimension along which log_softmax will be computed.
dtype (:class:`torch.dtype`, optional): the desired data type of returned tensor.
If specified, the input tensor is casted to :attr:`dtype` before the operation
is performed. This is useful for preventing data type overflows. Default: None.
"""
if dtype is None:
ret = torch.sub(torch.add(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.mul(logits_calibraion_degree, torch.sub(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.sub(input_b, torch.mul(torch.ones(input_b.size()).cuda(), torch.max(input_b)))))),
torch.log(torch.sum(torch.exp(torch.add(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.mul(logits_calibraion_degree, torch.sub(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.sub(input_b, torch.mul(torch.ones(input_b.size()).cuda(), torch.max(input_b))))))))))
else:
ret = torch.sub(torch.add(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.mul(logits_calibraion_degree, torch.sub(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.sub(input_b, torch.mul(torch.ones(input_b.size()).cuda(), torch.max(input_b)))))),
torch.log(torch.sum(torch.exp(torch.add(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.mul(logits_calibraion_degree, torch.sub(torch.sub(input_n, torch.mul(torch.ones(input_n.size()).cuda(), torch.max(input_n))), torch.sub(input_b, torch.mul(torch.ones(input_b.size()).cuda(), torch.max(input_b))))))))))
return ret
def mselc_loss(
input_n: Tensor,
input_b: Tensor,
target: Tensor,
logits_calibraion_degree: float,
size_average: Optional[bool] = None,
reduce: Optional[bool] = None,
reduction: str = "mean",
) -> Tensor:
r"""mselc_loss(input_n, input_b, target, logits_calibraion_degree, size_average=None, reduce=None, reduction='mean') -> Tensor
Measures the element-wise mean squared error with logits calibration.
See :class:`~torch.nn.MSELoss` for details.
"""
if not (target.size() == input_n.size() and target.size() == input_b.size()):
warnings.warn(
"Using a target size ({}) that is different to the input_n size ({}), input_b size ({})."
"This will likely lead to incorrect results due to broadcasting. "
"Please ensure they have the same size.".format(target.size(), input_n.size(), input_b.size()),
stacklevel=2,
)
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)
expanded_input_n, expanded_input_b, expanded_target = torch.broadcast_tensors(input_n, input_b, target)
reduction_enum = _Reduction.get_enum(reduction)
if reduction_enum == 0:
ret = torch.add(torch.pow(torch.sub(expanded_input_n, expanded_target), 2), torch.mul(logits_calibraion_degree, torch.pow(torch.sub(expanded_input_n, expanded_input_b), 2)))
elif reduction_enum == 1:
ret = torch.mean(torch.add(torch.pow(torch.sub(expanded_input_n, expanded_target), 2), torch.mul(logits_calibraion_degree, torch.pow(torch.sub(expanded_input_n, expanded_input_b), 2))))
elif reduction_enum == 2:
ret = torch.sum(torch.add(torch.pow(torch.sub(expanded_input_n, expanded_target), 2), torch.mul(logits_calibraion_degree, torch.pow(torch.sub(expanded_input_n, expanded_input_b), 2))))
else:
ret = -1 # TODO: remove once JIT exceptions support control flow
raise ValueError("{} is not a valid value for reduction".format(reduction))
return ret
def celc_loss(
input_n: Tensor,
input_b: Tensor,
target: Tensor,
logits_calibraion_degree: float,
weight: Optional[Tensor] = None,
size_average: Optional[bool] = None,
ignore_index: int = -100,
reduce: Optional[bool] = None,
reduction: str = "mean",
) -> Tensor:
r"""This criterion combines `log_softmax` and `nll_loss` in a single
function.
See :class:`~torch.nn.CrossEntropyLoss` for details.
Args:
input_n (Tensor) : :math:`(N, C)` where `C = number of classes` or :math:`(N, C, H, W)`
in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K \geq 1`
in the case of K-dimensional loss.
input_b (Tensor) : :math:`(N, C)` where `C = number of classes` or :math:`(N, C, H, W)`
in case of 2D Loss, or :math:`(N, C, d_1, d_2, ..., d_K)` where :math:`K \geq 1`
in the case of K-dimensional loss.
target (Tensor) : :math:`(N)` where each value is :math:`0 \leq \text{targets}[i] \leq C-1`,
or :math:`(N, d_1, d_2, ..., d_K)` where :math:`K \geq 1` for
K-dimensional loss.
logits_calibraion_degree (float): logits_calibraion_degree.
weight (Tensor, optional): a manual rescaling weight given to each
class. If given, has to be a Tensor of size `C`
size_average (bool, optional): Deprecated (see :attr:`reduction`). By default,
the losses are averaged over each loss element in the batch. Note that for
some losses, there multiple elements per sample. If the field :attr:`size_average`
is set to ``False``, the losses are instead summed for each minibatch. Ignored
when reduce is ``False``. Default: ``True``
ignore_index (int, optional): Specifies a target value that is ignored
and does not contribute to the input gradient. When :attr:`size_average` is
``True``, the loss is averaged over non-ignored targets. Default: -100
reduce (bool, optional): Deprecated (see :attr:`reduction`). By default, the
losses are averaged or summed over observations for each minibatch depending
on :attr:`size_average`. When :attr:`reduce` is ``False``, returns a loss per
batch element instead and ignores :attr:`size_average`. Default: ``True``
reduction (string, optional): Specifies the reduction to apply to the output:
``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
``'mean'``: the sum of the output will be divided by the number of
elements in the output, ``'sum'``: the output will be summed. Note: :attr:`size_average`
and :attr:`reduce` are in the process of being deprecated, and in the meantime,
specifying either of those two args will override :attr:`reduction`. Default: ``'mean'``
Examples::
>>> import loss_lc
>>> input_n = torch.randn(3, 5, requires_grad=True)
>>> input_b = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>> logits_calibraion_degree = torch.rand(1)
>>> loss = loss_lc.celc_loss(input_n, input_b, target, logits_calibraion_degree)
>>> loss.backward()
"""
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)
return F.nll_loss(log_softmax_lc(input_n, input_b, logits_calibraion_degree, 1), target, weight, None, ignore_index, None, reduction)
class MSELCLoss(_Loss):
r"""Creates a criterion that measures the mean squared error (squared L2 norm) between
each element in the input :math:`x` and target :math:`y`.
The unreduced (i.e. with :attr:`reduction` set to ``'none'``) loss can be described as:
.. math::
\ell(x, y) = L = \{l_1,\dots,l_N\}^\top, \quad
l_n = \left( x_n - y_n \right)^2,
where :math:`N` is the batch size. If :attr:`reduction` is not ``'none'``
(default ``'mean'``), then:
.. math::
\ell(x, y) =
\begin{cases}
\operatorname{mean}(L), & \text{if reduction} = \text{`mean';}\\
\operatorname{sum}(L), & \text{if reduction} = \text{`sum'.}
\end{cases}
:math:`x` and :math:`y` are tensors of arbitrary shapes with a total
of :math:`n` elements each.
The mean operation still operates over all the elements, and divides by :math:`n`.
The division by :math:`n` can be avoided if one sets ``reduction = 'sum'``.
Args:
size_average (bool, optional): Deprecated (see :attr:`reduction`). By default,
the losses are averaged over each loss element in the batch. Note that for
some losses, there are multiple elements per sample. If the field :attr:`size_average`
is set to ``False``, the losses are instead summed for each minibatch. Ignored
when :attr:`reduce` is ``False``. Default: ``True``
reduce (bool, optional): Deprecated (see :attr:`reduction`). By default, the
losses are averaged or summed over observations for each minibatch depending
on :attr:`size_average`. When :attr:`reduce` is ``False``, returns a loss per
batch element instead and ignores :attr:`size_average`. Default: ``True``
reduction (string, optional): Specifies the reduction to apply to the output:
``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will be applied,
``'mean'``: the sum of the output will be divided by the number of
elements in the output, ``'sum'``: the output will be summed. Note: :attr:`size_average`
and :attr:`reduce` are in the process of being deprecated, and in the meantime,
specifying either of those two args will override :attr:`reduction`. Default: ``'mean'``
Shape:
- Input_n: :math:`(N, *)` where :math:`*` means, any number of additional
dimensions
- Input_b: :math:`(N, *)` where :math:`*` means, any number of additional
dimensions
- Target: :math:`(N, *)`, same shape as the input
- logits_calibraion_degree (float): logits_calibraion_degree
Examples::
>>> import loss_lc
>>> loss = loss_lc.MSELCLoss()
>>> input_n = torch.randn(3, 5, requires_grad=True)
>>> input_b = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randn(3, 5)
>>> logits_calibraion_degree = torch.rand(1)
>>> output = loss(input_n, input_b, target, logits_calibraion_degree)
>>> output.backward()
"""
__constants__ = ['reduction']
def __init__(self, size_average=None, reduce=None, reduction: str = 'mean') -> None:
super(MSELCLoss, self).__init__(size_average, reduce, reduction)
def forward(self, input_n: Tensor, input_b: Tensor, target: Tensor, logits_calibraion_degree: float) -> Tensor:
return mselc_loss(input_n, input_b, target, logits_calibraion_degree, reduction=self.reduction)
class CELCLoss(_WeightedLoss):
r"""This criterion combines :class:`~torch.nn.LogSoftmax` and :class:`~torch.nn.NLLLoss` in one single class.
It is useful when training a classification problem with `C` classes.
If provided, the optional argument :attr:`weight` should be a 1D `Tensor`
assigning weight to each of the classes.
This is particularly useful when you have an unbalanced training set.
The `input` is expected to contain raw, unnormalized scores for each class.
`input` has to be a Tensor of size either :math:`(minibatch, C)` or
:math:`(minibatch, C, d_1, d_2, ..., d_K)`
with :math:`K \geq 1` for the `K`-dimensional case (described later).
This criterion expects a class index in the range :math:`[0, C-1]` as the
`target` for each value of a 1D tensor of size `minibatch`; if `ignore_index`
is specified, this criterion also accepts this class index (this index may not
necessarily be in the class range).
The loss can be described as:
.. math::
\text{loss}(x, class) = -\log\left(\frac{\exp(x[class])}{\sum_j \exp(x[j])}\right)
= -x[class] + \log\left(\sum_j \exp(x[j])\right)
or in the case of the :attr:`weight` argument being specified:
.. math::
\text{loss}(x, class) = weight[class] \left(-x[class] + \log\left(\sum_j \exp(x[j])\right)\right)
The losses are averaged across observations for each minibatch. If the
:attr:`weight` argument is specified then this is a weighted average:
.. math::
\text{loss} = \frac{\sum^{N}_{i=1} loss(i, class[i])}{\sum^{N}_{i=1} weight[class[i]]}
Can also be used for higher dimension inputs, such as 2D images, by providing
an input of size :math:`(minibatch, C, d_1, d_2, ..., d_K)` with :math:`K \geq 1`,
where :math:`K` is the number of dimensions, and a target of appropriate shape
(see below).
Args:
weight (Tensor, optional): a manual rescaling weight given to each class.
If given, has to be a Tensor of size `C`
size_average (bool, optional): Deprecated (see :attr:`reduction`). By default,
the losses are averaged over each loss element in the batch. Note that for
some losses, there are multiple elements per sample. If the field :attr:`size_average`
is set to ``False``, the losses are instead summed for each minibatch. Ignored
when :attr:`reduce` is ``False``. Default: ``True``
ignore_index (int, optional): Specifies a target value that is ignored
and does not contribute to the input gradient. When :attr:`size_average` is
``True``, the loss is averaged over non-ignored targets.
reduce (bool, optional): Deprecated (see :attr:`reduction`). By default, the
losses are averaged or summed over observations for each minibatch depending
on :attr:`size_average`. When :attr:`reduce` is ``False``, returns a loss per
batch element instead and ignores :attr:`size_average`. Default: ``True``
reduction (string, optional): Specifies the reduction to apply to the output:
``'none'`` | ``'mean'`` | ``'sum'``. ``'none'``: no reduction will
be applied, ``'mean'``: the weighted mean of the output is taken,
``'sum'``: the output will be summed. Note: :attr:`size_average`
and :attr:`reduce` are in the process of being deprecated, and in
the meantime, specifying either of those two args will override
:attr:`reduction`. Default: ``'mean'``
Shape:
- Input_n: :math:`(N, C)` where `C = number of classes`, or
:math:`(N, C, d_1, d_2, ..., d_K)` with :math:`K \geq 1`
in the case of `K`-dimensional loss.
- Input_b: :math:`(N, C)` where `C = number of classes`, or
:math:`(N, C, d_1, d_2, ..., d_K)` with :math:`K \geq 1`
in the case of `K`-dimensional loss.
- Target: :math:`(N)` where each value is :math:`0 \leq \text{targets}[i] \leq C-1`, or
:math:`(N, d_1, d_2, ..., d_K)` with :math:`K \geq 1` in the case of
K-dimensional loss.
- logits_calibraion_degree (float): logits_calibraion_degree
- Output: scalar.
If :attr:`reduction` is ``'none'``, then the same size as the target:
:math:`(N)`, or
:math:`(N, d_1, d_2, ..., d_K)` with :math:`K \geq 1` in the case
of K-dimensional loss.
Examples::
>>> import loss_lc
>>> loss = loss_lc.CELCLoss()
>>> input_n = torch.randn(3, 5, requires_grad=True)
>>> input_b = torch.randn(3, 5, requires_grad=True)
>>> target = torch.empty(3, dtype=torch.long).random_(5)
>>> logits_calibraion_degree = torch.rand(1)
>>> output = loss(input_n, input_b, target, logits_calibraion_degree)
>>> output.backward()
"""
__constants__ = ['ignore_index', 'reduction']
ignore_index: int
def __init__(self, weight: Optional[Tensor] = None, size_average=None, ignore_index: int = -100,
reduce=None, reduction: str = 'mean') -> None:
super(CELCLoss, self).__init__(weight, size_average, reduce, reduction)
self.ignore_index = ignore_index
def forward(self, input_n: Tensor, input_b: Tensor, target: Tensor, logits_calibraion_degree: float) -> Tensor:
assert self.weight is None or isinstance(self.weight, Tensor)
return celc_loss(input_n, input_b, target, logits_calibraion_degree, weight=self.weight, ignore_index=self.ignore_index, reduction=self.reduction)