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

Use weight vector *only* through getters/setters #3674

Merged
merged 1 commit into from Mar 23, 2017
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
6 changes: 4 additions & 2 deletions src/shogun/classifier/AveragedPerceptron.cpp
Expand Up @@ -50,7 +50,7 @@ bool CAveragedPerceptron::train_machine(CFeatures* data)
int32_t num_vec=features->get_num_vectors();

ASSERT(num_vec==train_labels.vlen)
w=SGVector<float64_t>(num_feat);
SGVector<float64_t> w(num_feat);
float64_t* tmp_w=SG_MALLOC(float64_t, num_feat);
float64_t* output=SG_MALLOC(float64_t, num_vec);

Expand All @@ -71,7 +71,7 @@ bool CAveragedPerceptron::train_machine(CFeatures* data)

for (int32_t i=0; i<num_vec; i++)
{
output[i]=apply_one(i);
output[i] = features->dense_dot(i, w.vector, w.vlen) + bias;

if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
{
Expand Down Expand Up @@ -102,5 +102,7 @@ bool CAveragedPerceptron::train_machine(CFeatures* data)
SG_FREE(output);
SG_FREE(tmp_w);

set_w(w);

return converged;
}
6 changes: 4 additions & 2 deletions src/shogun/classifier/FeatureBlockLogisticRegression.cpp
Expand Up @@ -191,7 +191,7 @@ bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)
new_w[i] = result.w[i];
set_bias(result.c[0]);

w = new_w;
set_w(new_w);
}
break;
case TREE:
Expand Down Expand Up @@ -221,7 +221,7 @@ bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)

set_bias(result.c[0]);

w = new_w;
set_w(new_w);
}
break;
default:
Expand All @@ -233,6 +233,7 @@ bool CFeatureBlockLogisticRegression::train_machine(CFeatures* data)

float64_t CFeatureBlockLogisticRegression::apply_one(int32_t vec_idx)
{
SGVector<float64_t> w = get_w();
return CMath::exp(-(features->dense_dot(vec_idx, w.vector, w.vlen) + bias));
}

Expand All @@ -250,6 +251,7 @@ SGVector<float64_t> CFeatureBlockLogisticRegression::apply_get_outputs(CFeatures
return SGVector<float64_t>();

int32_t num=features->get_num_vectors();
SGVector<float64_t> w = get_w();
ASSERT(num>0)
ASSERT(w.vlen==features->get_dim_feature_space())

Expand Down
4 changes: 3 additions & 1 deletion src/shogun/classifier/LDA.cpp
Expand Up @@ -251,12 +251,14 @@ bool CLDA::train_machine_templated(SGVector<int32_t> train_labels, CFeatures *da
bias=bias*(-1);
}

w=SGVector<float64_t>(num_feat);
SGVector<float64_t> w(num_feat);
w.zero();

//copy w_st into w
for(i = 0; i < w.size(); ++i)
w[i] = (float64_t) w_st[i];

set_w(w);

return true;
}
7 changes: 5 additions & 2 deletions src/shogun/classifier/Perceptron.cpp
Expand Up @@ -54,10 +54,11 @@ bool CPerceptron::train_machine(CFeatures* data)
ASSERT(num_vec==train_labels.vlen)
float64_t* output=SG_MALLOC(float64_t, num_vec);

SGVector<float64_t> w = get_w();
if (m_initialize_hyperplane)
{
w = SGVector<float64_t>(num_feat);
//start with uniform w, bias=0
w=SGVector<float64_t>(num_feat);
bias=0;
for (int32_t i=0; i<num_feat; i++)
w.vector[i]=1.0/num_feat;
Expand All @@ -71,7 +72,7 @@ bool CPerceptron::train_machine(CFeatures* data)
converged=true;
for (int32_t i=0; i<num_vec; i++)
{
output[i]=apply_one(i);
output[i] = features->dense_dot(i, w.vector, w.vlen) + bias;

if (CMath::sign<float64_t>(output[i]) != train_labels.vector[i])
{
Expand All @@ -91,6 +92,8 @@ bool CPerceptron::train_machine(CFeatures* data)

SG_FREE(output);

set_w(w);

return converged;
}

Expand Down
18 changes: 12 additions & 6 deletions src/shogun/classifier/svm/LibLinear.cpp
Expand Up @@ -117,6 +117,7 @@ bool CLibLinear::train_machine(CFeatures* data)
num_vec, num_train_labels);
}
}
SGVector<float64_t> w;
if (use_bias)
w=SGVector<float64_t>(SG_MALLOC(float64_t, num_feat+1), num_feat);
else
Expand Down Expand Up @@ -188,33 +189,35 @@ bool CLibLinear::train_machine(CFeatures* data)
break;
}
case L2R_L2LOSS_SVC_DUAL:
solve_l2r_l1l2_svc(&prob, epsilon, Cp, Cn, L2R_L2LOSS_SVC_DUAL);
solve_l2r_l1l2_svc(w, &prob, epsilon, Cp, Cn, L2R_L2LOSS_SVC_DUAL);
break;
case L2R_L1LOSS_SVC_DUAL:
solve_l2r_l1l2_svc(&prob, epsilon, Cp, Cn, L2R_L1LOSS_SVC_DUAL);
solve_l2r_l1l2_svc(w, &prob, epsilon, Cp, Cn, L2R_L1LOSS_SVC_DUAL);
break;
case L1R_L2LOSS_SVC:
{
//ASSUME FEATURES ARE TRANSPOSED ALREADY
solve_l1r_l2_svc(&prob, epsilon*CMath::min(pos,neg)/prob.l, Cp, Cn);
solve_l1r_l2_svc(w, &prob, epsilon*CMath::min(pos,neg)/prob.l, Cp, Cn);
break;
}
case L1R_LR:
{
//ASSUME FEATURES ARE TRANSPOSED ALREADY
solve_l1r_lr(&prob, epsilon*CMath::min(pos,neg)/prob.l, Cp, Cn);
solve_l1r_lr(w, &prob, epsilon*CMath::min(pos,neg)/prob.l, Cp, Cn);
break;
}
case L2R_LR_DUAL:
{
solve_l2r_lr_dual(&prob, epsilon, Cp, Cn);
solve_l2r_lr_dual(w, &prob, epsilon, Cp, Cn);
break;
}
default:
SG_ERROR("Error: unknown solver_type\n")
break;
}

set_w(w);

if (use_bias)
set_bias(w[w.vlen]);
else
Expand Down Expand Up @@ -255,6 +258,7 @@ bool CLibLinear::train_machine(CFeatures* data)
// To support weights for instances, use GETI(i) (i)

void CLibLinear::solve_l2r_l1l2_svc(
SGVector<float64_t>& w,
const liblinear_problem *prob, double eps, double Cp, double Cn, LIBLINEAR_SOLVER_TYPE st)
{
int l = prob->l;
Expand Down Expand Up @@ -455,6 +459,7 @@ void CLibLinear::solve_l2r_l1l2_svc(
// To support weights for instances, use GETI(i) (i)

void CLibLinear::solve_l1r_l2_svc(
SGVector<float64_t>& w,
liblinear_problem *prob_col, double eps, double Cp, double Cn)
{
int l = prob_col->l;
Expand Down Expand Up @@ -801,6 +806,7 @@ void CLibLinear::solve_l1r_l2_svc(
// To support weights for instances, use GETI(i) (i)

void CLibLinear::solve_l1r_lr(
SGVector<float64_t>& w,
const liblinear_problem *prob_col, double eps,
double Cp, double Cn)
{
Expand Down Expand Up @@ -1172,7 +1178,7 @@ void CLibLinear::solve_l1r_lr(
#define GETI(i) (y[i]+1)
// To support weights for instances, use GETI(i) (i)

void CLibLinear::solve_l2r_lr_dual(const liblinear_problem *prob, double eps, double Cp, double Cn)
void CLibLinear::solve_l2r_lr_dual(SGVector<float64_t>& w, const liblinear_problem *prob, double eps, double Cp, double Cn)
{
int l = prob->l;
int w_size = prob->n;
Expand Down
7 changes: 4 additions & 3 deletions src/shogun/classifier/svm/LibLinear.h
Expand Up @@ -192,11 +192,12 @@ class CLibLinear : public CLinearMachine

void train_one(const liblinear_problem *prob, const liblinear_parameter *param, double Cp, double Cn);
void solve_l2r_l1l2_svc(
SGVector<float64_t>& w,
const liblinear_problem *prob, double eps, double Cp, double Cn, LIBLINEAR_SOLVER_TYPE st);

void solve_l1r_l2_svc(liblinear_problem *prob_col, double eps, double Cp, double Cn);
void solve_l1r_lr(const liblinear_problem *prob_col, double eps, double Cp, double Cn);
void solve_l2r_lr_dual(const liblinear_problem *prob, double eps, double Cp, double Cn);
void solve_l1r_l2_svc(SGVector<float64_t>& w, liblinear_problem *prob_col, double eps, double Cp, double Cn);
void solve_l1r_lr(SGVector<float64_t>& w, const liblinear_problem *prob_col, double eps, double Cp, double Cn);
void solve_l2r_lr_dual(SGVector<float64_t>& w, const liblinear_problem *prob, double eps, double Cp, double Cn);


protected:
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/classifier/svm/SGDQN.cpp
Expand Up @@ -111,7 +111,7 @@ bool CSGDQN::train(CFeatures* data)
ASSERT(num_vec==num_train_labels)
ASSERT(num_vec>0)

w=SGVector<float64_t>(features->get_dim_feature_space());
SGVector<float64_t> w(features->get_dim_feature_space());
w.zero();

float64_t lambda= 1.0/(C1*num_vec);
Expand Down Expand Up @@ -198,6 +198,8 @@ bool CSGDQN::train(CFeatures* data)
SG_FREE(result);
SG_FREE(B);

set_w(w);

return true;
}

Expand Down
26 changes: 13 additions & 13 deletions src/shogun/classifier/svm/SVMOcas.cpp
Expand Up @@ -74,18 +74,18 @@ bool CSVMOcas::train_machine(CFeatures* data)
for (int32_t i=0; i<num_vec; i++)
lab[i] = ((CBinaryLabels*)m_labels)->get_label(i);

w=SGVector<float64_t>(features->get_dim_feature_space());
w.zero();
current_w = SGVector<float64_t>(features->get_dim_feature_space());
current_w.zero();

if (num_vec!=lab.vlen || num_vec<=0)
SG_ERROR("num_vec=%d num_train_labels=%d\n", num_vec, lab.vlen)

SG_FREE(old_w);
old_w=SG_CALLOC(float64_t, w.vlen);
old_w=SG_CALLOC(float64_t, current_w.vlen);
bias=0;
old_bias=0;

tmp_a_buf=SG_CALLOC(float64_t, w.vlen);
tmp_a_buf=SG_CALLOC(float64_t, current_w.vlen);
cp_value=SG_CALLOC(float64_t*, bufsize);
cp_index=SG_CALLOC(uint32_t*, bufsize);
cp_nz_dims=SG_CALLOC(uint32_t, bufsize);
Expand Down Expand Up @@ -144,6 +144,8 @@ bool CSVMOcas::train_machine(CFeatures* data)
SG_FREE(old_w);
old_w=NULL;

set_w(current_w);

return true;
}

Expand All @@ -158,8 +160,8 @@ float64_t CSVMOcas::update_W( float64_t t, void* ptr )
{
float64_t sq_norm_W = 0;
CSVMOcas* o = (CSVMOcas*) ptr;
uint32_t nDim = (uint32_t) o->w.vlen;
float64_t* W=o->w.vector;
uint32_t nDim = (uint32_t) o->current_w.vlen;
float64_t* W = o->current_w.vector;
float64_t* oldW=o->old_w;

for(uint32_t j=0; j <nDim; j++)
Expand Down Expand Up @@ -187,7 +189,7 @@ int CSVMOcas::add_new_cut(
{
CSVMOcas* o = (CSVMOcas*) ptr;
CDotFeatures* f = o->features;
uint32_t nDim=(uint32_t) o->w.vlen;
uint32_t nDim=(uint32_t) o->current_w.vlen;
float64_t* y = o->lab.vector;

float64_t** c_val = o->cp_value;
Expand Down Expand Up @@ -276,12 +278,10 @@ int CSVMOcas::compute_output(float64_t *output, void* ptr)

float64_t* y = o->lab.vector;

f->dense_dot_range(output, 0, nData, y, o->w.vector, o->w.vlen, 0.0);
f->dense_dot_range(output, 0, nData, y, o->current_w.vector, o->current_w.vlen, 0.0);

for (int32_t i=0; i<nData; i++)
output[i]+=y[i]*o->bias;
//CMath::display_vector(o->w, o->w.vlen, "w");
//CMath::display_vector(output, nData, "out");
return 0;
}

Expand All @@ -299,9 +299,9 @@ void CSVMOcas::compute_W(
void* ptr )
{
CSVMOcas* o = (CSVMOcas*) ptr;
uint32_t nDim= (uint32_t) o->w.vlen;
CMath::swap(o->w.vector, o->old_w);
float64_t* W=o->w.vector;
uint32_t nDim= (uint32_t) o->current_w.vlen;
CMath::swap(o->current_w.vector, o->old_w);
float64_t* W=o->current_w.vector;
float64_t* oldW=o->old_w;
memset(W, 0, sizeof(float64_t)*nDim);
float64_t old_bias=o->bias;
Expand Down
2 changes: 2 additions & 0 deletions src/shogun/classifier/svm/SVMOcas.h
Expand Up @@ -212,6 +212,8 @@ class CSVMOcas : public CLinearMachine
/** method */
E_SVM_TYPE method;

/** current W */
SGVector<float64_t> current_w;
/** old W */
float64_t* old_w;
/** old bias */
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/classifier/svm/SVMSGD.cpp
Expand Up @@ -88,7 +88,7 @@ bool CSVMSGD::train_machine(CFeatures* data)
ASSERT(num_vec==num_train_labels)
ASSERT(num_vec>0)

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

Expand Down Expand Up @@ -153,6 +153,8 @@ bool CSVMSGD::train_machine(CFeatures* data)
float64_t wnorm = CMath::dot(w.vector,w.vector, w.vlen);
SG_INFO("Norm: %.6f, Bias: %.6f\n", wnorm, bias)

set_w(w);

return true;
}

Expand Down
3 changes: 1 addition & 2 deletions src/shogun/latent/LatentSOSVM.cpp
Expand Up @@ -53,8 +53,7 @@ float64_t CLatentSOSVM::do_inner_loop(float64_t cooling_eps)
so->train();

/* copy the resulting w */
SGVector<float64_t> cur_w = so->get_w();
sg_memcpy(w.vector, cur_w.vector, cur_w.vlen*sizeof(float64_t));
set_w(so->get_w().clone());

/* get the primal objective value */
float64_t po = so->get_result().Fp;
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/latent/LatentSVM.cpp
Expand Up @@ -40,6 +40,7 @@ CLatentLabels* CLatentSVM::apply_latent()
if (m_model->get_num_vectors() < 1)
return NULL;

SGVector<float64_t> w = get_w();
index_t num_examples = m_model->get_num_vectors();
CLatentLabels* hs = new CLatentLabels(num_examples);
CBinaryLabels* ys = new CBinaryLabels(num_examples);
Expand Down Expand Up @@ -73,8 +74,7 @@ float64_t CLatentSVM::do_inner_loop(float64_t cooling_eps)
SG_UNREF(feats);

/* copy the resulting w */
SGVector<float64_t> cur_w = svm.get_w();
sg_memcpy(w.vector, cur_w.vector, cur_w.vlen*sizeof(float64_t));
set_w(svm.get_w().clone());

return svm.compute_primal_objective();
}
Expand Down
4 changes: 3 additions & 1 deletion src/shogun/machine/LinearLatentMachine.cpp
Expand Up @@ -28,8 +28,10 @@ CLinearLatentMachine::CLinearLatentMachine(CLatentModel* model, float64_t C)
set_model(model);

index_t feat_dim = m_model->get_dim();
SGVector<float64_t> w;
w.resize_vector(feat_dim);
w.zero();
set_w(w);
}

CLinearLatentMachine::~CLinearLatentMachine()
Expand Down Expand Up @@ -99,7 +101,7 @@ bool CLinearLatentMachine::train_machine(CFeatures* data)

/* find argmaxH */
SG_DEBUG("Find and set h_i = argmax_h (w, psi(x_i,h))\n")
m_model->argmax_h(w);
m_model->argmax_h(get_w());

SG_DEBUG("Recalculating PSI (x,h) with the new h variables\n")
m_model->cache_psi_features();
Expand Down
4 changes: 2 additions & 2 deletions src/shogun/machine/LinearMachine.cpp
Expand Up @@ -102,7 +102,7 @@ SGVector<float64_t> CLinearMachine::get_w() const

void CLinearMachine::set_w(const SGVector<float64_t> src_w)
{
w=src_w;
w = src_w;
}

void CLinearMachine::set_bias(float64_t b)
Expand Down Expand Up @@ -189,4 +189,4 @@ bool CLinearMachine::train(CFeatures* data)
compute_bias(data);

return result;
}
}