本人的人工神经网络代码什么都不干就直接RE,why?
#include<bits/stdc++.h>
using namespace std;
int cnt;
double q[200][200][200];//[第i层][第i+1的第j个神经元][第i层的第j个神经元]之间的权值
double cell_b[200][200];//[第i层][第i层的第j个神经元的偏置]
double cell_str[200][200];//[第i层][第i层的第j个神经元的倍率]
double answer[200][200];//[第i层][第i层的第j个神经元的输出]
int cell_sum[200];//第i层的神经元数量
double input[1000][200];//[第i组训练数据][第j个数据]
double output[1000][200];//[第i组训练数据][第j个数据]
double ReLu(double input){
if(input > 0) return input;
return 0;
}
double cell(double input[],double quan[],double b,double str){
int n = sizeof(input) / sizeof(input[0]);
double sum;
for(int i = 0;i < n;i++){
sum += input[i] * quan[i];
}
return str * ReLu(sum - b);
}
double sqr_dif(double ans,double fact){
return (ans - fact) * (ans - fact);
}
long long random(int n){
long long ans = n;
for(int i = 0;i < 10;i++){
ans = ans * 10 + ((1000 * ans + 1145) % 1001) % 10;
}
return ans;
return n;
}
int main(){
cout<<"初始化......"<<endl;
for(int i = 0;i < 200;i++){
for(int j = 0;j < 200;j++){
q[0][i][j] = 1;
}
}
cout<<"正在读入模型"<<endl;
ifstream fin("main.mod");
ofstream fout("main.mod");
int n;
fin>>n;
for(int i = 1;i <= n;i++){
int m;
fin>>m;
cell_sum[i] = m;
for(int j = 0;j < m;j++){
fin>>cell_b[i][j]>>cell_str[i][j];
}
}
for(int i = 1;i < n;i++){
for(int j = 0;j < cell_sum[i];j++){
for(int k = 0;k < cell_sum[i + 1];k++){
fin>>q[i][k][j];
}
}
}
cout<<"完成!"<<endl;
fin.close();
//模型读入完成
cout<<"正在读入训练数据input组"<<endl;
ifstream fin_input("input.dat");
int _n;
int _m;
fin>>_n;
fin>>_m;
for(int i = 0;i < _n;i++){
for(int j = 0;j < _m;j++){
fin_input>>input[i][j];
}
}
cout<<"完成!"<<endl;
fin.close();
//训练数据input组读入完成
cout<<"正在读入训练数据output组"<<endl;
ifstream fin_output("output.dat");
int n_;
int m_;
fin>>n_;
fin>>m_;
for(int i = 0;i < n_;i++){
for(int j = 0;j < m_;j++){
fin_output>>output[i][j];
}
}
cout<<"完成!"<<endl;
//训练数据output组读入完成
cout<<"开始训练!"<<endl;
cout<<"生成三组网络"<<endl;
double q1[200][200][200];//[第i层][第i层的第j个神经元][第i+1层的第个神经元]之间的权值
double cell_b1[200][200];//[第i层][第i层的第j个神经元的偏置]
double cell_str1[200][200];//[第i层][第i层的第j个神经元的倍率]
double answer1[200][200];//[第i层][第i层的第j个神经元的输出]
cout<<"完成1/3"<<endl;
double q2[200][200][200];//[第i层][第i层的第j个神经元][第i+1层的第个神经元]之间的权值
double cell_b2[200][200];//[第i层][第i层的第j个神经元的偏置]
double cell_str2[200][200];//[第i层][第i层的第j个神经元的倍率]
double answer2[200][200];//[第i层][第i层的第j个神经元的输出]
cout<<"完成2/3"<<endl;
double q3[200][200][200];//[第i层][第i层的第j个神经元][第i+1层的第个神经元]之间的权值
double cell_b3[200][200];//[第i层][第i层的第j个神经元的偏置]
double cell_str3[200][200];//[第i层][第i层的第j个神经元的倍率]
double answer3[200][200];//[第i层][第i层的第j个神经元的输出]
cout<<"完成3/3"<<endl;
int num;
cout<<"你希望训练多少轮?"<<endl;
cin>>num;
for(int qwertyuiop = 0;qwertyuiop < num;qwertyuiop++){
//模型随机修改为3个副本
for(int i = 1;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
cell_b1[i][j] = cell_b[i][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
cell_str1[i][j] = cell_str[i][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
cell_b2[i][j] = cell_b[i][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
cell_str2[i][j] = cell_str[i][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
cell_b3[i][j] = cell_b[i][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
cell_str3[i][j] = cell_str[i][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
}
}
for(int i = 1;i < n;i++){
for(int j = 0;j < cell_sum[i];j++){
for(int k = 0;k < cell_sum[i + 1];k++){
q1[i][k][j] = q[i][k][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
q2[i][k][j] = q[i][k][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
q3[i][k][j] = q[i][k][j] + (abs(random(random(time(NULL) + cnt))) % 100 / 100.0) - 0.5;
cnt++;
}
}
}
//计算
//模型1
double wrong1 = 0;
for(int asdfghjkl = 0;asdfghjkl < _n;asdfghjkl++){
for(int j = 0;j < cell_sum[1];j++){
answer1[1][j] = cell(input[asdfghjkl],q1[1][j],cell_b1[1][j],cell_str1[1][j]);
}
for(int i = 2;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
answer1[i][j] = cell(answer[i - 1],q1[i][j],cell_b1[i][j],cell_str1[i][j]);
}
}
for(int i = 0;i < n_;i++){
wrong1 += sqr_dif(answer[n][i],output[asdfghjkl][i]);
}
}
//模型2
double wrong2 = 0;
for(int asdfghjkl = 0;asdfghjkl < _n;asdfghjkl++){
for(int j = 0;j < cell_sum[1];j++){
answer2[1][j] = cell(input[asdfghjkl],q2[1][j],cell_b2[1][j],cell_str2[1][j]);
}
for(int i = 2;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
answer2[i][j] = cell(answer[i - 1],q2[i][j],cell_b2[i][j],cell_str2[i][j]);
}
}
for(int i = 0;i < n_;i++){
wrong2 += sqr_dif(answer[n][i],output[asdfghjkl][i]);
}
}
//模型3
double wrong3 = 0;
for(int asdfghjkl = 0;asdfghjkl < _n;asdfghjkl++){
for(int j = 0;j < cell_sum[1];j++){
answer3[1][j] = cell(input[asdfghjkl],q3[1][j],cell_b3[1][j],cell_str3[1][j]);
}
for(int i = 2;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
answer3[i][j] = cell(answer[i - 1],q3[i][j],cell_b3[i][j],cell_str3[i][j]);
}
}
for(int i = 0;i < n_;i++){
wrong3 += sqr_dif(answer[n][i],output[asdfghjkl][i]);
}
}
//保存最优副本
if(wrong1 < wrong2 && wrong1 < wrong3){
for(int i = 1;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
cell_b[i][j] = cell_b1[i][j];
}
}
for(int i = 1;i < n;i++){
for(int j = 0;j < cell_sum[i];j++){
for(int k = 0;k < cell_sum[i + 1];k++){
q[i][k][j] = q1[i][k][j];
}
}
}
cout<<"第"<<qwertyuiop<<"轮训练已完成,最小方差:"<<fixed<<setprecision(3)<<wrong1 / n_;
}
if(wrong2 < wrong1 && wrong2 < wrong3){
for(int i = 1;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
cell_b[i][j] = cell_b2[i][j];
}
}
for(int i = 1;i < n;i++){
for(int j = 0;j < cell_sum[i];j++){
for(int k = 0;k < cell_sum[i + 1];k++){
q[i][k][j] = q2[i][k][j];
}
}
}
cout<<"第"<<qwertyuiop<<"轮训练已完成,最小方差:"<<fixed<<setprecision(3)<<wrong2 / n_;
}
if(wrong3 < wrong2 && wrong3 < wrong1){
for(int i = 1;i <= n;i++){
for(int j = 0;j < cell_sum[i];j++){
cell_b[i][j] = cell_b3[i][j];
}
}
for(int i = 1;i < n;i++){
for(int j = 0;j < cell_sum[i];j++){
for(int k = 0;k < cell_sum[i + 1];k++){
q[i][k][j] = q3[i][k][j];
}
}
}
cout<<"第"<<qwertyuiop<<"轮训练已完成,最小方差:"<<fixed<<setprecision(3)<<wrong3 / n_;
}
}
cout<<"正在保存......"<<endl;
for(int i = 1;i <= n;i++){
int m;
fin>>m;
cell_sum[i] = m;
for(int j = 0;j < m;j++){
fout<<cell_b[i][j]<<cell_str[i][j];
}
}
for(int i = 1;i < n;i++){
for(int j = 0;j < cell_sum[i];j++){
for(int k = 0;k < cell_sum[i + 1];k++){
fout<<q[i][k][j];
}
}
}
cout<<"完成!"<<endl;
return 0;
}