softmax function in 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;
}

 

原文链接: https://www.cnblogs.com/donggongdechen/p/11049648.html

欢迎关注

微信关注下方公众号,第一时间获取干货硬货;公众号内回复【pdf】免费获取数百本计算机经典书籍

    softmax function in c++

原创文章受到原创版权保护。转载请注明出处:https://www.ccppcoding.com/archives/297098

非原创文章文中已经注明原地址,如有侵权,联系删除

关注公众号【高性能架构探索】,第一时间获取最新文章

转载文章受原作者版权保护。转载请注明原作者出处!

(0)
上一篇 2023年2月15日 下午6:25
下一篇 2023年2月15日 下午6:26

相关推荐