【问题标题】:Modify threshold in ReLU in Caffe framework在 Caffe 框架中修改 ReLU 中的阈值
【发布时间】:2017-03-26 13:04:14
【问题描述】:

我是 Caffe 的新手,现在我需要修改卷积神经网络中 ReLU 层的阈值。我现在用来修改阈值的方法是编辑caffe/src/caffe/layers/relu_layer.cpp中的C++源代码,然后重新编译。但是,这会在每次调用 ReLU 时将阈值更改为指定值。有没有办法在网络中的每个 ReLU 层中使用不同的值作为阈值?顺便说一句,我正在使用pycaffe接口,我找不到这样的方法。

最后,对不起我的英语不好,如果有不清楚的地方,请告诉我,我会尽量详细描述。

【问题讨论】:

  • Dale 的答案很好,但应该选择 Shai 的答案作为正确的答案。你应该避免在不需要时修改 Caffe。

标签: neural-network deep-learning caffe pycaffe


【解决方案1】:

如果我理解正确,您的“ReLU with threshold”基本上是

f(x) = x-threshold if x>threshold, 0 otherwise

您可以通过添加一个"Bias" 层来轻松实现它,该层在常规"ReLU" 层之前从输入中减去threshold

【讨论】:

  • 这应该是首选的解决方案。除非需要,否则应避免修改caffe
  • 谢谢,这个解决方案更容易实现。
【解决方案2】:

是的,你可以。在src/caffe/proto,添加一行:

message ReLUParameter {
  ...
  optional float threshold = 3 [default = 0]; #add this line
  ... 
}

src/caffe/layers/relu_layer.cpp 中,进行一些小的修改:

template <typename Dtype>
void ReLULayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
    const vector<Blob<Dtype>*>& top) {
  ...
  Dtype threshold = this->layer_param_.relu_param().threshold(); //add this line
  for (int i = 0; i < count; ++i) {
    top_data[i] = (bottom_data[i] > threshold) ? (bottom_data[i] - threshold) : 
                  (negative_slope * (bottom_data[i] - threshold));
  }
}

template <typename Dtype>
void ReLULayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
    const vector<bool>& propagate_down,
    const vector<Blob<Dtype>*>& bottom) {
  if (propagate_down[0]) {
    ...
    Dtype threshold = this->layer_param_.relu_param().threshold(); //this line
    for (int i = 0; i < count; ++i) {
      bottom_diff[i] = top_diff[i] * ((bottom_data[i] > threshold)
          + negative_slope * (bottom_data[i] <= threshold));
    }
  }
}

同样在src/caffe/layers/relu_layer.cu 中,代码应该类似于this

编译你的caffepycaffe之后,在你的net.prototxt中,你可以写一个relu层像:

layer {
  name: "threshold_relu"
  type: "ReLU"
  relu_param: {threshold: 1 #e.g. you want this relu layer to have a threshold 1}
  bottom: "input"
  top: "output"
}

【讨论】:

  • threshold = 3 是什么意思?为什么是3
  • 我明白threshold = 3 的意思。当我添加relu_param { threshold: 1 } 而不是threshold: 1 时,我的net.prototxt 有效。如果我使用threshold: 1,我会收到类似Message type "caffe.LayerParameter" has no field named "threshold".的错误
  • @zbqv 对不起,我的粗心。我已经更正了我的答案。
  • 在你的前向传递第二个参数为最小值。应该是阈值而不是零。这里的阈值是什么意思?我认为您需要转移功能...
  • @Shai 我的错。正如我从问题中读到的,它应该是你提到的第一个案例。我已经更正了,再次感谢!
猜你喜欢
  • 1970-01-01
  • 2021-07-30
  • 1970-01-01
  • 2015-04-19
  • 2019-07-27
  • 2018-09-22
  • 2015-03-19
  • 2018-02-05
  • 1970-01-01
相关资源
最近更新 更多