?? cpnet.cpp
字號:
MaxT = abs(Target[0]);
for(int i = 0; i < target_num; ++i) {
if(abs(Target[i]) > MaxT) {
MaxT = abs(Target[i]);
}
}
if(MaxT > 1) {
for(i = 0; i < target_num; ++i) {
Target[i] /= MaxT;
}
}
}
// denormalize target vector
void CPnet::DeNormalizeTarget() {
if(MaxT > 1) {
for(int i = 0; i < target_num; ++i) {
Target[i] *= MaxT;
}
}
}
// compute weighted sum for the current neuron
float CPnet::ComputeWeightedSum( float *Input ) {
float sum = 0.0f;
for(int i = 0; i < input_num; ++i) {
sum += Input[i]*Weight[i];
}
return sum;
}
// function for training process
void CPnet::Train() {
float net;
int first_index = 0, last_index = input_num;
bool out;
for(int i = 0; i < neuron_num; ++i) {
for(int j = first_index, k = 0; j < last_index; ++j, ++k) {
input[k] = Input[j];
if(eAnimType != DEFAULT) {
cout << Input[j];
if (j < last_index - 1) {
cout << ", ";
}
}
}
net = ComputeWeightedSum(input) + (-1)*threshold;
if (net>0)
out = 1;
else if (net <= 0)
out = 0;
Output[i] = out;
if(eAnimType != DEFAULT) {
cout << " -> " << out << endl;
if(eAnimType == ANIM_T3) {
WAIT(0.1*SECOND);
}
}
float d = Target[i];
j = first_index;
for(k = 0; j < last_index; ++j, ++k) {
delta[k] = LEARNING_RATE*(d-out)*Input[j];
Weight[k] += delta[k];
}
threshold -= (d-out);
first_index = j;
last_index += input_num;
}
if(bNeuralNetLoaded) {
bNewModifications = true;
}
}
// function for testing results after training
void CPnet::Run() {
float net;
int first_index = 0, last_index = input_num;
bool out;
for(int i = 0; i < neuron_num; ++i) {
WAIT(0.6*SECOND);
for(int j = first_index, k = 0; j < last_index; ++j, ++k) {
input[k] = Input[j];
cout << Input[j];
if (j < last_index - 1) {
cout << ", ";
}
}
net = ComputeWeightedSum(input) + (-1)*threshold;
if (net>0) {
out = 1;
}
else if (net <= 0) {
out = 0;
}
cout << " -> " << out << endl;
first_index = j;
last_index += input_num;
}
}
// Training Neural Net
void CPnet::TrainNetwork(void) {
cout << "\nTraining net..." << endl;
for(int i=0; i<CPN_ITER; ++i) {
Train();
if(eAnimType == ANIM_T1 || eAnimType == ANIM_T3) {
cout << "\nnumber of iterations = " << i + 1 << endl << endl ;
WAIT(0.3 * SECOND);
}
else if(eAnimType == ANIM_T2) {
cout << "\nnumber of iterations = " << i + 1 << endl << endl;
WAIT(0.3 * SECOND);
if (i < CPN_ITER - 1) {
CLEAR_SCREEN();
}
}
if(eAnimType != DEFAULT && i < CPN_ITER - 1) {
cout << "\nTraining net..." << endl;
}
}
bNeuralNetTrained = true;
WAIT(1*SECOND);
cout << "\n\nTraining completed,";
ComputeAverageError();
if (fAverageError <= MIN_ACCURACY) {
cout << "the learning process was successful!" << endl;
WAIT(0.6*SECOND);
cout << "\nNext step: Testing\n\n" << endl;
} else {
cout << "learning unsuccessful." << endl;
}
UpdateScreen();
}
// computes average error between
// target vector and ouput vector
void CPnet::ComputeAverageError() {
float sum = 0.0f;
for(int i = 0; i < target_num; ++i) {
sum += abs(Target[i] - Output[i]);
}
fAverageError = sum/target_num;
}
// Tests the current Neural Net with inputs
void CPnet::TestNetwork(void) {
cout << "\nTesting \"" << szNeuralNetName << "\" function..." << endl;
WAIT(0.5*SECOND);
cout << "\nTest results" << endl;
Run();
WAIT(1*SECOND);
cout << "\nTest completed!" << endl;
UpdateScreen();
}
// Test the Neural with the selected patterns
void CPnet::SelectiveTest( int pattern_num ) {
float net;
int first_index = 0, last_index = input_num;
bool out;
int nNumOfInputs = input_num * pattern_num;
float *Input = new float[nNumOfInputs];
for(int i = 0; i < nNumOfInputs; ++i) {
cout << "input[" << i << "] = ";
cin >> Input[i];
if ( input_num > 1 && !((i + 1) % input_num)) {
cout << endl;
}
WAIT(0.5*SECOND);
}
CLEAR_SCREEN();
WAIT(0.5*SECOND);
cout << "\nTesting \"" << szNeuralNetName << "\" function..." << endl;
WAIT(0.5*SECOND);
for(i = 0; i < pattern_num; ++i) {
for(int j = first_index, k = 0; j < last_index; ++j, ++k) {
input[k] = Input[j];
cout << Input[j];
if (j < last_index - 1) {
cout << ", ";
}
}
net = ComputeWeightedSum(input) + (-1)*threshold;
if (net>0)
out = 1;
else if (net <= 0)
out = 0;
cout << " -> " << out << endl;
WAIT(0.5*SECOND);
first_index = j;
last_index += input_num;
}
cout << "\nTest completed!" << endl;
delete Input;
}
void CPnet::UpdateScreen() {
cout << "Press any key to continue...";
getch();
CLEAR_SCREEN();
}
// checks to see if a file or directory exist
// in the current hard drive
bool CPnet::fExist( char* filepath ) {
WIN32_FIND_DATA file;
HANDLE hFile;
if (( hFile = FindFirstFile( filepath, &file ))
== INVALID_HANDLE_VALUE ) {
return false;
}
return true;
}
// saves current neural net data before proceding to a new step
void CPnet::SaveCurrentData() {
if(!bNeuralNetLoaded || (bNeuralNetLoaded && bNewModifications)) {
if(bNeuralNetCreated && !bNeuralNetSaved) {
cout << "\nBefore continuing,do you want to save the current Neural Net data?\n"
<< "Yes(y) No(n): ";
char response;
cin >> response;
response = tolower(response);
if(response == 'y') {
SaveNet();
cout << "The Neural Net was saved successfuly!" << endl;
UpdateScreen();
}
}
}
}
// Save Neural Net variables to a file
void CPnet::SaveNet(void) {
FILE *fw = fopen(szNeuralNetName,"w");
if(!fw) {
perror(szNeuralNetName);
}
// saving input number
fprintf(fw, "%d\n", input_num);
// saving neuron number
fprintf(fw, "%d\n", neuron_num);
// saving max input value
fprintf(fw, "%f\n", Max);
// saving max target value
fprintf(fw, "%f\n", MaxT);
// saving the inputs
total_input_num = input_num * neuron_num;
for(int i = 0; i < total_input_num; ++i) {
fprintf(fw, "%f ", Input[i]);
if (i > 0 && i < total_input_num - 1 && !((i + 1) % 8)) {
putc('\n', fw);
}
}
// saving the threshold
fprintf( fw, "\n%f\n", threshold );
// saving the targets
for(i = 0; i < target_num; ++i) {
fprintf(fw, "%f ", Target[i]);
}
putc('\n', fw);
// saving the weights
for(i = 0; i < input_num; ++i) {
fprintf(fw, "%f ", Weight[i]);
if (i > 0 && i < input_num - 1 && !((i + 1) % 8)) {
putc('\n', fw);
}
}
fflush(fw);
fclose(fw);
bNeuralNetSaved = true;
}
// Load Neural Net variables from a file
void CPnet::LoadNet(void) {
FILE *fw = fopen(szNeuralNetName,"r");
if(!fw) {
perror(szNeuralNetName);
}
// loading input number
fscanf(fw, "%d", &input_num);
// loading neuron number
fscanf(fw, "%d", &neuron_num);
// loading max input value
fscanf(fw, "%f", &Max);
// loading max target value
fscanf(fw, "%f", &MaxT);
// loading the Inputs
if(Input != NULL) {
delete Input;
}
if(input != NULL) {
delete input;
}
if(Output != NULL) {
delete Output;
}
if(Target != NULL) {
delete Target;
}
if(delta != NULL) {
delete delta;
}
bNeuralNetTrained = false;
total_input_num = input_num * neuron_num;
Input = new float[total_input_num];
if(!Input) {
std::cerr << "Error while allocating memory for inputs.\n";
}
input = new float[input_num];
if(!input) {
std::cerr << "Error while allocating memory for temporary inputs variable.\n";
}
Target = new float[neuron_num];
if(!Target) {
std::cerr << "Error while allocating memory for targets.\n";
}
Output = new float[neuron_num];
if(!Output) {
std::cerr << "Error while allocating memory for outputs.\n";
}
delta = new float[neuron_num];
if(!delta) {
std::cerr << "Error while allocating memory for delta.\n";
}
// loading the inputs
for(int i = 0; i < total_input_num; ++i) {
fscanf(fw, "%f", &Input[i]);
}
// loading the threshold
fscanf(fw, "%f", &threshold);
// loading the targets
for(i = 0; i < neuron_num; ++i) {
fscanf(fw, "%f ", &Target[i]);
}
// loading the weights
if(Weight != NULL) {
delete Weight;
}
Weight = new float[input_num];
if(!Weight) {
std::cerr << "Error while allocating memory for weights.\n";
}
for(i = 0; i < input_num; ++i) {
fscanf(fw, "%f", &Weight[i]);
}
target_num = neuron_num;
bNeuralNetCreated = true;
bNeuralNetLoaded = true;
bNewModifications = false;
if(Max > 0 && MaxT > 0) {
bNeuralNetTrained = true;
}
fclose(fw);
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -