softmax function in c++
2024-08-30 20:55:34
#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=; 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[]), 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 ;
}
最新文章
- HTML5在canvas中绘制复杂形状附效果截图
- 使用WebKit.net加载HTML编辑器
- Codeforces 682C Alyona and the Tree(树形DP)
- ViewHolder简洁写法
- Vue 模板
- Unity3D的杂记
- WCF z
- 开发板上修改时间方法date命令【转】
- w5cValidator【AngularJS】 2.0 版本发布
- Unity3D中使用MiniJson解析json的例子
- Linux使用ssh公钥实现免批量分发管理服务器
- xshell连接centos与ubuntu
- Unity进阶----Lua语言知识点(2018/11/08)
- &#127827; vue循环渲染本地图片不显示? &#127827;
- [亲测哪步都不能省可用]联想ThinkPad E450装系统后开机一直停留在BootMenu上,无法选择硬盘进入
- spring boot 的maven设置阿里云仓库
- Java Web之HTML5
- 【C++】 网络编程 01
- keras 设置GPU使用率
- 台式电脑、笔记本快捷选择启动项Boot 快捷键大全