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
7a66d988
Commit
7a66d988
authored
May 04, 2012
by
Davis King
Browse files
Moved sparse_to_dense() into the file with all the other sparse
vector functions.
parent
f581b5e6
Changes
4
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
76 additions
and
85 deletions
+76
-85
dlib/data_io/libsvm_io.h
dlib/data_io/libsvm_io.h
+4
-55
dlib/data_io/libsvm_io_abstract.h
dlib/data_io/libsvm_io_abstract.h
+0
-30
dlib/svm/sparse_vector.h
dlib/svm/sparse_vector.h
+42
-0
dlib/svm/sparse_vector_abstract.h
dlib/svm/sparse_vector_abstract.h
+30
-0
No files found.
dlib/data_io/libsvm_io.h
View file @
7a66d988
...
@@ -23,13 +23,6 @@ namespace dlib
...
@@ -23,13 +23,6 @@ namespace dlib
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
namespace
impl
{
template
<
typename
T
>
struct
strip_const
{
typedef
T
type
;
};
template
<
typename
T
>
struct
strip_const
<
const
T
>
{
typedef
T
type
;
};
template
<
typename
T
>
struct
strip_const
<
const
T
&>
{
typedef
T
type
;
};
}
template
<
typename
sample_type
,
typename
label_type
,
typename
alloc1
,
typename
alloc2
>
template
<
typename
sample_type
,
typename
label_type
,
typename
alloc1
,
typename
alloc2
>
void
load_libsvm_formatted_data
(
void
load_libsvm_formatted_data
(
const
std
::
string
&
file_name
,
const
std
::
string
&
file_name
,
...
@@ -39,7 +32,7 @@ namespace dlib
...
@@ -39,7 +32,7 @@ namespace dlib
{
{
using
namespace
std
;
using
namespace
std
;
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
impl
::
strip_const
<
typename
pair_type
::
first_type
>::
type
key_type
;
typedef
typename
basic_type
<
typename
pair_type
::
first_type
>::
type
key_type
;
typedef
typename
pair_type
::
second_type
value_type
;
typedef
typename
pair_type
::
second_type
value_type
;
// You must use unsigned integral key types in your sparse vectors
// You must use unsigned integral key types in your sparse vectors
...
@@ -116,7 +109,7 @@ namespace dlib
...
@@ -116,7 +109,7 @@ namespace dlib
)
)
{
{
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
impl
::
strip_const
<
typename
pair_type
::
first_type
>::
type
key_type
;
typedef
typename
basic_type
<
typename
pair_type
::
first_type
>::
type
key_type
;
if
(
samples
.
size
()
==
0
)
if
(
samples
.
size
()
==
0
)
return
;
return
;
...
@@ -157,7 +150,7 @@ namespace dlib
...
@@ -157,7 +150,7 @@ namespace dlib
)
)
{
{
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
impl
::
strip_const
<
typename
pair_type
::
first_type
>::
type
key_type
;
typedef
typename
basic_type
<
typename
pair_type
::
first_type
>::
type
key_type
;
if
(
samples
.
size
()
==
0
)
if
(
samples
.
size
()
==
0
)
return
;
return
;
...
@@ -193,7 +186,7 @@ namespace dlib
...
@@ -193,7 +186,7 @@ namespace dlib
)
)
{
{
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
impl
::
strip_const
<
typename
pair_type
::
first_type
>::
type
key_type
;
typedef
typename
basic_type
<
typename
pair_type
::
first_type
>::
type
key_type
;
// You must use unsigned integral key types in your sparse vectors
// You must use unsigned integral key types in your sparse vectors
COMPILE_TIME_ASSERT
(
is_unsigned_type
<
key_type
>::
value
);
COMPILE_TIME_ASSERT
(
is_unsigned_type
<
key_type
>::
value
);
...
@@ -273,50 +266,6 @@ namespace dlib
...
@@ -273,50 +266,6 @@ namespace dlib
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
sample_type
,
typename
alloc
>
std
::
vector
<
matrix
<
typename
sample_type
::
value_type
::
second_type
,
0
,
1
>
>
sparse_to_dense
(
const
std
::
vector
<
sample_type
,
alloc
>&
samples
)
{
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
impl
::
strip_const
<
typename
pair_type
::
first_type
>::
type
key_type
;
// You must use unsigned integral key types in your sparse vectors
COMPILE_TIME_ASSERT
(
is_unsigned_type
<
key_type
>::
value
);
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
impl
::
strip_const
<
typename
pair_type
::
first_type
>::
type
key_type
;
typedef
typename
pair_type
::
second_type
value_type
;
std
::
vector
<
matrix
<
value_type
,
0
,
1
>
>
result
;
// do nothing if there aren't any samples
if
(
samples
.
size
()
==
0
)
return
result
;
// figure out how many elements we need in our dense vectors.
const
unsigned
long
max_dim
=
max_index_plus_one
(
samples
);
// now turn all the samples into dense samples
result
.
resize
(
samples
.
size
());
for
(
unsigned
long
i
=
0
;
i
<
samples
.
size
();
++
i
)
{
result
[
i
].
set_size
(
max_dim
);
result
[
i
]
=
0
;
for
(
typename
sample_type
::
const_iterator
j
=
samples
[
i
].
begin
();
j
!=
samples
[
i
].
end
();
++
j
)
{
result
[
i
](
j
->
first
)
=
j
->
second
;
}
}
return
result
;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
dlib/data_io/libsvm_io_abstract.h
View file @
7a66d988
...
@@ -99,36 +99,6 @@ namespace dlib
...
@@ -99,36 +99,6 @@ namespace dlib
This exception is thrown if there is any problem saving data to file
This exception is thrown if there is any problem saving data to file
!*/
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
sample_type
,
typename
alloc
>
std
::
vector
<
matrix
<
typename
sample_type
::
value_type
::
second_type
,
0
,
1
>
>
sparse_to_dense
(
const
std
::
vector
<
sample_type
,
alloc
>&
samples
);
/*!
requires
- sample_type must be an STL container
- sample_type::value_type == std::pair<T,U> where T is some kind of
unsigned integral type
ensures
- converts from sparse sample vectors to dense (column matrix form)
- That is, this function returns a std::vector R such that:
- R contains column matrices
- R.size() == samples.size()
- for all valid i:
- R[i] == the dense (i.e. dlib::matrix) version of the sparse sample
given by samples[i]
- for all valid j:
- R[i](j) == the value of the element in samples[i] that has key
value j. That is, the key used for each element of a sparse
vector directly determines where that element gets put into a
dense vector. Note that elements not explicitly in the sparse
vector have a value of 0.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
template
<
typename
sample_type
,
typename
alloc
>
template
<
typename
sample_type
,
typename
alloc
>
...
...
dlib/svm/sparse_vector.h
View file @
7a66d988
...
@@ -634,6 +634,48 @@ namespace dlib
...
@@ -634,6 +634,48 @@ namespace dlib
return
temp
;
return
temp
;
}
}
// ----------------------------------------------------------------------------------------
template
<
typename
sample_type
,
typename
alloc
>
std
::
vector
<
matrix
<
typename
sample_type
::
value_type
::
second_type
,
0
,
1
>
>
sparse_to_dense
(
const
std
::
vector
<
sample_type
,
alloc
>&
samples
)
{
typedef
typename
sample_type
::
value_type
pair_type
;
typedef
typename
basic_type
<
typename
pair_type
::
first_type
>::
type
key_type
;
// You must use unsigned integral key types in your sparse vectors
COMPILE_TIME_ASSERT
(
is_unsigned_type
<
key_type
>::
value
);
typedef
typename
pair_type
::
second_type
value_type
;
std
::
vector
<
matrix
<
value_type
,
0
,
1
>
>
result
;
// do nothing if there aren't any samples
if
(
samples
.
size
()
==
0
)
return
result
;
// figure out how many elements we need in our dense vectors.
const
unsigned
long
max_dim
=
max_index_plus_one
(
samples
);
// now turn all the samples into dense samples
result
.
resize
(
samples
.
size
());
for
(
unsigned
long
i
=
0
;
i
<
samples
.
size
();
++
i
)
{
result
[
i
].
set_size
(
max_dim
);
result
[
i
]
=
0
;
for
(
typename
sample_type
::
const_iterator
j
=
samples
[
i
].
begin
();
j
!=
samples
[
i
].
end
();
++
j
)
{
result
[
i
](
j
->
first
)
=
j
->
second
;
}
}
return
result
;
}
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
dlib/svm/sparse_vector_abstract.h
View file @
7a66d988
...
@@ -361,6 +361,36 @@ namespace dlib
...
@@ -361,6 +361,36 @@ namespace dlib
of 0 elements.
of 0 elements.
!*/
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
sample_type
,
typename
alloc
>
std
::
vector
<
matrix
<
typename
sample_type
::
value_type
::
second_type
,
0
,
1
>
>
sparse_to_dense
(
const
std
::
vector
<
sample_type
,
alloc
>&
samples
);
/*!
requires
- sample_type must be an STL container
- sample_type::value_type == std::pair<T,U> where T is some kind of
unsigned integral type
ensures
- converts from sparse sample vectors to dense (column matrix form)
- That is, this function returns a std::vector R such that:
- R contains column matrices
- R.size() == samples.size()
- for all valid i:
- R[i] == the dense (i.e. dlib::matrix) version of the sparse sample
given by samples[i]
- for all valid j:
- R[i](j) == the value of the element in samples[i] that has key
value j. That is, the key used for each element of a sparse
vector directly determines where that element gets put into a
dense vector. Note that elements not explicitly in the sparse
vector have a value of 0.
!*/
// ----------------------------------------------------------------------------------------
// ----------------------------------------------------------------------------------------
}
}
...
...
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