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

Refactor linear machine api #11

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 5 additions & 26 deletions shogun/classifier/svm/SVMLin.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,6 @@ CSVMLin::CSVMLin()
init();
}

CSVMLin::CSVMLin(
float64_t C, CDotFeatures* traindat, CLabels* trainlab)
: CLinearMachine(), C1(C), C2(C), epsilon(1e-5), use_bias(true)
{
set_features(traindat);
set_labels(trainlab);

init();
}


CSVMLin::~CSVMLin()
{
}
Expand All @@ -51,22 +40,13 @@ void CSVMLin::init()
SG_ADD(&epsilon, "epsilon", "Convergence precision.");
}

bool CSVMLin::train_machine(CFeatures* data)
void CSVMLin::train_machine(CFeatures* features, CLabels* labels)
{
ASSERT(m_labels)

if (data)
{
if (!data->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n")
set_features((CDotFeatures*) data);
}

ASSERT(features)

SGVector<float64_t> train_labels=((CBinaryLabels*) m_labels)->get_labels();
int32_t num_feat=features->get_dim_feature_space();
int32_t num_vec=features->get_num_vectors();
SGVector<float64_t> train_labels = binary_labels(labels)->get_labels();
int32_t num_feat = features->as<CDotFeatures>()->get_dim_feature_space();
int32_t num_vec = features->get_num_vectors();

ASSERT(num_vec==train_labels.vlen)

Expand All @@ -81,7 +61,7 @@ bool CSVMLin::train_machine(CFeatures* data)
Data.n=num_feat+1;
Data.nz=num_feat+1;
Data.Y=train_labels.vector;
Data.features=features;
Data.features = features->as<CDotFeatures>();
Data.C = SG_MALLOC(float64_t, Data.l);

Options.algo = SVM;
Expand Down Expand Up @@ -119,5 +99,4 @@ bool CSVMLin::train_machine(CFeatures* data)

SG_FREE(Data.C);
SG_FREE(Outputs.vec);
return true;
}
14 changes: 2 additions & 12 deletions shogun/classifier/svm/SVMLin.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,15 +29,6 @@ class CSVMLin : public CLinearMachine
/** default constructor */
CSVMLin();

/** constructor
*
* @param C constant C
* @param traindat training features
* @param trainlab labels for features
*/
CSVMLin(
float64_t C, CDotFeatures* traindat,
CLabels* trainlab);
virtual ~CSVMLin();

/** set C
Expand Down Expand Up @@ -93,10 +84,9 @@ class CSVMLin : public CLinearMachine
* @param data training data (parameter can be avoided if distance or
* kernel-based classifiers are used and distance/kernels are
* initialized with train data)
*
* @return whether training was successful
* @param labels training labels
*/
virtual bool train_machine(CFeatures* data=NULL);
virtual void train_machine(CFeatures* features, CLabels* labels);

/** set up parameters */
void init();
Expand Down
29 changes: 12 additions & 17 deletions shogun/classifier/svm/SVMSGD.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -68,18 +68,10 @@ void CSVMSGD::set_loss_function(CLossFunction* loss_func)
loss=loss_func;
}

bool CSVMSGD::train_machine(CFeatures* data)
void CSVMSGD::train_machine(CFeatures* features, CLabels* labels)
{
// allocate memory for w and initialize everyting w and bias with 0
auto labels = binary_labels(m_labels);

if (data)
{
if (!data->has_property(FP_DOT))
SG_ERROR("Specified features are not of type CDotFeatures\n")
set_features((CDotFeatures*) data);
}

auto bin_labels = binary_labels(m_labels);
ASSERT(features)

int32_t num_train_labels = labels->get_num_labels();
Expand All @@ -88,7 +80,8 @@ bool CSVMSGD::train_machine(CFeatures* data)
ASSERT(num_vec==num_train_labels)
ASSERT(num_vec>0)

SGVector<float64_t> w(features->get_dim_feature_space());
SGVector<float64_t> w(
features->as<CDotFeatures>()->get_dim_feature_space());
w.zero();
bias=0;

Expand Down Expand Up @@ -121,13 +114,17 @@ bool CSVMSGD::train_machine(CFeatures* data)
for (int32_t i=0; i<num_vec; i++)
{
float64_t eta = 1.0 / (lambda * t);
float64_t y = labels->get_label(i);
float64_t z = y * (features->dense_dot(i, w.vector, w.vlen) + bias);
float64_t y = bin_labels->get_label(i);
float64_t z =
y *
(features->as<CDotFeatures>()->dense_dot(i, w.vector, w.vlen) +
bias);

if (z < 1 || is_log_loss)
{
float64_t etd = -eta * loss->first_derivative(z,1);
features->add_to_dense_vec(etd * y / wscale, i, w.vector, w.vlen);
float64_t etd = -eta * loss->first_derivative(z, 1);
features->as<CDotFeatures>()->add_to_dense_vec(
etd * y / wscale, i, w.vector, w.vlen);

if (use_bias)
{
Expand All @@ -153,8 +150,6 @@ bool CSVMSGD::train_machine(CFeatures* data)
SG_INFO("Norm: %.6f, Bias: %.6f\n", wnorm, bias)

set_w(w);

return true;
}

void CSVMSGD::calibrate()
Expand Down
7 changes: 3 additions & 4 deletions shogun/classifier/svm/SVMSGD.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,13 +138,12 @@ class CSVMSGD : public CLinearMachine

/** train classifier
*
* @param data training data (parameter can be avoided if distance or
* @param features training data (parameter can be avoided if distance or
* kernel-based classifiers are used and distance/kernels are
* initialized with train data)
*
* @return whether training was successful
* @param labels training labels
*/
virtual bool train_machine(CFeatures* data=NULL);
virtual void train_machine(CFeatures* features, CLabels* labels);

private:
void init();
Expand Down