当前位置: 首页 > news >正文

自己做免费网站的流程刷QQ砖的网站咋做

自己做免费网站的流程,刷QQ砖的网站咋做,网站建设客户怎么找,为成长持续赋能softmax 函数的多种实现方式 包括纯C语言、C版本、Eigen版本等 flyfish 先看这里Softmax函数介绍 版本1 规矩的写法 #include iostream #include vector #include algorithm #include numeric #include cmath// 计算 softmax 的函…softmax 函数的多种实现方式 包括纯C语言、C版本、Eigen版本等 flyfish 先看这里Softmax函数介绍 版本1 规矩的写法 #include iostream #include vector #include algorithm #include numeric #include cmath// 计算 softmax 的函数 std::vectordouble softmax(const std::vectordouble input) {// 找到最大元素以防止 exp 计算时溢出double maxProb *std::max_element(input.begin(), input.end());// 计算指数并求和std::vectordouble expVals;expVals.reserve(input.size());for (double val : input) {expVals.push_back(std::exp(val - maxProb)); // 计算每个元素的指数}double sumExp std::accumulate(expVals.begin(), expVals.end(), 0.0); // 求所有指数的和// 归一化指数值以得到 softmax 概率std::vectordouble softmaxProb;softmaxProb.reserve(input.size());for (double val : expVals) {softmaxProb.push_back(val / sumExp); // 每个指数值除以总和得到 softmax 概率}return softmaxProb; }// 示例用法 int main() {std::vectordouble input {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; // 示例输入std::vectordouble probabilities softmax(input);// 输出 softmax 概率std::cout Softmax 概率: std::endl;for (double prob : probabilities) {std::cout prob ;}std::cout std::endl;// 找到具有最高概率的类别auto maxElementIter std::max_element(probabilities.begin(), probabilities.end());int classId std::distance(probabilities.begin(), maxElementIter);double confidence *maxElementIter;std::cout 预测类别: classId 置信度: confidence std::endl;return 0; }版本2 合并循环只使用一个 softmaxProb 向量来存储指数值和最终的 softmax 概率 #include iostream #include vector #include algorithm #include numeric #include cmath// 计算 softmax 的函数 std::vectordouble softmax(const std::vectordouble input) {// 找到最大元素以防止 exp 计算时溢出double maxProb *std::max_element(input.begin(), input.end());// 计算指数和求和std::vectordouble softmaxProb(input.size());double sumExp 0.0;for (size_t i 0; i input.size(); i) {softmaxProb[i] std::exp(input[i] - maxProb);sumExp softmaxProb[i];}// 归一化指数值以得到 softmax 概率for (double val : softmaxProb) {val / sumExp;}return softmaxProb; }// 示例用法 int main() {std::vectordouble input {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; // 示例输入std::vectordouble probabilities softmax(input);// 输出 softmax 概率std::cout Softmax 概率: std::endl;for (double prob : probabilities) {std::cout prob ;}std::cout std::endl;// 找到具有最高概率的类别auto maxElementIter std::max_element(probabilities.begin(), probabilities.end());int classId std::distance(probabilities.begin(), maxElementIter);double confidence *maxElementIter;std::cout 预测类别: classId 置信度: confidence std::endl;return 0; }版本3 C17 使用并行执行策略 std::transform用于计算每个元素的指数值并存储在 expVals 中。使用并行执行策略可以提升计算效率。 std::reduce用于并行求和替代 std::accumulate。 #include iostream #include vector #include algorithm #include numeric #include cmath #include execution// 计算 softmax 的函数 std::vectordouble softmax(const std::vectordouble input) {// 找到最大元素以防止 exp 计算时溢出double maxProb *std::max_element(input.begin(), input.end());// 计算指数和求和同时避免重复遍历std::vectordouble expVals(input.size());std::transform(std::execution::par, input.begin(), input.end(), expVals.begin(), [maxProb](double val) {return std::exp(val - maxProb);});// 使用 std::reduce 并行求和double sumExp std::reduce(std::execution::par, expVals.begin(), expVals.end(), 0.0);// 归一化指数值以得到 softmax 概率std::vectordouble softmaxProb(input.size());std::transform(std::execution::par, expVals.begin(), expVals.end(), softmaxProb.begin(), [sumExp](double val) {return val / sumExp;});return softmaxProb; }// 示例用法 int main() {std::vectordouble input {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; // 示例输入std::vectordouble probabilities softmax(input);// 输出 softmax 概率std::cout Softmax 概率: std::endl;for (double prob : probabilities) {std::cout prob ;}std::cout std::endl;// 找到具有最高概率的类别auto maxElementIter std::max_element(probabilities.begin(), probabilities.end());int classId std::distance(probabilities.begin(), maxElementIter);double confidence *maxElementIter;std::cout 预测类别: classId 置信度: confidence std::endl;return 0; }版本4 Eigen 库实现 利用 Eigen 库可以高效地进行矩阵和向量运算。Eigen 库通过优化内存布局和利用 SIMD 指令集来提升性能。 Eigen::Map 可以将标准库中的容器如 std::vector映射为 Eigen 向量从而直接进行高效的向量运算。 配置 CMakeLists.txt # 添加 Eigen 目录 set(EIGEN3_INCLUDE_DIR path/to/eigen) # 将此路径替换为你解压缩 Eigen 的目录 include_directories(${EIGEN3_INCLUDE_DIR})#include iostream #include vector #include algorithm #include Eigen/Dense// 计算 softmax 的函数 std::vectordouble softmax(const std::vectordouble input) {// 将输入向量转换为 Eigen 向量Eigen::VectorXd vec Eigen::Mapconst Eigen::VectorXd(input.data(), input.size());// 找到最大元素以防止 exp 计算时溢出double maxProb vec.maxCoeff();// 计算指数和求和Eigen::VectorXd expVals (vec.array() - maxProb).exp();double sumExp expVals.sum();// 归一化指数值以得到 softmax 概率std::vectordouble softmaxProb(input.size());Eigen::VectorXd result expVals / sumExp;Eigen::VectorXd::Map(softmaxProb[0], result.size()) result;return softmaxProb; }// 示例用法 int main() {std::vectordouble input {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; // 示例输入std::vectordouble probabilities softmax(input);// 输出 softmax 概率std::cout Softmax 概率: std::endl;for (double prob : probabilities) {std::cout prob ;}std::cout std::endl;// 找到具有最高概率的类别auto maxElementIter std::max_element(probabilities.begin(), probabilities.end());int classId std::distance(probabilities.begin(), maxElementIter);double confidence *maxElementIter;std::cout 预测类别: classId 置信度: confidence std::endl;return 0; }版本5 纯C语言方式 #include stdio.h #include stdlib.h #include math.h// 计算 softmax 的函数 void softmax(const double* input, double* softmaxProb, int size) {// 找到最大元素以防止 exp 计算时溢出double maxProb input[0];for (int i 1; i size; i) {if (input[i] maxProb) {maxProb input[i];}}// 计算指数和求和double sumExp 0.0;for (int i 0; i size; i) {softmaxProb[i] exp(input[i] - maxProb);sumExp softmaxProb[i];}// 归一化指数值以得到 softmax 概率for (int i 0; i size; i) {softmaxProb[i] / sumExp;} }// 示例用法 int main() {double input[] {1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0}; // 示例输入int size sizeof(input) / sizeof(input[0]);double* probabilities (double*)malloc(size * sizeof(double));if (probabilities NULL) {fprintf(stderr, 内存分配失败\n);return 1;}softmax(input, probabilities, size);// 输出 softmax 概率printf(Softmax 概率:\n);for (int i 0; i size; i) {printf(%f , probabilities[i]);}printf(\n);// 找到具有最高概率的类别double maxProb probabilities[0];int classId 0;for (int i 1; i size; i) {if (probabilities[i] maxProb) {maxProb probabilities[i];classId i;}}double confidence maxProb;printf(预测类别: %d 置信度: %f\n, classId, confidence);free(probabilities);return 0; }
http://www.w-s-a.com/news/837390/

相关文章:

  • 做360网站中保存的图片存在哪里个人建立网站要多少钱
  • 网站安装部署无锡做网站的公司
  • 怎么将网站做成小程序安装wordpress到服务器
  • 企业网站建设的四大因素沈阳网站建设招标公司
  • wordpress仿站开发公司网站策划宣传
  • 金乡县网站开发网站开发三个流程
  • qq空间网站是多少纺织网站建设方案
  • 建设微网站项目报告网站优化难吗
  • 做网站需要自己上传产品吗企业网站系统设计
  • wordpress个人中心济南网站建设和优化
  • 网站pc端网址和手机端网址建设牡丹江 网站建设
  • 苏州新区城乡建设网站人才招聘网站开发
  • 一般网站是怎么做的威远移动网站建设
  • 赣州网站开发公司怎么才能设计好一个网站
  • 个人网站建设分几个步走培训网站开发哪个好
  • 智能网站价格河北城乡建设网站
  • 做动画在线观看网站网上花店 网站源代码
  • 做网站项目体会商业信息
  • 深圳的设计网站谷歌浏览器下载手机版官网
  • 苏州网站建设都找全网天下外贸响应式网站设计
  • 揭阳专业做网站网站迁移教材
  • 手机上怎么上传网站吗工程信息网站建设
  • 用手机建网站微信手机网站流程
  • 专注软件优化分享的网站梧州网页设计
  • 长春火车站和高铁站是一个站吗公司名称注册查询系统
  • 便利的集团网站建设网页的依托网站
  • 茶叶网站建设题库制作助手app
  • 网站建设栏目层级北京网站搭建公司电话
  • 网站开发运营经理打开百度竞价页面是网站是什么
  • 国内最专业的设计网站建设现在用什么语言做网站