| 212 | {} |
| 213 | |
| 214 | feature_vector_type make_psi ( |
| 215 | const sample_type& x, |
| 216 | const int label |
| 217 | ) const |
| 218 | /*! |
| 219 | ensures |
| 220 | - returns the vector PSI(x,label) |
| 221 | !*/ |
| 222 | { |
| 223 | // All we are doing here is taking x, which is a 3 dimensional sample vector in this |
| 224 | // example program, and putting it into one of 3 places in a 9 dimensional PSI |
| 225 | // vector, which we then return. So this function returns PSI(x,label). To see why |
| 226 | // we setup PSI like this, recall how predict_label() works. It takes in a 9 |
| 227 | // dimensional weight vector and breaks the vector into 3 pieces. Each piece then |
| 228 | // defines a different classifier and we use them in a one-vs-all manner to predict |
| 229 | // the label. So now that we are in the structural SVM code we have to define the |
| 230 | // PSI vector to correspond to this usage. That is, we need to setup PSI so that |
| 231 | // argmax_y dot(weights,PSI(x,y)) == predict_label(weights,x). This is how we tell |
| 232 | // the structural SVM solver what kind of problem we are trying to solve. |
| 233 | // |
| 234 | // It's worth emphasizing that the single biggest step in using a structural SVM is |
| 235 | // deciding how you want to represent PSI(x,label). It is always a vector, but |
| 236 | // deciding what to put into it to solve your problem is often not a trivial task. |
| 237 | // Part of the difficulty is that you need an efficient method for finding the label |
| 238 | // that makes dot(w,PSI(x,label)) the biggest. Sometimes this is easy, but often |
| 239 | // finding the max scoring label turns into a difficult combinatorial optimization |
| 240 | // problem. So you need to pick a PSI that doesn't make the label maximization step |
| 241 | // intractable but also still well models your problem. |
| 242 | // |
| 243 | // Finally, note that make_psi() is a helper routine we define in this example. In |
| 244 | // general, you are not required to implement it. That is, all you must implement |
| 245 | // are the four virtual functions defined below. |
| 246 | |
| 247 | |
| 248 | // So let's make an empty 9-dimensional PSI vector |
| 249 | feature_vector_type psi(get_num_dimensions()); |
| 250 | psi = 0; // zero initialize it |
| 251 | |
| 252 | // Now put a copy of x into the right place in PSI according to its label. So for |
| 253 | // example, if label is 1 then psi would be: [0 0 0 x(0) x(1) x(2) 0 0 0] |
| 254 | if (label == 0) |
| 255 | set_rowm(psi,range(0,2)) = x; |
| 256 | else if (label == 1) |
| 257 | set_rowm(psi,range(3,5)) = x; |
| 258 | else // the label must be 2 |
| 259 | set_rowm(psi,range(6,8)) = x; |
| 260 | |
| 261 | return psi; |
| 262 | } |
| 263 | |
| 264 | // We need to declare the dimensionality of the PSI vector (this is also the |
| 265 | // dimensionality of the weight vector we are learning). Similarly, we need to declare |