c++ softmax sigmoid

 

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.csdn.net/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;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI算法网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值