CMultitaskCompositeMachine::CMultitaskCompositeMachine(
     CMachine* machine, CFeatures* train_features, 
     CLabels* train_labels, CTaskGroup* task_group) :
	CMachine(), m_machine(NULL), m_features(NULL), 
	m_current_task(0), m_task_group(NULL)
{
	set_machine(machine);
	set_features(train_features);
	set_labels(train_labels);
	set_task_group(task_group);
	register_parameters();
}
Beispiel #2
0
CSLEPMachine::CSLEPMachine(
     float64_t z, CDotFeatures* train_features, 
     CLabels* train_labels) :
	CLinearMachine(), m_z(1.0)
{
	set_z(z);
	set_features(train_features);
	set_labels(train_labels);
	set_termination(slep_options::get_default_termination());
	set_regularization(slep_options::get_default_regularization());
	set_tolerance(slep_options::get_default_tolerance());
	set_max_iter(slep_options::get_default_max_iter());
}
Beispiel #3
0
// constructor/deconstructor
FeatureStatistics::FeatureStatistics(FeatureExtraction* featureObject, int sWinSize, int sHopSize){

	set_featureObject(FeatureExtraction* featureObject);


	set_sWinSize(sWinSize);
	set_sHopSize(sHopSize);
	calculate_nStatCols(); // no input; computed from nCols
	calculate_nStatRows(); // no input; computed from nRows and sHopSize

	set_features();
	set_d1Features();
	set_d2Features();
}
Beispiel #4
0
static PyObject *ncDatastoreAddAugment(PyObject *self, PyObject *args, PyObject *keywords)
{

	const char *path, *transapi = NULL;
	char *name = NULL;
	PyObject *PyFeatures = NULL;
	char *kwlist[] = {"model", "transapi", "features", NULL};

	/* Get input parameters */
	if (! PyArg_ParseTupleAndKeywords(args, keywords, "s|zO!", kwlist, &path, &transapi, &PyList_Type, &PyFeatures)) {
		return (NULL);
	}

	/* get name of the datastore for further referencing */
	if (ncds_model_info(path, &name, NULL, NULL, NULL, NULL, NULL) != EXIT_SUCCESS) {
		return (NULL);
	}

	/* create datastore */
	if (transapi) {
		if (ncds_add_augment_transapi(path, transapi) == EXIT_FAILURE) {
			free(name);
			return (NULL);
		}
	} else {
		if (ncds_add_model(path) == EXIT_FAILURE) {
			free(name);
			return (NULL);
		}
	}

	set_features(name, PyFeatures);
	free(name);

	if (ncds_consolidate() != EXIT_SUCCESS) {
		return (NULL);
	}

	Py_RETURN_NONE;
}
bool CMultitaskClusteredLogisticRegression::train_machine(CFeatures* data)
{
	if (data && (CDotFeatures*)data)
		set_features((CDotFeatures*)data);

	ASSERT(features)
	ASSERT(m_labels)
	ASSERT(m_task_relation)

	SGVector<float64_t> y(m_labels->get_num_labels());
	for (int32_t i=0; i<y.vlen; i++)
		y[i] = ((CBinaryLabels*)m_labels)->get_label(i);

	malsar_options options = malsar_options::default_options();
	options.termination = m_termination;
	options.tolerance = m_tolerance;
	options.max_iter = m_max_iter;
	options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
	options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();
	options.n_clusters = m_num_clusters;

#ifndef HAVE_CXX11
	malsar_result_t model = malsar_clustered(
		features, y.vector, m_rho1, m_rho2, options);

	m_tasks_w = model.w;
	m_tasks_c = model.c;
#else
	SG_WARNING("Clustered LR is unstable with C++11\n")
	m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks);
	m_tasks_w.set_const(0);
	m_tasks_c = SGVector<float64_t>(options.n_tasks);
	m_tasks_c.set_const(0);
#endif

	SG_FREE(options.tasks_indices);

	return true;
}
bool CMultitaskTraceLogisticRegression::train_machine(CFeatures* data)
{
	if (data && (CDotFeatures*)data)
		set_features((CDotFeatures*)data);

	ASSERT(features);
	ASSERT(m_labels);
	ASSERT(m_task_relation);

	SGVector<float64_t> y(m_labels->get_num_labels());
	for (int32_t i=0; i<y.vlen; i++)
		y[i] = ((CBinaryLabels*)m_labels)->get_label(i);
	
	malsar_options options = malsar_options::default_options();
	options.termination = m_termination;
	options.tolerance = m_tolerance;
	options.max_iter = m_max_iter;
	options.n_tasks = ((CTaskGroup*)m_task_relation)->get_num_tasks();
	options.tasks_indices = ((CTaskGroup*)m_task_relation)->get_tasks_indices();

#ifdef HAVE_EIGEN3
	malsar_result_t model = malsar_low_rank(
		features, y.vector, m_rho, options);

	m_tasks_w = model.w;
	m_tasks_c = model.c;
#else
	SG_WARNING("Please install Eigen3 to use MultitaskTraceLogisticRegression\n");
	m_tasks_w = SGMatrix<float64_t>(((CDotFeatures*)features)->get_dim_feature_space(), options.n_tasks); 
	m_tasks_c = SGVector<float64_t>(options.n_tasks); 
#endif

	for (int32_t i=0; i<options.n_tasks; i++)
		options.tasks_indices[i].~SGVector<index_t>();
	SG_FREE(options.tasks_indices);

	return true;
}
Beispiel #7
0
static PyObject *ncDatastoreAddImport(PyObject *self, PyObject *args, PyObject *keywords)
{
	const char *path;
	char *name = NULL;
	PyObject *PyFeatures = NULL;
	char *kwlist[] = {"model", "features", NULL};

	/* Get input parameters */
	if (! PyArg_ParseTupleAndKeywords(args, keywords, "s|O!", kwlist, &path, &PyList_Type, &PyFeatures)) {
		return (NULL);
	}

	if (ncds_model_info(path, &name, NULL, NULL, NULL, NULL, NULL) != EXIT_SUCCESS ||
			ncds_add_model(path) != EXIT_SUCCESS) {
		free(name);
		return (NULL);
	}

	set_features(name, PyFeatures);

	free(name);
	Py_RETURN_NONE;
}
void CMultitaskCompositeMachine::post_lock(CLabels* labels, CFeatures* features)
{
	ASSERT(m_task_group);
	set_features(m_features);
	if (!m_machine->is_data_locked())
		m_machine->data_lock(labels,features);

	int n_tasks = m_task_group->get_num_tasks();
	SGVector<index_t>* tasks_indices = m_task_group->get_tasks_indices();

	m_tasks_indices.clear();
	for (int32_t i=0; i<n_tasks; i++)
	{
		set<index_t> indices_set;
		SGVector<index_t> task_indices = tasks_indices[i];
		for (int32_t j=0; j<task_indices.vlen; j++)
			indices_set.insert(task_indices[j]);

		m_tasks_indices.push_back(indices_set);
	}

	SG_FREE(tasks_indices);
}
Beispiel #9
0
static PyObject *ncDatastoreAdd(PyObject *self, PyObject *args, PyObject *keywords)
{
	const char *path, *datastore = NULL, *transapi = NULL;
	char *name = NULL;
	NCDS_TYPE type = NCDS_TYPE_EMPTY;
	struct ncds_ds *ds;
	ncds_id dsid;
	PyObject *PyFeatures = NULL;
	char *kwlist[] = {"model", "datastore", "transapi", "features", NULL};

	/* Get input parameters */
	if (! PyArg_ParseTupleAndKeywords(args, keywords, "s|zzO!", kwlist, &path, &datastore, &transapi, &PyList_Type, &PyFeatures)) {
		return (NULL);
	}

	/* set correct type according to provided parameters */
	if (datastore) {
		type = NCDS_TYPE_FILE;
	}

	/* get name of the datastore for further referencing */
	if (ncds_model_info(path, &name, NULL, NULL, NULL, NULL, NULL) != EXIT_SUCCESS) {
		return (NULL);
	}

	/* create datastore */
	if (transapi) {
		if ((ds = ncds_new_transapi(type, path, transapi)) == NULL) {
			free(name);
			return (NULL);
		}
	} else {
		/* todo get_state() */
		if ((ds = ncds_new(type, path, NULL)) == NULL) {
			free(name);
			return (NULL);
		}
	}

	if (datastore) {
		if (ncds_file_set_path(ds, datastore) != EXIT_SUCCESS) {
			ncds_free(ds);
			free(name);
			return (NULL);
		}
	}

	if ((dsid = ncds_init(ds)) <= 0) {
		ncds_free(ds);
		free(name);
		return (NULL);
	}

	set_features(name, PyFeatures);

	if (ncds_consolidate() != EXIT_SUCCESS) {
		ncds_free(ds);
		free(name);
		return (NULL);
	}

	if (ncds_device_init(&dsid, global_cpblts, 0)) {
		ncds_free(ds);
		free(name);
		return (NULL);
	}

	PyDict_SetItem(datastores, PyUnicode_FromFormat("%d", dsid), PyUnicode_FromString(name));

	free(name);
	Py_RETURN_NONE;
}