Ejemplo n.º 1
0
void Perceptron::set_parameters(const Vector<double>& new_parameters)
{
   const size_t inputs_number = get_inputs_number();

   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t size = new_parameters.size();

   if(size != 1+inputs_number)
   {
	  std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n"
             << "void set_parameters(const Vector<double>&) method.\n"
             << "Size must be equal to one plus number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   bias = new_parameters[0];

   for(size_t i = 0; i < inputs_number; i++)
   {
      synaptic_weights[i] = new_parameters[i+1];
   }
}
Ejemplo n.º 2
0
void Perceptron::set_synaptic_weights(const Vector<double>& new_synaptic_weights)
{
   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t inputs_number = get_inputs_number();

   if(new_synaptic_weights.size() != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n"
             << "void set_synaptic_weights(const Vector<double>&) method.\n"
             << "Size of synaptic weights vector must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   // Set synaptic weights
   
   synaptic_weights = new_synaptic_weights;
}
Ejemplo n.º 3
0
void Perceptron::set_synaptic_weight(const size_t& synaptic_weight_index, const double& new_synaptic_weight)
{
   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t inputs_number = get_inputs_number();

   if(synaptic_weight_index >= inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n"
             << "void set_synaptic_weight(const size_t&, const double&) method.\n"
             << "Index of synaptic weight must be less than number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   // Set single synaptic weight

   synaptic_weights[synaptic_weight_index] = new_synaptic_weight;
}
Ejemplo n.º 4
0
Matrix<double> Perceptron::calculate_Hessian(const Vector<double>& inputs) const
{   
   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t inputs_number = get_inputs_number();
   const size_t inputs_size = inputs.size();

   if(inputs_size != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "Matrix<double> calculate_Hessian(const Vector<double>&) const method.\n"
             << "Size of inputs must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   const double combination = calculate_combination(inputs);
   const double activation_second_derivative = calculate_activation_second_derivative(combination);

   return(synaptic_weights.direct(synaptic_weights)*activation_second_derivative);
}
Ejemplo n.º 5
0
std::string Perceptron::write_expression(const Vector<std::string>& inputs_name, const std::string& output_name) const
{
   const size_t inputs_number = get_inputs_number();

   std::string activation_function_name = write_activation_function();

   std::ostringstream buffer;

   buffer << output_name << "=" << activation_function_name << "("
          << bias << "\n";

   for(size_t i = 0; i < inputs_number; i++)
   {
      if(synaptic_weights[i] >= 0)
      {
         buffer << "+";	     
      }

      buffer << synaptic_weights[i] << "*" << inputs_name[i];

      if(i != 0 && i%4 == 0 && i != inputs_number-1)
      {
            buffer << "\n";
      }
   }

   buffer << ");\n";

   return(buffer.str());
}
Ejemplo n.º 6
0
double Perceptron::calculate_output(const Vector<double>& inputs) const
{
   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t size = inputs.size();
   const size_t inputs_number = get_inputs_number();

   if(size != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "double calculate_output(const Vector<double>&) const method.\n"
             << "Size must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   // Calculate outputs 

   return(calculate_activation(calculate_combination(inputs)));  
}
Ejemplo n.º 7
0
Matrix<double> Perceptron::calculate_combination_Hessian(const Vector<double>&) const
{
   const size_t inputs_number = get_inputs_number();

   const Matrix<double> combination_Hessian(inputs_number, inputs_number, 0.0); 

   return(combination_Hessian);   
}
Ejemplo n.º 8
0
double Perceptron::calculate_combination(const Vector<double>& inputs, const Vector<double>& parameters) const
{
    const size_t inputs_number = get_inputs_number();

   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   std::ostringstream buffer;

   const size_t inputs_size = inputs.size();

   if(inputs_size != inputs_number)
   {
      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "double calculate_combination(const Vector<double>&, const Vector<double>&) const method.\n"
             << "Size of inputs must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   const size_t parameters_size = parameters.size();

   const size_t parameters_number = count_parameters_number();

   if(parameters_size != parameters_number)
   {
      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "double calculate_combination(const Vector<double>&, const Vector<double>&) const method.\n"
             << "Size of potential parameters (" << parameters_size << ") must be equal to number of parameters (" << parameters_number << ").\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   // Modified for performance.

   double combination = parameters[0];

   for(size_t i = 0; i < inputs_number; i++)
   {
       combination += parameters[i+1]*inputs[i];
   }

   return(combination);
}
Ejemplo n.º 9
0
Vector<double> Perceptron::arrange_parameters(void) const
{
   const size_t parameters_number = count_parameters_number();

   Vector<double> parameters(parameters_number);

   parameters[0] = bias;

   const size_t inputs_number = get_inputs_number();

   for(size_t i = 0; i < inputs_number; i++)
   {
      parameters[(size_t)1+i] = synaptic_weights[i];
   }

   return(parameters); 
}
Ejemplo n.º 10
0
double Perceptron::calculate_combination(const Vector<double>& inputs) const
{
    const size_t inputs_number = get_inputs_number();

   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   if(inputs_number == 0)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "calculate_combination(const Vector<double>&) method.\n"
             << "Number of inputs must be greater than zero.\n";

      throw std::logic_error(buffer.str());
   }

   const size_t inputs_size = inputs.size();

   if(inputs_size != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n"
             << "double calculate_combination(const Vector<double>&) method.\n"
             << "Size of inputs (" << inputs_size << ") must be equal to number of inputs (" << inputs_number << ").\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   // Calculate combination

   double combination = bias;

   for(size_t i = 0; i < inputs_number; i++)
   {
       combination += synaptic_weights[i]*inputs[i];
   }


   return(combination);
}
Ejemplo n.º 11
0
Vector<double> Perceptron::calculate_gradient(const Vector<double>& inputs, const Vector<double>& parameters) const
{
   const size_t inputs_number = get_inputs_number();

   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t size = inputs.size();

   if(size != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "double calculate_gradient(const Vector<double>&, const Vector<double>&) const method.\n"
             << "Size must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   // Calculate parameters gradient

   const double combination = calculate_combination(inputs, parameters);

   const double activation_derivative = calculate_activation_derivative(combination);

   Vector<double> gradient(1+inputs_number);

   // Bias

   gradient[0] = activation_derivative;

   // Synaptic weights

   for(size_t i = 1; i < 1+inputs_number; i++)
   {
      gradient[i] = inputs[i-1]*activation_derivative;
   }

   return(gradient);
}
Ejemplo n.º 12
0
double Perceptron::calculate_output(const Vector<double>& inputs, const Vector<double>& parameters) const
{
   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t inputs_size = inputs.size();
   const size_t inputs_number = get_inputs_number();

   if(inputs_size != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "double calculate_output(const Vector<double>&, const Vector<double>&) const method.\n"
             << "Size of inputs must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   const size_t parameters_size = parameters.size();

   const size_t parameters_number = count_parameters_number();

   if(parameters_size != parameters_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "double calculate_output(const Vector<double>&, const Vector<double>&) const method.\n"
             << "Size of potential parameters (" << parameters_size << ") must be equal to number of parameters (" << parameters_number << ").\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   return(calculate_activation(calculate_combination(inputs, parameters)));
}
Ejemplo n.º 13
0
void Perceptron::prune_input(const size_t& index)
{
    // Control sentence (if debug)

    #ifdef __OPENNN_DEBUG__

    const size_t inputs_number = get_inputs_number();

    if(index >= inputs_number)
    {
       std::ostringstream buffer;

       buffer << "OpenNN Exception: Perceptron class.\n"
              << "void prune_input(const size_t&) method.\n"
              << "Index of input is equal or greater than number of inputs.\n";

       throw std::logic_error(buffer.str());
    }

    #endif

   synaptic_weights.erase(synaptic_weights.begin()+index);
}
Ejemplo n.º 14
0
size_t Perceptron::count_parameters_number(void) const
{
   const size_t inputs_number = get_inputs_number();
   
   return(1 + inputs_number);
}
Ejemplo n.º 15
0
Matrix<double> Perceptron::calculate_Hessian(const Vector<double>& inputs, const Vector<double>& parameters) const
{
   // Control sentence (if debug)

   #ifdef __OPENNN_DEBUG__ 

   const size_t inputs_size = inputs.size();
   const size_t inputs_number = get_inputs_number();

   if(inputs_size != inputs_number)
   {
      std::ostringstream buffer;

      buffer << "OpenNN Exception: Perceptron class.\n" 
             << "Matrix<double> calculate_Hessian(const Vector<double>&, const Vector<double>&) const method.\n"
             << "Size of inputs must be equal to number of inputs.\n";

      throw std::logic_error(buffer.str());
   }

   #endif

   const double combination = calculate_combination(inputs, parameters);
   const double activation_second_derivative = calculate_activation_second_derivative(combination);

   const size_t parameters_number = count_parameters_number();

   Matrix<double> Hessian(parameters_number, parameters_number);

   // Bias - bias derivative

   Hessian(0,0) = activation_second_derivative;

   // Bias - synaptic weight derivative

   for(size_t i = 1; i < parameters_number; i++)
   {
      Hessian(0,i) = activation_second_derivative*inputs[i-1];
   }

   // Synaptic weight -synaptic weight derivative

   for(size_t i = 1; i < parameters_number; i++)
   {
      for(size_t j = 1; j < parameters_number; j++)
      {
         Hessian(i,j) = activation_second_derivative*inputs[i-1]*inputs[j-1];
      }
   }

   // Hessian symmetry

   for(size_t i = 0; i < parameters_number; i++)
   {
      for(size_t j = 0; j < i; j++)
      {
         Hessian(i,j) = Hessian(j,i);
      }
   }
   
   return(Hessian);
}