16 #include "probabilistic_layer.h"
40 set(new_probabilistic_neurons_number);
52 set(other_probabilistic_layer);
74 if(
this != &other_probabilistic_layer)
143 return(
"Competitive");
151 return(
"NoProbabilistic");
155 std::ostringstream buffer;
157 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
158 <<
"std::string write_probabilistic_method(void) const method.\n"
159 <<
"Unknown probabilistic method.\n";
161 throw std::logic_error(buffer.str());
175 return(
"competitive");
183 return(
"no probabilistic");
187 std::ostringstream buffer;
189 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
190 <<
"std::string write_probabilistic_method_text(void) const method.\n"
191 <<
"Unknown probabilistic method.\n";
193 throw std::logic_error(buffer.str());
298 if(new_probabilistic_method ==
"Competitive")
302 else if(new_probabilistic_method ==
"Softmax")
306 else if(new_probabilistic_method ==
"NoProbabilistic")
312 std::ostringstream buffer;
314 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
315 <<
"void set_probabilistic_method(const std::string&) method.\n"
316 <<
"Unknown probabilistic method: " << new_probabilistic_method <<
".\n";
318 throw std::logic_error(buffer.str());
349 std::ostringstream buffer;
351 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
352 <<
"void prune_probabilistic_neuron(void) method.\n"
353 <<
"Number of probabilistic neurons is zero.\n";
355 throw std::logic_error(buffer.str());
394 std::ostringstream buffer;
396 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
397 <<
"void initialize_random(void) method.\n"
398 <<
"Unknown probabilistic method.\n";
400 throw std::logic_error(buffer.str());
419 const size_t size = inputs.size();
423 std::ostringstream buffer;
425 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
426 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
427 <<
"Size must be equal to number of probabilistic neurons.\n";
429 throw std::logic_error(buffer.str());
448 case NoProbabilistic:
456 std::ostringstream buffer;
458 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
459 <<
"Vector<double> calculate_outputs(const Vector<double>&) const method.\n"
460 <<
"Unknown probabilistic method.\n";
462 throw std::logic_error(buffer.str());
483 const size_t size = inputs.size();
487 std::ostringstream buffer;
489 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
490 <<
"Matrix<double> calculate_Jacobian(const Vector<double>&) const method.\n"
491 <<
"Size must be equal to number of probabilistic neurons.\n";
493 throw std::logic_error(buffer.str());
512 case NoProbabilistic:
520 std::ostringstream buffer;
522 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
523 <<
"Matrix<double> calculate_Jacobian(const Vector<double>&) const method.\n"
524 <<
"Unknown probabilistic method.\n";
526 throw std::logic_error(buffer.str());
546 const size_t size = inputs.size();
550 std::ostringstream buffer;
552 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
553 <<
"Matrix<double> calculate_Hessian_form(const Vector<double>&) const method.\n"
554 <<
"Size must be equal to number of probabilistic neurons.\n";
556 throw std::logic_error(buffer.str());
575 case NoProbabilistic:
583 std::ostringstream buffer;
585 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
586 <<
"Matrix<double> calculate_Hessian_form(const Vector<double>&) const method.\n"
587 <<
"Unknown probabilistic method.\n";
589 throw std::logic_error(buffer.str());
614 std::ostringstream buffer;
616 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
617 <<
"Matrix<double> calculate_competitive_Jacobian(const Vector<double>&) const method.\n"
618 <<
"The competitive function is not derivable.\n";
620 throw std::logic_error(buffer.str());
630 std::ostringstream buffer;
632 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
633 <<
"Vector< Matrix<double> > calculate_competitive_Hessian_form(const Vector<double>&) const method.\n"
634 <<
"The competitive function is not derivable.\n";
636 throw std::logic_error(buffer.str());
668 probabilistic_Jacobian(i,i) = outputs[i]*(1.0 - outputs[i]);
672 probabilistic_Jacobian(i,j) = -outputs[i]*outputs[j];
677 return(probabilistic_Jacobian);
691 return(Hessian_form);
732 return(Hessian_form);
742 std::ostringstream buffer;
744 buffer <<
"Probabilistic layer\n"
747 <<
"Display: "<<
display <<
"\n";
749 return(buffer.str());
760 std::ostringstream buffer;
762 tinyxml2::XMLDocument* document =
new tinyxml2::XMLDocument;
764 tinyxml2::XMLElement* root_element = document->NewElement(
"ProbabilisticLayer");
766 document->InsertFirstChild(root_element);
768 tinyxml2::XMLElement* element = NULL;
769 tinyxml2::XMLText* text = NULL;
773 element = document->NewElement(
"ProbabilisticNeuronsNumber");
774 root_element->LinkEndChild(element);
779 text = document->NewText(buffer.str().c_str());
780 element->LinkEndChild(text);
785 element = document->NewElement(
"ProbabilisticMethod");
786 root_element->LinkEndChild(element);
789 element->LinkEndChild(text);
794 element = document->NewElement(
"Display");
795 root_element->LinkEndChild(element);
800 text = document->NewText(buffer.str().c_str());
801 element->LinkEndChild(text);
815 std::ostringstream buffer;
817 const tinyxml2::XMLElement* probabilistic_layer_element = document.FirstChildElement(
"ProbabilisticLayer");
819 if(!probabilistic_layer_element)
821 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
822 <<
"void from_XML(const tinyxml2::XMLDocument&) method.\n"
823 <<
"Probabilistic layer element is NULL.\n";
825 throw std::logic_error(buffer.str());
830 const tinyxml2::XMLElement* element = probabilistic_layer_element->FirstChildElement(
"ProbabilisticNeuronsNumber");
834 const char* text = element->GetText();
842 catch(
const std::logic_error& e)
844 std::cout << e.what() << std::endl;
852 const tinyxml2::XMLElement* element = probabilistic_layer_element->FirstChildElement(
"ProbabilisticMethod");
856 const char* text = element->GetText();
862 std::string new_probabilistic_method(text);
866 catch(
const std::logic_error& e)
868 std::cout << e.what() << std::endl;
876 const tinyxml2::XMLElement* display_element = probabilistic_layer_element->FirstChildElement(
"Display");
880 std::string new_display_string = display_element->GetText();
886 catch(
const std::logic_error& e)
888 std::cout << e.what() << std::endl;
903 std::ostringstream buffer;
907 buffer << outputs_names[i] <<
"= Competitive(" << inputs_names.
to_string(
",") <<
");\n";
910 return(buffer.str());
922 std::ostringstream buffer;
926 buffer << outputs_names[i] <<
"= Softmax(" << inputs_names.
to_string(
",") <<
");\n";
929 return(buffer.str());
941 std::ostringstream buffer;
945 buffer << outputs_names[i] <<
" = " << inputs_names.
to_string() <<
";\n";
948 return(buffer.str());
975 case NoProbabilistic:
983 std::ostringstream buffer;
985 buffer <<
"OpenNN Exception: ProbabilisticLayer class.\n"
986 <<
"std::string write_expression(const Vector<std::string>&, const Vector<std::string>&) const method.\n"
987 <<
"Unknown probabilistic method.\n";
989 throw std::logic_error(buffer.str());
void set_display(const bool &)
std::string write_competitive_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< Matrix< double > > calculate_no_probabilistic_Hessian_form(const Vector< double > &) const
std::string write_softmax_expression(const Vector< std::string > &, const Vector< std::string > &) const
ProbabilisticMethod probabilistic_method
Probabilistic processing method.
bool operator==(const ProbabilisticLayer &) const
Vector< Matrix< double > > calculate_Hessian_form(const Vector< double > &) const
const ProbabilisticMethod & get_probabilistic_method(void) const
bool display
Display messages to screen.
std::string write_probabilistic_method(void) const
void prune_probabilistic_neuron(void)
std::string write_no_probabilistic_expression(const Vector< std::string > &, const Vector< std::string > &) const
std::string to_string(void) const
Returns a string representation of the current probabilistic layer object.
Vector< double > calculate_outputs(const Vector< double > &) const
Vector< T > calculate_softmax(void) const
Matrix< double > calculate_no_probabilistic_Jacobian(const Vector< double > &) const
virtual void set_default(void)
virtual tinyxml2::XMLDocument * to_XML(void) const
size_t probabilistic_neurons_number
Number of probabilistic neurons in the layer.
void initialize_random(void)
Initializes at random the probabilistic method.
const size_t & get_probabilistic_neurons_number(void) const
Returns the number of probabilistic neurons in the layer.
Vector< double > calculate_competitive_output(const Vector< double > &) const
void set_probabilistic_method(const ProbabilisticMethod &)
ProbabilisticLayer & operator=(const ProbabilisticLayer &)
std::string write_probabilistic_method_text(void) const
void initialize_identity(void)
const bool & get_display(void) const
void set_probabilistic_neurons_number(const size_t &)
Vector< T > calculate_competitive(void) const
virtual void from_XML(const tinyxml2::XMLDocument &)
Vector< double > calculate_no_probabilistic_output(const Vector< double > &) const
Matrix< double > calculate_softmax_Jacobian(const Vector< double > &) const
std::string write_expression(const Vector< std::string > &, const Vector< std::string > &) const
Vector< double > calculate_softmax_output(const Vector< double > &) const
virtual ~ProbabilisticLayer(void)
Vector< Matrix< double > > calculate_competitive_Hessian_form(const Vector< double > &) const
This method throws an exception, since the competitive function is not derivable. ...
std::string to_string(const std::string &=" ") const
Returns a string representation of this vector.
ProbabilisticMethod
Enumeration of available methods for interpreting variables as probabilities.
Vector< Matrix< double > > calculate_softmax_Hessian_form(const Vector< double > &) const
Matrix< double > calculate_Jacobian(const Vector< double > &) const
Matrix< double > calculate_competitive_Jacobian(const Vector< double > &) const
This method throws an exception, since the competitive function is not derivable. ...