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
3fa93d7e
"git@developer.sourcefind.cn:change/sglang.git" did not exist on "e2cdc8a5b5c6a6d5a68e39d8c3e2a0c46248a2d2"
Commit
3fa93d7e
authored
Apr 30, 2012
by
Davis King
Browse files
Fixed the code so it works with sparse vectors.
parent
e7552958
Changes
2
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
26 deletions
+49
-26
dlib/svm/structural_graph_labeling_trainer.h
dlib/svm/structural_graph_labeling_trainer.h
+38
-2
dlib/svm/structural_svm_graph_labeling_problem.h
dlib/svm/structural_svm_graph_labeling_problem.h
+11
-24
No files found.
dlib/svm/structural_graph_labeling_trainer.h
View file @
3fa93d7e
...
...
@@ -147,13 +147,49 @@ namespace dlib
matrix
<
double
,
0
,
1
>
w
;
solver
(
prob
,
w
,
prob
.
get_num_edge_weights
());
vector_type
edge_weights
=
rowm
(
w
,
range
(
0
,
prob
.
get_num_edge_weights
()
-
1
));
vector_type
node_weights
=
rowm
(
w
,
range
(
prob
.
get_num_edge_weights
(),
w
.
size
()
-
1
));
vector_type
edge_weights
;
vector_type
node_weights
;
populate_weights
(
w
,
edge_weights
,
node_weights
,
prob
.
get_num_edge_weights
());
return
graph_labeler
<
vector_type
>
(
edge_weights
,
node_weights
);
}
private:
template
<
typename
T
>
typename
enable_if
<
is_matrix
<
T
>
>::
type
populate_weights
(
const
matrix
<
double
,
0
,
1
>&
w
,
T
&
edge_weights
,
T
&
node_weights
,
long
split_idx
)
const
{
edge_weights
=
rowm
(
w
,
range
(
0
,
split_idx
-
1
));
node_weights
=
rowm
(
w
,
range
(
split_idx
,
w
.
size
()
-
1
));
}
template
<
typename
T
>
typename
disable_if
<
is_matrix
<
T
>
>::
type
populate_weights
(
const
matrix
<
double
,
0
,
1
>&
w
,
T
&
edge_weights
,
T
&
node_weights
,
long
split_idx
)
const
{
edge_weights
.
clear
();
node_weights
.
clear
();
for
(
long
i
=
0
;
i
<
split_idx
;
++
i
)
{
if
(
w
(
i
)
!=
0
)
edge_weights
.
insert
(
edge_weights
.
end
(),
std
::
make_pair
(
i
,
w
(
i
)));
}
for
(
long
i
=
split_idx
;
i
<
w
.
size
();
++
i
)
{
if
(
w
(
i
)
!=
0
)
node_weights
.
insert
(
node_weights
.
end
(),
std
::
make_pair
(
i
-
split_idx
,
w
(
i
)));
}
}
double
C
;
oca
solver
;
double
eps
;
...
...
dlib/svm/structural_svm_graph_labeling_problem.h
View file @
3fa93d7e
...
...
@@ -12,6 +12,7 @@
#include <iterator>
#include "structural_svm_problem_threaded.h"
#include "../graph.h"
#include "sparse_vector.h"
// ----------------------------------------------------------------------------------------
...
...
@@ -170,36 +171,22 @@ namespace dlib
"
\t
structural_svm_graph_labeling_problem::structural_svm_graph_labeling_problem()"
<<
"
\n\t
invalid inputs were given to this function"
);
using
namespace
dlib
::
sparse_vector
;
// Figure out how many dimensions are in a node vector. Just pick
// the first node we find and use it as the representative example.
node_dims
=
0
;
for
(
unsigned
long
i
=
0
;
i
<
samples
.
size
();
++
i
)
{
if
(
samples
[
i
].
number_of_nodes
()
>
0
)
{
node_dims
=
samples
[
i
].
node
(
0
).
data
.
size
();
break
;
}
}
//
F
igure out how many dimensions are in an edge vector.
Just pick
// the first edge we find and use it as the representative example.
//
f
igure out how many dimensions are in
the node
an
d
edge vector
s
.
node_dims
=
0
;
edge_dims
=
0
;
for
(
unsigned
long
i
=
0
;
i
<
samples
.
size
();
++
i
)
{
for
(
unsigned
long
j
=
0
;
j
<
samples
[
i
].
number_of_nodes
();
++
j
)
{
if
(
samples
[
i
].
node
(
j
).
number_of_neighbors
()
!=
0
)
node_dims
=
std
::
max
(
node_dims
,(
long
)
max_index_plus_one
(
samples
[
i
].
node
(
j
).
data
));
for
(
unsigned
long
n
=
0
;
n
<
samples
[
i
].
node
(
j
).
number_of_neighbors
();
++
n
)
{
edge_dims
=
samples
[
i
].
node
(
j
).
edge
(
0
).
size
();
break
;
edge_dims
=
std
::
max
(
edge_dims
,
(
long
)
max_index_plus_one
(
samples
[
i
].
node
(
j
).
edge
(
n
)));
}
}
// if we found an edge then stop
if
(
edge_dims
!=
0
)
break
;
}
}
...
...
@@ -270,9 +257,9 @@ namespace dlib
unsigned
long
offset
)
const
{
for
(
unsigned
long
i
=
0
;
i
<
vect
.
size
();
++
i
)
for
(
typename
T
::
const_iterator
i
=
vect
.
begin
()
;
i
!=
vect
.
end
();
++
i
)
{
psi
.
push_back
(
std
::
make_pair
(
vect
[
i
].
first
+
offset
,
vect
[
i
].
second
));
psi
.
insert
(
psi
.
end
(),
std
::
make_pair
(
i
->
first
+
offset
,
i
->
second
));
}
}
...
...
@@ -282,9 +269,9 @@ namespace dlib
const
T
&
vect
)
const
{
for
(
unsigned
long
i
=
0
;
i
<
vect
.
size
();
++
i
)
for
(
typename
T
::
const_iterator
i
=
vect
.
begin
()
;
i
!=
vect
.
end
();
++
i
)
{
psi
.
push_back
(
std
::
make_pair
(
vect
[
i
].
first
,
-
vect
[
i
].
second
));
psi
.
insert
(
psi
.
end
(),
std
::
make_pair
(
i
->
first
,
-
i
->
second
));
}
}
...
...
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