Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

more refactoring of API examples and required c++ adjustments #4275

Merged
merged 1 commit into from
May 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion data
8 changes: 4 additions & 4 deletions examples/meta/src/base_api/put_get_add.sg
Original file line number Diff line number Diff line change
Expand Up @@ -26,11 +26,11 @@ matrix[1,1] = 0.4
#RealMatrix matrix2 = feats.get_real_matrix("feature_matrix")
#feats.put("feature_matrix", matrix2)

EuclideanDistance distance()
knn.put("distance", distance)
Distance dist = distance("EuclideanDistance")
knn.put("distance", dist)

SGObject distance2 = knn.get("distance")
knn.put("distance", distance2)
SGObject dist2 = knn.get("distance")
knn.put("distance", dist2)

LibSVM svm()
svm.put("kernel", k)
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/binary/averaged_perceptron.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_binary_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
BinaryLabels labels_train(f_labels_train)
BinaryLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![set_parameters]
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/binary/linear_support_vector_machine.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_binary_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
BinaryLabels labels_train(f_labels_train)
BinaryLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![set_parameters]
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/binary/perceptron.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_binary_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
BinaryLabels labels_train(f_labels_train)
BinaryLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_instance]
Expand Down
6 changes: 3 additions & 3 deletions examples/meta/src/multiclass/ecoc_random.sg
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_classifier]
LibLinear classifier()
Machine classifier = machine("LibLinear")
#![create_classifier]

#![choose_strategy]
Expand Down
6 changes: 3 additions & 3 deletions examples/meta/src/multiclass/linear.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_classifier]
LibLinear classifier()
Machine classifier = machine("LibLinear")
#![create_classifier]

#![choose_strategy]
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/multiclass/linear_discriminant_analysis.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_instance]
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/multiclass/logistic_regression.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_instance]
Expand Down
4 changes: 2 additions & 2 deletions examples/meta/src/multiclass/shareboost.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/classifier_4class_2d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
MulticlassLabels labels_train(f_labels_train)
MulticlassLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![create_instance]
Expand Down
8 changes: 4 additions & 4 deletions examples/meta/src/regression/least_angle_regression.sg
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ CSVFile f_labels_test("../../data/regression_1d_linear_labels_test.dat")
#![create_features]
Features features_train = features(f_feats_train)
Features features_test = features(f_feats_test)
RegressionLabels labels_train(f_labels_train)
RegressionLabels labels_test(f_labels_test)
Labels labels_train = labels(f_labels_train)
Labels labels_test = labels(f_labels_test)
#![create_features]

#![preprocess_features]
Expand All @@ -28,7 +28,7 @@ Machine lars = machine("LeastAngleRegression", labels=labels_train, lasso=False,

#![train_and_apply]
lars.train(features_train)
RegressionLabels labels_predict = lars.apply_regression(features_test)
Labels labels_predict = lars.apply(features_test)

#[!extract_w]
RealVector weights = lars.get_real_vector("w")
Expand All @@ -40,5 +40,5 @@ real mse = eval.evaluate(labels_predict, labels_test)
#![evaluate_error]

# integration testing variables
RealVector output = labels_test.get_labels()
RealVector output = labels_predict.get_real_vector("labels")

3 changes: 1 addition & 2 deletions examples/undocumented/libshogun/classifier_lda.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,11 @@ void test()
lda->train();

// Classify and display output
CMulticlassLabels* output=CLabelsFactory::to_multiclass(lda->apply());
auto output = multiclass_labels(lda->apply());
SG_REF(output);
SGVector<float64_t>::display_vector(output->get_labels().vector, output->get_num_labels());

// Free memory
SG_UNREF(output);
SG_UNREF(lda);
#endif
}
Expand Down
2 changes: 1 addition & 1 deletion src/gpl
3 changes: 1 addition & 2 deletions src/shogun/classifier/AveragedPerceptron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@ void CAveragedPerceptron::init()
bool CAveragedPerceptron::train_machine(CFeatures* data)
{
ASSERT(m_labels)
ASSERT(m_labels->get_label_type() == LT_BINARY)

if (data)
{
Expand All @@ -54,7 +53,7 @@ bool CAveragedPerceptron::train_machine(CFeatures* data)
ASSERT(features)
bool converged=false;
int32_t iter=0;
SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
SGVector<int32_t> train_labels = binary_labels(m_labels)->get_int_labels();
int32_t num_feat=features->get_dim_feature_space();
int32_t num_vec=features->get_num_vectors();

Expand Down
3 changes: 1 addition & 2 deletions src/shogun/classifier/Perceptron.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ CPerceptron::~CPerceptron()
bool CPerceptron::train_machine(CFeatures* data)
{
ASSERT(m_labels)
ASSERT(m_labels->get_label_type() == LT_BINARY)

if (data)
{
Expand All @@ -59,7 +58,7 @@ bool CPerceptron::train_machine(CFeatures* data)
ASSERT(features)
bool converged=false;
int32_t iter=0;
SGVector<int32_t> train_labels=((CBinaryLabels*) m_labels)->get_int_labels();
SGVector<int32_t> train_labels = binary_labels(m_labels)->get_int_labels();
int32_t num_feat=features->get_dim_feature_space();
int32_t num_vec=features->get_num_vectors();

Expand Down
4 changes: 2 additions & 2 deletions src/shogun/classifier/svm/LibLinear.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,6 @@ bool CLibLinear::train_machine(CFeatures* data)
{

ASSERT(m_labels)
ASSERT(m_labels->get_label_type() == LT_BINARY)
init_linear_term();

if (data)
Expand Down Expand Up @@ -142,9 +141,10 @@ bool CLibLinear::train_machine(CFeatures* data)
double Cp = get_C1();
double Cn = get_C2();

auto labels = binary_labels(m_labels);
for (int32_t i = 0; i < prob.l; i++)
{
prob.y[i] = ((CBinaryLabels*)m_labels)->get_int_label(i);
prob.y[i] = labels->get_int_label(i);
if (prob.y[i] == +1)
Cs[i] = get_C1();
else if (prob.y[i] == -1)
Expand Down
13 changes: 7 additions & 6 deletions src/shogun/evaluation/MulticlassAccuracy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,16 @@ float64_t CMulticlassAccuracy::evaluate(CLabels* predicted, CLabels* ground_trut
{
ASSERT(predicted && ground_truth)
ASSERT(predicted->get_num_labels() == ground_truth->get_num_labels())
ASSERT(predicted->get_label_type() == LT_MULTICLASS)
ASSERT(ground_truth->get_label_type() == LT_MULTICLASS)
int32_t length = predicted->get_num_labels();
auto predicted_mc = multiclass_labels(predicted);
auto ground_truth_mc = multiclass_labels(ground_truth);
int32_t correct = 0;
if (m_ignore_rejects)
{
for (int32_t i=0; i<length; i++)
{
if (((CMulticlassLabels*) predicted)->get_int_label(i)==((CMulticlassLabels*) ground_truth)->get_int_label(i))
if (predicted_mc->get_int_label(i) ==
ground_truth_mc->get_int_label(i))
correct++;
}
return ((float64_t)correct)/length;
Expand All @@ -33,11 +34,11 @@ float64_t CMulticlassAccuracy::evaluate(CLabels* predicted, CLabels* ground_trut
int32_t total = length;
for (int32_t i=0; i<length; i++)
{
int32_t predicted_label = ((CMulticlassLabels*) predicted)->get_int_label(i);
int32_t predicted_label = predicted_mc->get_int_label(i);

if (predicted_label==((CMulticlassLabels*) predicted)->REJECTION_LABEL)
if (predicted_label == predicted_mc->REJECTION_LABEL)
total--;
else if (predicted_label==((CMulticlassLabels*) ground_truth)->get_int_label(i))
else if (predicted_label == ground_truth_mc->get_int_label(i))
correct++;
}
m_rejects_num = length-total;
Expand Down
3 changes: 2 additions & 1 deletion src/shogun/machine/MulticlassMachine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ bool CMulticlassMachine::train_machine(CFeatures* data)
SG_REF(train_labels);
m_machine->set_labels(train_labels);

m_multiclass_strategy->train_start(CLabelsFactory::to_multiclass(m_labels), train_labels);
m_multiclass_strategy->train_start(
multiclass_labels(m_labels), train_labels);
while (m_multiclass_strategy->train_has_more())
{
SGVector<index_t> subset=m_multiclass_strategy->train_prepare_next();
Expand Down
5 changes: 3 additions & 2 deletions src/shogun/multiclass/ShareBoost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ void CShareBoost::compute_pred(const float64_t *W)

void CShareBoost::compute_rho()
{
CMulticlassLabels *lab = dynamic_cast<CMulticlassLabels *>(m_labels);
auto lab = multiclass_labels(m_labels);

for (int32_t i=0; i < m_rho.num_rows; ++i)
{ // i loop classes
for (int32_t j=0; j < m_rho.num_cols; ++j)
Expand All @@ -163,6 +164,7 @@ void CShareBoost::compute_rho()
int32_t CShareBoost::choose_feature()
{
SGVector<float64_t> l1norm(m_fea.num_rows);
auto lab = multiclass_labels(m_labels);
for (int32_t j=0; j < m_fea.num_rows; ++j)
{
if (std::find(&m_activeset[0], &m_activeset[m_activeset.vlen], j) !=
Expand All @@ -173,7 +175,6 @@ int32_t CShareBoost::choose_feature()
else
{
l1norm[j] = 0;
CMulticlassLabels *lab = dynamic_cast<CMulticlassLabels *>(m_labels);
for (int32_t k=0; k < m_multiclass_strategy->get_num_classes(); ++k)
{
float64_t abssum = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/shogun/multiclass/ShareBoostOptimizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ float64_t ShareBoostOptimizer::lbfgs_evaluate(void *userdata, const float64_t *W
int32_t k = optimizer->m_sb->m_multiclass_strategy->get_num_classes();

SGMatrix<float64_t> fea = optimizer->m_sb->m_fea;
CMulticlassLabels *lab = dynamic_cast<CMulticlassLabels *>(optimizer->m_sb->m_labels);
auto lab = multiclass_labels(optimizer->m_sb->m_labels);

// compute gradient
for (int32_t i=0; i < m; ++i)
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/multiclass/tree/C45ClassifierTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -530,14 +530,16 @@ void CC45ClassifierTree::prune_tree_from_current_node(CDenseFeatures<float64_t>*
SG_UNREF(right_child);
}

SG_UNREF(children);
SG_UNREF(children)

CMulticlassLabels* predicted_unpruned=apply_multiclass_from_current_node(feats, current);
SG_REF(predicted_unpruned);
SGVector<float64_t> pruned_labels=SGVector<float64_t>(feature_matrix.num_cols);
for (int32_t i=0; i<feature_matrix.num_cols; i++)
pruned_labels[i]=current->data.class_label;

CMulticlassLabels* predicted_pruned=new CMulticlassLabels(pruned_labels);
SG_REF(predicted_pruned);

CMulticlassAccuracy* accuracy=new CMulticlassAccuracy();
float64_t unpruned_accuracy=accuracy->evaluate(predicted_unpruned, gnd_truth);
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/multiclass/tree/ID3ClassifierTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -335,11 +335,13 @@ void CID3ClassifierTree::prune_tree_machine(CDenseFeatures<float64_t>* feats,
SG_UNREF(children);

CMulticlassLabels* predicted_unpruned = apply_multiclass_from_current_node(feats, current);
SG_REF(predicted_unpruned);
SGVector<float64_t> pruned_labels = SGVector<float64_t>(feature_matrix.num_cols);
for (int32_t i=0; i<feature_matrix.num_cols; i++)
pruned_labels[i] = current->data.class_label;

CMulticlassLabels* predicted_pruned = new CMulticlassLabels(pruned_labels);
SG_REF(predicted_pruned);

CMulticlassAccuracy* accuracy = new CMulticlassAccuracy();
float64_t unpruned_accuracy = accuracy->evaluate(predicted_unpruned, gnd_truth);
Expand Down
5 changes: 1 addition & 4 deletions src/shogun/regression/LeastAngleRegression.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,6 @@ void CLeastAngleRegression::plane_rot(ST x0, ST x1,

bool CLeastAngleRegression::train_machine(CFeatures* data)
{
REQUIRE(m_labels->get_label_type() == LT_REGRESSION, "Provided labels (%s) are of type (%d) - they should be regression labels (%d) instead.\n"
, m_labels->get_name(), m_labels->get_label_type(), LT_REGRESSION, m_labels->get_label_type())

if (!data)
{
REQUIRE(features, "No features provided.\n")
Expand Down Expand Up @@ -155,7 +152,7 @@ bool CLeastAngleRegression::train_machine_templated(CDenseFeatures<ST> * data)
m_is_active.resize(n_fea);
fill(m_is_active.begin(), m_is_active.end(), false);

SGVector<ST> y = ((CRegressionLabels*) m_labels)->template get_labels_t<ST>();
SGVector<ST> y = regression_labels(m_labels)->template get_labels_t<ST>();
typename SGVector<ST>::EigenVectorXtMap map_y(y.vector, y.size());

// transpose(X) is more convenient to work with since we care
Expand Down
1 change: 1 addition & 0 deletions tests/unit/multiclass/MulticlassOCAS_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ TEST(MulticlassOCASTest,train)
mocas->train();

CMulticlassLabels* pred = (CMulticlassLabels*)mocas->apply(test_feats);
SG_REF(pred);
CMulticlassAccuracy evaluate = CMulticlassAccuracy();
float64_t result = evaluate.evaluate(pred, ground_truth);
EXPECT_GT(result, 0.99);
Expand Down
4 changes: 4 additions & 0 deletions tests/unit/multiclass/tree/C45ClassifierTree_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,8 @@ TEST(C45ClassifierTree, tree_prune_categorical_attributes)
CDenseFeatures<float64_t>* train_features=new CDenseFeatures<float64_t>(data);
CMulticlassLabels* train_lab=new CMulticlassLabels(train_labels);
CMulticlassLabels* validation_lab=new CMulticlassLabels(validation_labels);
SG_REF(train_lab);
SG_REF(validation_lab);

CC45ClassifierTree* c45tree=new CC45ClassifierTree();
c45tree->set_labels(train_lab);
Expand Down Expand Up @@ -581,8 +583,10 @@ TEST(C45ClassifierTree, tree_prune_continuous_attributes)

CDenseFeatures<float64_t>* train_features=new CDenseFeatures<float64_t>(data);
CMulticlassLabels* train_lab=new CMulticlassLabels(train_labels);
SG_REF(train_lab);
CDenseFeatures<float64_t>* validation_features=new CDenseFeatures<float64_t>(validation_data);
CMulticlassLabels* validation_lab=new CMulticlassLabels(validation_labels);
SG_REF(validation_lab);

CC45ClassifierTree* c45tree=new CC45ClassifierTree();
c45tree->set_labels(train_lab);
Expand Down