• 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;
    }
  • 相关阅读:
    systemd管理服务
    卷积神经网络
    matplotlib-3.2.1
    pandas-1.0.3
    numpy-1.18.4
    降维
    无监督学习-聚类
    集成学习
    人工神经网络
    贝叶斯分类
  • 原文地址:https://www.cnblogs.com/donggongdechen/p/11049648.html
Copyright © 2020-2023  润新知