/usr/include/dlib/statistics/sammon.h is in libdlib-dev 18.18-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | // Copyright (C) 2012 Emanuele Cesena (emanuele.cesena@gmail.com), Davis E. King
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_SAMMoN_Hh_
#define DLIB_SAMMoN_Hh_
#include "sammon_abstract.h"
#include "../matrix.h"
#include "../algs.h"
#include "dpca.h"
#include <vector>
namespace dlib
{
class sammon_projection
{
public:
// ------------------------------------------------------------------------------------
template <typename matrix_type>
std::vector<matrix<double,0,1> > operator() (
const std::vector<matrix_type>& data,
const long num_dims
)
{
// make sure requires clause is not broken
DLIB_ASSERT(num_dims > 0,
"\t std::vector<matrix<double,0,1> > sammon_projection::operator()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t num_dims: " << num_dims
);
std::vector<matrix<double,0,1> > result; // projections
if (data.size() == 0)
{
return result;
}
#ifdef ENABLE_ASSERTS
DLIB_ASSERT(0 < num_dims && num_dims <= data[0].size(),
"\t std::vector<matrix<double,0,1> > sammon_projection::operator()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t data.size(): " << data.size()
<< "\n\t num_dims: " << num_dims
<< "\n\t data[0].size(): " << data[0].size()
);
for (unsigned long i = 0; i < data.size(); ++i)
{
DLIB_ASSERT(is_col_vector(data[i]) && data[i].size() == data[0].size(),
"\t std::vector<matrix<double,0,1> > sammon_projection::operator()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t data["<<i<<"].size(): " << data[i].size()
<< "\n\t data[0].size(): " << data[0].size()
<< "\n\t is_col_vector(data["<<i<<"]): " << is_col_vector(data[i])
);
}
#endif
double err; // error (discarded)
do_sammon_projection(data, num_dims, result, err);
return result;
}
// ------------------------------------------------------------------------------------
template <typename matrix_type>
void operator() (
const std::vector<matrix_type>& data,
const long num_dims,
std::vector<matrix<double,0,1> >& result,
double &err,
const unsigned long num_iters = 1000,
const double err_delta = 1.0e-9
)
{
// make sure requires clause is not broken
DLIB_ASSERT(num_dims > 0 && num_iters > 0 && err_delta > 0.0,
"\t std::vector<matrix<double,0,1> > sammon_projection::operator()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t data.size(): " << data.size()
<< "\n\t num_dims: " << num_dims
<< "\n\t num_iters: " << num_iters
<< "\n\t err_delta: " << err_delta
);
if (data.size() == 0)
{
result.clear();
err = 0;
return;
}
#ifdef ENABLE_ASSERTS
DLIB_ASSERT(0 < num_dims && num_dims <= data[0].size(),
"\t std::vector<matrix<double,0,1> > sammon_projection::operator()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t data.size(): " << data.size()
<< "\n\t num_dims: " << num_dims
<< "\n\t data[0].size(): " << data[0].size()
);
for (unsigned long i = 0; i < data.size(); ++i)
{
DLIB_ASSERT(is_col_vector(data[i]) && data[i].size() == data[0].size(),
"\t std::vector<matrix<double,0,1> > sammon_projection::operator()"
<< "\n\t Invalid inputs were given to this function."
<< "\n\t data["<<i<<"].size(): " << data[i].size()
<< "\n\t data[0].size(): " << data[0].size()
<< "\n\t is_col_vector(data["<<i<<"]): " << is_col_vector(data[i])
);
}
#endif
do_sammon_projection(data, num_dims, result, err, num_iters, err_delta);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
private:
void compute_relative_distances(
matrix<double,0,1>& dist, // relative distances (output)
matrix<double,0,0>& data, // input data (matrix whose columns are the input vectors)
double eps_ratio = 1.0e-7 // to compute the minimum distance eps
)
/*!
requires
- dist.nc() == comb( data.nc(), 2 ), preallocated
- eps_ratio > 0
ensures
- dist[k] == lenght(data[i] - data[j]) for k = j(j-1)/2 + i
!*/
{
const long N = data.nc(); // num of points
double eps; // minimum distance, forced to avoid vectors collision
// computed at runtime as eps_ration * mean(vectors distances)
for (int k = 0, i = 1; i < N; ++i)
for (int j = 0; j < i; ++j)
dist(k++) = length(colm(data, i) - colm(data, j));
eps = eps_ratio * mean(dist);
dist = lowerbound(dist, eps);
}
// ----------------------------------------------------------------------------------------
template <typename matrix_type>
void do_sammon_projection(
const std::vector<matrix_type>& data, // input data
unsigned long num_dims, // dimension of the reduced space
std::vector<matrix<double,0,1> >& result, // projections (output)
double &err, // error (output)
unsigned long num_iters = 1000, // max num of iterations: stop condition
const double err_delta = 1.0e-9 // delta error: stop condition
)
/*!
requires
- matrix_type should be a kind of dlib::matrix<double,N,1>
- num_dims > 0
- num_iters > 0
- err_delta > 0
ensures
- result == a set of matrix<double,num_dims,1> objects that represent
the Sammon's projections of data vectors.
- err == the estimated error done in the projection, with the extra
property that err(at previous iteration) - err < err_delta
!*/
{
// other params
const double mf = 0.3; // magic factor
matrix<double> mdata; // input data as matrix
matrix<double> projs; // projected vectors, i.e. output data as matrix
// std::vector<matrix> -> matrix
mdata.set_size(data[0].size(), data.size());
for (unsigned int i = 0; i < data.size(); i++)
set_colm(mdata, i) = data[i];
const long N = mdata.nc(); // num of points
const long d = num_dims; // size of the reduced space
const long nd = N * (N - 1) / 2; // num of pairs of points = size of the distances vectors
matrix<double, 0, 1> dsij, inv_dsij; // d*_ij: pair-wise distances in the input space (and inverses)
dsij.set_size(nd, 1);
inv_dsij.set_size(nd, 1);
double ic; // 1.0 / sum of dsij
matrix<double, 0, 1> dij; // d_ij: pair-wise distances in the reduced space
dij.set_size(nd, 1);
matrix<double, 0, 0> dE, dE2, dtemp; // matrices representing error partial derivatives
dE.set_size(d, N);
dE2.set_size(d, N);
dtemp.set_size(d, N);
matrix<double, 0, 1> inv_dij, alpha; // utility vectors used to compute the partial derivatives
inv_dij.set_size(N, 1); // inv_dij is 1.0/dij, but we only need it column-wise
alpha.set_size(N, 1); // (slightly wasting a bit of computation)
// alpha = 1.0/dij - 1.0/dsij, again column-wise
// initialize projs with PCA
discriminant_pca<matrix<double> > dpca;
for (int i = 0; i < mdata.nc(); ++i)
{
dpca.add_to_total_variance(colm(mdata, i));
}
matrix<double> mat = dpca.dpca_matrix_of_size(num_dims);
projs = mat * mdata;
// compute dsij, inv_dsij and ic
compute_relative_distances(dsij, mdata);
inv_dsij = 1.0 / dsij;
ic = 1.0 / sum(dsij);
// compute dij and err
compute_relative_distances(dij, projs);
err = ic * sum(pointwise_multiply(squared(dij - dsij), inv_dsij));
// start iterating
while (num_iters--)
{
// compute dE, dE2 progressively column by column
for (int p = 0; p < N; ++p)
{
// compute
// - alpha_p, the column vector with 1/d_pj - 1/d*_pj
// - dtemp, the matrix with the p-th column repeated all along
//TODO: optimize constructions
for (int i = 0; i < N; ++i)
{
int pos = (i < p) ? p * (p - 1) / 2 + i : i * (i - 1) / 2 + p;
inv_dij(i) = (i == p) ? 0.0 : 1.0 / dij(pos);
alpha(i) = (i == p) ? 0.0 : inv_dij(i) - inv_dsij(pos);
set_colm(dtemp, i) = colm(projs, p);
}
dtemp -= projs;
set_colm(dE, p) = dtemp * alpha;
double sum_alpha = sum(alpha);
set_colm(dE2, p) = abs( sum_alpha + squared(dtemp) * cubed(inv_dij) );
}
// compute the update projections
projs += pointwise_multiply(dE, mf * reciprocal(dE2));
// compute new dij and error
compute_relative_distances(dij, projs);
double err_new = ic * sum( pointwise_multiply(squared(dij - dsij), inv_dsij) );
if (err - err_new < err_delta)
break;
err = err_new;
}
// matrix -> std::vector<matrix>
result.clear();
for (int i = 0; i < projs.nc(); ++i)
result.push_back(colm(projs, i));
}
};
} // namespace dlib
#endif // DLIB_SAMMoN_Hh_
|