• 手撸任意层神经网络-读从文本s.txt取网络结构初始化neuralNetwork


    现代c++读取文本文件,文本文件:"s.txt"中有字符串,如:"{2,4,3,1}",获取数字如:2,4,3,1赋值给变量 vectovLa;

    1. #include
    2. #include
    3. #include
    4. //#include
    5. #include
    6. #include //Eigen::MatrixXd forwardPropagation(const std::vector
    7. using namespace std;
    8. /*
    9. std::vector parseBracketContent(const std::string& line) {
    10. std::vector result;
    11. // 找到大括号的位置
    12. std::size_t start_pos = line.find('{');
    13. std::size_t end_pos = line.find('}');
    14. // 确保找到合法的大括号
    15. if (start_pos == std::string::npos || end_pos == std::string::npos || start_pos >= end_pos) {
    16. return result; // 返回空的vector
    17. }
    18. // 截取大括号之间的内容
    19. std::string content = line.substr(start_pos + 1, end_pos - start_pos - 1);
    20. // 使用istringstream和getline进行分割
    21. std::istringstream ss(content);
    22. std::string token;
    23. while (std::getline(ss, token, ',')) {
    24. // 使用stoi将字符串转换为整数,并添加到结果vector中
    25. result.push_back(std::stoi(token));
    26. }
    27. return result;
    28. }//std::vector parseBracketContent( */
    29. std::vector<int> readLayers(const std::string& filename) {
    30. std::ifstream file(filename);
    31. std::string line;
    32. std::vector<int> layers;
    33. std::getline(file, line);
    34. //------------------------------------------
    35. // 找到大括号的位置
    36. std::size_t start_pos = line.find('{');
    37. std::size_t end_pos = line.find('}');
    38. // 确保找到合法的大括号
    39. if (start_pos == std::string::npos || end_pos == std::string::npos || start_pos >= end_pos) {
    40. cout << "没有大括号!" << endl;
    41. // return result; // 返回空的vector
    42. }
    43. // 截取大括号之间的内容
    44. std::string content = line.substr(start_pos + 1, end_pos - start_pos - 1);
    45. cout <<"Line55:" << content << endl;
    46. // 使用istringstream和getline进行分割
    47. std::istringstream ss(content);
    48. std::string token;
    49. while (std::getline(ss, token, ',')) {
    50. // 使用stoi将字符串转换为整数,并添加到结果vector中
    51. layers.push_back(std::stoi(token));
    52. cout << std::stoi(token) << std::endl;
    53. }
    54. // std::cout << layers << std::endl;
    55. //-------------------------------------------
    56. // if (file.is_open()) {
    57. std::getline(file, line);
    58. std::istringstream iss(line);
    59. int num;
    60. while (iss >> num) {
    61. // layers.push_back(num);
    62. }
    63. // }//if (file.is_open()) {
    64. return layers;
    65. }//std::vector readLayers(
    66. Eigen::MatrixXd forwardPropagation(const std::vector& weights, const Eigen::VectorXd& input) {
    67. Eigen::MatrixXd output = input;
    68. for (const auto& w : weights) {
    69. output.conservativeResize(output.rows(), 1); // Make sure it's a column vector
    70. output = w * output;
    71. output = output.unaryExpr([](double x) { return 1.0 / (1.0 + std::exp(-x)); }); // Activation function (sigmoid)
    72. }
    73. return output;
    74. }
    75. int main() {
    76. // Read network architecture from file
    77. std::vector<int> layers = readLayers("\\s.txt");
    78. // Initialize weights randomly
    79. std::default_random_engine generator;
    80. std::normal_distribution<double> distribution(0.0, 1.0);
    81. std::vector weights;
    82. for (size_t i = 0; i < layers.size() - 1; ++i) {
    83. Eigen::MatrixXd w(layers[i + 1], layers[i]);
    84. w = w.unaryExpr([&](double x) { return distribution(generator); });
    85. weights.push_back(w);
    86. }
    87. // Initialize input (example)
    88. Eigen::VectorXd input(layers[0]);
    89. input << 0.5, 0.6;
    90. // Perform forward propagation
    91. Eigen::MatrixXd output = forwardPropagation(weights, input);
    92. std::cout << "Output: \n" << output << std::endl;
    93. return 0;
    94. }//main

  • 相关阅读:
    Linux内核调试工具——devmem
    基于springboot实现的极验校验
    Softmax 回归 + 损失函数 + 图片分类数据集
    如何监控公司电脑上网记录(员工上网行为监控软件有哪些?)
    pycharm插件translation 更新TTK失败,请检查网络连接的解决办法
    PostgreSQL中实现数学中的组合问题
    1.1.1 算法的概念(第 1 课时)
    2021 中国系统java面试笔试题(含面试题解析)
    【Linux】基本指令(一)
    使用cpolar发布群晖NAS博客网站 1(7.X版)
  • 原文地址:https://blog.csdn.net/aw344/article/details/132661918