📄 pittnet.cpp
字号:
//transmit interface output to units in cluster layer
for(d = 0; d < dimensions_of_signal; d++)
{
for(c = 0; c < number_of_cluster_units; c++)
{node_in_cluster_layer[c].input_value[d] = node_in_interface_layer[d].output_value[c];}
}
}
void ART_Topology::cluster_nodes_compete_for_activation(int train_or_test)
{
int d, cluster;
float champion = 0.0;
for(cluster = 0; cluster < clusterange + 1; cluster++)
{
if(node_in_cluster_layer[cluster].activation != -1.0)
{
node_in_cluster_layer[cluster].calculate_net_input();
if(node_in_cluster_layer[cluster].net_input > champion)
{
champion = node_in_cluster_layer[cluster].net_input;
cluster_champ = cluster;
}
}
}
if((node_in_cluster_layer[cluster_champ].cluster_tag == 0) && (train_or_test < 2))
{
node_in_cluster_layer[cluster_champ].cluster_tag = clustercount + 1;
clustercount = clustercount + 1;
}
if(train_or_test < 2)
{
for(cluster = 0; cluster < clusterange + 1; cluster++)
{
if(cluster == cluster_champ)
{node_in_cluster_layer[cluster].activation = 1.0;}
else
{
if(node_in_cluster_layer[cluster].activation != -1.0)
{node_in_cluster_layer[cluster].activation = 0.0;}
}
node_in_cluster_layer[cluster].establish_node_output();
// send output signals to Interface layer
for(d = 0; d < dimensions_of_signal; d++)
{node_in_interface_layer[d].input_value[cluster + 1] = node_in_cluster_layer[cluster].output_value[d];}
}
}
}
void ART_Topology::compute_norm_of_activation_vector(void)
{
norm_of_activation_vector = 0.0;
for(int d = 0; d < dimensions_of_signal; d++)
{norm_of_activation_vector += node_in_interface_layer[d].activation;}
compute_norm_of_input_vector();
}
void ART_Topology::compute_norm_of_input_vector(void)
{
norm_of_input_vector = 0.0;
for(int d = 0; d < dimensions_of_signal; d++)
{norm_of_input_vector += node_in_input_layer[d].signal_value;}
}
void ART_Topology::recompute_activation_vector_of_interface_layer(void)
{
for(int d = 0; d < dimensions_of_signal; d++)
{node_in_interface_layer[d].recompute_activation(cluster_champ);}
}
void ART_Topology:: update_the_network(void)
{
recompute_activation_vector_of_interface_layer();
compute_norm_of_activation_vector();
float ratio_test = norm_of_activation_vector / norm_of_input_vector;
if(ratio_test < vigilance_parameter)
{
node_in_cluster_layer[cluster_champ].activation = -1.0;
reset_value = 1;
resetcount += reset_value;
if(resetcount == number_of_cluster_units - 1)
{
clusterange = clusterange + 1;
if(clusterange > number_of_cluster_units)
{clusterange = number_of_cluster_units;}
}
}
else
{
// update the weights of the champion cluster unit
for(int u = 0; u < node_in_cluster_layer[cluster_champ].number_of_inputs; u++)
{node_in_cluster_layer[cluster_champ].input_weight_vector[u] = (weight_update_parameter * node_in_interface_layer[u].activation * node_in_cluster_layer[cluster_champ].input_weight_vector[u]) / ((weight_update_parameter - 1.0) + norm_of_activation_vector);}
for(int n = 0; n < dimensions_of_signal; n++)
{node_in_interface_layer[n].input_weight_vector[cluster_champ] = node_in_interface_layer[n].input_weight_vector[cluster_champ] * node_in_interface_layer[n].activation;}
reset_value = 0;
resetcount = 0;
}
}
void ART_Topology::set_cluster_activation_to_zero(void)
{
for(int cnode = 0; cnode < clusterange + 1; cnode++)
{node_in_cluster_layer[cnode].activation = 0.0;}
}
void ART_Topology::savenet(void)
{
char savename[13];
ofstream save_ptr;
int node, dim;
cout << "\n\n";
cout << "Please enter the name of the file which will hold the ART network"<<"\n";
cin >> savename; cout <<"\n";
save_ptr.open(savename, ios::out);
save_ptr << 2 << "\n"; //network identifier number
save_ptr << dimensions_of_signal << "\n";
save_ptr << weight_update_parameter << "\n";
save_ptr << vigilance_parameter << "\n";
save_ptr << clusterange << "\n";
save_ptr << clustercount << "\n";
save_ptr << number_of_cluster_units << "\n";
for(node = 0; node < dimensions_of_signal; node++)
{
for(dim = 1; dim < number_of_cluster_units + 1; dim++)
{save_ptr << node_in_interface_layer[node].input_weight_vector[dim] << " ";}
save_ptr << "\n";
}
for(node = 0; node < number_of_cluster_units; node++)
{
save_ptr << node_in_cluster_layer[node].cluster_tag << "\n";
for(dim = 0; dim < dimensions_of_signal; dim++)
{save_ptr << node_in_cluster_layer[node].input_weight_vector[dim] << " ";}
save_ptr << "\n";
}
save_ptr.close();
}
// Classes which specifies the containers of ART training and test data
class ART_Training_Data : public Data_type
{
public:
void determine_sample_number(void);
void load_data_into_array(void);
virtual void request_ART_data(int net_no);
};
void ART_Training_Data::load_data_into_array(void)
{
int d, i;
float dimensions;
// open the file containing the data
ifstream Afile_ptr; // pointer to a file
Afile_ptr.open(filename, ios::in);
//create a dynamic array to hold the specified number of samples
number_of_samples = new sample_data[sample_number];
for(i = 0; i < sample_number; i++)
{number_of_samples[i].data_in_sample = new float[signal_dimensions];}
// read in data from file and place in array
for(i = 0; i < sample_number; i++)
{
for(d = 0; d < signal_dimensions; d++)
{
Afile_ptr >> dimensions;
number_of_samples[i].data_in_sample[d] = dimensions;
}
}
Afile_ptr.close();
}
void ART_Training_Data :: determine_sample_number(void)
{
ifstream dfile_ptr; // pointer to a file
dfile_ptr.open(filename, ios::in);
float hold;
int lock = 1;
sample_number = 0;
do
{
if(dfile_ptr.eof()){lock = 0;}
else
{dfile_ptr >> hold; sample_number += 1;}
}while(lock > 0);
dfile_ptr.close();
sample_number = int(sample_number / signal_dimensions);
}
void ART_Training_Data::request_ART_data(int net_no)
{
cout << "Please enter the file name containing the training data for ART network no. "<< net_no << "\n";
cin >> filename; cout << "\n";
specify_signal_sample_size();
}
class ART_Test_Data : public ART_Training_Data
{public: void request_ART_data(int net_no);};
void ART_Test_Data::request_ART_data(int net_no)
{
cout << "Please enter the file name containing the test data for ART network no. " << net_no << "\n";
cin >> filename; cout << "\n";
specify_signal_sample_size();
}
//************************************************************************//
class NeuralA // class containing the ART1 neural net structure
{ // along with training and testing data
private:
ART_Training_Data ART_Train;
ART_Test_Data * ART_Test; // the number of test is variable
int number_of_ART_tests;
void initialize_ART_training_storage_array(int AN);
void establish_ART_test_battery_size(void);
void train_ART_network(int ARTN);
void test_ART_network(int ANET);
public:
ART_Topology ART_Design;
void construct_ART_network(void);
void network_training_testing(int TT);
~NeuralA();
};
//****************************************************************************//
NeuralA::~NeuralA()
{ delete [] ART_Test; }
void NeuralA::construct_ART_network(void)
{
int looploc = 0;
clrscr();
cout << " **** Adaptive Resonance Theory network for binary signals **** " <<"\n\n\n";
do
{
cout <<"\n";
cout << "Do you wish to" << "\n\n";
cout << "C. Create your own ART1 Network " << "\n";
cout << "U. Upload an existing ART1 Network " << "\n\n";
cout << "Your choice?: "; cin >> ART_Design.netcreate;
cout << "\n\n";
ART_Design.netcreate = toupper(ART_Design.netcreate);
if((ART_Design.netcreate == 'C') || (ART_Design.netcreate == 'U')) {looploc = 1;}
} while(looploc <= 0);
if(ART_Design.netcreate == 'U')
{ART_Design.upload_network();}
else
{
cout << "\n";
cout << "Please enter the dimensions of the ART network's input signal vector: ";
cin >> ART_Design.dimensions_of_signal; cout << "\n";
cout << "Please enter the vigilance parameter of the ART network: ";
cin >> ART_Design.vigilance_parameter; cout << "\n";
}
}
void NeuralA::initialize_ART_training_storage_array(int AN)
{
int AT = AN;
ART_Train.acquire_net_info(ART_Design.dimensions_of_signal, ART_Design.number_of_cluster_units);
ART_Train.request_ART_data(AT);
if(ART_Design.netcreate == 'C') // constructing new network
{
ART_Design.number_of_cluster_units = ART_Train.sample_number;
ART_Design.establish_net_topology();
}
}
void NeuralA::train_ART_network(int ARTN)
{
int dim, nodes_available_for_clustering;
char savetrain;
int dolock = 0;
clrscr();
cout << "\n\n";
cout << "For Neural Network #" << ARTN << "\n";
do
{
cout << "do you wish to save the ART Training results to a file? (Y or N): ";
cin >> savetrain;
savetrain = toupper(savetrain);
if((savetrain == 'N') || (savetrain == 'Y')) {dolock = 1;}
cout << "\n";
} while(dolock <= 0);
if(savetrain == 'Y')
{
cout << "please enter the name of the file to hold the results of the ART Training" << "\n";
cin >> ART_Train.resultsname; cout << "\n";
}
for(int pattern = 0; pattern < ART_Train.sample_number; pattern++)
{
// present pattern to input layer
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{ART_Design.node_in_input_layer[dim].signal_value = ART_Train.number_of_samples[pattern].data_in_sample[dim];}
nodes_available_for_clustering = ART_Design.number_of_cluster_units;
do
{
ART_Design.transmit_pattern_to_interface();
ART_Design.broadcast_output_to_cluster_layer();
ART_Design.cluster_nodes_compete_for_activation(1);
ART_Design.update_the_network();
nodes_available_for_clustering = nodes_available_for_clustering - ART_Design.reset_value;
if(nodes_available_for_clustering < 1) // input pattern cannot be clustered
{
// clrscr();
cout << "Input pattern #" << pattern + 1 << ": ";
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{cout << int(ART_Design.node_in_input_layer[dim].signal_value);}
cout << " cannot be clustered" << "\n";
break;
}
} while (ART_Design.reset_value >=1);
if(savetrain == 'Y')
{
ofstream ART_savefile_ptr(ART_Train.resultsname, ios::out|ios::app);
ART_savefile_ptr << pattern + 1 << " ";
for(dim = 0; dim < ART_Design.dimensions_of_signal; dim++)
{ART_savefile_ptr << int(ART_Design.node_in_input_layer[dim].signal_value);}
ART_savefile_ptr << " " << ART_Design.node_in_cluster_layer[ART_Design.cluster_champ].cluster_tag << "\n";
ART_savefile_ptr.close();
}
ART_Design.set_cluster_activation_to_zero();
}
// delete array containing training data
ART_Train.delete_signal_array();
}
void NeuralA::establish_ART_test_battery_size(void)
{
cout <<"Please enter the number of tests you wish to run on the ART neural network: ";
cin >> number_of_ART_tests; cout <<"\n";
// create testing array
if(number_of_ART_tests > 0)
{
ART_Test = new ART_Test_Data[number_of_ART_tests];
for(int t = 0; t < number_of_ART_tests; t++)
{ART_Test[t].acquire_net_info(ART_Design.dimensions_of_signal, ART_Design.number_of_cluster_units);}
}
}
void NeuralA::test_ART_network(int ANET)
{
int tnet, dim, pattern;
tnet = ANET;
for(int Atest = 0; Atest < number_of_ART_tests; Atest++)
{
ART_Test[Atest].request_ART_data(tnet);
cout << "For ART1 neural network #" << ANET <<" and test #"<<Atest+1<<":" <<"\n";
cout << "please enter the name of the file to hold the results of the ART Testing " << "\n";
cin >> ART_Test[Atest].resultsname; cout << "\n";
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -