// c++神经网络手撸20梯度下降22_230820a.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
#include
#include
#include
#include
#include
using namespace std;
//
float Loss误差损失之和001 = 0.0;
class NN神经网络NN {
private:
const int inputNode输入层之节点数s, hidden01Node隐藏层01结点数s, hidden22Nodes, outputNode输出层结点数s;
/*
vector
vector
vector
void initLayer每一层的WeightsAndBiases(vector
{
for (size_t i = 0; i < weights权重.size(); ++i) {//for110i
for (size_t j = 0; j < weights权重[0].size(); ++j) { weights权重[i][j] = ((rand() % 2) - 1) / 1.0; }
biases偏置[i] = ((rand() % 2) - 1) / 1.0;
}//for110i
}//void initLayerWeightsAndBiases(
void initWeightsAndBiases初始化权重和偏置矩阵() {
initLayer每一层的WeightsAndBiases(输入层到第1隐藏层之权重矩阵, 隐藏层1偏置1);
initLayer每一层的WeightsAndBiases(隐藏层1到第二隐藏层2之权重矩阵1to2, 隐藏层2偏置2);
initLayer每一层的WeightsAndBiases(隐藏22到输出层de权重矩阵 , O输出层偏置);
}
//激活函数-激活的过程
vector
vector
for (size_t i = 0; i < weights.size(); i++) {
for (size_t j = 0; j < inputs.size(); j++) {
layer_output[i] += inputs[j] * weights[i][j];
}//for220j
layer_output[i] += biases[i];
layer_output[i] = sigmoid(layer_output[i]);
}//for110i
return layer_output;
}//vector
//subtract求差:两个 向量的差
vector
vector
for (size_t i = 0; i < a.size(); i++) {
result[i] = a[i] - b[i];
}
return result;
}//vector
//dotT点乘
vector
vector
for (size_t i = 0; i < b[0].size(); i++) {
for (size_t j = 0; j < a.size(); j++) {
result[i] += a[j] * b[j][i];
}
}
return result;
}
//更新权重矩阵s(们), 和偏置(向量)S们
void updateWeights(const vector
vector< vector
for (size_t i = 0; i < weights.size(); i++) {
for (size_t j = 0; j < weights[0].size(); j++) {
weights[i][j] += lr * errors[i] * sigmoid导函数prime(outputs[i]) * inputs[j];
}
biases[i] += lr * errors[i] * sigmoid导函数prime(outputs[i]);
}
}//void updateWeights(
public:
vector
vector
vector
NN神经网络NN(int inputNode输入层之节点数s, int hidden01Node隐藏层01结点数s, int hidden22Nodes, int outputNode输出层结点数s)
:inputNode输入层之节点数s(inputNode输入层之节点数s), hidden01Node隐藏层01结点数s(hidden01Node隐藏层01结点数s), hidden22Nodes(hidden22Nodes), outputNode输出层结点数s(outputNode输出层结点数s)
{
srand(time(NULL));
//初始换权重矩阵
输入层到第1隐藏层之权重矩阵.resize(hidden01Node隐藏层01结点数s, vector
隐藏层1到第二隐藏层2之权重矩阵1to2.resize(hidden22Nodes, vector
隐藏22到输出层de权重矩阵.resize(outputNode输出层结点数s, vector
隐藏层1偏置1.resize(hidden01Node隐藏层01结点数s);
隐藏层2偏置2.resize(hidden22Nodes);
O输出层偏置.resize(outputNode输出层结点数s);
initWeightsAndBiases初始化权重和偏置矩阵();
}//NN神经网络NN(i
//sigmoid激活函数及导数
float sigmoid(float x){ return 1.0 / (1.0 + exp(-x)); }
float sigmoid导函数prime(float x) { return x * (1 - x); }
//Forward前向传播
vector
//用激活函数sigmoid-激活的过程
隐藏层1数据1 = activate(input输入数据, 输入层到第1隐藏层之权重矩阵, 隐藏层1偏置1); //激活函数
// 第一隐藏层到第二隐藏层
隐藏层2数据2 = activate(隐藏层1数据1, 隐藏层1到第二隐藏层2之权重矩阵1to2, 隐藏层2偏置2);//hidden1, wh1h2, bias_h2);
// 第二隐藏层到输出层
输出数据output = activate(隐藏层2数据2, 隐藏22到输出层de权重矩阵, O输出层偏置);// , wh2o, bias_o);
return 输出数据output;
}//vector
// 反向传播//Backpropagation
void train(const vector
vector
// 输出层误差
vector
Loss误差损失之和001 = 0.0;
for (int ii = 0; ii < outputNode输出层结点数s; ++ii) { Loss误差损失之和001 += fabs(output_error输出误差s[ii]); }
//=========================================================================
// 隐藏层2误差
vector
// 隐藏层1误差
vector
// 更新权重: 隐藏层2到输出层(的权重矩阵
updateWeights(隐藏层2数据2, output_error输出误差s, output尝试的输出数据s, 隐藏22到输出层de权重矩阵, O输出层偏置, lr学习率);
// 更新权重: 隐藏层1到隐藏层2
updateWeights(隐藏层1数据1, hidden2_errors, 隐藏层2数据2, 隐藏层1到第二隐藏层2之权重矩阵1to2, 隐藏层2偏置2, lr学习率);
// 更新权重: 输入层到隐藏层1的权重矩阵)
updateWeights(inputs, hidden1_errors, 隐藏层1数据1, 输入层到第1隐藏层之权重矩阵, 隐藏层1偏置1, lr学习率);
}// void train(
// // 反向传播//Backpropagation
};//class NN神经网络NN {
//----------------------------------------------------------------------------------------
void writeVectorToFile(const std::vector
std::ofstream outFile(fileName);
if (outFile.is_open()) {
for (float value : A) {
outFile << value << std::endl;
}
outFile.close();
}
else {
std::cerr << "Unable to open file for writing: " << fileName << std::endl;
}
}//writeVectorToFile
void readVectorFromFile(std::vector
std::ifstream inFile(fileName);
float value;
if (inFile.is_open()) {
while (inFile >> value) {
B.push_back(value);
}
inFile.close();
}
else {
std::cerr << "Unable to open file for reading: " << fileName << std::endl;
}
}//readVectorFromFile
#include
#include
#include
#include
#include
void writeToFile( const std::vector
std::ofstream file(filename);
if (!file) {
std::cerr << "Error opening file for writing: " << filename << std::endl;
return;
}
for (const auto& row : A) {
for (size_t i = 0; i < row.size(); ++i) {
file << row[i];
if (i != row.size() - 1) {
file << ",";
}
}
file << "\n";
}
file.close();
}//void writeToFile
std::vector
std::vector
std::ifstream file(filename);
if (!file) {
std::cerr << "Error opening file for reading: " << filename << std::endl;
return B;
}
std::string line;
while (std::getline(file, line)) {
std::vector
std::stringstream ss(line);
std::string value;
while (std::getline(ss, value, ',')) {
row.push_back(std::stof(value));
}
B.push_back(row);
}
file.close();
return B;
}//readFromFile(
//----------------------------------------------------------------------------------------
#define Num训练数据的个数s 4
int main()
{
NN神经网络NN nn(2, 4, 3, 1);// 2, 3, 2, 1);// 11, 10, 4);
// Example
int 训练数据的个数s = Num训练数据的个数s;
vector
/* input[0] = {0,1,0, 0,1,0, 0,1,0}; //1“竖线”或 “1”字{ 1.0, 0.5, 0.25, 0.125 };
input[1] = { 0,0,0, 1,1,1,0,0,0 }; //-“横线”或 “-”减号{ 1.0, 0.5, 0.25, 0.125 };
input[2] = { 0,1,0, 1,1,1, 0,1,0 }; //+“+”加号{ 1.0, 0.5, 0.25, 0.125 };
input[3] = { 0,1,0, 0,1.2, 0, 0,1, 0 }; // '1'或 '|'字型{ 1.0, 0.5, 0.25, 0.125 };
input[4] = { 1,1,0, 1,0,1.2, 1,1,1 }; //“口”字型+{ 1.0, 0.5, 0.25, 0.125 };
vector
target[0] = { 1.0, 0,0,0 };// , 0};//1 , 0}; //0.0, 1.0, 0.5}; //{ 0.0, 1.0 };
target[1] = { 0, 1.0 ,0,0 };// , 0};//- 91.0, 0};// , 0, 0}; //
target[2] = { 0,0,1.0,0 };// , 0};//+ 1.0, 0.5};
target[3] = { 1.0 ,0,0, 0.5 };// , 0}; //1
target[4] = { 0,0,0,0 };// , 1.0}; //“口”
*/
vector
input[0] = { 0,0 }; target[0] = { 0 }; //"-"
input[1] = { 1,0 }; target[1] = { 1 };
input[2] = { 1,1}; target[2] = { 0};
input[3] = { 0,1 }; target[3] = { 1 };
string str0001;
LabeStart001:
//--------------------------------------------------------------------------------------
cout << "1_Trainning;" << endl;
cout << "2_Test;" << endl;
cout << "3_quit." << endl;
getline(cin, str0001);
stringstream s01s001(str0001);
string temp;
getline(s01s001, temp, ',');
int choice = (float)stof(temp); //
switch (choice) {
case 1:
goto LabeTraining;
case 2:
goto LabeTest;
case 3:
return 0;
}
LabeTraining:
for (int i = 0; i < 90000; ++i) {//for110i
for (int jj = 0; jj < Num训练数据的个数s ; ++jj) {
//for (auto& val: input ) {
nn.train(input[jj], target[jj], 0.001);
if (0 ==i % 10000) { std::cout << "[Lost:" << Loss误差损失之和001 << endl; }
}//for220jj
}//for110i
// writeToFile( nn.输入层到第1隐藏层之权重矩阵 , "/file输入层到第1隐藏层之权重矩阵Name220101.txt");
writeToFile(nn.输入层到第1隐藏层之权重矩阵, "/file输入层到第1隐藏层之权重矩阵Name220101.txt");
writeToFile(nn.隐藏22到输出层de权重矩阵, "/file隐藏22到输出层de权重矩阵Name220101.txt");
writeToFile(nn.隐藏层1到第二隐藏层2之权重矩阵1to2, "/file隐藏层1到第二隐藏层2之权重矩阵1to2Name220101.txt");
writeVectorToFile( nn.隐藏层1偏置1, "/file隐藏层1偏置1.txt");
writeVectorToFile(nn.隐藏层2偏置2, "/file隐藏层2偏置2.txt");
writeVectorToFile(nn.O输出层偏置, "/fileO输出层偏置.txt");
std::cout << endl;
LabeTest:
//--------------------------------------
input[1] = { 0,1 };// 0, 0, 1, 1, 0.98, 0, 0, 0}; //1/
vector
for (auto& val : outpu输出数据001t)
std::cout << fixed << setprecision(9) << val << " ";
std::cout << endl;
//-------------------------------------------------------------
// do {
std::cout << endl << "请输入一个字符串(要求字符串是包含9个由逗号分隔的数字的字符串,如 1,2,0,0,5,0,0,8,9等): " << endl;
getline( cin, str0001);
stringstream
s01s002(str0001);
for (int i = 0; i < 2;++i) {//
//9; ++i) {
string temp;
getline(s01s002, temp, ',');
input[1][i] = (float) stof(temp); // 将字符串转化为整数
}
std::cout << "数字数组为: ";
for (int i = 0; i < 2;++i) {// 9; ++i) {
std::cout << input[1][i] << " ";
}
//
readVectorFromFile(nn.隐藏层1偏置1, "/file隐藏层1偏置1.txt");
readVectorFromFile(nn.隐藏层2偏置2, "/file隐藏层2偏置2.txt");
readVectorFromFile(nn.O输出层偏置, "/fileO输出层偏置.txt");
nn.输入层到第1隐藏层之权重矩阵 = readFromFile("/file输入层到第1隐藏层之权重矩阵Name220101.txt");
nn.隐藏层1到第二隐藏层2之权重矩阵1to2 = readFromFile("/file隐藏层1到第二隐藏层2之权重矩阵1to2Name220101.txt");
nn.隐藏22到输出层de权重矩阵 = readFromFile("/file隐藏22到输出层de权重矩阵Name220101.txt");
//
outpu输出数据001t = nn.predict(input[1]);
std::cout << endl;
for (auto& val : outpu输出数据001t)
std::cout << fixed << setprecision(9) << val << " ";
cout << endl;
// } while (true);// 1 == 1);
//======================================
std::cout << "Hello World!请继续……您可以继续训练网络,或者测试网络!\n";
goto LabeStart001;
}//main