<commit_before>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2012 <NAME>
 * Copyright (C) 2012 <NAME>
 */


#include <shogun/classifier/svm/MulticlassLibLinear.h>
#include <shogun/classifier/svm/SVM_linear.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

bool CMulticlassLibLinear::train_machine(CFeatures* data)
{
	if (data)
		set_features((CDotFeatures*)data);

	int32_t num_vectors = m_features->get_num_vectors();
	int32_t num_classes = labels->get_num_classes();
	
	problem mc_problem;
	mc_problem.l = num_vectors;
	mc_problem.n = m_features->get_dim_feature_space()+1;
	mc_problem.y = SG_MALLOC(int32_t, mc_problem.l);
	for (int32_t i=0; i<num_vectors; i++)
		mc_problem.y[i] = labels->get_int_label(i);

	mc_problem.x = m_features;
	mc_problem.use_bias = m_use_bias;

	float64_t* w = SG_MALLOC(float64_t, mc_problem.n*num_classes);
	float64_t* C = SG_MALLOC(float64_t, num_vectors);
	for (int32_t i=0; i<num_vectors; i++)
		C[i] = m_C;

	Solver_MCSVM_CS solver(&mc_problem,num_classes,C,m_epsilon,m_max_iter);
	solver.Solve(w);

	m_machines.destroy_vector();
	m_machines = SGVector<CMachine*>(num_classes);
	for (int32_t i=0; i<num_classes; i++)
	{
		CLinearMachine* machine = new CLinearMachine();
		float64_t* cw = SG_MALLOC(float64_t, mc_problem.n);
		for (int32_t j=0; j<mc_problem.n-1; j++)
			cw[j] = w[j*num_classes+i];
		machine->set_w(SGVector<float64_t>(cw,mc_problem.n-1));
		CMath::display_vector(cw,mc_problem.n);
		machine->set_bias(w[(mc_problem.n-1)*num_classes+i]);

		m_machines[i] = machine;
	}

	SG_FREE(C);
	SG_FREE(w);
	SG_FREE(mc_problem.y);

	return true;
}<commit_msg>Added bias usage capability for crammer-singer liblinear<commit_after>/*
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 3 of the License, or
 * (at your option) any later version.
 *
 * Written (W) 2012 <NAME>
 * Copyright (C) 2012 <NAME>
 */


#include <shogun/classifier/svm/MulticlassLibLinear.h>
#include <shogun/classifier/svm/SVM_linear.h>
#include <shogun/mathematics/Math.h>

using namespace shogun;

bool CMulticlassLibLinear::train_machine(CFeatures* data)
{
	if (data)
		set_features((CDotFeatures*)data);

	int32_t num_vectors = m_features->get_num_vectors();
	int32_t num_classes = labels->get_num_classes();
	
	problem mc_problem;
	mc_problem.l = num_vectors;
	mc_problem.n = m_features->get_dim_feature_space()+1;
	mc_problem.y = SG_MALLOC(int32_t, mc_problem.l);
	for (int32_t i=0; i<num_vectors; i++)
		mc_problem.y[i] = labels->get_int_label(i);

	mc_problem.x = m_features;
	mc_problem.use_bias = m_use_bias;

	float64_t* w = SG_MALLOC(float64_t, mc_problem.n*num_classes);
	float64_t* C = SG_MALLOC(float64_t, num_vectors);
	for (int32_t i=0; i<num_vectors; i++)
		C[i] = m_C;

	Solver_MCSVM_CS solver(&mc_problem,num_classes,C,m_epsilon,m_max_iter);
	solver.Solve(w);

	m_machines.destroy_vector();
	m_machines = SGVector<CMachine*>(num_classes);
	for (int32_t i=0; i<num_classes; i++)
	{
		CLinearMachine* machine = new CLinearMachine();
		float64_t* cw = SG_MALLOC(float64_t, mc_problem.n);
		for (int32_t j=0; j<mc_problem.n-1; j++)
			cw[j] = w[j*num_classes+i];
		machine->set_w(SGVector<float64_t>(cw,mc_problem.n-1));
		//CMath::display_vector(cw,mc_problem.n);
		machine->set_bias(w[(mc_problem.n-1)*num_classes+i]);

		m_machines[i] = machine;
	}

	SG_FREE(C);
	SG_FREE(w);
	SG_FREE(mc_problem.y);

	return true;
}
{'fraction_non_alphanumeric': 0.13742298419501742, 'fraction_numerical': 0.02410929547281007, 'mean_word_length': 5.597173144876325, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 29, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2006701', 'n_tokens_mistral': 1674, 'n_tokens_neox': 1512, 'n_words': 290}