Skip to content

Commit

Permalink
fixed memory bug when cross-validation was performed twice with diffe…
Browse files Browse the repository at this point in the history
…rent number of runs
  • Loading branch information
karlnapf committed Aug 18, 2012
1 parent 38642f1 commit cf6beaf
Showing 1 changed file with 25 additions and 5 deletions.
30 changes: 25 additions & 5 deletions src/shogun/evaluation/CrossValidationMKLStorage.cpp
Expand Up @@ -30,19 +30,39 @@ void CCrossValidationMKLStorage::update_trained_machine(

SGVector<float64_t> w=kernel->get_subkernel_weights();

/* evtl re-allocate memory (different number of runs from evaluation before) */
if (m_mkl_weights.num_rows!=w.vlen ||
m_mkl_weights.num_cols!=m_num_folds*m_num_runs)
{
if (m_mkl_weights.matrix)
{
SG_DEBUG("deleting memory for mkl weight matrix\n");
m_mkl_weights=SGMatrix<float64_t>();
}
}

/* evtl allocate memory (first call) */
if (!m_mkl_weights.matrix)
{
SG_PRINT("allocating memory for mkl weight matrix\n");
m_mkl_weights=SGMatrix<float64_t>(w.vlen, m_num_folds*m_num_runs);
SG_DEBUG("allocating memory for mkl weight matrix\n");
m_mkl_weights=SGMatrix<float64_t>(w.vlen,m_num_folds*m_num_runs);
}

/* put current mkl weights into matrix, copy memory vector wise to make
* things fast */
index_t n=m_current_run_index*m_current_fold_index;
index_t first_idx=n*w.vlen+m_current_fold_index*w.vlen;
* things fast. Compute index of address to where vector goes */

/* number of runs is w.vlen*m_num_folds shift */
index_t run_shift=m_current_run_index*w.vlen*m_num_folds;

/* fold shift is m_current_fold_index*w-vlen */
index_t fold_shift=m_current_fold_index*w.vlen;

/* add both index shifts */
index_t first_idx=run_shift+fold_shift;
SG_DEBUG("run %d, fold %d, matrix index %d\n",m_current_run_index,
m_current_fold_index, first_idx);

/* copy memory */
memcpy(&m_mkl_weights.matrix[first_idx], w.vector,
w.vlen*sizeof(float64_t));

Expand Down

0 comments on commit cf6beaf

Please sign in to comment.