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
a7ea7d00
Commit
a7ea7d00
authored
Nov 18, 2015
by
Davis King
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Implemented CPU version of tanh
parent
9b36bb98
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
47 additions
and
4 deletions
+47
-4
cpu_dlib.cpp
dlib/dnn/cpu_dlib.cpp
+9
-4
dnn.cpp
dlib/test/dnn.cpp
+38
-0
No files found.
dlib/dnn/cpu_dlib.cpp
View file @
a7ea7d00
...
...
@@ -650,8 +650,10 @@ namespace dlib
const
tensor
&
src
)
{
// TODO
DLIB_CASSERT
(
false
,
""
);
const
auto
d
=
dest
.
host
();
const
auto
s
=
src
.
host
();
for
(
size_t
i
=
0
;
i
<
src
.
size
();
++
i
)
d
[
i
]
=
std
::
tanh
(
s
[
i
]);
}
void
tanh_gradient
(
...
...
@@ -660,8 +662,11 @@ namespace dlib
const
tensor
&
gradient_input
)
{
// TODO
DLIB_CASSERT
(
false
,
""
);
const
auto
g
=
grad
.
host
();
const
auto
d
=
dest
.
host
();
const
auto
in
=
gradient_input
.
host
();
for
(
size_t
i
=
0
;
i
<
dest
.
size
();
++
i
)
g
[
i
]
=
in
[
i
]
*
(
1
-
d
[
i
]
*
d
[
i
]);
}
// ------------------------------------------------------------------------------------
...
...
dlib/test/dnn.cpp
View file @
a7ea7d00
...
...
@@ -39,6 +39,43 @@ namespace
return
max_error
;
}
// ----------------------------------------------------------------------------------------
void
test_tanh
()
{
print_spinner
();
resizable_tensor
src
(
5
,
5
),
dest
(
5
,
5
),
gradient_input
(
5
,
5
);
src
=
matrix_cast
<
float
>
(
gaussian_randm
(
5
,
5
,
0
));
dest
=
matrix_cast
<
float
>
(
gaussian_randm
(
5
,
5
,
1
));
gradient_input
=
matrix_cast
<
float
>
(
gaussian_randm
(
5
,
5
,
2
));
auto
grad_src
=
[
&
](
long
idx
)
{
auto
f
=
[
&
](
float
eps
)
{
const
float
old
=
src
.
host
()[
idx
];
src
.
host
()[
idx
]
+=
eps
;
tanh
(
dest
,
src
);
float
result
=
dot
(
gradient_input
,
dest
);
src
.
host
()[
idx
]
=
old
;
return
result
;
};
const
float
eps
=
0.01
;
return
(
f
(
+
eps
)
-
f
(
-
eps
))
/
(
2
*
eps
);
};
resizable_tensor
src_grad
;
src_grad
.
copy_size
(
src
);
src_grad
=
0
;
tanh
(
dest
,
src
);
tanh_gradient
(
src_grad
,
dest
,
gradient_input
);
auto
grad_error
=
compare_gradients
(
src_grad
,
grad_src
);
dlog
<<
LINFO
<<
"src error: "
<<
grad_error
;
DLIB_TEST
(
grad_error
<
0.001
);
}
void
test_sigmoid
()
{
print_spinner
();
...
...
@@ -324,6 +361,7 @@ namespace
void
perform_test
(
)
{
test_tanh
();
test_softmax
();
test_sigmoid
();
test_batch_normalize
();
...
...
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