激活函数之logistic sigmoid函数介绍及C++实现
logistic sigmoid函數:
logistic sigmoid函數通常用來產生Bernoulli分布中的參數?,因為它的范圍是(0,1),處在?的有效取值范圍內。logisitic sigmoid函數在變量取絕對值非常大的正值或負值時會出現飽和(saturate)現象,意味著函數會變得很平,并且對輸入的微小改變會變得不敏感。
logistic sigmoid函數在深度學習中經常用作激活函數。有時為了加快計算速度,也使用稱為fast sigmoid 函數,即:σ(x)=x/(1+|x|)
Logistic function:A logistic function or logistic curve is a common “S” shape(sigmoid curve), with equation:
where, e = the natural logarithm base(also known as Euler’s number); x0 = the x-value of the sigmoid’s midpoint; L = the curve’s maximum value; k = the steepness of the curve. For values of x in the range of real numbers from -∞ to +∞.
The standard logistic function is the logistic function with parameters (k = 1, x0= 0, L = 1) which yields:
The logistic function has the symmetry(對稱) property that: 1-f(x)=f(-x).
以上內容摘自:?《深度學習中文版》和?維基百科
以下是C++測試code:
#include "funset.hpp"
#include <math.h>
#include <iostream>
#include <string>
#include <vector>
#include <opencv2/opencv.hpp>
#include "common.hpp"// =============================== 計算 sigmoid函數 ==========================
template<typename _Tp>
int sigmoid_function(const _Tp* src, _Tp* dst, int length)
{for (int i = 0; i < length; ++i) {dst[i] = (_Tp)(1. / (1. + exp(-src[i])));}return 0;
}template<typename _Tp>
int sigmoid_function_fast(const _Tp* src, _Tp* dst, int length)
{for (int i = 0; i < length; ++i) {dst[i] = (_Tp)(src[i] / (1. + fabs(src[i])));}return 0;
}int test_sigmoid_function()
{std::vector<double> src{ 1.23f, 4.14f, -3.23f, -1.23f, 5.21f, 0.234f, -0.78f, 6.23f };int length = src.size();std::vector<double> dst1(length), dst2(length);fprintf(stderr, "source vector: \n");fbc::print_matrix(src);fprintf(stderr, "calculate sigmoid function:\n");fprintf(stderr, "type: sigmoid functioin, result: \n");fbc::sigmoid_function(src.data(), dst1.data(), length);fbc::print_matrix(dst1);fprintf(stderr, "type: fast sigmoid function, result: \n");fbc::sigmoid_function_fast(src.data(), dst2.data(), length);fbc::print_matrix(dst2);return 0;
}
GitHub: https://github.com/fengbingchun/NN_Test
總結
以上是生活随笔為你收集整理的激活函数之logistic sigmoid函数介绍及C++实现的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 概率论中指数分布介绍及C++11中std
- 下一篇: 激活函数之ReLU/softplus介绍