酷代码 AI
菜单
全部AI  /  热门  /  AI写代码神器
服务商

#define _crt_secure_no_warnings #include <iostream> #include <cstdlib> #include <cmath> #include <vector> #include <cstdio> using namespace std; // 你的代码... const int nx = 784, nb = 500, ny = 10;//输入层x,隐藏层b,输出层y节点数 const double eta = 0.1;//学习率 struct node { double val{}; double bias{}; vector<double> weight; } x[nx], b[nb], y[ny];//输入层x,隐藏层b,输出层y double g[ny], e[nb];//用于反向传播 double trainx[nx], trainy[ny];//训练数据 double sigmoid(double x) { return 1.0 / (1.0 + exp(-x)); } double

dsigmoid(double x) { return x * (1 - x); } void init() {//参数初始化 for (int i = 0; i < nx; i++) { x[i].val = 0; x[i].bias = ((rand() % 1000) - 500) / 10000.0; for (auto &w : x[i].weight) { w = ((rand() % 1000) - 500) / 10000.0; } } for (int i = 0; i < nb; i++) { b[i].val = 0; b[i].bias = ((rand() % 1000) - 500) / 10000.0; for (auto &w : b[i].weight) { w = ((rand() % 1000) - 500) / 10000.0; } } for (int i = 0; i < ny; i++) { y[i].val = 0; y[i].bias = ((rand() % 1000) - 500) / 10000.0; for (auto &w : y[i].weight) { w = ((rand() % 1000) - 500) / 10000.0; } } } void forward_propagation() {//前向传播 for (int i = 0; i < nb; i++) { double sum = 0; for (int j = 0; j < nx; j++) { sum += x[j].val * x[j].weight[i]; } b[i].val = sigmoid(sum + b[i].bias); } for (int i = 0; i < ny; i++) { double sum = 0; for (int j = 0; j < nb; j++) { sum += b[j].val * b[j].weight[i]; } y[i].val = sigmoid(sum + y[i].bias); } } void back_propagation() {//反向传播 for (int i = 0; i < ny; i++) { g[i] *= dsigmoid(y[i].val); } for (int i = 0; i < nb; i++) { e[i] = 0; for (int j = 0; j < ny; j++) { e[i] += g[j] * y[j].weight[i]; } e[i] *= dsigmoid(b[i].val); } for (int i = 0; i < ny; i++) { for (int j = 0; j < nb; j++) { y[i].weight[j] += eta * g[i] * b[j].val; } y[i].bias += eta * g[i]; } for (int i = 0; i < nb; i++) { for (int j = 0; j < nx; j++) { b[i].weight[j] += eta * e[i] * x[j].val; } b[i].bias += eta * e[i]; } } void train() { // 训练代码,根据需要进行实现 } int main() { init(); train(); return 0; } [2024-09-06 08:46:09 | AI写代码神器 | 1309点数解答]

相关提问