SHOGUN  4.2.0
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
ECOCIHDDecoder.cpp
Go to the documentation of this file.
1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 3 of the License, or
5  * (at your option) any later version.
6  *
7  * Written (W) 2012 Chiyuan Zhang
8  * Copyright (C) 2012 Chiyuan Zhang
9  */
10 
11 #include <shogun/lib/config.h>
12 
13 #ifdef HAVE_LAPACK
14 
19 
20 using namespace shogun;
21 
22 
24 {
25  update_delta_cache(codebook);
26 
27  SGVector<float64_t> query = binarize(outputs);
28  SGVector<float64_t> L(codebook.num_cols);
29  for (int32_t i=0; i < codebook.num_cols; ++i)
30  L[i] = CECOCUtil::hamming_distance(query.vector, codebook.get_column_vector(i), query.vlen);
31 
32  SGVector<float64_t> res(codebook.num_cols);
33  res.zero();
34  // res = m_delta * L
35  cblas_dgemv(CblasColMajor, CblasNoTrans, m_delta.num_cols, m_delta.num_cols,
36  1, m_delta.matrix, m_delta.num_cols, L.vector, 1, 1, res.vector, 1);
37  return CMath::arg_max(res.vector, 1, res.vlen);
38 }
39 
41 {
42  if (codebook.matrix == m_codebook.matrix)
43  return; // memory address the same
44 
45  if (codebook.num_cols == m_codebook.num_cols && codebook.num_rows == m_codebook.num_rows)
46  {
47  bool the_same = true;
48  for (int32_t i=0; i < codebook.num_rows && the_same; ++i)
49  for (int32_t j=0; j < codebook.num_cols && the_same; ++j)
50  if (codebook(i,j) != m_codebook(i,j))
51  the_same = false;
52  if (the_same)
53  return; // no need to update delta
54  }
55 
56  m_codebook = codebook; // operator=
57  m_delta = SGMatrix<float64_t>(codebook.num_cols, codebook.num_cols);
58  m_delta.zero();
59  for (int32_t i=0; i < codebook.num_cols; ++i)
60  {
61  for (int32_t j=i+1; j < codebook.num_cols; ++j)
62  {
63  m_delta(i, j) = m_delta(j, i) =
64  CECOCUtil::hamming_distance(codebook.get_column_vector(i), codebook.get_column_vector(j), codebook.num_rows);
65  }
66  }
67 
68  // compute inverse of delta
70  clapack_dgetrf(CblasColMajor, m_delta.num_cols, m_delta.num_cols, m_delta.matrix, m_delta.num_cols, IPIV.vector);
71  clapack_dgetri(CblasColMajor, m_delta.num_cols, m_delta.matrix, m_delta.num_cols, IPIV.vector);
72 }
73 
74 #endif // HAVE_LAPACK
static int32_t arg_max(T *vec, int32_t inc, int32_t len, T *maxv_ptr=NULL)
Definition: Math.h:262
SGMatrix< float64_t > m_delta
static int32_t hamming_distance(T1 *c1, T2 *c2, int32_t len)
Definition: ECOCUtil.h:31
index_t num_cols
Definition: SGMatrix.h:376
index_t num_rows
Definition: SGMatrix.h:374
index_t vlen
Definition: SGVector.h:494
SGMatrix< int32_t > m_codebook
SGVector< float64_t > binarize(const SGVector< float64_t > query)
Definition: ECOCDecoder.cpp:16
T * get_column_vector(index_t col) const
Definition: SGMatrix.h:113
virtual int32_t decide_label(const SGVector< float64_t > outputs, const SGMatrix< int32_t > codebook)
all of classes and functions are contained in the shogun namespace
Definition: class_list.h:18
void update_delta_cache(const SGMatrix< int32_t > codebook)

SHOGUN Machine Learning Toolbox - Documentation