oneflow.nn.MarginRankingLoss¶
-
class
oneflow.nn.MarginRankingLoss(margin: float = 0.0, reduction: str = 'mean')¶ Creates a criterion that measures the loss given inputs \(x1\), \(x2\), two 1D mini-batch Tensors, and a label 1D mini-batch tensor \(y\) (containing 1 or -1).
If \(y = 1\) then it assumed the first input should be ranked higher (have a larger value) than the second input, and vice-versa for \(y = -1\).
The loss function for each sample in the mini-batch is:
\[\text{loss}(x1, x2, y) = \max(0, -y * (x1 - x2) + \text{margin})\]- Parameters
margin (float, optional) – Has a default value of \(0\).
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. Default:'mean'
- Shape:
x1 : \((N, D)\) where N is the batch size and D is the size of a sample.
x2 : \((N, D)\) where N is the batch size and D is the size of a sample.
Target: \((N)\)
Output: scalar. If
reductionis'none', then \((N)\).
For example:
>>> import oneflow as flow >>> import numpy as np >>> x1 = flow.tensor(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]), dtype=flow.float32) >>> x2 = flow.tensor(np.array([[2, 2, 2], [2, 2, 2], [2, 2, 2]]), dtype=flow.float32) >>> target = flow.tensor(np.array([[1, -1, 1],[-1, 1, -1], [1, 1, 1]]), dtype=flow.float32) >>> m = flow.nn.MarginRankingLoss(margin =1.0, reduction="none") >>> out = m(x1, x2, target) >>> out tensor([[2., 1., 0.], [3., 0., 5.], [0., 0., 0.]], dtype=oneflow.float32) >>> m = flow.nn.MarginRankingLoss(margin = 0.3, reduction="sum") >>> out = m(x1, x2, target) >>> out tensor(8.2000, dtype=oneflow.float32) >>> m = flow.nn.MarginRankingLoss(margin = 10, reduction="mean") >>> out = m(x1, x2, target) >>> out tensor(8.3333, dtype=oneflow.float32)
-
__init__(margin: float = 0.0, reduction: str = 'mean') → None¶ Initialize self. See help(type(self)) for accurate signature.
Methods
__call__(*args, **kwargs)Call self as a function.
__delattr__(name, /)Implement delattr(self, name).
__dir__()Default dir() implementation.
__eq__(value, /)Return self==value.
__format__(format_spec, /)Default object formatter.
__ge__(value, /)Return self>=value.
__getattr__(name)__getattribute__(name, /)Return getattr(self, name).
__gt__(value, /)Return self>value.
__hash__()Return hash(self).
__init__([margin, reduction])Initialize self.
__init_subclass__This method is called when a class is subclassed.
__le__(value, /)Return self<=value.
__lt__(value, /)Return self<value.
__ne__(value, /)Return self!=value.
__new__(**kwargs)Create and return a new object.
__reduce__()Helper for pickle.
__reduce_ex__(protocol, /)Helper for pickle.
__repr__()Return repr(self).
__setattr__(name, value)Implement setattr(self, name, value).
__sizeof__()Size of object in memory, in bytes.
__str__()Return str(self).
__subclasshook__Abstract classes can override this to customize issubclass().
_apply(fn[, applied_dict])_get_name()_load_from_state_dict(state_dict, prefix, …)_named_members(get_members_fn[, prefix, recurse])_save_to_state_dict(destination, prefix, …)_shallow_repr()add_module(name, module)Adds a child module to the current module.
apply(fn)Applies
fnrecursively to every submodule (as returned by.children()) as well as self.buffers([recurse])Returns an iterator over module buffers.
children()Returns an iterator over immediate children modules.
cpu()Moves all model parameters and buffers to the CPU.
cuda([device])Moves all model parameters and buffers to the GPU.
double()Casts all floating point parameters and buffers to
doubledatatype.eval()Sets the module in evaluation mode.
extra_repr()Set the extra representation of the module
float()Casts all floating point parameters and buffers to
floatdatatype.forward(input1, input2, target)half()Casts all floating point parameters and buffers to
halfdatatype.load_state_dict(state_dict[, strict])Copies parameters and buffers from
state_dictinto this module and its descendants.modules()Returns an iterator over all modules in the network.
named_buffers([prefix, recurse])Returns an iterator over module buffers, yielding both the name of the buffer as well as the buffer itself.
named_children()Returns an iterator over immediate children modules, yielding both the name of the module as well as the module itself.
named_modules([memo, prefix])Returns an iterator over all modules in the network, yielding both the name of the module as well as the module itself.
named_parameters([prefix, recurse])Returns an iterator over module parameters, yielding both the name of the parameter as well as the parameter itself.
parameters([recurse])Returns an iterator over module parameters.
register_buffer(name, tensor[, persistent])Adds a buffer to the module.
register_forward_hook(hook)Registers a forward hook on the module.
register_forward_pre_hook(hook)Registers a forward pre-hook on the module.
register_parameter(name, param)Adds a parameter to the module.
state_dict([destination, prefix, keep_vars])Returns a dictionary containing a whole state of the module.
to([device])Moves the parameters and buffers.
to_consistent(*args, **kwargs)This interface is no longer available, please use
oneflow.nn.Module.to_global()instead.to_global([placement, sbp])Convert the parameters and buffers to global.
train([mode])Sets the module in training mode.
zero_grad([set_to_none])Sets gradients of all model parameters to zero.