标题
增加 1 行删除 1 行
1神经网络1【工程】神经网络
正文
增加 389 行删除 1 行
481 行未改动全部展开482483484485486487488489490297 行未改动全部展开788789790791481 行未改动全部展开int OUTSIZE = 2; //输出层宽度db Learn = 0.1; //学习率static constexpr int MAXINSIZE = 32; //最大输入层宽度static constexpr int MAXWEBSIZE = 32; //最大神经层宽度static constexpr int MAXWEBDEPTH = 3; //最大神经层深度static constexpr int MAXOUTSIZE = 32; //最大输出层宽度static constexpr int MAXDATASIZE = 1e5; //最多待使用训练数据缓存组数struct DAT {db I[MAXINSIZE + 2], O[MAXOUTSIZE + 2];297 行未改动全部展开return 0;}```:::481 行未改动全部展开482483484485486487488489490297 行未改动全部展开788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179481 行未改动全部展开int OUTSIZE = 2; //输出层宽度db Learn = 0.1; //学习率static constexpr int MAXINSIZE = 32; //最大输入层宽度static constexpr int MAXWEBSIZE = 32; //最大神经层宽度static constexpr int MAXWEBDEPTH = 32; //最大神经层深度static constexpr int MAXOUTSIZE = 32; //最大输出层宽度static constexpr int MAXDATASIZE = 1e5; //最多待使用训练数据缓存组数struct DAT {db I[MAXINSIZE + 2], O[MAXOUTSIZE + 2];297 行未改动全部展开return 0;}```:::调试中...代码如下:::info[Code]```cpp#include<bits/stdc++.h>using namespace std;using db = long double;struct NET { //神经网络封装/*内置超参数*/static constexpr int MAXINSIZE = 32; //最大输入层宽度static constexpr int MAXWEBSIZE = 32; //最大神经层宽度static constexpr int MAXWEBDEPTH = 8; //最大神经层深度static constexpr int MAXOUTSIZE = 64; //最大输出层宽度static constexpr int MAXDATASIZE = 1e6; //最多待使用训练数据缓存组数static constexpr int SAVEDATALIM = 30000; //保存数据时间间隔(毫秒)static constexpr int MAXTEXTSIZE = 1e7 + 5; //最大文本集大小struct DAT { //现有训练数据db I[MAXINSIZE + 2], O[MAXOUTSIZE + 2];} stk[MAXDATASIZE + 2];int top = 0; //训练数据栈顶位置int INSIZE = 32; //输入层宽度int WEBSIZE = 32; //神经层宽度int WEBDEPTH = 4; //神经层深度int OUTSIZE = 60; //输出层宽度db Learn = 0.1; //学习率int LstT = 0; //上次保存数据(毫秒)int TU1[205], TU2[205]; //TU1 <-> TU2: int <-> charshort text[MAXTEXTSIZE + 2]; //文本集int TLEN; //文本总量/*参数变量*///前向传播db x[MAXINSIZE + 2]; //传入参数db wx[MAXINSIZE + 2][MAXWEBSIZE + 2]; //传入层 -> 神经层 向后权重参数db w[MAXWEBDEPTH + 2][MAXWEBSIZE + 2][MAXWEBSIZE + 2]; //神经层内部向后权重参数db wo[MAXWEBSIZE + 2][MAXOUTSIZE + 2]; //神经层 -> 输出层 向后权重参数db bh[MAXWEBDEPTH + 2][MAXWEBSIZE + 2]; //神经层偏置db bo[MAXOUTSIZE + 2]; //输出层偏置db zh[MAXWEBDEPTH + 2][MAXWEBSIZE + 2]; //神经层节点受传入权值db zo[MAXOUTSIZE + 2]; //输出层节点受传入权值db h[MAXWEBDEPTH + 2][MAXWEBSIZE + 2]; //神经层节点传出权值db ho[MAXOUTSIZE + 2]; //输出层节点传出权值//反向传播db dwx[MAXINSIZE + 2][MAXWEBSIZE + 2]; //传入层 -> 神经层 向后权重参数梯度db dw[MAXWEBDEPTH + 2][MAXWEBSIZE + 2][MAXWEBSIZE + 2]; //神经层内部向后权重参数梯度db dwo[MAXWEBSIZE + 2][MAXOUTSIZE + 2]; //神经层 -> 输出层 向后权重参数梯度db dbh[MAXWEBDEPTH + 2][MAXWEBSIZE + 2]; //神经层偏置梯度db dbo[MAXOUTSIZE + 2]; //输出层偏置梯度db dzh[MAXWEBDEPTH + 2][MAXWEBSIZE + 2]; //神经层节点受传入权值梯度db dzo[MAXOUTSIZE + 2]; //输出层节点受传入权值梯度db dh[MAXWEBDEPTH + 2][MAXWEBSIZE + 2]; //神经层节点传出权值梯度db dho[MAXOUTSIZE + 2]; //输出层节点传出权值梯度//数据db st[MAXOUTSIZE + 2]; //标准输出db L; //每个输出位的均方误差之和的累加/*内置函数*/void PRE() { //模型预处理for (int i = 0; i <= 200; ++i) TU2[i] = 60;for (int i = 'a'; i <= 'z'; ++i) TU2[i] = i - 'a' + 1, TU1[i - 'a' + 1] = i;for (int i = 'A'; i <= 'Z'; ++i) TU2[i] = i - 'A' + 27, TU1[i - 'A' + 27] = i;TU2[','] = 53, TU2['.'] = 54, TU2['!'] = 55, TU2['?'] = 56, TU2[':'] = 57, TU2['\''] = 58, TU2['\"'] = 59, TU2[' '] = 60;TU1[53] = ',', TU1[54] = '.', TU1[55] = '!', TU1[56] = '?', TU1[57] = ':', TU1[58] = '\'', TU1[59] = '\"', TU1[60] = ' ';}void JC(char c) { //在文本集中加入字符 Join Charint x = TU2[c];if (TLEN and x == text[TLEN] and x > 52 and x != 54) return; //不接受连续非字母字符text[++TLEN] = x; //在文本集中加入字符}void READ(string path = "data.txt") {cout << "文本读入开始\n";ifstream fin(path);string tmp;while (getline(fin, tmp)) {for (char c : tmp) JC(c);JC(' ');}fin.close();cout << "文本读入结束,共接受 " << TLEN << " 个字符\n";}void LO(string path) { //向文件导出参数 load outofstream fout(path);fout << INSIZE << ' ' << WEBSIZE << ' ' << WEBDEPTH << ' ' << OUTSIZE << '\n' << '\n';//wxfor (int i = 1; i <= INSIZE; ++i)for (int j = 1; j <= WEBSIZE; ++j)fout << wx[i][j] << ' ';fout << '\n' << '\n';//wfor (int i = 1; i < WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)for (int k = 1; k <= WEBSIZE; ++k)fout << w[i][j][k] << ' ';fout << '\n' << '\n';//wofor (int i = 1; i <= WEBSIZE; ++i)for (int j = 1; j <= OUTSIZE; ++j)fout << wo[i][j] << ' ';fout << '\n' << '\n';//bhfor (int i = 1; i <= WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)fout << bh[i][j] << ' ';fout << '\n' << '\n';//bofor (int i = 1; i <= OUTSIZE; ++i)fout << bo[i] << ' ';//closefout.close();}void LI(string path) { //从文件导入参数 load inifstream fin(path);fin >> INSIZE >> WEBSIZE >> WEBDEPTH >> OUTSIZE;//wxfor (int i = 1; i <= INSIZE; ++i)for (int j = 1; j <= WEBSIZE; ++j)fin >> wx[i][j];//wfor (int i = 1; i < WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)for (int k = 1; k <= WEBSIZE; ++k)fin >> w[i][j][k];//wofor (int i = 1; i <= WEBSIZE; ++i)for (int j = 1; j <= OUTSIZE; ++j)fin >> wo[i][j];//bhfor (int i = 1; i <= WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)fin >> bh[i][j];//bofor (int i = 1; i <= OUTSIZE; ++i)fin >> bo[i];//closefin.close();}void SAVE(string path = "Model.txt") {if (clock() - LstT > SAVEDATALIM) { //满足保存时间间隔限制LstT = clock(); //设置最后保存时间LO(path); //导出数据cout << "已保存模型到 " << path << '\n';}}db sig(db x) { //sigmoid 映射函数return 1.0 / (1.0 + exp(-x));}db relu(db x) { //ReLU 映射函数return (x > 0) ? x : 0;}db drelu(db x) { //relu 函数的导数return (x > 0) ? 1 : 0;}random_device RAND;mt19937 rng{RAND()};void INIT() { //随机初始化参数//随机赋值权重db rg1 = sqrt(2.0 / INSIZE), rg2 = sqrt(2.0 / WEBSIZE), rg3 = sqrt(2.0 / WEBSIZE);uniform_real_distribution<db> dist1{-rg1, rg1}, dist2{-rg2, rg2}, dist3{-rg3, rg3};for (int i = 1; i <= INSIZE; ++i)for (int j = 1; j <= WEBSIZE; ++j)wx[i][j] = dist1(rng);for (int i = 1; i < WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)for (int k = 1; k <= WEBSIZE; ++k)w[i][j][k] = dist2(rng);for (int i = 1; i <= WEBSIZE; ++i)for (int j = 1; j <= OUTSIZE; ++j)wo[i][j] = dist3(rng);//清空偏置for (int i = 1; i <= WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)bh[i][j] = 0.01;for (int i = 1; i <= OUTSIZE; ++i)bo[i] = 0.01;}void FP() { //前向传播 Forward Pass//注:此写法无需初始化//in -> zh[1]for (int i = 1; i <= WEBSIZE; ++i) {zh[1][i] = bh[1][i];for (int j = 1; j <= INSIZE; ++j)zh[1][i] += x[j] * wx[j][i];}//zh[i] ->...-> zh[WEBDEPTH]for (int i = 1; i < WEBDEPTH; ++i) {for (int j = 1; j <= WEBSIZE; ++j)h[i][j] = relu(zh[i][j]);for (int j = 1; j <= WEBSIZE; ++j) {zh[i + 1][j] = bh[i + 1][j];for (int k = 1; k <= WEBSIZE; ++k)zh[i + 1][j] += h[i][k] * w[i][k][j];}}//zh[WEBDEPTH] -> h[WEBDEPTH]for (int i = 1; i <= WEBSIZE; ++i)h[WEBDEPTH][i] = relu(zh[WEBDEPTH][i]);//h[WEBDEPTH] -> zofor (int i = 1; i <= OUTSIZE; ++i) {zo[i] = bo[i];for (int j = 1; j <= WEBSIZE; ++j)zo[i] += h[WEBDEPTH][j] * wo[j][i];}//zo -> ho (softmax)db mxo = zo[1];for (int i = 2; i <= OUTSIZE; ++i)if (zo[i] > mxo) mxo = zo[i];db sxp = 0.0;for (int i = 1; i <= OUTSIZE; ++i) {ho[i] = exp(zo[i] - mxo);sxp += ho[i];}for (int i = 1; i <= OUTSIZE; ++i)ho[i] /= sxp;}void BP() { //反向传播//dzo = ho - stint tg = -1;for (int i = 1; i <= OUTSIZE; ++i)if (fabs(st[i] - 1.0) < 1e-9) {tg = i;break;}if (tg != -1)L += -log( max(ho[tg], (db)1e-15) );for (int i = 1; i <= OUTSIZE; ++i)dzo[i] = ho[i] - st[i];//dbofor (int i = 1; i <= OUTSIZE; ++i)dbo[i] = dzo[i];//dwo, dh[WEBDEPTH]for (int i = 1; i <= WEBSIZE; ++i)dh[WEBDEPTH][i] = 0.0;for (int i = 1; i <= WEBSIZE; ++i)for (int j = 1; j <= OUTSIZE; ++j)dwo[i][j] = h[WEBDEPTH][i] * dzo[j], dh[WEBDEPTH][i] += wo[i][j] * dzo[j];//神经层 WEBDEPTH -> 2for (int i = WEBDEPTH; i >= 2; --i) {//dzhfor (int j = 1; j <= WEBSIZE; ++j)dzh[i][j] = drelu(zh[i][j]) * dh[i][j];//dbhfor (int j = 1; j <= WEBSIZE; ++j)dbh[i][j] = dzh[i][j];//w,h [i-1]for (int j = 1; j <= WEBSIZE; ++j)dh[i - 1][j] = 0.0;for (int j = 1; j <= WEBSIZE; ++j)for (int k = 1; k <= WEBSIZE; ++k)dw[i - 1][j][k] = h[i - 1][j] * dzh[i][k], dh[i - 1][j] += w[i - 1][j][k] * dzh[i][k];}//dzh[1]for (int j = 1; j <= WEBSIZE; ++j)dzh[1][j] = drelu(zh[1][j]) * dh[1][j];//dbh[1]for (int j = 1; j <= WEBSIZE; ++j)dbh[1][j] = dzh[1][j];//dwxfor (int i = 1; i <= INSIZE; ++i)for (int j = 1; j <= WEBSIZE; ++j)dwx[i][j] = x[i] * dzh[1][j];}void OP() { //强化参数//wxfor (int i = 1; i <= INSIZE; ++i)for (int j = 1; j <= WEBSIZE; ++j)wx[i][j] -= dwx[i][j] * Learn;//wfor (int i = 1; i < WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)for (int k = 1; k <= WEBSIZE; ++k)w[i][j][k] -= dw[i][j][k] * Learn;//wofor (int i = 1; i <= WEBSIZE; ++i)for (int j = 1; j <= OUTSIZE; ++j)wo[i][j] -= dwo[i][j] * Learn;//bhfor (int i = 1; i <= WEBDEPTH; ++i)for (int j = 1; j <= WEBSIZE; ++j)bh[i][j] -= dbh[i][j] * Learn;//bofor (int i = 1; i <= OUTSIZE; ++i)bo[i] -= dbo[i] * Learn;}bool PD() { //准备数据if (!top) return 0; //数据消耗殆尽for (int i = 1; i <= INSIZE; ++i) //配置输入x[i] = stk[top].I[i];for (int i = 1; i <= OUTSIZE; ++i) //配置输出st[i] = stk[top].O[i];--top;return 1;}void AD(vector<db> v1, vector<db> v2) { //添加训练数据 Add Data++top;for (int i = 0; i < v1.size(); ++i) //设置输入stk[top].I[i + 1] = v1[i];for (int i = 0; i < v2.size(); ++i) //设置标准输出stk[top].O[i + 1] = v2[i];}void GD(int len = 0) { //随机从文本集中获取数据 Get Dataif (!len) len = INSIZE;uniform_int_distribution<int> dist{len + 1, TLEN};vector<db> v1, v2;int pos = dist(rng), tmp = text[pos];for (int i = pos - len; i < pos; ++i)v1.push_back(text[i] / 60.0);for (int i = 1; i <= OUTSIZE; ++i)if (i == tmp) v2.push_back(1);else v2.push_back(0);AD(v1, v2);}char PRINT(int ty = 0, int plen = 3) { //打印模型预测结果//传入 type 表示输出是否具体,默认为不是;pointlen 表示输出保留小数点位数,默认为 3if (ty == 1) cout << "预测结果:\n";int id = 1;double mx = 0.0;for (int i = 1; i <= OUTSIZE; ++i) {if (ho[i] > mx) mx = ho[i], id = i;if (ty == 1) cout << i << ": " << fixed << setprecision(plen) << ho[i] << '\n';}if (ty < 2) cout << "最终预测: ";cout << char(TU1[id]);if (ty < 2) cout << '\n';return TU1[id];}void TRAIN(db Lrate = 0.1, int ALL = -1) { //学习//Lrate 传参表示学习率,默认 0.1;ALL 传参表示学习数据组数,默认为现有全部数据Learn = Lrate; //设置学习率if (ALL == -1) ALL = top; //设置学习数据组数for (int i = 1; i <= ALL; ++i) { //学习if (!PD()) break; //寻找训练数据并配置FP(); //网络前向传播BP(); //网络反向传播OP(); //优化参数}}void TEST() {cout << "测试开始,请输入\n";string str;getline(cin, str);for (int i = 1; i <= INSIZE; ++i) //设置输入x[i] = TU2[str[i - 1]] / 60.0;FP(); //前向传播PRINT(); //打印模型预测}void AG() { //自回归生成 Autoregressive Generationcout << "自回归开始,请输入提示词\n";string str;getline(cin, str);for (int i = 1; i <= INSIZE; ++i) //设置输入x[i] = TU2[str[i - 1]] / 60.0;while (1) {FP(); //前向传播char c = PRINT(2, 3); //打印模型预测for (int i = 2; i <= INSIZE; ++i)x[i - 1] = x[i];x[INSIZE] = TU2[c] / 60.0;}}} M;signed main() {M.PRE();// M.INIT();M.LI("Model.txt");M.AG();// while (1) M.TEST();M.READ("data.txt");for (db rate = 0.01; rate > 0.0001; rate *= 0.99) {for (int go = 1; go <= 1e4; ++go) M.GD();M.TRAIN(rate), M.SAVE("Model.txt");cout << "学习率为 " << rate << " 的训练已完成\n";cout << "损失 = " << M.L << '\n', M.L = 0;}return 0;}```:::