From fa870fc4477c3814f0e349ec9cddec9b6eb70e7c Mon Sep 17 00:00:00 2001 From: TomorrowIsAnOtherDay <2466956298@qq.com> Date: Wed, 14 Feb 2018 18:13:16 +0800 Subject: [PATCH 1/3] add python API for one_hot OP --- python/paddle/v2/fluid/layers/nn.py | 38 +++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) diff --git a/python/paddle/v2/fluid/layers/nn.py b/python/paddle/v2/fluid/layers/nn.py index 5f1842f5fb95e0..8be26e6f7d5a83 100644 --- a/python/paddle/v2/fluid/layers/nn.py +++ b/python/paddle/v2/fluid/layers/nn.py @@ -68,6 +68,7 @@ 'layer_norm', 'softmax_with_cross_entropy', 'smooth_l1', + 'one_hot', ] @@ -3212,3 +3213,40 @@ def smooth_l1(x, y, inside_weight=None, outside_weight=None, sigma=None): 'Out': loss}, attrs={'sigma': sigma}) return loss + +def one_hot(input, depth): + """ + One Hot Operator. This operator creates the one-hot representations for input + index values. The following example will help to explain the function of this + operator. + + Args: + input(Tensor/LodTensor): A Tensor/LodTensor of indices, last dimension must be 1. + depth(scalar): an interger defining the depth of the one hot dimension. + + Returns: + The one-hot tensor or LodTensor, same as input. + + Examples: + X is a LoDTensor: + X.lod = [[0, 1, 4]] + X.shape = [4, 1] + X.data = [[1], [1], [3], [0]] + set depth = 4 + Out is a LoDTensor: + Out.lod = [[0, 1, 4]] + Out.shape = [4, 4] + Out.data = [[0., 1., 0., 0.], + [0., 1., 0., 0.], + [0., 0., 0., 1.], + [1., 0., 0., 0.]] + """ + helper = LayerHelper("one_hot", **locals()) + one_hot_out = helper.create_tmp_variable(dtype='float32') + helper.append_op( + type="one_hot", + inputs={'X': input}, + attrs={'depth': depth}, + outputs={'Out': one_hot_out}) + return one_hot_out + From 3cf7442213c2b1d53950e08cdb21b2a59d6f5ac5 Mon Sep 17 00:00:00 2001 From: TomorrowIsAnOtherDay <2466956298@qq.com> Date: Wed, 14 Feb 2018 18:23:11 +0800 Subject: [PATCH 2/3] fix code style --- python/paddle/v2/fluid/layers/nn.py | 1 + 1 file changed, 1 insertion(+) diff --git a/python/paddle/v2/fluid/layers/nn.py b/python/paddle/v2/fluid/layers/nn.py index 8be26e6f7d5a83..8475632500c5af 100644 --- a/python/paddle/v2/fluid/layers/nn.py +++ b/python/paddle/v2/fluid/layers/nn.py @@ -3214,6 +3214,7 @@ def smooth_l1(x, y, inside_weight=None, outside_weight=None, sigma=None): attrs={'sigma': sigma}) return loss + def one_hot(input, depth): """ One Hot Operator. This operator creates the one-hot representations for input From 4f8f5c66527dcb2252d2333751d9df90379cf9a4 Mon Sep 17 00:00:00 2001 From: TomorrowIsAnOtherDay <2466956298@qq.com> Date: Wed, 14 Feb 2018 18:30:09 +0800 Subject: [PATCH 3/3] fix code style_2 --- python/paddle/v2/fluid/layers/nn.py | 1 - 1 file changed, 1 deletion(-) diff --git a/python/paddle/v2/fluid/layers/nn.py b/python/paddle/v2/fluid/layers/nn.py index 8475632500c5af..d1ac6583dd40d0 100644 --- a/python/paddle/v2/fluid/layers/nn.py +++ b/python/paddle/v2/fluid/layers/nn.py @@ -3250,4 +3250,3 @@ def one_hot(input, depth): attrs={'depth': depth}, outputs={'Out': one_hot_out}) return one_hot_out -