00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #include "SMO.h"
00020
00021 using namespace std;
00022 using namespace Marsyas;
00023
00024 SMO::SMO(mrs_string name):MarSystem("SMO",name)
00025 {
00026
00027
00028
00029 addControls();
00030 }
00031
00032
00033 SMO::~SMO()
00034 {
00035 }
00036
00037
00038 MarSystem*
00039 SMO::clone() const
00040 {
00041 return new SMO(*this);
00042 }
00043
00044 void
00045 SMO::addControls()
00046 {
00047 addctrl("mrs_string/mode", "train", modePtr_);
00048 addctrl("mrs_natural/nLabels", 1, nlabelsPtr_);
00049 setctrlState("mrs_natural/nLabels", true);
00050 weights_.create(1);
00051 addctrl("mrs_realvec/weights", weights_, weightsPtr_);
00052 addctrl("mrs_bool/done", false, donePtr_);
00053 setctrlState("mrs_bool/done", true);
00054
00055 }
00056
00057
00058 void
00059 SMO::myUpdate(MarControlPtr sender)
00060 {
00061 (void) sender;
00062 MRSDIAG("SMO.cpp - SMO:myUpdate");
00063
00064 ctrl_onSamples_->setValue(ctrl_inSamples_, NOUPDATE);
00065 ctrl_onObservations_->setValue(2, NOUPDATE);
00066 ctrl_osrate_->setValue(ctrl_israte_, NOUPDATE);
00067
00068
00069 mrs_natural inObservations = ctrl_inObservations_->to<mrs_natural>();
00070
00071
00072
00073
00074 mrs_natural mcols = (getctrl("mrs_realvec/weights")->to<mrs_realvec>()).getCols();
00075 mrs_natural ncols = weights_.getCols();
00076
00077
00078
00079 if (inObservations != mcols)
00080 {
00081 weights_.create(inObservations);
00082 updControl("mrs_realvec/weights", weights_);
00083 }
00084
00085
00086 if (inObservations != ncols)
00087 {
00088 weights_.create(inObservations);
00089 }
00090
00091 mrs_string mode = getctrl("mrs_string/mode")->to<mrs_string>();
00092 if (mode == "predict")
00093 {
00094 weights_ = getctrl("mrs_realvec/weights")->to<mrs_realvec>();
00095 }
00096 }
00097
00098
00099 void
00100 SMO::myProcess(realvec& in, realvec& out)
00101 {
00102 mrs_natural t,o;
00103 mrs_string mode = modePtr_->to<mrs_string>();
00104 mrs_natural prediction = 0;
00105 mrs_real label;
00106 mrs_real thres;
00107
00108 if (mode == "train")
00109 {
00110 for (t = 0; t < inSamples_; t++)
00111 {
00112 label = in(inObservations_-1, t);
00113 out(0,t) = (mrs_real) label;
00114 out(1,t) = (mrs_real) label;
00115 }
00116
00117 weights_(0) = 0.4122;
00118 weights_(1) = -4.599;
00119 weights_(2) = -14.0203;
00120 weights_(3) = -6.2503;
00121 weights_(4) = -0.8447;
00122 weights_(5) = -2.0753;
00123 weights_(6) = 0.9826;
00124 weights_(7) = -4.1159;
00125 weights_(8) = -1.6985;
00126 weights_(9) = -1.1419;
00127 weights_(10) = 3.5605;
00128 weights_(11) = 1.9987;
00129 weights_(12) = 1.3641;
00130 weights_(13) = -6.412;
00131 weights_(14) = 7.7704;
00132 weights_(15) = 0.6565;
00133 weights_(16) = -0.3749;
00134 weights_(17) = -0.3507;
00135 weights_(18) = 2.5022;
00136 weights_(19) = 0.8658;
00137 weights_(20) = -2.6361;
00138 weights_(21) = 3.9029;
00139 weights_(22) = 0.4051;
00140 weights_(23) = -2.8185;
00141 weights_(24) = 2.4864;
00142 weights_(25) = -1.8054;
00143 weights_(26) = -2.7731;
00144 weights_(27) = 2.2423;
00145 weights_(28) = -2.1786;
00146 weights_(29) = -1.0741;
00147 weights_(30) = -0.5614;
00148 weights_(31) = -3.5967;
00149 weights_(32) = 7.7832;
00150
00151
00152
00153
00154
00155
00156
00157
00158
00159
00160
00161
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216
00217
00218 }
00219
00220
00221
00222 if (mode == "predict")
00223 {
00224 for (t = 0; t < inSamples_; t++)
00225 {
00226 label = in(inObservations_-1, t);
00227 thres = 0.0;
00228 for (o = 0; o < inObservations_-1; o++)
00229 {
00230
00231 thres += (weights_(o) * in(o,t));
00232 }
00233 thres += weights_(inObservations_-1);
00234
00235 if (thres <= 0 )
00236 {
00237 prediction = 0;
00238 }
00239
00240 else
00241 {
00242 prediction = 1;
00243 }
00244
00245 out(0,t) = (mrs_real) prediction;
00246 out(1,t) = (mrs_real) label;
00247 }
00248
00249
00250 }
00251
00252 if (donePtr_->to<mrs_bool>())
00253 {
00254 updControl(weightsPtr_, weights_);
00255 }
00256
00257
00258 }
00259
00260
00261
00262
00263
00264
00265
00266
00267
00268
00269
00270