Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ClipLayer #3112

Merged
merged 7 commits into from
Aug 3, 2017
Merged

Add ClipLayer #3112

merged 7 commits into from
Aug 3, 2017

Conversation

guoshengCS
Copy link
Contributor

Add ClipLayer for clipping the input value by the threshold.

@guoshengCS
Copy link
Contributor Author

不太清楚commit的时候为什么python/paddle/trainer_config_helpers/layers.py里__all__ = 的格式被调整了

Matrix::resizeOrCreate(
tmpMtx, outG->getHeight(), outG->getWidth(), false, useGpu_);
tmpMtx->clipDerivative(*inV, clipThresholdLow_, clipThresholdHigh_);
inG->addDotMul(*outG, *tmpMtx, 1, 1);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if(inG) {
  MatrixPtr outV = getOutputValue();
  MatrixPtr outG = getOutputGrad();
  MatrixPtr tmpMtx;
  Matrix::resizeOrCreate(
       tmpMtx, outG->getHeight(), outG->getWidth(), false, useGpu_);
  tmpMtx->clipDerivative(*inV, clipThresholdLow_, clipThresholdHigh_);
  inG->addDotMul(*outG, *tmpMtx, 1, 1);
}

Copy link
Contributor

@lcy-seso lcy-seso Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

反向的计算不对吧?梯度直接往前层传递,不需要对梯度截断。
抱歉,我犯蠢了,梯度计算是正确的。
image

Copy link
Contributor

@qingqing01 qingqing01 Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor

@lcy-seso lcy-seso Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • 好的,明白啦。我犯蠢了,这里计算的是如果将clip当做一种运算,clip 这运算的导数:超过上下阈值部分输出是常数,对$x$求导梯度为0;否则对$x$求导,梯度为1。

$$y=\text{clip}(x)$$

$$\text{x} = \left{ \begin{align} &a &x< a \\ &x &a \le x \le b \\ &b &x <b \end{align}\right.$$

$$\frac{\partial{L}}{\partial{x}} = \frac{\partial{L}}{\partial{y}}\frac{\partial{y}}{\partial{x}}$$ $$\frac{\partial{y}}{\partial{x}}=\left{ \begin{align} &1 &a \le x \le b \\ &0 &\text{else} \end{align}\right.$$

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

对的,clipDerivative计算的是clip的(sub)gradient

@@ -2169,6 +2169,23 @@ def __init__(self, name, inputs, context_length, **xargs):
self.create_input_parameter(0, psize, dims)


@config_layer('clip')
class ClipLayer(LayerBase):
def __init__(self, name, inputs, clip_threshold_low, clip_threshold_high):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

def __init__(self, name, inputs, clip_threshold_low, clip_threshold_high, **xargs):

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.已添加

@@ -289,6 +289,11 @@ message DetectionOutputConfig {
optional uint32 width = 9 [default = 1];
}

message ClipConfig {
required float clip_threshold_low = 1;
required float clip_threshold_high = 2;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe double is better.

Copy link
Contributor

@lcy-seso lcy-seso Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • clip_threshold_low -->min
  • clip_threshold_high-->max

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,已将type和name进行修改

clip_threshold_low=clip_threshold_low,
clip_threshold_high=clip_threshold_high)
return LayerOutput(
name, LayerType.CLIP_LAYER, parents=[input], size=input.size)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. unit test for this interface
  2. add this layer in the doc: https://github.com/PaddlePaddle/Paddle/blob/develop/doc/api/v2/config/layer.rst

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.已添加

* \f]
*/

class ClipLayer : public Layer {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • ClipLayer 重命名成 ValueClipLayer

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ClipLayer已经是个Layer了,forward一定是value的操作,所以其实觉得ClipLayer名字还好

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,同意。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,使用ClipLayer命名

class ClipLayer : public Layer {
protected:
real clipThresholdLow_;
real clipThresholdHigh_;
Copy link
Contributor

@lcy-seso lcy-seso Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • clipThresholdLow_ --> min_
  • clipThresholdHigh_ --> max_

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

还有一个问题,一般这种截断都是对称截断,因此不需要upper、lower 两个阈值,请问一定需要两个阈值吗?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

觉得两个阈值功能多些,caffe2的clip_op也是min,max两个阈值: https://github.com/caffe2/caffe2/blob/master/caffe2/operators/clip_op.h#L55

Copy link
Contributor

@lcy-seso lcy-seso Aug 2, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

好的,明白啦。同意。

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done,已修改为min_和max_

Matrix::resizeOrCreate(
tmpMtx, outG->getHeight(), outG->getWidth(), false, useGpu_);
tmpMtx->clipDerivative(*inV, clipThresholdLow_, clipThresholdHigh_);
inG->addDotMul(*outG, *tmpMtx, 1, 1);
Copy link
Contributor

@lcy-seso lcy-seso Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

反向的计算不对吧?梯度直接往前层传递,不需要对梯度截断。
抱歉,我犯蠢了,梯度计算是正确的。
image

@@ -289,6 +289,11 @@ message DetectionOutputConfig {
optional uint32 width = 9 [default = 1];
}

message ClipConfig {
required float clip_threshold_low = 1;
required float clip_threshold_high = 2;
Copy link
Contributor

@lcy-seso lcy-seso Aug 1, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • clip_threshold_low -->min
  • clip_threshold_high-->max

Copy link
Contributor

@lcy-seso lcy-seso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@guoshengCS guoshengCS merged commit 0c181c6 into PaddlePaddle:develop Aug 3, 2017
heavengate pushed a commit to heavengate/Paddle that referenced this pull request Aug 16, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants