|
107 | 107 | 'log',
|
108 | 108 | 'crop',
|
109 | 109 | 'rank_loss',
|
| 110 | + 'margin_rank_loss', |
110 | 111 | 'elu',
|
111 | 112 | 'relu6',
|
112 | 113 | 'pow',
|
@@ -5827,6 +5828,54 @@ def rank_loss(label, left, right, name=None):
|
5827 | 5828 | return out
|
5828 | 5829 |
|
5829 | 5830 |
|
| 5831 | +def margin_rank_loss(label, left, right, margin=0.1, name=None): |
| 5832 | + """ |
| 5833 | + Margin Ranking Loss Layer for ranking problem, |
| 5834 | + which compares left score and right score passed in. |
| 5835 | + The ranking loss can be defined as following equation: |
| 5836 | +
|
| 5837 | + .. math:: |
| 5838 | +
|
| 5839 | + rank\_loss &= max(0, -label * (left - right) + margin) |
| 5840 | +
|
| 5841 | + Args: |
| 5842 | + label (Variable): Indicates whether the left is ranked higher than the right or not. |
| 5843 | + left (Variable): Ranking score for left. |
| 5844 | + right (Variable): Ranking score for right. |
| 5845 | + margin (float): Indicates the given margin. |
| 5846 | + name (str|None): A name for this layer (optional). If set None, the layer |
| 5847 | + will be named automatically. |
| 5848 | + Returns: |
| 5849 | + Variable: The ranking loss. |
| 5850 | + Raises: |
| 5851 | + ValueError: Any of label, left, and right is not a Variable. |
| 5852 | + Examples: |
| 5853 | + .. code-block:: python |
| 5854 | + label = fluid.layers.data(name="label", shape=[4, 1], dtype="float32") |
| 5855 | + left = fluid.layers.data(name="left", shape=[4, 1], dtype="float32") |
| 5856 | + right = fluid.layers.data(name="right", shape=[4, 1], dtype="float32") |
| 5857 | + out = fluid.layers.margin_rank_loss(label, left, right) |
| 5858 | + """ |
| 5859 | + helper = LayerHelper('margin_rank_loss', **locals()) |
| 5860 | + if not isinstance(label, Variable): |
| 5861 | + raise ValueError("The label should be a Variable.") |
| 5862 | + if not isinstance(left, Variable): |
| 5863 | + raise ValueError("The left should be a Variable.") |
| 5864 | + if not isinstance(right, Variable): |
| 5865 | + raise ValueError("The right should be a Variable.") |
| 5866 | + out = helper.create_tmp_variable(left.dtype) |
| 5867 | + act = helper.create_tmp_variable(left.dtype) |
| 5868 | + helper.append_op( |
| 5869 | + type='margin_rank_loss', |
| 5870 | + inputs={"Label": label, |
| 5871 | + "X1": left, |
| 5872 | + "X2": right}, |
| 5873 | + outputs={'Out': out, |
| 5874 | + 'Activated': act}, |
| 5875 | + attrs={'margin': margin}) |
| 5876 | + return out |
| 5877 | + |
| 5878 | + |
5830 | 5879 | def pad2d(input,
|
5831 | 5880 | paddings=[0, 0, 0, 0],
|
5832 | 5881 | mode='constant',
|
@@ -6290,6 +6339,7 @@ def sequence_enumerate(input, win_size, pad_value=0, name=None):
|
6290 | 6339 | outputs={'Out': out},
|
6291 | 6340 | attrs={'win_size': win_size,
|
6292 | 6341 | 'pad_value': pad_value})
|
| 6342 | + return out |
6293 | 6343 |
|
6294 | 6344 |
|
6295 | 6345 | def sequence_mask(x, maxlen=None, dtype='int64', name=None):
|
|
0 commit comments