Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
OpenDAS
dlib
Commits
5f191804
Commit
5f191804
authored
Dec 25, 2011
by
Davis King
Browse files
Moved create_random_projection_hash() into its own file.
parent
320d56ce
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
120 additions
and
83 deletions
+120
-83
dlib/lsh.h
dlib/lsh.h
+1
-0
dlib/lsh/create_random_projection_hash.h
dlib/lsh/create_random_projection_hash.h
+93
-0
dlib/lsh/create_random_projection_hash_abstract.h
dlib/lsh/create_random_projection_hash_abstract.h
+26
-0
dlib/lsh/projection_hash.h
dlib/lsh/projection_hash.h
+0
-73
dlib/lsh/projection_hash_abstract.h
dlib/lsh/projection_hash_abstract.h
+0
-10
No files found.
dlib/lsh.h
View file @
5f191804
...
@@ -5,6 +5,7 @@
...
@@ -5,6 +5,7 @@
#include "lsh/projection_hash.h"
#include "lsh/projection_hash.h"
#include "lsh/create_random_projection_hash.h"
#endif // DLIB_LSh_
#endif // DLIB_LSh_
...
...
dlib/lsh/create_random_projection_hash.h
0 → 100644
View file @
5f191804
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#ifndef DLIB_CREATE_RANDOM_PROJECTION_HAsH_H__
#define DLIB_CREATE_RANDOM_PROJECTION_HAsH_H__
#include "create_random_projection_hash_abstract.h"
#include "projection_hash.h"
#include "../matrix.h"
#include "../rand.h"
#include <vector>
namespace
dlib
{
// ----------------------------------------------------------------------------------------
template
<
typename
vector_type
>
projection_hash
create_random_projection_hash
(
const
vector_type
&
v
,
const
int
bits
)
{
// compute a whitening matrix
matrix
<
double
>
whiten
=
trans
(
chol
(
pinv
(
covariance
(
vector_to_matrix
(
v
)))));
// hashes
std
::
vector
<
unsigned
long
>
h
(
v
.
size
(),
0
);
std
::
vector
<
double
>
vals
(
v
.
size
(),
0
);
// number of hits for each hash value
std
::
vector
<
unsigned
long
>
counts
;
std
::
vector
<
double
>
temp
;
// build a random projection matrix
dlib
::
rand
rnd
;
matrix
<
double
>
proj
(
bits
,
v
[
0
].
size
());
for
(
long
r
=
0
;
r
<
proj
.
nr
();
++
r
)
for
(
long
c
=
0
;
c
<
proj
.
nc
();
++
c
)
proj
(
r
,
c
)
=
rnd
.
get_random_gaussian
();
// merge whitening matrix with projection matrix
proj
=
proj
*
whiten
;
matrix
<
double
,
0
,
1
>
offset
(
bits
);
// figure out what the offset values should be
for
(
int
itr
=
0
;
itr
<
offset
.
size
();
++
itr
)
{
counts
.
assign
(
std
::
pow
(
2
,
bits
),
0
);
// count the popularity of each hash value
for
(
unsigned
long
i
=
0
;
i
<
h
.
size
();
++
i
)
{
h
[
i
]
<<=
1
;
counts
[
h
[
i
]]
+=
1
;
}
const
unsigned
long
max_h
=
index_of_max
(
vector_to_matrix
(
counts
));
temp
.
clear
();
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
vals
[
i
]
=
dot
(
rowm
(
proj
,
itr
),
v
[
i
]);
if
(
h
[
i
]
==
max_h
)
temp
.
push_back
(
vals
[
i
]);
}
// split down the middle
std
::
sort
(
temp
.
begin
(),
temp
.
end
());
const
double
split
=
temp
[
temp
.
size
()
/
2
];
offset
(
itr
)
=
-
split
;
for
(
unsigned
long
i
=
0
;
i
<
vals
.
size
();
++
i
)
{
if
(
vals
[
i
]
-
split
>
0
)
h
[
i
]
|=
1
;
}
}
return
projection_hash
(
proj
,
offset
);
}
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_CREATE_RANDOM_PROJECTION_HAsH_H__
dlib/lsh/create_random_projection_hash_abstract.h
0 → 100644
View file @
5f191804
// Copyright (C) 2011 Davis E. King (davis@dlib.net)
// License: Boost Software License See LICENSE.txt for the full license.
#undef DLIB_CREATE_RANDOM_PROJECTION_HAsH_ABSTRACT_H__
#ifdef DLIB_CREATE_RANDOM_PROJECTION_HAsH_ABSTRACT_H__
#include "projection_hash_abstract.h"
#include "../rand.h"
namespace
dlib
{
// ----------------------------------------------------------------------------------------
template
<
typename
vector_type
>
projection_hash
create_random_projection_hash
(
const
vector_type
&
v
,
const
int
bits
);
// ----------------------------------------------------------------------------------------
}
#endif // DLIB_CREATE_RANDOM_PROJECTION_HAsH_ABSTRACT_H__
dlib/lsh/projection_hash.h
View file @
5f191804
...
@@ -89,79 +89,6 @@ namespace dlib
...
@@ -89,79 +89,6 @@ namespace dlib
item
=
projection_hash
(
proj
,
offset
);
item
=
projection_hash
(
proj
,
offset
);
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
vector_type
>
projection_hash
create_random_projection_hash
(
const
vector_type
&
v
,
const
int
bits
)
{
// compute a whitening matrix
matrix
<
double
>
whiten
=
trans
(
chol
(
pinv
(
covariance
(
vector_to_matrix
(
v
)))));
// hashes
std
::
vector
<
unsigned
long
>
h
(
v
.
size
(),
0
);
std
::
vector
<
double
>
vals
(
v
.
size
(),
0
);
// number of hits for each hash value
std
::
vector
<
unsigned
long
>
counts
;
std
::
vector
<
double
>
temp
;
// build a random projection matrix
dlib
::
rand
rnd
;
matrix
<
double
>
proj
(
bits
,
v
[
0
].
size
());
for
(
long
r
=
0
;
r
<
proj
.
nr
();
++
r
)
for
(
long
c
=
0
;
c
<
proj
.
nc
();
++
c
)
proj
(
r
,
c
)
=
rnd
.
get_random_gaussian
();
// merge whitening matrix with projection matrix
proj
=
proj
*
whiten
;
matrix
<
double
,
0
,
1
>
offset
(
bits
);
// figure out what the offset values should be
for
(
int
itr
=
0
;
itr
<
offset
.
size
();
++
itr
)
{
counts
.
assign
(
std
::
pow
(
2
,
bits
),
0
);
// count the popularity of each hash value
for
(
unsigned
long
i
=
0
;
i
<
h
.
size
();
++
i
)
{
h
[
i
]
<<=
1
;
counts
[
h
[
i
]]
+=
1
;
}
const
unsigned
long
max_h
=
index_of_max
(
vector_to_matrix
(
counts
));
temp
.
clear
();
for
(
unsigned
long
i
=
0
;
i
<
v
.
size
();
++
i
)
{
vals
[
i
]
=
dot
(
rowm
(
proj
,
itr
),
v
[
i
]);
if
(
h
[
i
]
==
max_h
)
temp
.
push_back
(
vals
[
i
]);
}
// split down the middle
std
::
sort
(
temp
.
begin
(),
temp
.
end
());
const
double
split
=
temp
[
temp
.
size
()
/
2
];
offset
(
itr
)
=
-
split
;
for
(
unsigned
long
i
=
0
;
i
<
vals
.
size
();
++
i
)
{
if
(
vals
[
i
]
-
split
>
0
)
h
[
i
]
|=
1
;
}
}
return
projection_hash
(
proj
,
offset
);
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
dlib/lsh/projection_hash_abstract.h
View file @
5f191804
...
@@ -4,8 +4,6 @@
...
@@ -4,8 +4,6 @@
#ifdef DLIB_PROJECTION_HASh_ABSTRACT_H__
#ifdef DLIB_PROJECTION_HASh_ABSTRACT_H__
#include "../matrix.h"
#include "../matrix.h"
#include "../rand.h"
#include <vector>
namespace
dlib
namespace
dlib
{
{
...
@@ -55,14 +53,6 @@ namespace dlib
...
@@ -55,14 +53,6 @@ namespace dlib
std
::
istream
&
in
std
::
istream
&
in
);
);
// ----------------------------------------------------------------------------------------
template
<
typename
vector_type
>
projection_hash
create_random_projection_hash
(
const
vector_type
&
v
,
const
int
bits
);
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment