目錄
- 目錄
- 簡單介紹
-
- ReLU 激活函數
- Sigmoid 激活函數
- Tanh 激活函數
-
- ReLU 主要函數
-
- Forward_cpu 函數
- Backward_cpu 函數
-
- Sigmoid主要函數
-
- Forward_cpu 函數
- Backward_cpu 函數
-
- Tanh主要函數
-
- Forward_cpu 函數
- Backward_cpu 函數
-
簡單介紹
ReLU 激活函數:
ReLu使得網絡可以自行引入稀疏性,在沒做預訓練情況下,以ReLu為激活的網絡性能優于其它激活函數。
數學表達式: y=max(0,x)
Sigmoid 激活函數:
sigmoid 激活函數在神經網絡學習方面,可以将重點特征推向中央區,将非重點特征推向兩側區。
數學表達式: y=(1+exp(−x))−1
Tanh 激活函數:
Tanh 激活函數使得輸出與輸入的關系能保持非線性單調上升和下降關系,比sigmoid 函數延遲了飽和期,對神經網路的容錯性好。
數學表達式: y=exp(x)−exp(−x)exp(x)+exp(−x)
ReLU 主要函數
Forward_cpu 函數:
template <typename Dtype>
void ReLULayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[]->cpu_data();
Dtype* top_data = top[]->mutable_cpu_data();
const int count = bottom[]->count();
Dtype negative_slope = this->layer_param_.relu_param().negative_slope(); //輸入小于0時的斜率,預設為0;
for (int i = ; i < count; ++i) {
top_data[i] = std::max(bottom_data[i], Dtype())
+ negative_slope * std::min(bottom_data[i], Dtype());
}//輸入大于零斜率為1,小于0斜率為negative_slope。
}
Backward_cpu 函數:
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[]) {
const Dtype* bottom_data = bottom[]->cpu_data();
const Dtype* top_diff = top[]->cpu_diff();
Dtype* bottom_diff = bottom[]->mutable_cpu_diff();
const int count = bottom[]->count();
Dtype negative_slope = this->layer_param_.relu_param().negative_slope();
for (int i = ; i < count; ++i) {
bottom_diff[i] = top_diff[i] * ((bottom_data[i] > )
+ negative_slope * (bottom_data[i] <= ));
}
}
}
Sigmoid主要函數
Forward_cpu 函數:
template <typename Dtype>
void SigmoidLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[]->cpu_data();
Dtype* top_data = top[]->mutable_cpu_data();
const int count = bottom[]->count();
for (int i = ; i < count; ++i) {
top_data[i] = sigmoid(bottom_data[i]);
}
}
sigmoid 函數定義如下:
template <typename Dtype>
inline Dtype sigmoid(Dtype x) {
return / ( + exp(-x));
}
Backward_cpu 函數:
求導:
dydx=−1(1+exp(−x))2×(−exp(−x))=11+exp(−x)×(1−11+exp(−x))
template <typename Dtype>
void SigmoidLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[]) {
const Dtype* top_data = top[]->cpu_data();
const Dtype* top_diff = top[]->cpu_diff();
Dtype* bottom_diff = bottom[]->mutable_cpu_diff();
const int count = bottom[]->count();
for (int i = ; i < count; ++i) {
const Dtype sigmoid_x = top_data[i];
bottom_diff[i] = top_diff[i] * sigmoid_x * ( - sigmoid_x);
}
}
}
Tanh主要函數
Forward_cpu 函數:
template <typename Dtype>
void TanHLayer<Dtype>::Forward_cpu(const vector<Blob<Dtype>*>& bottom,
const vector<Blob<Dtype>*>& top) {
const Dtype* bottom_data = bottom[]->cpu_data();
Dtype* top_data = top[]->mutable_cpu_data();
const int count = bottom[]->count();
for (int i = ; i < count; ++i) {
top_data[i] = tanh(bottom_data[i]);
}
}
Backward_cpu 函數:
求導:
dydx=(exp(x)+exp(−x))2−(exp(x)−exp(−x))2(exp(x)+exp(−x))2=1−(exp(x)−exp(−x)exp(x)+exp(−x))2
template <typename Dtype>
void TanHLayer<Dtype>::Backward_cpu(const vector<Blob<Dtype>*>& top,
const vector<bool>& propagate_down,
const vector<Blob<Dtype>*>& bottom) {
if (propagate_down[]) {
const Dtype* top_data = top[]->cpu_data();
const Dtype* top_diff = top[]->cpu_diff();
Dtype* bottom_diff = bottom[]->mutable_cpu_diff();
const int count = bottom[]->count();
Dtype tanhx;
for (int i = ; i < count; ++i) {
tanhx = top_data[i];
bottom_diff[i] = top_diff[i] * ( - tanhx * tanhx);
}
}
}