2016-11-12 7 views
4

私はCaffeを初めて使いました。畳み込みニューラルネットワークのReLUレイヤーのしきい値を変更する必要があります。私が今閾値を変更するために使用しているのは、caffe/src/caffe/layers/relu_layer.cppのC++ソースコードを編集し、それを再コンパイルすることです。ただし、これにより、ReLUが呼び出されるたびにしきい値が指定された値に変更されます。ネットワーク内の各ReLU層で異なる値をしきい値として使用する方法はありますか?ちなみに、私はpycaffeインターフェースを使用しています。そのような方法はありません。CaffeフレームワークのReLUのしきい値を変更してください

最後に、英語の貧しい人には申し訳ありませんが、不明な点がある場合はお知らせください。詳しく説明します。

+0

デールの答えは良いですが、シャイの答えは正しいものとして選ぶべきです。 Caffeが必要でないときは、Caffeを変更しないでください。 – Jonathan

答えて

3

私が正しく理解していれば、あなたの "閾値とReLUは" 基本的に

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

ですを引いた"Bias"レイヤーを通常の"ReLU"の直前の入力から追加すると簡単に実装できますer

+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ようでなければなりません。

そして、あなたの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" 
} 
+0

'threshold = 3 'とは何ですか? __3__はなぜですか? – zbqv

+0

私は 'threshold = 3 'の意味を持っています。そして私の 'net.prototxt'は' threshold:1'の代わりに 'relu_param {threshold:1}' を追加すると動作します。私が 'threshold:1'を使うと、' message type "caffe.LayerParameter"に "threshold"という名前のフィールドがありません。 " – zbqv

+0

@zbqv申し訳ありません、私の不注意です。私は答えを修正しました。あなたの順方向パスの – Dale

関連する問題