Skip to content

Commit

Permalink
Add unit tests on lmnn termination
Browse files Browse the repository at this point in the history
  • Loading branch information
vinx13 committed Feb 27, 2018
1 parent 014d306 commit c3b7be7
Showing 1 changed file with 51 additions and 0 deletions.
51 changes: 51 additions & 0 deletions tests/unit/metric/LMNN_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,57 @@ TEST(LMNN,train_identity_init)
SG_UNREF(lmnn)
}

TEST(LMNN, train_termination)
{
// create features, each column is a feature vector
SGMatrix<float64_t> feat_mat(2,4);
// 1st feature vector
feat_mat(0,0)=0;
feat_mat(1,0)=0;
// 2nd feature vector
feat_mat(0,1)=0;
feat_mat(1,1)=-1;
// 3rd feature vector
feat_mat(0,2)=1;
feat_mat(1,2)=1;
// 4th feature vector
feat_mat(0,3)=-1;
feat_mat(1,3)=1;
// wrap feat_mat in Shogun features
CDenseFeatures<float64_t>* features=new CDenseFeatures<float64_t>(feat_mat);

// create labels
SGVector<float64_t> lab_vec(4);
lab_vec[0]=0;
lab_vec[1]=0;
lab_vec[2]=1;
lab_vec[3]=1;
// two-class data, use MulticlassLabels because it works in general for more than
// two classes
CMulticlassLabels* labels=new CMulticlassLabels(lab_vec);

// create LMNN metric machine
int32_t k=1; // number of target neighbors per example
CLMNN* lmnn=new CLMNN(features,labels,k);
// use the identity matrix as initial transform for LMNN
SGMatrix<float64_t> init_transform=SGMatrix<float64_t>::create_identity_matrix(2,1);
// set number of maximum iterations and train
lmnn->set_maxiter(1500);
lmnn->train(init_transform);

// check linear transform solution
SGMatrix<float64_t> L=lmnn->get_linear_transform();
EXPECT_NEAR(L(0,0),0.000041647483219,1e-10);
EXPECT_NEAR(L(0,1),0,1e-10);
EXPECT_NEAR(L(1,0),0,1e-10);
EXPECT_NEAR(L(1,1),0.988162395685451,1e-10);

// check number of iterations
auto stat = lmnn->get_statistics();
EXPECT_EQ(stat->obj.vlen, 1234);
SG_UNREF(lmnn)
}

TEST(LMNN,train_pca_init)
{
// create features, each column is a feature vector
Expand Down

0 comments on commit c3b7be7

Please sign in to comment.