00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020 #include <cstdio>
00021 #include <cstdlib>
00022 #include <string>
00023
00024 #include <boost/test/unit_test.hpp>
00025
00026 #include "Hildreth.h"
00027 #include "Optimiser.h"
00028 #include "ScoreComponentCollection.h"
00029
00030 using namespace std;
00031 using namespace Moses;
00032 using namespace Mira;
00033
00034 namespace MosesTest
00035 {
00036
00037 class MockSingleFeature : public StatelessFeatureFunction
00038 {
00039 public:
00040 MockSingleFeature(): StatelessFeatureFunction("MockSingle",1) {}
00041 std::string GetScoreProducerWeightShortName(unsigned) const {
00042 return "sf";
00043 }
00044 };
00045
00046 class MockMultiFeature : public StatelessFeatureFunction
00047 {
00048 public:
00049 MockMultiFeature(): StatelessFeatureFunction("MockMulti",5) {}
00050 std::string GetScoreProducerWeightShortName(unsigned) const {
00051 return "mf";
00052 }
00053 };
00054
00055 class MockSparseFeature : public StatelessFeatureFunction
00056 {
00057 public:
00058 MockSparseFeature(): StatelessFeatureFunction("MockSparse", ScoreProducer::unlimited) {}
00059 std::string GetScoreProducerWeightShortName(unsigned) const {
00060 return "sf";
00061 }
00062 };
00063
00064 struct MockProducers {
00065 MockProducers() {}
00066
00067 MockSingleFeature single;
00068 MockMultiFeature multi;
00069 MockSparseFeature sparse;
00070 };
00071
00072
00073
00074 BOOST_AUTO_TEST_SUITE(hildreth_test)
00075
00076 BOOST_FIXTURE_TEST_CASE(test_hildreth_1, MockProducers)
00077 {
00078
00079 cerr << "\n>>>>>Hildreth test, without slack and with 0.01 slack" << endl << endl;
00080 vector< ScoreComponentCollection> featureValueDiffs;
00081 vector< float> lossMinusModelScoreDiff;
00082
00083
00084 float w[] = { 1, 1, 1, 1, 0 };
00085 vector<float> vec(w,w+5);
00086 ScoreComponentCollection weights;
00087 weights.PlusEquals(&multi, vec);
00088
00089
00090
00091
00092
00093
00094
00095 ScoreComponentCollection s1, s2, s3;
00096 float arr1[] = { 0, 0, -2.0672, 0, 0 };
00097 float arr2[] = { 0, 0, 0, 0, 0 };
00098 float arr3[] = { 0, 0, -2.0672, 0, 0 };
00099
00100 float loss1 = 2.34085;
00101 float loss2 = 0;
00102 float loss3 = 2.34085;
00103
00104 vector<float> vec1(arr1,arr1+5);
00105 vector<float> vec2(arr2,arr2+5);
00106 vector<float> vec3(arr3,arr3+5);
00107
00108 s1.PlusEquals(&multi,vec1);
00109 s2.PlusEquals(&multi,vec2);
00110 s3.PlusEquals(&multi,vec3);
00111
00112 featureValueDiffs.push_back(s1);
00113 featureValueDiffs.push_back(s2);
00114 featureValueDiffs.push_back(s3);
00115
00116 cerr << "feature value diff: " << featureValueDiffs[0] << endl;
00117 cerr << "feature value diff: " << featureValueDiffs[1] << endl;
00118 cerr << "feature value diff: " << featureValueDiffs[2] << endl << endl;
00119
00120 float oldModelScoreDiff1 = featureValueDiffs[0].InnerProduct(weights);
00121 float oldModelScoreDiff2 = featureValueDiffs[1].InnerProduct(weights);
00122 float oldModelScoreDiff3 = featureValueDiffs[2].InnerProduct(weights);
00123
00124 cerr << "model score diff: " << oldModelScoreDiff1 << ", loss: " << loss1 << endl;
00125 cerr << "model score diff: " << oldModelScoreDiff2 << ", loss: " << loss2 << endl;
00126 cerr << "model score diff: " << oldModelScoreDiff3 << ", loss: " << loss3 << endl << endl;
00127
00128 lossMinusModelScoreDiff.push_back(loss1 - oldModelScoreDiff1);
00129 lossMinusModelScoreDiff.push_back(loss2 - oldModelScoreDiff2);
00130 lossMinusModelScoreDiff.push_back(loss3 - oldModelScoreDiff3);
00131
00132 vector< float> alphas1 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff);
00133 vector< float> alphas2 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff, 0.01);
00134
00135 cerr << "\nalphas without slack:" << endl;
00136 for (size_t i = 0; i < alphas1.size(); ++i) {
00137 cerr << "alpha " << i << ": " << alphas1[i] << endl;
00138 }
00139 cerr << endl;
00140
00141 cerr << "partial updates:" << endl;
00142 vector< ScoreComponentCollection> featureValueDiffs1(featureValueDiffs);
00143 FVector totalUpdate1 = ScoreComponentCollection::CreateFVector();
00144 for (size_t k = 0; k < featureValueDiffs1.size(); ++k) {
00145 featureValueDiffs1[k].MultiplyEquals(alphas1[k]);
00146 cerr << k << ": " << featureValueDiffs1[k].GetScoresVector() << endl;
00147 FVector update = featureValueDiffs1[k].GetScoresVector();
00148 totalUpdate1 += update;
00149 }
00150 cerr << endl;
00151 cerr << "total update: " << totalUpdate1 << endl << endl;
00152
00153 ScoreComponentCollection weightsUpdate1(weights);
00154 weightsUpdate1.PlusEquals(totalUpdate1);
00155 cerr << "new weights: " << weightsUpdate1 << endl << endl;
00156
00157 float newModelScoreDiff1 = featureValueDiffs[0].InnerProduct(weightsUpdate1);
00158 float newModelScoreDiff2 = featureValueDiffs[1].InnerProduct(weightsUpdate1);
00159 float newModelScoreDiff3 = featureValueDiffs[2].InnerProduct(weightsUpdate1);
00160
00161 cerr << "new model score diff: " << newModelScoreDiff1 << ", loss: " << loss1 << endl;
00162 cerr << "new model score diff: " << newModelScoreDiff2 << ", loss: " << loss2 << endl;
00163 cerr << "new model score diff: " << newModelScoreDiff3 << ", loss: " << loss3 << endl;
00164
00165 cerr << "\n\nalphas with slack 0.01:" << endl;
00166 for (size_t i = 0; i < alphas2.size(); ++i) {
00167 cerr << "alpha " << i << ": " << alphas2[i] << endl;
00168 }
00169 cerr << endl;
00170
00171 cerr << "partial updates:" << endl;
00172 vector< ScoreComponentCollection> featureValueDiffs2(featureValueDiffs);
00173 FVector totalUpdate2 = ScoreComponentCollection::CreateFVector();
00174 for (size_t k = 0; k < featureValueDiffs2.size(); ++k) {
00175 featureValueDiffs2[k].MultiplyEquals(alphas2[k]);
00176 cerr << k << ": " << featureValueDiffs2[k].GetScoresVector() << endl;
00177 FVector update = featureValueDiffs2[k].GetScoresVector();
00178 totalUpdate2 += update;
00179 }
00180 cerr << endl;
00181 cerr << "total update: " << totalUpdate2 << endl << endl;
00182
00183 ScoreComponentCollection weightsUpdate2(weights);
00184 weightsUpdate2.PlusEquals(totalUpdate2);
00185 cerr << "new weights: " << weightsUpdate2 << endl << endl;
00186
00187 float newModelScoreDiff4 = featureValueDiffs[0].InnerProduct(weightsUpdate2);
00188 float newModelScoreDiff5 = featureValueDiffs[1].InnerProduct(weightsUpdate2);
00189 float newModelScoreDiff6 = featureValueDiffs[2].InnerProduct(weightsUpdate2);
00190
00191 cerr << "new model score diff: " << newModelScoreDiff4 << ", loss: " << loss1 << endl;
00192 cerr << "new model score diff: " << newModelScoreDiff5 << ", loss: " << loss2 << endl;
00193 cerr << "new model score diff: " << newModelScoreDiff6 << ", loss: " << loss3 << endl;
00194 }
00195
00196
00197 BOOST_FIXTURE_TEST_CASE(test_hildreth_3, MockProducers)
00198 {
00199
00200 cerr << "\n>>>>>Hildreth test, without slack and with 0.01 slack" << endl << endl;
00201 vector< ScoreComponentCollection> featureValueDiffs;
00202 vector< float> lossMinusModelScoreDiff;
00203
00204
00205 float w[] = { 1, 1, 0.638672, 1, 0 };
00206 vector<float> vec(w,w+5);
00207 ScoreComponentCollection weights;
00208 weights.PlusEquals(&multi, vec);
00209
00210 int numberOfConstraints = 21;
00211
00212
00213
00214 ScoreComponentCollection s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, s21;
00215 float arr1[] = { 0, 0, -2.0672, 0, 0 };
00216 float arr2[] = { 0, 0, 0, 0, 0 };
00217 float arr3[] = { 0, 0, -2.08436, 1.38629, 0 };
00218 float arr4[] = { 0, 0, -0.0171661, 1.38629, 0 };
00219 float arr5[] = { 0, 0, 4.4283, 0, 0 };
00220 float arr6[] = { 0, 0, 3.84829, 1.38629, 0 };
00221 float arr7[] = { 0, 0, 6.83689, 0, 0 };
00222 float arr8[] = { 0, 0, 0, 0, 0 };
00223 float arr9[] = { 0, 0, -2.0672, 0, 0 };
00224 float arr10[] = { 0, 0, -0.0171661, 1.38629, 0 };
00225 float arr11[] = { 0, 0, -2.08436, 1.38629, 0 };
00226 float arr12[] = { 0, 0, 4.4283, 0, 0 };
00227 float arr13[] = { 3, 0, 2.41089, 0, 0 };
00228 float arr14[] = { 3, 0, 2.32709, 0, 0 };
00229 float arr15[] = { 0, 0, -2.0672, 0, 0 };
00230 float arr16[] = { 0, 0, -2.08436, 1.38629, 0 };
00231 float arr17[] = { 0, 0, 4.4283, 0, 0 };
00232 float arr18[] = { 0, 0, 3.84829, 1.38629, 0 };
00233 float arr19[] = { 0, 0, -0.0171661, 1.38629, 0 };
00234 float arr20[] = { 0, 0, 0, 0, 0 };
00235 float arr21[] = { 0, 0, 6.83689, 0, 0 };
00236
00237 vector<float> losses;
00238 losses.push_back(2.73485);
00239 losses.push_back(0);
00240 losses.push_back(3.64118);
00241 losses.push_back(1.47347);
00242 losses.push_back(3.64118);
00243 losses.push_back(4.16278);
00244 losses.push_back(3.13952);
00245 losses.push_back(0);
00246 losses.push_back(2.73485);
00247 losses.push_back(1.47347);
00248 losses.push_back(3.64118);
00249 losses.push_back(3.64118);
00250 losses.push_back(2.51662);
00251 losses.push_back(2.73485);
00252 losses.push_back(2.73485);
00253 losses.push_back(3.64118);
00254 losses.push_back(3.64118);
00255 losses.push_back(4.16278);
00256 losses.push_back(1.47347);
00257 losses.push_back(0);
00258 losses.push_back(3.13952);
00259
00260 vector<float> vec1(arr1,arr1+5);
00261 vector<float> vec2(arr2,arr2+5);
00262 vector<float> vec3(arr3,arr3+5);
00263 vector<float> vec4(arr4,arr4+5);
00264 vector<float> vec5(arr5,arr5+5);
00265 vector<float> vec6(arr6,arr6+5);
00266 vector<float> vec7(arr7,arr7+5);
00267 vector<float> vec8(arr8,arr8+5);
00268 vector<float> vec9(arr9,arr9+5);
00269 vector<float> vec10(arr10,arr10+5);
00270 vector<float> vec11(arr11,arr11+5);
00271 vector<float> vec12(arr12,arr12+5);
00272 vector<float> vec13(arr13,arr13+5);
00273 vector<float> vec14(arr14,arr14+5);
00274 vector<float> vec15(arr15,arr15+5);
00275 vector<float> vec16(arr16,arr16+5);
00276 vector<float> vec17(arr17,arr17+5);
00277 vector<float> vec18(arr18,arr18+5);
00278 vector<float> vec19(arr19,arr19+5);
00279 vector<float> vec20(arr20,arr20+5);
00280 vector<float> vec21(arr21,arr21+5);
00281
00282 s1.PlusEquals(&multi,vec1);
00283 s2.PlusEquals(&multi,vec2);
00284 s3.PlusEquals(&multi,vec3);
00285 s4.PlusEquals(&multi,vec4);
00286 s5.PlusEquals(&multi,vec5);
00287 s6.PlusEquals(&multi,vec6);
00288 s7.PlusEquals(&multi,vec7);
00289 s8.PlusEquals(&multi,vec8);
00290 s9.PlusEquals(&multi,vec9);
00291 s10.PlusEquals(&multi,vec10);
00292 s11.PlusEquals(&multi,vec11);
00293 s12.PlusEquals(&multi,vec12);
00294 s13.PlusEquals(&multi,vec13);
00295 s14.PlusEquals(&multi,vec14);
00296 s15.PlusEquals(&multi,vec15);
00297 s16.PlusEquals(&multi,vec16);
00298 s17.PlusEquals(&multi,vec17);
00299 s18.PlusEquals(&multi,vec18);
00300 s19.PlusEquals(&multi,vec19);
00301 s20.PlusEquals(&multi,vec20);
00302 s21.PlusEquals(&multi,vec21);
00303
00304 featureValueDiffs.push_back(s1);
00305 featureValueDiffs.push_back(s2);
00306 featureValueDiffs.push_back(s3);
00307 featureValueDiffs.push_back(s4);
00308 featureValueDiffs.push_back(s5);
00309 featureValueDiffs.push_back(s6);
00310 featureValueDiffs.push_back(s7);
00311 featureValueDiffs.push_back(s8);
00312 featureValueDiffs.push_back(s9);
00313 featureValueDiffs.push_back(s10);
00314 featureValueDiffs.push_back(s11);
00315 featureValueDiffs.push_back(s12);
00316 featureValueDiffs.push_back(s13);
00317 featureValueDiffs.push_back(s14);
00318 featureValueDiffs.push_back(s15);
00319 featureValueDiffs.push_back(s16);
00320 featureValueDiffs.push_back(s17);
00321 featureValueDiffs.push_back(s18);
00322 featureValueDiffs.push_back(s19);
00323 featureValueDiffs.push_back(s20);
00324 featureValueDiffs.push_back(s21);
00325
00326 vector<float> oldModelScoreDiff;
00327 for (int i = 0; i < numberOfConstraints; ++i) {
00328 oldModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weights));
00329 }
00330
00331 for (int i = 0; i < numberOfConstraints; ++i) {
00332 cerr << "old model score diff: " << oldModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (oldModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00333 }
00334
00335 for (int i = 0; i < numberOfConstraints; ++i) {
00336 lossMinusModelScoreDiff.push_back(losses[i] - oldModelScoreDiff[i]);
00337 }
00338
00339 for (int i = 0; i < numberOfConstraints; ++i) {
00340 cerr << "A: " << featureValueDiffs[i] << ", b: " << lossMinusModelScoreDiff[i] << endl;
00341 }
00342
00343 vector< float> alphas1 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff);
00344 vector< float> alphas2 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff, 0.01);
00345
00346 cerr << "\nalphas without slack:" << endl;
00347 for (size_t i = 0; i < alphas1.size(); ++i) {
00348 cerr << "alpha " << i << ": " << alphas1[i] << endl;
00349 }
00350 cerr << endl;
00351
00352 cerr << "partial updates:" << endl;
00353 vector< ScoreComponentCollection> featureValueDiffs1(featureValueDiffs);
00354 FVector totalUpdate1 = ScoreComponentCollection::CreateFVector();
00355 for (size_t k = 0; k < featureValueDiffs1.size(); ++k) {
00356 featureValueDiffs1[k].MultiplyEquals(alphas1[k]);
00357 cerr << k << ": " << featureValueDiffs1[k].GetScoresVector() << endl;
00358 FVector update = featureValueDiffs1[k].GetScoresVector();
00359 totalUpdate1 += update;
00360 }
00361 cerr << endl;
00362 cerr << "total update: " << totalUpdate1 << endl << endl;
00363
00364 ScoreComponentCollection weightsUpdate1(weights);
00365 weightsUpdate1.PlusEquals(totalUpdate1);
00366 cerr << "old weights: " << weights << endl;
00367 cerr << "new weights: " << weightsUpdate1 << endl << endl;
00368
00369 vector<float> newModelScoreDiff;
00370 for (int i = 0; i < numberOfConstraints; ++i) {
00371 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate1));
00372 }
00373
00374 for (int i = 0; i < numberOfConstraints; ++i) {
00375 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (newModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00376 }
00377
00378 cerr << "\n\nalphas with slack 0.01:" << endl;
00379 for (size_t i = 0; i < alphas2.size(); ++i) {
00380 cerr << "alpha " << i << ": " << alphas2[i] << endl;
00381 }
00382 cerr << endl;
00383
00384 cerr << "partial updates:" << endl;
00385 vector< ScoreComponentCollection> featureValueDiffs2(featureValueDiffs);
00386 FVector totalUpdate2 = ScoreComponentCollection::CreateFVector();
00387 for (size_t k = 0; k < featureValueDiffs2.size(); ++k) {
00388 featureValueDiffs2[k].MultiplyEquals(alphas2[k]);
00389 cerr << k << ": " << featureValueDiffs2[k].GetScoresVector() << endl;
00390 FVector update = featureValueDiffs2[k].GetScoresVector();
00391 totalUpdate2 += update;
00392 }
00393 cerr << endl;
00394 cerr << "total update: " << totalUpdate2 << endl << endl;
00395
00396 ScoreComponentCollection weightsUpdate2(weights);
00397 weightsUpdate2.PlusEquals(totalUpdate2);
00398 cerr << "old weights: " << weights << endl;
00399 cerr << "new weights: " << weightsUpdate2 << endl << endl;
00400
00401 newModelScoreDiff.clear();
00402 for (int i = 0; i < numberOfConstraints; ++i) {
00403 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate2));
00404 }
00405
00406 for (int i = 0; i < numberOfConstraints; ++i) {
00407 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << endl;
00408 }
00409 }
00410
00411 BOOST_FIXTURE_TEST_CASE(test_hildreth_4, MockProducers)
00412 {
00413
00414 cerr << "\n>>>>>Hildreth test, without slack and with 0.01 slack" << endl << endl;
00415 vector< ScoreComponentCollection> featureValueDiffs;
00416 vector< float> lossMinusModelScoreDiff;
00417
00418
00419 float w[] = { 1, 1, 0.638672, 1, 0 };
00420 vector<float> vec(w,w+5);
00421 ScoreComponentCollection weights;
00422 weights.PlusEquals(&multi, vec);
00423
00424 int numberOfConstraints = 8;
00425
00426
00427
00428 ScoreComponentCollection s1, s2, s3, s4, s5, s6, s7, s8, s9, s10, s11, s12, s13, s14, s15, s16, s17, s18, s19, s20, s21;
00429 float arr1[] = { 0, 0, -2.0672, 0, 0 };
00430 float arr2[] = { 0, 0, 0, 0, 0 };
00431 float arr3[] = { 0, 0, -2.08436, 1.38629, 0 };
00432 float arr4[] = { 0, 0, -0.0171661, 1.38629, 0 };
00433
00434
00435
00436
00437 float arr8[] = { 0, 0, 0, 0, 0 };
00438 float arr9[] = { 0, 0, -2.0672, 0, 0 };
00439
00440
00441
00442
00443
00444
00445 float arr15[] = { 0, 0, -2.0672, 0, 0 };
00446 float arr16[] = { 0, 0, -2.08436, 1.38629, 0 };
00447
00448
00449
00450
00451
00452
00453 vector<float> losses;
00454 losses.push_back(2.73485);
00455 losses.push_back(0);
00456 losses.push_back(3.64118);
00457 losses.push_back(1.47347);
00458
00459
00460
00461 losses.push_back(0);
00462 losses.push_back(2.73485);
00463
00464
00465
00466
00467
00468 losses.push_back(2.73485);
00469 losses.push_back(3.64118);
00470
00471
00472
00473
00474
00475
00476 vector<float> vec1(arr1,arr1+5);
00477 vector<float> vec2(arr2,arr2+5);
00478 vector<float> vec3(arr3,arr3+5);
00479 vector<float> vec4(arr4,arr4+5);
00480
00481
00482
00483 vector<float> vec8(arr8,arr8+5);
00484 vector<float> vec9(arr9,arr9+5);
00485
00486
00487
00488
00489
00490 vector<float> vec15(arr15,arr15+5);
00491 vector<float> vec16(arr16,arr16+5);
00492
00493
00494
00495
00496
00497
00498 s1.PlusEquals(&multi,vec1);
00499 s2.PlusEquals(&multi,vec2);
00500 s3.PlusEquals(&multi,vec3);
00501 s4.PlusEquals(&multi,vec4);
00502
00503
00504
00505 s8.PlusEquals(&multi,vec8);
00506 s9.PlusEquals(&multi,vec9);
00507
00508
00509
00510
00511
00512 s15.PlusEquals(&multi,vec15);
00513 s16.PlusEquals(&multi,vec16);
00514
00515
00516
00517
00518
00519
00520 featureValueDiffs.push_back(s1);
00521 featureValueDiffs.push_back(s2);
00522 featureValueDiffs.push_back(s3);
00523 featureValueDiffs.push_back(s4);
00524
00525
00526
00527 featureValueDiffs.push_back(s8);
00528 featureValueDiffs.push_back(s9);
00529
00530
00531
00532
00533
00534 featureValueDiffs.push_back(s15);
00535 featureValueDiffs.push_back(s16);
00536
00537
00538
00539
00540
00541
00542 vector<float> oldModelScoreDiff;
00543 for (int i = 0; i < numberOfConstraints; ++i) {
00544 oldModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weights));
00545 }
00546
00547 for (int i = 0; i < numberOfConstraints; ++i) {
00548 cerr << "old model score diff: " << oldModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (oldModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00549 }
00550
00551 for (int i = 0; i < numberOfConstraints; ++i) {
00552 lossMinusModelScoreDiff.push_back(losses[i] - oldModelScoreDiff[i]);
00553 }
00554
00555 for (int i = 0; i < numberOfConstraints; ++i) {
00556 cerr << "A: " << featureValueDiffs[i] << ", b: " << lossMinusModelScoreDiff[i] << endl;
00557 }
00558
00559 vector< float> alphas1 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff);
00560 vector< float> alphas2 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff, 0.01);
00561
00562 cerr << "\nalphas without slack:" << endl;
00563 for (size_t i = 0; i < alphas1.size(); ++i) {
00564 cerr << "alpha " << i << ": " << alphas1[i] << endl;
00565 }
00566 cerr << endl;
00567
00568 cerr << "partial updates:" << endl;
00569 vector< ScoreComponentCollection> featureValueDiffs1(featureValueDiffs);
00570 FVector totalUpdate1 = ScoreComponentCollection::CreateFVector();
00571 for (size_t k = 0; k < featureValueDiffs1.size(); ++k) {
00572 featureValueDiffs1[k].MultiplyEquals(alphas1[k]);
00573 cerr << k << ": " << featureValueDiffs1[k].GetScoresVector() << endl;
00574 FVector update = featureValueDiffs1[k].GetScoresVector();
00575 totalUpdate1 += update;
00576 }
00577 cerr << endl;
00578 cerr << "total update: " << totalUpdate1 << endl << endl;
00579
00580 ScoreComponentCollection weightsUpdate1(weights);
00581 weightsUpdate1.PlusEquals(totalUpdate1);
00582 cerr << "old weights: " << weights << endl;
00583 cerr << "new weights: " << weightsUpdate1 << endl << endl;
00584
00585 vector<float> newModelScoreDiff;
00586 for (int i = 0; i < numberOfConstraints; ++i) {
00587 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate1));
00588 }
00589
00590 for (int i = 0; i < numberOfConstraints; ++i) {
00591 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (newModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00592 }
00593
00594 cerr << "\n\nalphas with slack 0.01:" << endl;
00595 for (size_t i = 0; i < alphas2.size(); ++i) {
00596 cerr << "alpha " << i << ": " << alphas2[i] << endl;
00597 }
00598 cerr << endl;
00599
00600 cerr << "partial updates:" << endl;
00601 vector< ScoreComponentCollection> featureValueDiffs2(featureValueDiffs);
00602 FVector totalUpdate2 = ScoreComponentCollection::CreateFVector();
00603 for (size_t k = 0; k < featureValueDiffs2.size(); ++k) {
00604 featureValueDiffs2[k].MultiplyEquals(alphas2[k]);
00605 cerr << k << ": " << featureValueDiffs2[k].GetScoresVector() << endl;
00606 FVector update = featureValueDiffs2[k].GetScoresVector();
00607 totalUpdate2 += update;
00608 }
00609 cerr << endl;
00610 cerr << "total update: " << totalUpdate2 << endl << endl;
00611
00612 ScoreComponentCollection weightsUpdate2(weights);
00613 weightsUpdate2.PlusEquals(totalUpdate2);
00614 cerr << "old weights: " << weights << endl;
00615 cerr << "new weights: " << weightsUpdate2 << endl << endl;
00616
00617 newModelScoreDiff.clear();
00618 for (int i = 0; i < numberOfConstraints; ++i) {
00619 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate2));
00620 }
00621
00622 for (int i = 0; i < numberOfConstraints; ++i) {
00623 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << endl;
00624 }
00625 }
00626
00627 BOOST_FIXTURE_TEST_CASE(test_hildreth_5, MockProducers)
00628 {
00629
00630 cerr << "\n>>>>>Hildreth test, without slack and with 0.01 slack" << endl << endl;
00631 vector< ScoreComponentCollection> featureValueDiffs;
00632 vector< float> lossMinusModelScoreDiff;
00633
00634
00635 float w[] = { 1, 1, 0.638672, 1, 0 };
00636 vector<float> vec(w,w+5);
00637 ScoreComponentCollection weights;
00638 weights.PlusEquals(&multi, vec);
00639
00640 int numberOfConstraints = 2;
00641
00642
00643
00644 ScoreComponentCollection s1, s17;
00645 float arr1[] = { 0, 0, -2.0672, 0, 0 };
00646 float arr17[] = { 0, 0, 4.4283, 0, 0 };
00647 vector<float> losses;
00648 losses.push_back(2.73485);
00649 losses.push_back(3.64118);
00650
00651 vector<float> vec1(arr1,arr1+5);
00652 vector<float> vec17(arr17,arr17+5);
00653
00654 s1.PlusEquals(&multi,vec1);
00655 s17.PlusEquals(&multi,vec17);
00656
00657 featureValueDiffs.push_back(s1);
00658 featureValueDiffs.push_back(s17);
00659
00660 vector<float> oldModelScoreDiff;
00661 for (int i = 0; i < numberOfConstraints; ++i) {
00662 oldModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weights));
00663 }
00664
00665 float sumOfOldError = 0;
00666 for (int i = 0; i < numberOfConstraints; ++i) {
00667 cerr << "old model score diff: " << oldModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (oldModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00668 sumOfOldError += (losses[i] - oldModelScoreDiff[i]);
00669 }
00670 cerr << "sum of old error: " << sumOfOldError << endl;
00671
00672 for (int i = 0; i < numberOfConstraints; ++i) {
00673 lossMinusModelScoreDiff.push_back(losses[i] - oldModelScoreDiff[i]);
00674 }
00675
00676 for (int i = 0; i < numberOfConstraints; ++i) {
00677 cerr << "A: " << featureValueDiffs[i] << ", b: " << lossMinusModelScoreDiff[i] << endl;
00678 }
00679
00680 vector< float> alphas1 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff);
00681 vector< float> alphas2 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff, 0.01);
00682 vector< float> alphas3 = Hildreth::optimise(featureValueDiffs, lossMinusModelScoreDiff, 0.1);
00683
00684 cerr << "\nalphas without slack:" << endl;
00685 for (size_t i = 0; i < alphas1.size(); ++i) {
00686 cerr << "alpha " << i << ": " << alphas1[i] << endl;
00687 }
00688 cerr << endl;
00689
00690 cerr << "partial updates:" << endl;
00691 vector< ScoreComponentCollection> featureValueDiffs1(featureValueDiffs);
00692 FVector totalUpdate1 = ScoreComponentCollection::CreateFVector();
00693 for (size_t k = 0; k < featureValueDiffs1.size(); ++k) {
00694 featureValueDiffs1[k].MultiplyEquals(alphas1[k]);
00695 cerr << k << ": " << featureValueDiffs1[k].GetScoresVector() << endl;
00696 FVector update = featureValueDiffs1[k].GetScoresVector();
00697 totalUpdate1 += update;
00698 }
00699 cerr << endl;
00700 cerr << "total update: " << totalUpdate1 << endl << endl;
00701
00702 ScoreComponentCollection weightsUpdate1(weights);
00703 weightsUpdate1.PlusEquals(totalUpdate1);
00704 cerr << "old weights: " << weights << endl;
00705 cerr << "new weights: " << weightsUpdate1 << endl << endl;
00706
00707 vector<float> newModelScoreDiff;
00708 for (int i = 0; i < numberOfConstraints; ++i) {
00709 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate1));
00710 }
00711
00712 float sumOfNewError = 0;
00713 for (int i = 0; i < numberOfConstraints; ++i) {
00714 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (newModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00715 sumOfNewError += (losses[i] - newModelScoreDiff[i]);
00716 }
00717 cerr << "sum of new error: " << sumOfNewError << endl;
00718
00719 cerr << "\n\nalphas with slack 0.01:" << endl;
00720 for (size_t i = 0; i < alphas2.size(); ++i) {
00721 cerr << "alpha " << i << ": " << alphas2[i] << endl;
00722 }
00723 cerr << endl;
00724
00725 cerr << "partial updates:" << endl;
00726 vector< ScoreComponentCollection> featureValueDiffs2(featureValueDiffs);
00727 FVector totalUpdate2 = ScoreComponentCollection::CreateFVector();
00728 for (size_t k = 0; k < featureValueDiffs2.size(); ++k) {
00729 featureValueDiffs2[k].MultiplyEquals(alphas2[k]);
00730 cerr << k << ": " << featureValueDiffs2[k].GetScoresVector() << endl;
00731 FVector update = featureValueDiffs2[k].GetScoresVector();
00732 totalUpdate2 += update;
00733 }
00734 cerr << endl;
00735 cerr << "total update: " << totalUpdate2 << endl << endl;
00736
00737 ScoreComponentCollection weightsUpdate2(weights);
00738 weightsUpdate2.PlusEquals(totalUpdate2);
00739 cerr << "old weights: " << weights << endl;
00740 cerr << "new weights: " << weightsUpdate2 << endl << endl;
00741
00742 newModelScoreDiff.clear();
00743 for (int i = 0; i < numberOfConstraints; ++i) {
00744 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate2));
00745 }
00746
00747 sumOfNewError = 0;
00748 for (int i = 0; i < numberOfConstraints; ++i) {
00749 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (newModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00750 sumOfNewError += (losses[i] - newModelScoreDiff[i]);
00751 }
00752 cerr << "sum of new error: " << sumOfNewError << endl;
00753
00754 cerr << "\n\nalphas with slack 0.1:" << endl;
00755 for (size_t i = 0; i < alphas3.size(); ++i) {
00756 cerr << "alpha " << i << ": " << alphas3[i] << endl;
00757 }
00758 cerr << endl;
00759
00760 cerr << "partial updates:" << endl;
00761 vector< ScoreComponentCollection> featureValueDiffs3(featureValueDiffs);
00762 FVector totalUpdate3 = ScoreComponentCollection::CreateFVector();
00763 for (size_t k = 0; k < featureValueDiffs3.size(); ++k) {
00764 featureValueDiffs3[k].MultiplyEquals(alphas3[k]);
00765 cerr << k << ": " << featureValueDiffs3[k].GetScoresVector() << endl;
00766 FVector update = featureValueDiffs3[k].GetScoresVector();
00767 totalUpdate3 += update;
00768 }
00769 cerr << endl;
00770 cerr << "total update: " << totalUpdate3 << endl << endl;
00771
00772 ScoreComponentCollection weightsUpdate3(weights);
00773 weightsUpdate3.PlusEquals(totalUpdate3);
00774 cerr << "old weights: " << weights << endl;
00775 cerr << "new weights: " << weightsUpdate3 << endl << endl;
00776
00777 newModelScoreDiff.clear();
00778 for (int i = 0; i < numberOfConstraints; ++i) {
00779 newModelScoreDiff.push_back(featureValueDiffs[i].InnerProduct(weightsUpdate3));
00780 }
00781
00782 sumOfNewError = 0;
00783 for (int i = 0; i < numberOfConstraints; ++i) {
00784 cerr << "new model score diff: " << newModelScoreDiff[i] << ", loss: " << losses[i] << "\t" << (newModelScoreDiff[i] >= losses[i] ? 1 : 0) << endl;
00785 sumOfNewError += (losses[i] - newModelScoreDiff[i]);
00786 }
00787 cerr << "sum of new error: " << sumOfNewError << endl;
00788 }
00789
00790 BOOST_AUTO_TEST_SUITE_END()
00791
00792 }
00793