float sigmoid(float x)
{
return (1 / (1 + exp(-x)));
}
float sigmoid_dy_dz(float x)
{
return (x * (1.0 - x));
}
float tanh_dy_dz(float x)
{
return (1.0 - x*x);
}
//对每一行进行softmax
void softmax(float *x, int row, int column)
{
for (int j = 0; j < row; ++j)
{
float max = 0.0;
float sum = 0.0;
for (int k = 0; k < column; ++k)
if (max < x[k + j*column])
max = x[k + j*column];
for (int k = 0; k < column; ++k)
{
x[k + j*column] = exp(x[k + j*column] - max); // prevent data overflow
sum += x[k + j*column];
}
for (int k = 0; k < column; ++k) x[k + j*column] /= sum;
}
} //row*column
下面是softmax:
这个可以:
https://blog.youkuaiyun.com/u013381011/article/details/82500805
//对每一行进行softmax
void softmax(float* x, int row, int column)
{
for (int j = 0; j < row; ++j)
{
float max = 0.0;
float sum = 0.0;
for (int k = 0; k < column; ++k)
if (max < x[k + j * column])
max = x[k + j * column];
for (int k = 0; k < column; ++k)
{
x[k + j * column] = exp(x[k + j * column] - max); // prevent data overflow
sum += x[k + j * column];
}
for (int k = 0; k < column; ++k) x[k + j * column] /= sum;
}
} //row*column
int main()
{
float a[] = { 0.1,0.2,0.3,0.4 ,-0.1};
softmax(a,1,5);
std::cout << "Hello World!\n";
}
softmax c++实现with opencv
cv::Mat_<double> mat(3,3);
mat(0,0)=VIRTUAL_FOCAL;
mat(0,1)=0;
mat(0,2)=roiSize_x/2;
mat(1,0)=0;
int softmax(const cv::Mat & src, cv::Mat & dst)
{
float max = 0.0;
float sum = 0.0;
max = *max_element(src.begin<float>(), src.end<float>());
cv::exp((src - max), dst);
sum = cv::sum(dst)[0];
dst /= sum;
return 0;
}
c++:
#include <iostream>
#include <vector>
#include <cmath>
#include <algorithm>
#include <numeric>
double myfunction(double num) {
return exp(num);
}
template <typename T>
void softmax(const typename::std::vector<T> &v, typename::std::vector<T> &s){
double sum=0.0;
transform(v.begin(), v.end(), s.begin(), myfunction);
sum=accumulate(s.begin(), s.end(), sum);
for(size_t i=0; i<s.size(); ++i)
s.at(i)/=sum;
}
int main() {
double a[]={1.0, 3.0, 2.0};
std::vector<double> v_a(a, a+sizeof a/sizeof a[0]), v_b(v_a);
std::vector<double>::const_iterator it=v_a.begin();
for(; it!=v_a.end(); ++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
softmax(v_a, v_b);
it=v_b.begin();
for(; it!=v_b.end(); ++it) {
std::cout<<*it<<" ";
}
std::cout<<std::endl;
return 0;
}