Skip to content
Projects
Groups
Snippets
Help
Loading...
Sign in
Toggle navigation
D
dlib
Project
Project
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
钟尚武
dlib
Commits
2e7e20f2
Commit
2e7e20f2
authored
May 05, 2012
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
- Added make_sparse_vector()
- Refined the sparse_to_dense() routines a little.
parent
ad3ede2d
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
90 additions
and
18 deletions
+90
-18
sparse_vector.h
dlib/svm/sparse_vector.h
+45
-14
sparse_vector_abstract.h
dlib/svm/sparse_vector_abstract.h
+45
-4
No files found.
dlib/svm/sparse_vector.h
View file @
2e7e20f2
...
...
@@ -641,7 +641,7 @@ namespace dlib
template
<
typename
sparse_vector_type
>
inline
matrix
<
typename
sparse_vector_type
::
value_type
::
second_type
,
0
,
1
>
sparse_to_dense
(
const
sparse_vector_type
&
vect
,
long
num_dimensions
unsigned
long
num_dimensions
)
{
// You must use unsigned integral key types in your sparse vectors
...
...
@@ -674,7 +674,7 @@ namespace dlib
template
<
typename
idx_type
,
typename
value_type
,
typename
alloc
>
matrix
<
value_type
,
0
,
1
>
sparse_to_dense
(
const
std
::
vector
<
std
::
pair
<
idx_type
,
value_type
>
,
alloc
>&
vect
,
long
num_dimensions
unsigned
long
num_dimensions
)
{
return
impl
::
sparse_to_dense
(
vect
,
num_dimensions
);
...
...
@@ -695,7 +695,7 @@ namespace dlib
template
<
typename
T1
,
typename
T2
,
typename
T3
,
typename
T4
>
matrix
<
T2
,
0
,
1
>
sparse_to_dense
(
const
std
::
map
<
T1
,
T2
,
T3
,
T4
>&
vect
,
long
num_dimensions
unsigned
long
num_dimensions
)
{
return
impl
::
sparse_to_dense
(
vect
,
num_dimensions
);
...
...
@@ -720,23 +720,25 @@ namespace dlib
template
<
typename
EXP
>
matrix
<
typename
EXP
::
type
,
0
,
1
>
sparse_to_dense
(
const
matrix
<
EXP
>&
item
,
long
num
const
matrix
_exp
<
EXP
>&
item
,
unsigned
long
num
)
{
if
(
item
.
size
()
==
num
)
typedef
typename
EXP
::
type
type
;
if
(
item
.
size
()
==
(
long
)
num
)
return
item
;
else
if
(
item
.
size
()
<
num
)
return
join_cols
(
item
,
zeros_matrix
(
num
-
item
.
size
(),
1
));
else
if
(
item
.
size
()
<
(
long
)
num
)
return
join_cols
(
item
,
zeros_matrix
<
type
>
((
long
)
num
-
item
.
size
(),
1
));
else
return
rowm
(
item
,
0
,
num
);
return
colm
(
item
,
0
,(
long
)
num
);
}
// ----------------------------------------------------------------------------------------
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
const
std
::
vector
<
sample_type
,
alloc
>&
samples
,
unsigned
long
num_dimensions
)
{
typedef
typename
sample_type
::
value_type
pair_type
;
...
...
@@ -744,20 +746,49 @@ namespace dlib
std
::
vector
<
matrix
<
value_type
,
0
,
1
>
>
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
]
=
sparse_to_dense
(
samples
[
i
],
max_dim
);
result
[
i
]
=
sparse_to_dense
(
samples
[
i
],
num_dimensions
);
}
return
result
;
}
// ----------------------------------------------------------------------------------------
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
)
{
return
sparse_to_dense
(
samples
,
max_index_plus_one
(
samples
));
}
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
T
make_sparse_vector
(
const
T
&
v
)
{
// You must use unsigned integral key types in your sparse vectors
typedef
typename
T
::
value_type
::
first_type
idx_type
;
typedef
typename
T
::
value_type
::
second_type
value_type
;
COMPILE_TIME_ASSERT
(
is_unsigned_type
<
idx_type
>::
value
);
std
::
map
<
idx_type
,
value_type
>
temp
;
for
(
typename
T
::
const_iterator
i
=
v
.
begin
();
i
!=
v
.
end
();
++
i
)
{
temp
[
i
->
first
]
+=
i
->
second
;
}
return
T
(
temp
.
begin
(),
temp
.
end
());
}
// ----------------------------------------------------------------------------------------
}
...
...
dlib/svm/sparse_vector_abstract.h
View file @
2e7e20f2
...
...
@@ -371,7 +371,7 @@ namespace dlib
);
/*!
requires
- vect must be a
n unsorted
sparse vector or a dense column vector.
- vect must be a sparse vector or a dense column vector.
ensures
- converts the single sparse or dense vector vect to a dense (column matrix form)
representation. That is, this function returns a vector V such that:
...
...
@@ -389,11 +389,11 @@ namespace dlib
>
matrix
<
typename
sample_type
::
value_type
::
second_type
,
0
,
1
>
sparse_to_dense
(
const
sample_type
&
vect
,
long
num_dimensions
unsigned
long
num_dimensions
);
/*!
requires
- vect must be a
n unsorted
sparse vector or a dense column vector.
- vect must be a sparse vector or a dense column vector.
ensures
- converts the single sparse or dense vector vect to a dense (column matrix form)
representation. That is, this function returns a vector V such that:
...
...
@@ -415,7 +415,7 @@ namespace dlib
);
/*!
requires
- all elements of samples must be
unsorted
sparse vectors or dense column vectors.
- all elements of samples must be sparse vectors or dense column vectors.
ensures
- converts from sparse sample vectors to dense (column matrix form)
- That is, this function returns a std::vector R such that:
...
...
@@ -427,6 +427,47 @@ namespace dlib
given by samples[i].)
!*/
// ----------------------------------------------------------------------------------------
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
,
unsigned
long
num_dimensions
);
/*!
requires
- all elements of samples must be sparse vectors or dense column vectors.
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] == sparse_to_dense(samples[i], num_dimensions)
(i.e. the dense (i.e. dlib::matrix) version of the sparse sample
given by samples[i].)
!*/
// ----------------------------------------------------------------------------------------
template
<
typename
T
>
T
make_sparse_vector
(
const
T
&
v
);
/*!
requires
- v is an unsorted sparse vector
ensures
- returns a copy of v which is a sparse vector.
(i.e. it will be properly sorted and not have any duplicate
key values).
!*/
// ----------------------------------------------------------------------------------------
}
...
...
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