Code: Select all

```
#include <iostream>
#include <math.h>
#include <stdlib.h>
#include <fstream>
using namespace std;
double lr=1;
double sigmoid(double x){
double e=2.71828;
double k=pow(e, x)/(pow(e, x)+1);
return k;
}
double dsigmoid(double x){
double e=2.71828;
double k=0;
double g=700;
if (x<g){
double j=pow(e, x);
double l=j+1;
double h=pow(l,2);
k=j/h;
}
return k;
}
int feedforward(double **weightinlayer, double *input, double *layer1, int in, int layer, double b){
double placeholder[in][layer];
double placeholder2[layer][in];
double placeholder1[layer];
double placeholder3[layer];
double r;
for (int i=0;i<in;i++){
for (int j=0;j<layer;j++){
placeholder[i][j]=0;
}
}
for (int i=0;i<in;i++){
for (int j=0;j<layer;j++){
placeholder2[j][i]=0;
}
}
for (int i=0;i<layer;i++){
placeholder1[i]=0;
}
for (int i=0;i<layer;i++){
placeholder3[i]=0;
}
for (int i=0;i<in;i++){
for (int j=0;j<layer;j++){
placeholder[i][j]=input[i]*weightinlayer[i][j];
}
}
for (int i=0;i<in;i++){
for(int j=0;j<layer;j++){
placeholder2[j][i]=placeholder[i][j];
}
}
for (int i=0;i<in;i++){
for (int j=0;j<layer;j++){
placeholder3[j]=placeholder2[i][j]+placeholder3[j];
}
}
for (int i=0;i<layer;i++){
r=placeholder3[i];
r=r+b;
r=sigmoid(r);
layer1[i]=r;
}
return 0;
}
int backprop(double **weightlayerout, double **weightinlayer, double *input, double *layer1, double *output, int in, int layer, int out, double b, int answer, int h, double b2){
double placeholder[in][layer];
double placeholder2[layer][in];
double placeholder1[layer];
double placeholder3[layer];
double dsig[layer];
double r;
for (int j=0;j<layer;j++){
placeholder3[j]=0;
}
for (int i=0;i<layer;i++){
for (int j=0;j<in;j++){
placeholder2[i][j]=0;
}
}
for (int i=0;i<in;i++){
for (int j=0;j<layer;j++){
placeholder[i][j]=input[i]*weightinlayer[i][j];
}
}
for (int i=0;i<in;i++){
for(int j=0;j<layer;j++){
placeholder2[j][i]=placeholder[i][j];
}
}
for (int i=0;i<layer;i++){
for (int j=0;j<in;j++){
placeholder3[j]=placeholder2[i][j]+placeholder3[j];
}
}
for (int i=0;i<layer;i++){
r=placeholder3[i];
r=r+b;
r=dsigmoid(r);
dsig[i]=r;
//cout<<dsig[i]<<" ";
}
ofstream fileplaceholder2;
fileplaceholder2.open("placeholder2.txt", ios::app);
fileplaceholder2 << h << ": ";
for (int i=0;i<layer;i++){
for (int j=0;j<in;j++){
fileplaceholder2 << placeholder2[i][j]<< " ";
}
cout << "\n";
}
fileplaceholder2<< "\n";
fileplaceholder2.close();
ofstream fileplaceholder3;
fileplaceholder3.open("placeholder3.txt", ios::app);
fileplaceholder3 << h << ": ";
for (int j=0;j<layer;j++){
fileplaceholder3 << placeholder3[j]<< " ";
}
fileplaceholder3<< "\n";
fileplaceholder3.close();
ofstream filedsig;
filedsig.open("dsig.txt", ios::app);
filedsig << h << ": ";
for (int j=0;j<layer;j++){
filedsig << dsig[j]<< " ";
}
filedsig<< "\n";
filedsig.close();
double aplaceholder[layer][out];
double aplaceholder2[out][layer];
double aplaceholder1[out];
double aplaceholder3[out];
double dsig2[out];
for (int i=0;i<layer;i++){
for (int j=0;j<out;j++){
aplaceholder[i][j]=layer1[i]*weightlayerout[i][j];
// cout<< h << ": " << aplaceholder[i][j]<< " ";
// cout<< h << ": " << layer1[i]<<" ";
//cout << weightlayerout[i][j]<<" ";
}
cout<<"\n";
}
for(int i=0;i<out;i++){
for(int j=0;j<layer;j++){
aplaceholder2[i][j]=0;
}
}
for(int j=0;j<out;j++){
aplaceholder1[j]=0;
}
for(int j=0;j<out;j++){
aplaceholder3[j]=0;
}
for (int i=0;i<layer;i++){
for(int j=0;j<out;j++){
aplaceholder1[j]=aplaceholder[i][j];
}
for(int j=0;j<out;j++){
aplaceholder2[j][i]=aplaceholder1[j];
}
}
for (int j=0;j<out;j++){
aplaceholder3[j]=0;
}
for (int i=0;i<layer;i++){
for (int j=0;j<out;j++){
aplaceholder3[j]=aplaceholder2[i][j]+aplaceholder3[j];
//cout << h << ": " << aplaceholder3[j] << "\n";
}
}
for (int i=0;i<out;i++){
//r=0;
r=aplaceholder3[i];
//cout << h << ": "<<r<<" " << "\n";
r=r+b2;
dsig2[i]=dsigmoid(r);
//dsig2[i]=r;
}
ofstream fileaplaceholder3;
fileaplaceholder3.open("aplaceholder3.txt", ios::app);
fileaplaceholder3 << h << ": ";
for (int j=0;j<out;j++){
fileaplaceholder3 << aplaceholder3[j]<< " ";
}
fileaplaceholder3<< "\n";
fileaplaceholder3.close();
ofstream filedsig2;
filedsig2.open("dsig2.txt", ios::app);
filedsig2 << h << ": ";
for (int j=0;j<out;j++){
filedsig2 << dsig2[j]<< " ";
}
filedsig2<< "\n";
filedsig2.close();
double pcpw[layer][out];
double er[out];
for (int j=0; j<out;j++){
if (answer==j){
er[j]=1-output[j];
}
else if (answer!=j){
er[j]=0-output[j];
}
}
for (int i=0;i<out;i++){
//cout<<er[i]<<" ";
}
for (int i=0;i<layer;i++){
for (int j=0;j<out;j++){
pcpw[i][j]=2*er[j]*dsig2[j]*layer1[i];
}
}
double tplaceholder[out][layer];
double tplaceholder1[out];
double tplaceholder2[layer][out];
for (int i=0;i<out;i++){
for (int j=0;j<layer;j++){
tplaceholder[i][j]=0;
}
}
for (int j=0;j<out;j++){
tplaceholder1[j]=0;
}
for (int i=0;i<out;i++){
for (int j=0;j<layer;j++){
tplaceholder2[j][i]=0;
}
}
for (int i=0;i<layer;i++){
for (int j=0;j<out;j++){
tplaceholder2[i][j]=weightlayerout[i][j];
//cout << tplaceholder2[i][j] << " ";
}
//cout <<"\n";
}
for (int i=0;i<layer;i++){
for(int j=0;j<out;j++){
tplaceholder[j][i]=tplaceholder2[i][j];
}
}
for (int i=0;i<out;i++){
for (int j=0;j<layer;j++){
tplaceholder[i][j]=tplaceholder[i][j]*er[i];
}
}
double erplaceholder[layer];
for(int i=0; i<layer; i++){
erplaceholder[i]=0;
}
for(int i=0;i<out;i++){
for (int j=0;j<layer;j++){
erplaceholder[j]=tplaceholder[i][j]*dsig[i]+erplaceholder[j];
}
}
ofstream filetplaceholder;
filetplaceholder.open("tplaceholder.txt", ios::app);
filetplaceholder << h << ": ";
for (int j=0;j<layer;j++){
filetplaceholder << tplaceholder[j]<< " ";
}
filetplaceholder << "\n";
filetplaceholder.close();
double pcpw2[in][layer];
ofstream fileer;
fileer.open("er.txt", ios::app);
fileer << h << ": ";
for (int j=0;j<out;j++){
fileer << er[j]<< " ";
}
fileer<< "\n";
fileer.close();
ofstream fileerplaceholder;
fileerplaceholder.open("erplaceholder.txt", ios::app);
fileerplaceholder << h << ": ";
for (int j=0;j<layer;j++){
fileerplaceholder << erplaceholder[j]<< " ";
}
fileerplaceholder << "\n";
fileerplaceholder.close();
for(int i=0;i<in;i++){
for (int j=0;j<layer;j++){
pcpw2[i][j]=erplaceholder[j]*input[i];
}
}
for (int i=0;i<layer;i++){
for (int j=0;j<out;j++){
weightlayerout[i][j]=weightlayerout[i][j]-(lr*pcpw[i][j]);
}
}
double k;
for (int i=0;i<in;i++){
for (int j=0;j<layer;j++){
k=lr*pcpw2[i][j];
weightinlayer[i][j]=weightinlayer[i][j]-k;
}
}
for (int i=0; i<out;i++){
b=b-2*er[i]*dsig2[i];
}
ofstream fileb;
fileb.open("b.txt", ios::app);
fileb << h << ": ";
fileb << b << " ";
fileb << "\n";
fileb.close();
cout << b << " ";
for (int i=0; i<out;i++){
b2=b2-2*erplaceholder[i]*dsig[i];
}
ofstream fileb2;
fileb2.open("b2.txt", ios::app);
fileb2 << h << ": ";
fileb2 << b2 << " ";
fileb2 << "\n";
fileb2.close();
ofstream filepcpw2;
filepcpw2.open("pcpw2.txt", ios::app);
for (int i=0;i<in;i++){
filepcpw2 << h << ": ";
for (int j=0;j<layer;j++){
filepcpw2 << pcpw2[i][j]<< " ";
}
filepcpw2 << "\n";
}
filepcpw2.close();
ofstream filepcpw;
filepcpw.open("pcpw.txt", ios::app);
for (int i=0;i<layer;i++){
filepcpw << h << ": ";
for (int j=0;j<out;j++){
filepcpw << pcpw[i][j]<< " ";
}
filepcpw << "\n";
}
filepcpw.close();
return 0;
}
int checkanswer(double *output, int out){
double answertrue=0;
double h, l;
for (int i=0; i<out; i++){
if (h<output[i]){
h=output[i];
l=i;
}
}
answertrue=l;
return answertrue;
}
int main(){
int set=4;
int in=2*set;
int out=set*set;
int layer=(in+out)/2;
double input[in];
double layer1[layer];
double output[out];
double b=rand()%10;
double b2=rand()%10;
double **weightinlayer=new double *[in];
for (int i=0;i<in;i++){
weightinlayer[i]=new double [layer];
}
double **weightlayerout=new double *[layer];
for (int i=0;i<layer;i++){
weightlayerout[i]=new double [out];
}
srand(0);
for (int i=0;i<in;i++){
for(int j=0;j<layer;j++){
weightinlayer[i][j]=rand()%10;
}
}
for (int i=0;i<in;i++){
for(int j=0;j<layer;j++){
weightlayerout[i][j]=rand()%10;
}
}
remove("b2.txt");
remove("b.txt");
remove("placeholder2.txt");
remove("tplaceholder.txt");
remove("placeholder3.txt");
remove("dsig.txt");
remove("output.txt");
remove("er.txt");
remove("input.txt");
remove("erplaceholder.txt");
remove("pcpw2.txt");
remove("pcpw.txt");
remove("weightlo.txt");
remove("weightil.txt");
ofstream fileweightil;
ofstream fileweightlo;
double batch=100;
double correct;
for (int i=0; i<batch;i++){
//cout<< i << ": ";
int one=rand()%4;
int two=rand()%4;
for ( int i=0; i<in; i++){
input[i]=0;
}
input[one]=1;
input[two+set]=1;
double answer=one+two;
feedforward(weightinlayer, input, layer1, in, layer, b2);
/*for (int k=0;k<layer; k++){
cout << layer1[i] << " ";
}
cout << "\n";*/
feedforward(weightlayerout, layer1, output, layer, out, b);
/*for (int i=0;i<layer;i++){
cout << layer1[i]<<" ";
}
cout << "\n";
/*for (int i=0; i<out; i++){
cout << output[i] << " ";
}
cout<< i <<":";
for (int k=0;k<in;k++){
for (int j=0;j<layer;j++){
cout << " "<< weightinlayer[k][j]<< " ";
}
cout << "\n";
}*//*
for (int k=0;k<layer;k++){
for (int j=0;j<out;j++){
cout << " "<< weightlayerout[k][j]<< " ";
}
cout << "\n";
}/*
for (int i=0;i<out;i++){
cout << output[i] << " ";
}*/
fileweightil.open("weightil.txt", ios::app);
for (int k=0; k<in;k++){
fileweightil<< i << ": ";
for (int j=0;j<layer;j++){
fileweightil << weightinlayer[k][j] << " ";
}
fileweightil << "\n";
}
fileweightil.close();
fileweightlo.open("weightlo.txt", ios::app);
for (int k=0; k<layer;k++){
fileweightlo<< i << ": ";
for (int j=0;j<out;j++){
fileweightlo << weightlayerout[k][j] << " ";
}
fileweightlo << "\n";
}
fileweightlo.close();
backprop(weightlayerout, weightinlayer, input, layer1, output, in, layer, out, b, answer, i, b2);
/*for (int k=0;k<in;k++){
for (int j=0;j<layer; j++){
if (weightinlayer[k][j]!=weightinlayer[k][j]){
weightinlayer[k][j]=rand()%10;
}
}
}
for (int k=0;k<layer;k++){
for (int j=0;j<out; j++){
if (weightlayerout[k][j]!=weightlayerout[k][j]){
weightlayerout[k][j]=rand()%10;
}
}
}*/
int answertrue=checkanswer(output, out);
ofstream fileinput;
fileinput.open("input.txt", ios::app);
fileinput << i << ": ";
for (int j=0;j<in;j++){
fileinput << input[j]<< " ";
}
fileinput << "\n";
fileinput.close();
ofstream fileout;
fileout.open("output.txt", ios::app);
fileout << i << ": ";
for (int j=0;j<out;j++){
fileout << output[j]<< " ";
}
fileout<< "\n";
fileout.close();
if (answertrue==answer){
correct++;
}
//cout << "\n";
//cout<< answertrue<< " "<< answer << "\n";
}
double percent=correct/batch*100;
cout << correct << " "<< percent << "%\n";
}
```

Also, hi Dinosawer.