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
de365031
Commit
de365031
authored
Sep 30, 2015
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
renamed a variable
parent
463e1ce0
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
47 additions
and
48 deletions
+47
-48
core.h
dlib/dnn/core.h
+47
-48
No files found.
dlib/dnn/core.h
View file @
de365031
...
@@ -126,17 +126,17 @@ namespace dlib
...
@@ -126,17 +126,17 @@ namespace dlib
typedef
T
wrapped_type
;
typedef
T
wrapped_type
;
const
static
size_t
num_layers
=
T
::
num_layers
;
const
static
size_t
num_layers
=
T
::
num_layers
;
subnet_wrapper
(
T
&
l_
)
:
l
(
l_
),
sub
(
l
.
subnet
())
{}
subnet_wrapper
(
T
&
l_
)
:
l
(
l_
),
sub
network
(
l
.
subnet
())
{}
const
tensor
&
get_output
()
const
{
return
l
.
get_output
();
}
const
tensor
&
get_output
()
const
{
return
l
.
get_output
();
}
tensor
&
get_gradient_input
()
{
return
l
.
get_gradient_input
();
}
tensor
&
get_gradient_input
()
{
return
l
.
get_gradient_input
();
}
const
subnet_wrapper
<
typename
T
::
subnet_type
>&
subnet
()
const
{
sub
;
}
const
subnet_wrapper
<
typename
T
::
subnet_type
>&
subnet
()
const
{
sub
network
;
}
subnet_wrapper
<
typename
T
::
subnet_type
>&
subnet
()
{
sub
;
}
subnet_wrapper
<
typename
T
::
subnet_type
>&
subnet
()
{
sub
network
;
}
private
:
private
:
T
&
l
;
T
&
l
;
subnet_wrapper
<
typename
T
::
subnet_type
>
sub
;
subnet_wrapper
<
typename
T
::
subnet_type
>
sub
network
;
};
};
}
}
...
@@ -773,7 +773,7 @@ namespace dlib
...
@@ -773,7 +773,7 @@ namespace dlib
const
add_loss_layer
<
T
,
U
>&
item
const
add_loss_layer
<
T
,
U
>&
item
)
:
)
:
loss
(
item
.
loss_details
()),
loss
(
item
.
loss_details
()),
sub
(
item
.
subnet
())
sub
network
(
item
.
subnet
())
{}
{}
template
<
typename
...
T
>
template
<
typename
...
T
>
...
@@ -782,7 +782,7 @@ namespace dlib
...
@@ -782,7 +782,7 @@ namespace dlib
T
&&
...
args
T
&&
...
args
)
:
)
:
loss
(
layer_det
),
loss
(
layer_det
),
sub
(
std
::
forward
<
T
>
(
args
)...)
sub
network
(
std
::
forward
<
T
>
(
args
)...)
{
{
}
}
...
@@ -792,7 +792,7 @@ namespace dlib
...
@@ -792,7 +792,7 @@ namespace dlib
T
&&
...
args
T
&&
...
args
)
:
)
:
loss
(
std
::
move
(
layer_det
)),
loss
(
std
::
move
(
layer_det
)),
sub
(
std
::
forward
<
T
>
(
args
)...)
sub
network
(
std
::
forward
<
T
>
(
args
)...)
{
{
}
}
...
@@ -800,9 +800,8 @@ namespace dlib
...
@@ -800,9 +800,8 @@ namespace dlib
add_loss_layer
(
add_loss_layer
(
T
...
args
T
...
args
)
:
)
:
sub
(
std
::
move
(
args
)...)
sub
network
(
std
::
move
(
args
)...)
{
{
// TODO, rename sub to subnetwork
}
}
template
<
typename
input_iterator
>
template
<
typename
input_iterator
>
...
@@ -812,7 +811,7 @@ namespace dlib
...
@@ -812,7 +811,7 @@ namespace dlib
resizable_tensor
&
data
resizable_tensor
&
data
)
const
)
const
{
{
sub
.
to_tensor
(
ibegin
,
iend
,
data
);
sub
network
.
to_tensor
(
ibegin
,
iend
,
data
);
}
}
template
<
typename
output_iterator
>
template
<
typename
output_iterator
>
...
@@ -821,8 +820,8 @@ namespace dlib
...
@@ -821,8 +820,8 @@ namespace dlib
output_iterator
obegin
output_iterator
obegin
)
)
{
{
sub
.
forward
(
x
);
sub
network
.
forward
(
x
);
const
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
);
const
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
network
);
loss
.
to_label
(
wsub
,
obegin
);
loss
.
to_label
(
wsub
,
obegin
);
}
}
...
@@ -849,8 +848,8 @@ namespace dlib
...
@@ -849,8 +848,8 @@ namespace dlib
label_iterator
lbegin
label_iterator
lbegin
)
)
{
{
sub
.
forward
(
x
);
sub
network
.
forward
(
x
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
network
);
return
loss
.
compute_loss
(
x
,
lbegin
,
wsub
);
return
loss
.
compute_loss
(
x
,
lbegin
,
wsub
);
}
}
...
@@ -869,8 +868,8 @@ namespace dlib
...
@@ -869,8 +868,8 @@ namespace dlib
const
tensor
&
x
const
tensor
&
x
)
)
{
{
sub
.
forward
(
x
);
sub
network
.
forward
(
x
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
network
);
return
loss
.
compute_loss
(
x
,
wsub
);
return
loss
.
compute_loss
(
x
,
wsub
);
}
}
...
@@ -891,10 +890,10 @@ namespace dlib
...
@@ -891,10 +890,10 @@ namespace dlib
sstack
<
solver_type
,
num_layers
>&
solvers
sstack
<
solver_type
,
num_layers
>&
solvers
)
)
{
{
sub
.
forward
(
x
);
sub
network
.
forward
(
x
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
network
);
double
l
=
loss
.
compute_loss
(
x
,
lbegin
,
wsub
);
double
l
=
loss
.
compute_loss
(
x
,
lbegin
,
wsub
);
sub
.
update
(
x
,
solvers
);
sub
network
.
update
(
x
,
solvers
);
return
l
;
return
l
;
}
}
...
@@ -916,10 +915,10 @@ namespace dlib
...
@@ -916,10 +915,10 @@ namespace dlib
sstack
<
solver_type
,
num_layers
>&
solvers
sstack
<
solver_type
,
num_layers
>&
solvers
)
)
{
{
sub
.
forward
(
x
);
sub
network
.
forward
(
x
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
);
dimpl
::
subnet_wrapper
<
subnet_type
>
wsub
(
sub
network
);
double
l
=
loss
.
compute_loss
(
x
,
wsub
);
double
l
=
loss
.
compute_loss
(
x
,
wsub
);
sub
.
update
(
x
,
solvers
);
sub
network
.
update
(
x
,
solvers
);
return
l
;
return
l
;
}
}
...
@@ -934,8 +933,8 @@ namespace dlib
...
@@ -934,8 +933,8 @@ namespace dlib
return
update
(
temp_tensor
,
solvers
);
return
update
(
temp_tensor
,
solvers
);
}
}
const
subnet_type
&
subnet
()
const
{
return
sub
;
}
const
subnet_type
&
subnet
()
const
{
return
sub
network
;
}
subnet_type
&
subnet
()
{
return
sub
;
}
subnet_type
&
subnet
()
{
return
sub
network
;
}
const
loss_details_type
&
loss_details
()
const
{
return
loss
;
}
const
loss_details_type
&
loss_details
()
const
{
return
loss
;
}
loss_details_type
&
loss_details
()
{
return
loss
;
}
loss_details_type
&
loss_details
()
{
return
loss
;
}
...
@@ -943,13 +942,13 @@ namespace dlib
...
@@ -943,13 +942,13 @@ namespace dlib
)
)
{
{
temp_tensor
.
clear
();
temp_tensor
.
clear
();
sub
.
clear
();
sub
network
.
clear
();
}
}
private
:
private
:
loss_details_type
loss
;
loss_details_type
loss
;
subnet_type
sub
;
subnet_type
sub
network
;
// These two objects don't logically contribute to the state of this object. They
// These two objects don't logically contribute to the state of this object. They
// are here to prevent them from being reallocated over and over.
// are here to prevent them from being reallocated over and over.
...
@@ -1219,17 +1218,17 @@ namespace dlib
...
@@ -1219,17 +1218,17 @@ namespace dlib
const
tensor
&
get_output
()
const
{
return
output
;
}
const
tensor
&
get_output
()
const
{
return
output
;
}
const
test_layer_subnet
&
subnet
()
const
{
init_sub
();
return
*
sub
;
}
const
test_layer_subnet
&
subnet
()
const
{
init_sub
();
return
*
sub
network
;
}
tensor
&
get_gradient_input
()
{
return
gradient_input
;
}
tensor
&
get_gradient_input
()
{
return
gradient_input
;
}
test_layer_subnet
&
subnet
()
{
init_sub
();
return
*
sub
;
}
test_layer_subnet
&
subnet
()
{
init_sub
();
return
*
sub
network
;
}
unsigned
long
count_outputs
()
const
unsigned
long
count_outputs
()
const
{
{
if
(
sub
)
if
(
sub
network
)
return
sub
->
count_outputs
()
+
output
.
size
();
return
sub
network
->
count_outputs
()
+
output
.
size
();
else
else
return
output
.
size
();
return
output
.
size
();
}
}
...
@@ -1256,12 +1255,12 @@ namespace dlib
...
@@ -1256,12 +1255,12 @@ namespace dlib
// subnet()
// subnet()
void
init_sub
()
const
void
init_sub
()
const
{
{
if
(
!
sub
)
if
(
!
sub
network
)
sub
.
reset
(
new
test_layer_subnet
(
rnd
));
sub
network
.
reset
(
new
test_layer_subnet
(
rnd
));
}
}
dlib
::
rand
&
rnd
;
dlib
::
rand
&
rnd
;
mutable
std
::
unique_ptr
<
test_layer_subnet
>
sub
;
mutable
std
::
unique_ptr
<
test_layer_subnet
>
sub
network
;
resizable_tensor
output
;
resizable_tensor
output
;
resizable_tensor
gradient_input
;
resizable_tensor
gradient_input
;
};
};
...
@@ -1289,14 +1288,14 @@ namespace dlib
...
@@ -1289,14 +1288,14 @@ namespace dlib
using
namespace
timpl
;
using
namespace
timpl
;
// Do some setup
// Do some setup
dlib
::
rand
rnd
;
dlib
::
rand
rnd
;
test_layer_subnet
sub
(
rnd
);
test_layer_subnet
sub
network
(
rnd
);
resizable_tensor
output
,
out2
,
out3
;
resizable_tensor
output
,
out2
,
out3
;
// Run setup() and forward() as well to make sure any calls to subnet() have
// Run setup() and forward() as well to make sure any calls to subnet() have
// happened before we start assuming we know how many data elements there are
// happened before we start assuming we know how many data elements there are
// (since we do a lazy layer creation thing based on calls to subnet() inside
// (since we do a lazy layer creation thing based on calls to subnet() inside
// test_layer_subnet).
// test_layer_subnet).
l
.
setup
(
sub
);
l
.
setup
(
sub
network
);
l
.
forward
(
sub
,
output
);
l
.
forward
(
sub
network
,
output
);
resizable_tensor
input_grad
;
resizable_tensor
input_grad
;
input_grad
.
copy_size
(
output
);
input_grad
.
copy_size
(
output
);
...
@@ -1307,14 +1306,14 @@ namespace dlib
...
@@ -1307,14 +1306,14 @@ namespace dlib
// parameter and data values is:
// parameter and data values is:
std
::
cout
<<
"f(data,params): "
<<
dot
(
output
,
input_grad
)
<<
std
::
endl
;
std
::
cout
<<
"f(data,params): "
<<
dot
(
output
,
input_grad
)
<<
std
::
endl
;
// We are going to save a copy of the sub.get_gradient_input() data before we do
// We are going to save a copy of the sub
network
.get_gradient_input() data before we do
// backpropagation since the backward() function is supposed to *add* to the
// backpropagation since the backward() function is supposed to *add* to the
// gradients rather than overwrite them. We will use this saved data to check if
// gradients rather than overwrite them. We will use this saved data to check if
// that is the case.
// that is the case.
const
unsigned
long
num_data_inputs
=
sub
.
count_outputs
();
const
unsigned
long
num_data_inputs
=
sub
network
.
count_outputs
();
std
::
vector
<
float
>
initial_gradient_input
(
num_data_inputs
);
std
::
vector
<
float
>
initial_gradient_input
(
num_data_inputs
);
for
(
unsigned
long
i
=
0
;
i
<
num_data_inputs
;
++
i
)
for
(
unsigned
long
i
=
0
;
i
<
num_data_inputs
;
++
i
)
initial_gradient_input
[
i
]
=
sub
.
get_gradient_input_element
(
i
);
initial_gradient_input
[
i
]
=
sub
network
.
get_gradient_input_element
(
i
);
// Now tell the layer to compute all the gradients. In the rest of this function
// Now tell the layer to compute all the gradients. In the rest of this function
...
@@ -1325,7 +1324,7 @@ namespace dlib
...
@@ -1325,7 +1324,7 @@ namespace dlib
random_noise
.
copy_size
(
l
.
get_layer_params
());
random_noise
.
copy_size
(
l
.
get_layer_params
());
randomize_parameters
(
random_noise
,
5
,
rnd
);
randomize_parameters
(
random_noise
,
5
,
rnd
);
params_grad
=
random_noise
;
params_grad
=
random_noise
;
l
.
backward
(
input_grad
,
sub
,
params_grad
);
l
.
backward
(
input_grad
,
sub
network
,
params_grad
);
running_stats
<
double
>
rs_param
,
rs_data
;
running_stats
<
double
>
rs_param
,
rs_data
;
...
@@ -1340,9 +1339,9 @@ namespace dlib
...
@@ -1340,9 +1339,9 @@ namespace dlib
eps
=
base_eps
;
eps
=
base_eps
;
const
float
oldval
=
l1
.
get_layer_params
().
host
()[
i
];
const
float
oldval
=
l1
.
get_layer_params
().
host
()[
i
];
l1
.
get_layer_params
().
host
()[
i
]
=
oldval
+
eps
;
l1
.
get_layer_params
().
host
()[
i
]
=
oldval
+
eps
;
l1
.
forward
(
sub
,
out2
);
l1
.
forward
(
sub
network
,
out2
);
l1
.
get_layer_params
().
host
()[
i
]
=
oldval
-
eps
;
l1
.
get_layer_params
().
host
()[
i
]
=
oldval
-
eps
;
l1
.
forward
(
sub
,
out3
);
l1
.
forward
(
sub
network
,
out3
);
// Compute a reference derivative via a central differences approximation and
// Compute a reference derivative via a central differences approximation and
// compare it to the one output by the layer and make sure they match.
// compare it to the one output by the layer and make sure they match.
...
@@ -1364,19 +1363,19 @@ namespace dlib
...
@@ -1364,19 +1363,19 @@ namespace dlib
// now validate the data gradients
// now validate the data gradients
for
(
unsigned
long
i
=
0
;
i
<
num_data_inputs
;
++
i
)
for
(
unsigned
long
i
=
0
;
i
<
num_data_inputs
;
++
i
)
{
{
const
float
oldval
=
sub
.
get_output_element
(
i
);
const
float
oldval
=
sub
network
.
get_output_element
(
i
);
float
eps
=
oldval
*
base_eps
;
float
eps
=
oldval
*
base_eps
;
if
(
eps
==
0
)
if
(
eps
==
0
)
eps
=
base_eps
;
eps
=
base_eps
;
sub
.
get_output_element
(
i
)
=
oldval
+
eps
;
sub
network
.
get_output_element
(
i
)
=
oldval
+
eps
;
l
.
forward
(
sub
,
out2
);
l
.
forward
(
sub
network
,
out2
);
sub
.
get_output_element
(
i
)
=
oldval
-
eps
;
sub
network
.
get_output_element
(
i
)
=
oldval
-
eps
;
l
.
forward
(
sub
,
out3
);
l
.
forward
(
sub
network
,
out3
);
// Compute a reference derivative via a central differences approximation and
// Compute a reference derivative via a central differences approximation and
// compare it to the one output by the layer and make sure they match.
// compare it to the one output by the layer and make sure they match.
double
reference_derivative
=
(
dot
(
out2
,
input_grad
)
-
dot
(
out3
,
input_grad
))
/
(
2
*
eps
);
double
reference_derivative
=
(
dot
(
out2
,
input_grad
)
-
dot
(
out3
,
input_grad
))
/
(
2
*
eps
);
double
output_derivative
=
sub
.
get_gradient_input_element
(
i
)
-
initial_gradient_input
[
i
];
double
output_derivative
=
sub
network
.
get_gradient_input_element
(
i
)
-
initial_gradient_input
[
i
];
double
relative_error
=
(
reference_derivative
-
output_derivative
)
/
(
reference_derivative
+
1e-100
);
double
relative_error
=
(
reference_derivative
-
output_derivative
)
/
(
reference_derivative
+
1e-100
);
if
(
std
::
abs
(
relative_error
)
>
0
.
01
)
if
(
std
::
abs
(
relative_error
)
>
0
.
01
)
{
{
...
...
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