CMulticlassLabels* CRelaxedTree::apply_multiclass(CFeatures* data)
{
    if (data != NULL)
    {
        CDenseFeatures<float64_t> *feats = dynamic_cast<CDenseFeatures<float64_t>*>(data);
        REQUIRE(feats != NULL, ("Require non-NULL dense features of float64_t\n"))
        set_features(feats);
    }

    // init kernels for all sub-machines
    for (int32_t i=0; i<m_machines->get_num_elements(); i++)
    {
        CSVM *machine = (CSVM*)m_machines->get_element(i);
        CKernel *kernel = machine->get_kernel();
        CFeatures* lhs = kernel->get_lhs();
        kernel->init(lhs, m_feats);
        SG_UNREF(machine);
        SG_UNREF(kernel);
        SG_UNREF(lhs);
    }

    CMulticlassLabels *lab = new CMulticlassLabels(m_feats->get_num_vectors());
    SG_REF(lab);
    for (int32_t i=0; i < lab->get_num_labels(); ++i)
    {
        lab->set_int_label(i, int32_t(apply_one(i)));
    }

    return lab;
}
void CGaussianProcessRegression::update_kernel_matrices()
{
	CKernel* kernel = NULL;

	if (m_method)
		kernel = m_method->get_kernel();

	if (kernel)
	{
		float64_t m_scale = m_method->get_scale();

		CFeatures* latent_features = m_method->get_latent_features();
		
		if (latent_features)
			kernel->init(latent_features, m_data);
		else
			kernel->init(m_data, m_data);

		//K(X_test, X_train)
		m_k_trts = kernel->get_kernel_matrix();

		for (index_t i = 0; i < m_k_trts.num_rows; i++)
		{
			for (index_t j = 0; j < m_k_trts.num_cols; j++)
				m_k_trts(i,j) *= (m_scale*m_scale);
		}

		kernel->init(m_data, m_data);

		m_k_tsts = kernel->get_kernel_matrix();

		for (index_t i = 0; i < m_k_tsts.num_rows; i++)
		{
			for (index_t j = 0; j < m_k_tsts.num_cols; j++)
				m_k_tsts(i,j) *= (m_scale*m_scale);
		}
		
		kernel->remove_lhs_and_rhs();

		SG_UNREF(kernel);
		SG_UNREF(latent_features);
	}
}
Exemple #3
0
void CGaussianProcessRegression::update_kernel_matrices()
{
	CKernel* kernel = NULL;

	if (m_method)
		kernel = m_method->get_kernel();

	if (kernel)
	{
		float64_t m_scale = m_method->get_scale();

		kernel->cleanup();

		kernel->init(m_features, m_data);

		//K(X_test, X_train)
		m_k_trts = kernel->get_kernel_matrix();

		for (index_t i = 0; i < m_k_trts.num_rows; i++)
		{
			for (index_t j = 0; j < m_k_trts.num_cols; j++)
				m_k_trts(i,j) *= (m_scale*m_scale);
		}

		kernel->cleanup();

		kernel->init(m_data, m_data);

		m_k_tsts = kernel->get_kernel_matrix();

		for (index_t i = 0; i < m_k_tsts.num_rows; i++)
		{
			for (index_t j = 0; j < m_k_tsts.num_cols; j++)
				m_k_tsts(i,j) *= (m_scale*m_scale);
		}

		SG_UNREF(kernel);
	}
}
SGVector<int32_t> CRelaxedTree::train_node_with_initialization(const CRelaxedTree::entry_t &mu_entry, SGVector<int32_t> classes, CSVM *svm)
{
    SGVector<int32_t> mu(classes.vlen), prev_mu(classes.vlen);
    mu.zero();
    mu[mu_entry.first.first] = 1;
    mu[mu_entry.first.second] = -1;

    SGVector<int32_t> long_mu(m_num_classes);
    svm->set_C(m_svm_C, m_svm_C);
    svm->set_epsilon(m_svm_epsilon);

    for (int32_t iiter=0; iiter < m_max_num_iter; ++iiter)
    {
        long_mu.zero();
        for (int32_t i=0; i < classes.vlen; ++i)
        {
            if (mu[i] == 1)
                long_mu[classes[i]] = 1;
            else if (mu[i] == -1)
                long_mu[classes[i]] = -1;
        }

        SGVector<int32_t> subset(m_feats->get_num_vectors());
        SGVector<float64_t> binlab(m_feats->get_num_vectors());
        int32_t k=0;

        CMulticlassLabels *labs = dynamic_cast<CMulticlassLabels *>(m_labels);
        for (int32_t i=0; i < binlab.vlen; ++i)
        {
            int32_t lab = labs->get_int_label(i);
            binlab[i] = long_mu[lab];
            if (long_mu[lab] != 0)
                subset[k++] = i;
        }

        subset.vlen = k;

        CBinaryLabels *binary_labels = new CBinaryLabels(binlab);
        SG_REF(binary_labels);
        binary_labels->add_subset(subset);
        m_feats->add_subset(subset);

        CKernel *kernel = (CKernel *)m_kernel->shallow_copy();
        kernel->init(m_feats, m_feats);
        svm->set_kernel(kernel);
        svm->set_labels(binary_labels);
        svm->train();

        binary_labels->remove_subset();
        m_feats->remove_subset();
        SG_UNREF(binary_labels);

        std::copy(&mu[0], &mu[mu.vlen], &prev_mu[0]);

        mu = color_label_space(svm, classes);

        bool bbreak = true;
        for (int32_t i=0; i < mu.vlen; ++i)
        {
            if (mu[i] != prev_mu[i])
            {
                bbreak = false;
                break;
            }
        }

        if (bbreak)
            break;
    }

    return mu;
}