在 Caffe 框架中修改 ReLU 中的阈值
Modify threshold in ReLU in Caffe framework
我是Caffe的新手,现在我需要修改卷积神经网络中ReLU层的阈值。我现在使用的修改阈值的方法是编辑caffe/src/caffe/layers/relu_layer.cpp
中的C++源代码,然后重新编译。但是,这会在每次调用 ReLU 时将阈值更改为指定值。有没有办法在网络中的每个 ReLU 层中使用不同的值作为阈值?顺便说一句,我使用的是pycaffe界面,我找不到这样的方法。
最后,抱歉我的英语不好,如果有什么不清楚的地方,请告诉我,我会尽量详细描述。
是的,你可以。在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。
并且在编译 caffe
和 pycaffe
之后,在 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"
}
如果我理解正确的话你的"ReLU with threshold"基本上是
f(x) = x-threshold if x>threshold, 0 otherwise
您可以通过添加一个 "Bias"
层来轻松实现它,该层在常规 "ReLU"
层
之前从输入中减去 threshold
我是Caffe的新手,现在我需要修改卷积神经网络中ReLU层的阈值。我现在使用的修改阈值的方法是编辑caffe/src/caffe/layers/relu_layer.cpp
中的C++源代码,然后重新编译。但是,这会在每次调用 ReLU 时将阈值更改为指定值。有没有办法在网络中的每个 ReLU 层中使用不同的值作为阈值?顺便说一句,我使用的是pycaffe界面,我找不到这样的方法。
最后,抱歉我的英语不好,如果有什么不清楚的地方,请告诉我,我会尽量详细描述。
是的,你可以。在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。
并且在编译 caffe
和 pycaffe
之后,在 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"
}
如果我理解正确的话你的"ReLU with threshold"基本上是
f(x) = x-threshold if x>threshold, 0 otherwise
您可以通过添加一个 "Bias"
层来轻松实现它,该层在常规 "ReLU"
层
threshold