#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include <math.h>
#include <fstream>
#include <iostream>
#include <float.h>
#include <conio.h>
using namespace std;
#define In_number 64
#define Pats_number 5
#define Out_number 5
#define HN_number 10
const double Etta_H = 0.06;
const double Etta_O = 0.06;
void W_initialize();
void Pattern_load();
void NN_calculation();
void Test_Er_calculation();
int Pattern_Index = 0;
double Er_pat[Out_number];
double Out_pat[Out_number];
double Error = 0.0;
double Neuron_out[HN_number];
double W_H[In_number][HN_number];
double W_O[HN_number][Out_number];
double Teta_H[HN_number];
double Teta_O[Out_number];
int In_trainData[Pats_number][In_number];
int Out_trainData[Pats_number][Out_number];
void NN_calculation(void)
{
int i,j = 0;
for(i = 0;i<HN_number;i++)
{
Neuron_out[i] = Teta_H[i];
for(int j = 0;j<In_number;j++)
{
Neuron_out[i] = Neuron_out[i] + (In_trainData[Pattern_Index][j] * W_H[j][i]);
}
Neuron_out[i] = tanh(Neuron_out[i]); // tanh function as hidden neuron function
// Neuron_out[i] = 1/(1 + exp(-Neuron_out[i])); // Sigmoid function as hidden neuron function
}
for (i = 0;i<Out_number;i++)
{
Out_pat[i] = Teta_O[i];
Er_pat[i] = 0;
}
for (i = 0;i<Out_number;i++)
{
for(j = 0;j<HN_number;j++)
{
Out_pat[i] = Out_pat[i] + Neuron_out[j] * W_O[j][i];
}
}
for (i = 0;i<Out_number;i++) Er_pat[i] = Out_pat[i] - Out_trainData[Pattern_Index][i];
}
void loadWeights(void)
{
FILE *pFile;
const char* strFileName = "Optimum_weights.txt";
errno_t err;
if( (err = fopen_s( &pFile, strFileName, "r" )) !=0)
printf( "Error loading file '%s' - file does not exist" );
else
printf( "\nThe file 'Optimum_weights.txt' was opened\n" );
printf("Loading weights ...\n\n");
for(int j = 0;j<HN_number;j++)
{
for(int i = 0;i<In_number;i++)
{
fscanf_s (pFile , "%lg", &(W_H[i][j]) );
}
}
for(int j = 0;j<HN_number;j++)
{
fscanf_s (pFile , "%lg", &(Teta_H[j]));
}
for(int i = 0;i<Out_number;i++)
{
for(int j = 0;j<HN_number;j++)
{
fscanf_s (pFile , "%lg", &(W_O[j][i]));
}
}
for(int i = 0;i<Out_number;i++)
{
fscanf_s (pFile , "%lg", &(Teta_O[i]));
}
fclose(pFile);
}
void loadTest(void)
{
FILE *pFile;
int i,j;
const char* strFileName = "Test_data.txt";
errno_t err;
if( (err = fopen_s( &pFile, strFileName, "r" )) !=0)
printf( "Error loading file '%s' - file does not exist" );
else
printf( "\nThe file 'Test_results.txt' was created\n" );
for (i = 0; i < Pats_number; i++ )
{
for ( j = 0; j < In_number; j++ )
{
//nNumberOfFields = fscanf_s (pFile , "%lg %lg", &dX , &dY );
fscanf_s (pFile , "%d", &(In_trainData[i][j]) );
// In_trainData[i][j] = Input[j][i];
}
for (j = 0; j < Out_number; j++ )
{
fscanf_s (pFile , "%d", &(Out_trainData[i][j]) );
}
}
fclose(pFile);
}
void Test_Er_calculation(void)
{
Error = 0.0;
//for(int i = 0;i<Pats_number;i++)
for(int j = 0;j<Out_number;j++)
{
//Pattern_Index = i;
NN_calculation();
Error = Error + (Er_pat[j] * Er_pat[j]);
}
}
int main(void)
{
loadWeights();
loadTest();
ofstream OutF1("Test_results.txt");
OutF1 << "Outputs results for the test patterns" <<endl;
OutF1 << "-------------------------------------" <<endl;
OutF1 << "Pattern" << " " << "Error " << " " << "Neural Netwok Outputs" <<endl;
OutF1 << "-------" << " " << "---------" << " " << "---------------------" <<endl;
for(int i = 0;i<Pats_number;i++)
{
OutF1 << i <<" ";
Pattern_Index = i;
Test_Er_calculation();
OutF1 << Error << " ";
for(int j = 0;j<Out_number;j++)
{
NN_calculation();
OutF1 << Out_pat[j] <<" ";
}
OutF1 << endl;
}
getch();
return 0;
}