pytorch

Pytorch Manual

  • F.cross_entropy
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
def (input, target, weight=None, size_average=None, ignore_index=-100,
reduce=None, reduction='mean'):

r"""This criterion combines `log_softmax` and `nll_loss` in a single
function.

See :class:`~torch.nn.CrossEntropyLoss` for details.

Args:
input (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 > 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.
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::

>>> input = torch.randn(3, 5, requires_grad=True)
>>> target = torch.randint(5, (3,), dtype=torch.int64)
>>> loss = F.cross_entropy(input, target)
>>> loss.backward()
"""
if size_average is not None or reduce is not None:
reduction = _Reduction.legacy_get_string(size_average, reduce)
return nll_loss(log_softmax(input, 1), target, weight, None, ignore_index, None, reduction)
1
2


cross_entropy

  • torch.nn.functional.``cross_entropy(input, target, weight=None, size_average=None, ignore_index=-100, reduce=None, reduction=’mean’)[SOURCE]

    This criterion combines log_softmax and nll_loss in a single function.See CrossEntropyLoss for details.Parameters:

input (Tensor) – (N, C)(N,C) where C = number of classes or (N, C, H, W)(N,C,H,W) in case of 2D Loss, or (N, C, d_1, d_2, …, d_K)(N,C,d1,d2,…,dK) where K > 1K>1 in the case of K-dimensional loss.

target (Tensor) – (N)(N) where each value is

0≤targets[i]≤C−1, or(N,d1,d2,…,dK) where K≥1 for K-dimensional loss.

weight (Tensor, optional) – a manual rescaling weight given to each class. If given, has to be a Tensor of size Csize_average (bool, optional) – Deprecated (see 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 size_average is set to False, the losses are instead summed for each minibatch. Ignored when reduce is False. Default: Trueignore_index (int, optional) – Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets. Default: -100reduce (bool, optional) – Deprecated (see reduction). By default, the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch element instead and ignores size_average. Default: Truereduction (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: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: ‘mean’Examples:>>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randint(5, (3,), dtype=torch.int64) >>> loss = F.cross_entropy(input, target) >>> loss.backward()

This criterion combines log_softmax and nll_loss in a single function.See CrossEntropyLoss for details.Parameters:
input (Tensor) – (N, C)(N,C) where C = number of classes or (N, C, H, W)(N,C,H,W) in case of 2D Loss, or (N, C, d_1, d_2, …, d_K)(N,C,d1,d2,…,dK) where K > 1K>1 in the case of K-dimensional loss.
target (Tensor) – (N)(N) where each value is 0 leq text{targets}[i] leq C-10≤targets[i]≤C−1, or (N, d_1, d_2, …, d_K)(N,d1,d2,…,dK)where K geq 1K≥1 for K-dimensional loss.weight (Tensor, optional) – a manual rescaling weight given to each class. If given, has to be a Tensor of size Csize_average (bool, optional) – Deprecated (see 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 size_average is set to False, the losses are instead summed for each minibatch. Ignored when reduce is False. Default: Trueignore_index (int, optional) – Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets. Default: -100reduce (bool, optional) – Deprecated (see reduction). By default, the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch element instead and ignores size_average. Default: Truereduction (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: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: ‘mean’Examples:>>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randint(5, (3,), dtype=torch.int64) >>> loss = F.cross_entropy(input, target) >>> loss.backward()

This criterion combines log_softmax and nll_loss in a single function.See CrossEntropyLoss for details.Parameters:input (Tensor) – (N, C)(N,C) where C = number of classes or (N, C, H, W)(N,C,H,W) in case of 2D Loss, or (N, C, d_1, d_2, …, d_K)(N,C,d1,d2,…,dK) where K > 1K>1 in the case of K-dimensional loss.target (Tensor) – (N)(N) where each value is 0 leq text{targets}[i] leq C-10≤targets[i]≤C−1, or (N, d_1, d_2, …, d_K)(N,d1,d2,…,dK)where K geq 1K≥1 for K-dimensional loss.weight (Tensor, optional) – a manual rescaling weight given to each class. If given, has to be a Tensor of size Csize_average (bool, optional) – Deprecated (see 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 size_average is set to False, the losses are instead summed for each minibatch. Ignored when reduce is False. Default: Trueignore_index (int, optional) – Specifies a target value that is ignored and does not contribute to the input gradient. When size_average is True, the loss is averaged over non-ignored targets. Default: -100reduce (bool, optional) – Deprecated (see reduction). By default, the losses are averaged or summed over observations for each minibatch depending on size_average. When reduce is False, returns a loss per batch element instead and ignores size_average. Default: Truereduction (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: size_average and reduce are in the process of being deprecated, and in the meantime, specifying either of those two args will override reduction. Default: ‘mean’Examples:>>> input = torch.randn(3, 5, requires_grad=True) >>> target = torch.randint(5, (3,), dtype=torch.int64) >>> loss = F.cross_entropy(input, target) >>> loss.backward()

  • Tensor.round()
    • 四舍五入取整