00001 // This is mul/clsfy/clsfy_random_classifier.h 00002 #ifndef clsfy_random_classifier_h_ 00003 #define clsfy_random_classifier_h_ 00004 // Copyright: (C) 2001 British Telecommunications plc 00005 #ifdef VCL_NEEDS_PRAGMA_INTERFACE 00006 #pragma interface 00007 #endif 00008 //: 00009 // \file 00010 // \brief Describe a random classifier 00011 // \author Ian Scott 00012 // \date 2000/05/10 00013 // \verbatim 00014 // Modifications 00015 // 2 May 2001 IMS Converted to VXL 00016 // \endverbatim 00017 00018 #include "clsfy_classifier_base.h" 00019 #include <vcl_vector.h> 00020 #include <vnl/vnl_vector.h> 00021 #include <vnl/vnl_random.h> 00022 #include <vsl/vsl_binary_io.h> 00023 00024 //: A common interface for 1-out-of-N classifiers 00025 // This class takes a vector and classifies into one of 00026 // N classes. 00027 // 00028 // And derived classes with binary in the name indicates that 00029 // the classifier works with only two classes, 0 and 1. 00030 00031 class clsfy_random_classifier : public clsfy_classifier_base 00032 { 00033 public: 00034 // Dflt constructor 00035 clsfy_random_classifier(); 00036 00037 // Destructor 00038 virtual ~clsfy_random_classifier() {} 00039 00040 //: Return the probability the input being in each class. 00041 // output(i) i<nClasses, contains the probability that the input is in class i 00042 virtual void class_probabilities(vcl_vector<double> &outputs, const vnl_vector<double> &input) const; 00043 00044 //: Log likelihood of being in class (binary classifiers only) 00045 // class probability = 1 / (1+exp(-log_l)) 00046 // Operation of this method is undefined for multiclass classifiers 00047 virtual double log_l(const vnl_vector<double> &input) const; 00048 00049 //: The number of possible output classes. 00050 virtual unsigned n_classes() const; 00051 00052 //: The dimensionality of input vectors. 00053 virtual unsigned n_dims() const; 00054 00055 //: Name of the class 00056 virtual vcl_string is_a() const; 00057 00058 //: Name of the class 00059 virtual bool is_class(vcl_string const& s) const; 00060 00061 //: Create a copy on the heap and return base class pointer 00062 virtual clsfy_classifier_base* clone() const; 00063 00064 //: Print class to os 00065 virtual void print_summary(vcl_ostream& os) const; 00066 00067 //: Save class to binary file stream 00068 virtual void b_write(vsl_b_ostream& bfs) const; 00069 00070 //: Load class from binary file stream 00071 virtual void b_read(vsl_b_istream& bfs); 00072 00073 //: The probabilities of returning a value in each class. 00074 const vcl_vector<double> & probs() const; 00075 00076 //: Set the prior probabilities of each class 00077 // The values are normalised to sum to 1. 00078 void set_probs(const vcl_vector<double> &); 00079 00080 //: Set the number of dimensions the classifier reports that it uses. 00081 // The classifier itself pays no attention to this value, but it 00082 // may be useful for other error checking code which calls n_dims() 00083 void set_n_dims(unsigned); 00084 00085 //: The mean confidence noise added to class probabilities. 00086 double confidence() const; 00087 00088 //: Set the mean of confidence noise added to class probabilities. 00089 // If the value is 0.0, then the probability of the winning class, will 00090 // only be just large enough to guarantee the win. Larger values will allow 00091 // more confident winning class probabilities - however each actual 00092 // confidence increase is random. 00093 void set_confidence(double); 00094 00095 //: Reseeds the internal random number generator 00096 // To achieve quasi-random initialisation use 00097 // \code 00098 // #include <vcl_ctime.h> 00099 // .. 00100 // sampler.reseed(vcl_time(0)); 00101 // \endcode 00102 virtual void reseed(unsigned long); 00103 00104 private: 00105 //: Calculate the minimum value each class probability needs to be biased by to win 00106 void calc_min_to_win(); 00107 00108 //: The probabilities of each class. 00109 // The values will always sum to 1. 00110 // If the vector is empty then the builder will use the prior probability 00111 vcl_vector<double> probs_; 00112 00113 //: The mean confidence noise added to class probabilities 00114 double confidence_; 00115 00116 //: The classifier may get asked this 00117 unsigned n_dims_; 00118 00119 //: Give the same answers if the same vector is presented twice in a row 00120 mutable vnl_vector<double> last_inputs_; 00121 00122 //: The last class probabilities calculated. 00123 mutable vcl_vector<double> last_outputs_; 00124 00125 //: The random number generator used to sample classes. 00126 mutable vnl_random rng_; 00127 00128 //: The minimum value each class probability needs to be biased by to win. 00129 vcl_vector<double> min_to_win_; 00130 }; 00131 00132 #endif // clsfy_random_classifier_h_
1.5.1