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

这篇博客介绍了如何利用现代C++语言读取文本文件中的网络结构描述,例如'{2,4,3,1}',并将这些数字转换为向量用于初始化神经网络的层级。" 120312051,10950438,理解Redux:action、reducer与store的详解,"['react.js', 'redux', 'state管理']
摘要由CSDN通过智能技术生成

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


#include <iostream>
#include <vector>
#include <fstream>
//#include <sstream>
#include <random>
#include <Eigen/Dense>    //Eigen::MatrixXd forwardPropagation(const std::vector<Eigen::MatrixXd>
using namespace std;

/*
std::vector<int> parseBracketContent(const std::string& line) {
    std::vector<int> result;

    // 找到大括号的位置
    std::size_t start_pos = line.find('{');
    std::size_t end_pos = line.find('}');

    // 确保找到合法的大括号
    if (start_pos == std::string::npos || end_pos == std::string::npos || start_pos >= end_pos) {
        return result;  // 返回空的vector
    }

    // 截取大括号之间的内容
    std::string content = line.substr(start_pos + 1, end_pos - start_pos - 1);

    // 使用istringstream和getline进行分割
    std::istringstream ss(content);
    std::string token;
    while (std::getline(ss, token, ',')) {
        // 使用stoi将字符串转换为整数,并添加到结果vector中
        result.push_back(std::stoi(token));
    }

    return result;
}//std::vector<int> parseBracketContent( */


std::vector<int> readLayers(const std::string& filename) {
    std::ifstream file(filename);
    std::string line;
    std::vector<int> layers;


    std::getline(file, line);
    //------------------------------------------
     // 找到大括号的位置
    std::size_t start_pos = line.find('{');
    std::size_t end_pos = line.find('}');

    // 确保找到合法的大括号
    if (start_pos == std::string::npos || end_pos == std::string::npos || start_pos >= end_pos) {
        cout << "没有大括号!" << endl;
//        return result;  // 返回空的vector
    }

    // 截取大括号之间的内容
    std::string content = line.substr(start_pos + 1, end_pos - start_pos - 1);

    cout <<"Line55:" << content << endl;

    // 使用istringstream和getline进行分割
    std::istringstream ss(content);
    std::string token;
    while (std::getline(ss, token, ',')) {
        // 使用stoi将字符串转换为整数,并添加到结果vector中
        layers.push_back(std::stoi(token));
        cout << std::stoi(token) << std::endl;
    }
//    std::cout << layers << std::endl;

    //-------------------------------------------

//    if (file.is_open()) {
        std::getline(file, line);
        std::istringstream iss(line);
        int num;
        while (iss >> num) {
//            layers.push_back(num);
        }
//    }//if (file.is_open()) {


    return layers;
}//std::vector<int> readLayers(

Eigen::MatrixXd forwardPropagation(const std::vector<Eigen::MatrixXd>& weights, const Eigen::VectorXd& input) {
    Eigen::MatrixXd output = input;
    for (const auto& w : weights) {
        output.conservativeResize(output.rows(), 1);  // Make sure it's a column vector
        output = w * output;
        output = output.unaryExpr([](double x) { return 1.0 / (1.0 + std::exp(-x)); }); // Activation function (sigmoid)
    }
    return output;
}

int main() {
    // Read network architecture from file
    std::vector<int> layers = readLayers("\\s.txt");

    // Initialize weights randomly
    std::default_random_engine generator;
    std::normal_distribution<double> distribution(0.0, 1.0);
    std::vector<Eigen::MatrixXd> weights;

    for (size_t i = 0; i < layers.size() - 1; ++i) {
        Eigen::MatrixXd w(layers[i + 1], layers[i]);
        w = w.unaryExpr([&](double x) { return distribution(generator); });
        weights.push_back(w);
    }

    // Initialize input (example)
    Eigen::VectorXd input(layers[0]);
    input << 0.5, 0.6;

    // Perform forward propagation
    Eigen::MatrixXd output = forwardPropagation(weights, input);

    std::cout << "Output: \n" << output << std::endl;

    return 0;
}//main

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值