⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pittnet.cpp

📁 神经网络c++源程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    establish_activation_functions(); // for hidden and output nodes
  }
} // end construct and initialize neural network function

void Back_Topology::upload_network(void)
{
  char getname[13];
  ifstream get_ptr;
  int netid, nodes, dim, inputs_to_output_node, hid, inputs;
  int dolock = 0;

  do
  {
    cout << "\n\n";
    cout << "Please enter the name of the file which holds the Backpropagation network" << "\n";
    cin >> getname; cout << "\n";
    get_ptr.open(getname, ios::in);
    get_ptr >> netid;
    if(netid == 1) {dolock = 1;}
    else
    {
      cout << "Error** file contents do not match Backprop specifications" << "\n";
      cout << "try again" << "\n";
      get_ptr.close();
    }
  } while(dolock <= 0);

  get_ptr >> signal_dimensions;
  get_ptr >> activation_function_for_output_layer;
  get_ptr >> nodes_in_output_layer;
  get_ptr >> inputs_to_output_node;

  // establish output layer
  node_in_output_layer = new Output_units[nodes_in_output_layer];
  for(nodes = 0; nodes < nodes_in_output_layer; nodes++)
  {
   node_in_output_layer[nodes].number_of_input_units = inputs_to_output_node;
   node_in_output_layer[nodes].establish_array_of_processing_unit_inputs();
   node_in_output_layer[nodes].establish_weight_vector_for_processing_units();
   get_ptr >> node_in_output_layer[nodes].bias;
  }
  for(nodes = 0; nodes < nodes_in_output_layer; nodes++)
  {
   for(dim = 0; dim < inputs_to_output_node; dim++)
   {get_ptr >> node_in_output_layer[nodes].weight_of_inputs[dim];}
  }

  // establish hidden layer(s)
  get_ptr >> number_of_hidden_layers;
  if(number_of_hidden_layers > 0)
  {
    hidden_layer_number = new Hidden_layer[number_of_hidden_layers];
    get_ptr >> activation_function_for_hidden_layer;
    for(hid = 0; hid < number_of_hidden_layers; hid++)
    {
      get_ptr >> hidden_layer_number[hid].nodes_in_hidden_layer;
      nodes = hidden_layer_number[hid].nodes_in_hidden_layer;
      hidden_layer_number[hid].node_in_hidden_layer = new Hidden_units[nodes];

      if(hid == 0) {inputs = signal_dimensions;}
      else
      {inputs = hidden_layer_number[0].nodes_in_hidden_layer;}

      for(nodes = 0; nodes < hidden_layer_number[hid].nodes_in_hidden_layer; nodes++)
      {
	hidden_layer_number[hid].node_in_hidden_layer[nodes].number_of_input_units = inputs;
	hidden_layer_number[hid].node_in_hidden_layer[nodes].establish_array_of_processing_unit_inputs();
	get_ptr >> hidden_layer_number[hid].node_in_hidden_layer[nodes].bias;
      }
      for(nodes = 0; nodes < hidden_layer_number[hid].nodes_in_hidden_layer; nodes++)
      {
	for(dim = 0; dim < inputs; dim++)
	{get_ptr >> hidden_layer_number[hid].node_in_hidden_layer[nodes].weight_of_inputs[dim];}
      }
    }
  }
  get_ptr.close();
}

void Back_Topology::savenet(void)
{
  char savename[13];
  ofstream save_ptr;
  int nodes, dim, inputs, hid;

  cout << "\n\n";
  cout << "Please enter the name of the file that will hold" << "\n";
  cout << "the Backpropagation network:  "; cin >> savename;

  save_ptr.open(savename, ios::out);
  save_ptr << 1 << "\n";   // network identifier number
  save_ptr << signal_dimensions << "\n";
  save_ptr << activation_function_for_output_layer << "\n";
  save_ptr << nodes_in_output_layer << "\n";

  if(number_of_hidden_layers > 0)
  {inputs = hidden_layer_number[number_of_hidden_layers - 1].nodes_in_hidden_layer;}
  else
  {inputs = signal_dimensions;}
  save_ptr << inputs << "\n";
  for(nodes = 0; nodes < nodes_in_output_layer; nodes++)
  {save_ptr << node_in_output_layer[nodes].bias << " ";}
  save_ptr << "\n";


  for(nodes = 0; nodes < nodes_in_output_layer; nodes++)
  {
    for(dim = 0; dim < inputs; dim++)
    {save_ptr << node_in_output_layer[nodes].weight_of_inputs[dim] << " ";}
    save_ptr << "\n";
  }

  save_ptr << number_of_hidden_layers << "\n";

  if(number_of_hidden_layers > 0)
  {
    save_ptr << activation_function_for_hidden_layer << "\n";

    for(hid = 0; hid < number_of_hidden_layers; hid++)
    {
      save_ptr << hidden_layer_number[hid].nodes_in_hidden_layer << "\n";
      if(hid == 0) {inputs = signal_dimensions;}
      else {inputs = hidden_layer_number[0].nodes_in_hidden_layer;}

      for(nodes = 0; nodes < hidden_layer_number[hid].nodes_in_hidden_layer; nodes++)
      {save_ptr << hidden_layer_number[hid].node_in_hidden_layer[nodes].bias << " ";}
      save_ptr << "\n";

      for(nodes = 0; nodes < hidden_layer_number[hid].nodes_in_hidden_layer; nodes++)
      {
	for(dim = 0; dim < inputs; dim++)
	{save_ptr << hidden_layer_number[hid].node_in_hidden_layer[nodes].weight_of_inputs[dim] << " ";}
	save_ptr << "\n";
      }
    }
  }
  save_ptr.close();
}

Back_Topology::~Back_Topology()
{
   delete [] hidden_layer_number;
   delete [] node_in_output_layer;
}

void Back_Topology::establish_activation_functions(void)
{
  int bchoice, count;
  int dolock = 1;

  for(count = 0; count < 2; count++)
  {
    cout << "\n";
    if((count == 0) && (number_of_hidden_layers > 0))
    {cout << "For the nodes in the hidden layer(s):" << "\n";}
    else
    {cout << "For the output layer:" << "\n";}
    do
    {
      cout << "please select the type of activation function you wish the nodes to use" << "\n\n";
      cout << "1.  Binary Sigmoid Function " << "\n";
      cout << "2.  Bipolar Sigmoid Function " << "\n\n";
      cout << "Your Selection "; cin >> bchoice;
      cout << "\n\n";
      if((bchoice == 1) || (bchoice == 2)) {dolock = 0;}
    } while(dolock >= 1);

    if((count == 0) && (number_of_hidden_layers > 0))
    {activation_function_for_hidden_layer = bchoice;}
    else
    {activation_function_for_output_layer = bchoice;}
  }
}

// Declare classes that will establish training and testing data arrays
class sample_data
{
  public:
  float *data_in_sample; // pointer to the dimensions of a single signal
  ~sample_data();
};

sample_data:: ~sample_data()
{delete [] data_in_sample;}

class Data_type
{
 public:
 char filename[13];                       // File containing data for network training or testing
 char resultsname[13];                    // File containing data for results of training or testing
 int signal_dimensions;                   // Number of dimensions contained in signal
 int sample_number;                       // Number of signals in training set
 int nodes_in_output_layer;               // Dimensions of test data output
 sample_data *number_of_samples;          // Pointer to the array containing signals
 float *max_output_value;
 float *min_output_value;
 virtual void determine_sample_number(void);
 void specify_signal_sample_size(void);
 virtual void load_data_into_array(void); // Function to place data into the array
 void acquire_net_info(int signal, int no_output_nodes);
 void delete_signal_array(void);          // Function to free memory allocated to hold signals
 virtual void normalize_data_in_array(void);
 ~Data_type();   // class destructor
};

Data_type::~Data_type()
{
  delete [] max_output_value;
  delete [] min_output_value;
}

// define functions of Data_type Class
void Data_type :: 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 + nodes_in_output_layer));
}

void Data_type::specify_signal_sample_size(void)
{
  char tchoice;
  int dolock = 1;
  do
  {
    cout <<"\n";
    cout << "Please select the number of samples you wish to use" << "\n\n";
    cout << "	A.  All samples in the file" << "\n";
    cout << "	S.  Specific number of samples"<< "\n\n";
    cout << "	Your Selection: "; cin >> tchoice;
    cout << "\n\n";
    tchoice = toupper(tchoice);
    if((tchoice == 'A') || (tchoice == 'S')) {dolock = 0;}
  } while(dolock >= 1);
  cout <<"\n";
  if(tchoice == 'A') {determine_sample_number();}
  else
  {
    cout << "\n";
    cout << "please enter the number of testing samples you wish to use: ";
    cin >> sample_number;
    cout << "\n";
  }
  load_data_into_array();
}

void Data_type::normalize_data_in_array(void)
{
  int imax, imin, trigger;
  float min, max, rmax, rmin;
  int total_dimension = signal_dimensions + nodes_in_output_layer;
  int i, j;
  max_output_value = new float[nodes_in_output_layer];
  min_output_value = new float[nodes_in_output_layer];

  for(j = 0; j < total_dimension; j++)
  {
    trigger = 1;
    // identify the minimum and maximum values for each dimension
    for(i = 0; i < sample_number; i++)
    {
      if(i == 0)
      {
	max = number_of_samples[i].data_in_sample[j];
	min = number_of_samples[i].data_in_sample[j];
	if((j >= (total_dimension - nodes_in_output_layer)))
	{
	  min_output_value[j - (total_dimension - nodes_in_output_layer)] = min;
	  max_output_value[j - (total_dimension - nodes_in_output_layer)] = max;
	}
      }
      else
      {
	if(number_of_samples[i].data_in_sample[j] < min)
	{
	  min = number_of_samples[i].data_in_sample[j];
	  if(j >= (total_dimension - nodes_in_output_layer))
	  {min_output_value[j - (total_dimension - nodes_in_output_layer)] = min;}
	}

	if(number_of_samples[i].data_in_sample[j] > max)
	{
	  max = number_of_samples[i].data_in_sample[j];
	  if(j >= (total_dimension - nodes_in_output_layer))
	  {max_output_value[j - (total_dimension - nodes_in_output_layer)] = max;}
	}
      }
    }

    imax = int(max_output_value[j - (total_dimension - nodes_in_output_layer)]);
    imin = int(min_output_value[j - (total_dimension - nodes_in_output_layer)]);
    rmax = max_output_value[j - (total_dimension - nodes_in_output_layer)];
    rmin = min_output_value[j - (total_dimension - nodes_in_output_layer)];

      if((imax == 1) && (imin == 0) && (rmax <= 1.0) && (rmin <= 0.0))
      {trigger = 0;}

      if((imax == 1) && (imin == 1) && (rmax <= 1.0) && (rmin <= 1.0))
      {trigger = 0;}

      if((imax == 0) && (imin == 0) && (rmax <= 0.0) && (rmin <= 0.0))
      {trigger = 0;}

    // normalize the values in each dimension of the signal
   if(trigger != 0)
   {
    for(i = 0; i < sample_number; i++)
    {number_of_samples[i].data_in_sample[j] = (number_of_samples[i].data_in_sample[j] - min) / (max - min);}
   }
  }
}

void Data_type :: acquire_net_info(int signal, int no_output_nodes)
{
  signal_dimensions = signal;
  nodes_in_output_layer = no_output_nodes;
}

void Data_type :: load_data_into_array(void)
{
  // open the file containing the data
  ifstream file_ptr;  // pointer to a file
  int i;
  file_ptr.open(filename, ios::in);

  // create dynamic array to hold the specified number of samples
  number_of_samples = new sample_data[sample_number];

  for(i = 0; i < sample_number; i++)
  // create a dynamic array to hold the dimensions of each signal
  {number_of_samples[i].data_in_sample = new float[signal_dimensions + nodes_in_output_layer];}

  int dimensions = signal_dimensions + nodes_in_output_layer;

  //read in data from file and place in array
  for(i = 0; i < sample_number; i++)
  {
    for(int j = 0; j < dimensions; j++)
    {file_ptr >> number_of_samples[i].data_in_sample[j];}
  }
  file_ptr.close();
  cout << "\n";
}

void Data_type::delete_signal_array(void)
{delete [] number_of_samples;}

class signal_data // Class for randomizing the input signals
{
  public:
  int signal_value;
  float signal_rank;
};

class Training : public Data_type   // Derived Class For Training Data
{
  public:
  void request_training_data(int net_no); // Function to request data for training
  int number_of_epochs;
  signal_data *signalpoint;
  float rate_of_learning; // learning rate constant used by the net
  char presentation_order; // determines fixed or random signal presentation
  void scramble_data_in_array(void);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -