SHOGUN  3.2.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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 SGVector<float64_t>::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

SHOGUN Machine Learning Toolbox - Documentation