2016-11-12 119 views
4

我是Caffe的新手,现在我需要修改卷积神经网络中ReLU图层的阈值。我现在用来修改阈值的方式是编辑caffe/src/caffe/layers/relu_layer.cpp中的C++源代码,并重新编译它。但是,每次调用ReLU时,都会将阈值更改为指定值。在网络中的每个ReLU图层中是否有任何方法使用不同的值作为阈值?顺便说一句,我正在使用pycaffe界面,我找不到这样做的方法。在Caffe框架的ReLU中修改阈值

最后,对不起我的英文不好,如果有什么不清楚的地方,请告诉我,我会尽力详细描述它。

+0

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

回答

3

基本上

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

和编译你的caffepycaffe,在你net.prototxt后,你可以写一个relu层像您可以通过添加一个"Bias"图层来轻松实现该图层,该图层从输入中减去threshold,然后在常规图层"ReLU"

+1

一个很好的解决方案。 – Dale

+1

这应该是首选解决方案。除非需要,否则应该避免修改'caffe'。 – Jonathan

+1

谢谢,这个解决方案更容易实现。 – zbqv

3

是的,你可以。在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。如果我正确理解你的 “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" 
} 
+0

什么是'threshold = 3'是什么意思?为什么是__3__? – zbqv

+0

我有什么'threshold = 3'的意思。当我添加'relu_param {threshold:1}' 而不是'threshold:1'时,我的'net.prototxt'工作。如果我使用'threshold:1',我得到了一个错误信息,例如“消息类型”caffe.LayerParameter“没有名为”threshold“的字段。' – zbqv

+0

@zbqv对不起,我的疏忽。我已经纠正了我的答案。 – Dale