int main()
{
	init_shogun_with_defaults();
	CTwoStateModel* tsm = new CTwoStateModel();
	CHMSVMModel* model = tsm->simulate_data(100,250,3,1);
	CStructuredLabels* labels = model->get_labels();
	CDualLibQPBMSOSVM* sosvm = new CDualLibQPBMSOSVM(model, labels, 5000.0);
	sosvm->train();

	SG_UNREF(sosvm);
	SG_UNREF(labels);
	SG_UNREF(tsm);
	exit_shogun();
	return 0;
}
Ejemplo n.º 2
0
float64_t CLatentSOSVM::do_inner_loop(float64_t cooling_eps)
{
	float64_t lambda = 1/m_C;
  CDualLibQPBMSOSVM* so = new CDualLibQPBMSOSVM();
	so->train();

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

	/* get the primal objective value */
	float64_t po = so->get_result().Fp;

	SG_UNREF(so);

	return po;
}
Ejemplo n.º 3
0
int main(int argc, char * argv[])
{
	// initialization
	//-------------------------------------------------------------------------

	float64_t lambda=0.01, eps=0.01;
	bool icp=1;
	uint32_t cp_models=1;
	ESolver solver=BMRM;
	uint32_t feat_dim, num_feat;

	init_shogun_with_defaults();

	if (argc > 1 && argc < 8)
	{
		SG_SERROR("Usage: so_multiclass_BMRM <data.in> <feat_dim> <num_feat> <lambda> <icp> <epsilon> <solver> [<cp_models>]\n");
		return -1;
	}

	if (argc > 1)
	{
		// parse command line arguments for parameters setting

		SG_SPRINT("arg[1] = %s\n", argv[1]);

		feat_dim=::atoi(argv[2]);
		num_feat=::atoi(argv[3]);
		lambda=::atof(argv[4]);
		icp=::atoi(argv[5]);
		eps=::atof(argv[6]);

		if (strcmp("BMRM", argv[7])==0)
			solver=BMRM;

		if (strcmp("PPBMRM", argv[7])==0)
			solver=PPBMRM;

		if (strcmp("P3BMRM", argv[7])==0)
			solver=P3BMRM;

		if (argc > 8)
		{
			cp_models=::atoi(argv[8]);
		}
	}
	else
	{
		// default parameters

		feat_dim=DIMS;
		num_feat=NUM_SAMPLES*NUM_CLASSES;
		lambda=1e3;
		icp=1;
		eps=0.01;
		solver=BMRM;
	}

	SGVector<float64_t> labs(num_feat);

	SGMatrix<float64_t> feats(feat_dim, num_feat);

	if (argc==1)
	{
		gen_rand_data(labs, feats);
	}
	else
	{
		// read data
		read_data(argv[1], feat_dim, num_feat, labs, feats);
	}

	// Create train labels
	CMulticlassSOLabels* labels = new CMulticlassSOLabels(labs);

	// Create train features
	CDenseFeatures< float64_t >* features =
		new CDenseFeatures< float64_t >(feats);

	// Create structured model
	CMulticlassModel* model = new CMulticlassModel(features, labels);

	// Create SO-SVM
	CDualLibQPBMSOSVM* sosvm =
		new CDualLibQPBMSOSVM(
				model,
				labels,
				lambda);
	SG_REF(sosvm);

	sosvm->set_cleanAfter(10);
	sosvm->set_cleanICP(icp);
	sosvm->set_TolRel(eps);
	sosvm->set_cp_models(cp_models);
	sosvm->set_solver(solver);

	// Train
	//-------------------------------------------------------------------------

	SG_SPRINT("Train using lambda = %lf ICP removal = %d \n",
			sosvm->get_lambda(), sosvm->get_cleanICP());

	sosvm->train();

	BmrmStatistics res = sosvm->get_result();

	SG_SPRINT("result = { Fp=%lf, Fd=%lf, nIter=%d, nCP=%d, nzA=%d, exitflag=%d }\n",
			res.Fp, res.Fd, res.nIter, res.nCP, res.nzA, res.exitflag);

	CStructuredLabels* out =
		CLabelsFactory::to_structured(sosvm->apply());
	SG_REF(out);

	SG_SPRINT("\n");

	// Compute error
	//-------------------------------------------------------------------------
	float64_t error=0.0;

	for (uint32_t i=0; i<num_feat; ++i)
	{
		CRealNumber* rn = CRealNumber::obtain_from_generic( out->get_label(i) );
		error+=(rn->value==labs.get_element(i)) ? 0.0 : 1.0;
		SG_UNREF(rn);	// because of out->get_label(i) above
	}

	SG_SPRINT("Error = %lf %% \n", error/num_feat*100);


	// Free memory
	SG_UNREF(sosvm);
	SG_UNREF(out);

	exit_shogun();

	return 0;
}
Ejemplo n.º 4
0
int main(int argc, char ** argv)
{
	init_shogun_with_defaults();
	
	SGVector< float64_t > labs(NUM_CLASSES*NUM_SAMPLES);
	SGMatrix< float64_t > feats(DIMS, NUM_CLASSES*NUM_SAMPLES);

	gen_rand_data(labs, feats);
	//read_data(labs, feats);

	// Create train labels
	CMulticlassSOLabels* labels = new CMulticlassSOLabels(labs);
	CMulticlassLabels*  mlabels = new CMulticlassLabels(labs);

	// Create train features
	CDenseFeatures< float64_t >* features = new CDenseFeatures< float64_t >(feats);

	// Create structured model
	CMulticlassModel* model = new CMulticlassModel(features, labels);

	// Create loss function
	CHingeLoss* loss = new CHingeLoss();

	// Create SO-SVM
	CPrimalMosekSOSVM* sosvm = new CPrimalMosekSOSVM(model, loss, labels);
	CDualLibQPBMSOSVM* bundle = new CDualLibQPBMSOSVM(model, loss, labels, 1000);
	bundle->set_verbose(false);
	SG_REF(sosvm);
	SG_REF(bundle);

	CTime start;
	float64_t t1;
	sosvm->train();
	SG_SPRINT(">>>> PrimalMosekSOSVM trained in %9.4f\n", (t1 = start.cur_time_diff(false)));
	bundle->train();
	SG_SPRINT(">>>> BMRM trained in %9.4f\n", start.cur_time_diff(false)-t1);
	CStructuredLabels* out = CStructuredLabels::obtain_from_generic(sosvm->apply());
	CStructuredLabels* bout = CStructuredLabels::obtain_from_generic(bundle->apply());

	// Create liblinear svm classifier with L2-regularized L2-loss
	CLibLinear* svm = new CLibLinear(L2R_L2LOSS_SVC);

	// Add some configuration to the svm
	svm->set_epsilon(EPSILON);
	svm->set_bias_enabled(false);

	// Create a multiclass svm classifier that consists of several of the previous one
	CLinearMulticlassMachine* mc_svm = 
			new CLinearMulticlassMachine( new CMulticlassOneVsRestStrategy(), 
			(CDotFeatures*) features, svm, mlabels);
	SG_REF(mc_svm);

	// Train the multiclass machine using the data passed in the constructor
	mc_svm->train();
	CMulticlassLabels* mout = CMulticlassLabels::obtain_from_generic(mc_svm->apply());

	SGVector< float64_t > w = sosvm->get_w();
	for ( int32_t i = 0 ; i < w.vlen ; ++i )
		SG_SPRINT("%10f ", w[i]);
	SG_SPRINT("\n\n");

	for ( int32_t i = 0 ; i < NUM_CLASSES ; ++i )
	{
		CLinearMachine* lm = (CLinearMachine*) mc_svm->get_machine(i);
		SGVector< float64_t > mw = lm->get_w();
		for ( int32_t j = 0 ; j < mw.vlen ; ++j )
			SG_SPRINT("%10f ", mw[j]);

		SG_UNREF(lm); // because of CLinearMulticlassMachine::get_machine()
	}
	SG_SPRINT("\n");

	CStructuredAccuracy* structured_evaluator = new CStructuredAccuracy();
	CMulticlassAccuracy* multiclass_evaluator = new CMulticlassAccuracy();
	SG_REF(structured_evaluator);
	SG_REF(multiclass_evaluator);

	SG_SPRINT("SO-SVM: %5.2f%\n", 100.0*structured_evaluator->evaluate(out, labels));
	SG_SPRINT("BMRM:   %5.2f%\n", 100.0*structured_evaluator->evaluate(bout, labels));
	SG_SPRINT("MC:     %5.2f%\n", 100.0*multiclass_evaluator->evaluate(mout, mlabels));

	// Free memory
	SG_UNREF(multiclass_evaluator);
	SG_UNREF(structured_evaluator);
	SG_UNREF(mout);
	SG_UNREF(mc_svm);
	SG_UNREF(bundle);
	SG_UNREF(sosvm);
	SG_UNREF(bout);
	SG_UNREF(out);
	exit_shogun();

	return 0;
}