Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
T
tvm
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Model registry
Operate
Environments
Monitor
Incidents
Service Desk
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Terms and privacy
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
cld
ml
tvm
Commits
a9313787
Commit
a9313787
authored
6 years ago
by
larrywyang
Committed by
Tianqi Chen
6 years ago
Browse files
Options
Downloads
Patches
Plain Diff
[WIP] [NNVM] Fix softmax gradient (#1201)
[NNVM] Fix softmax gradient
parent
61dad72e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
nnvm/src/top/nn/nn.cc
+21
-17
21 additions, 17 deletions
nnvm/src/top/nn/nn.cc
nnvm/tests/python/compiler/test_top_level1.py
+2
-2
2 additions, 2 deletions
nnvm/tests/python/compiler/test_top_level1.py
with
23 additions
and
19 deletions
nnvm/src/top/nn/nn.cc
+
21
−
17
View file @
a9313787
...
...
@@ -366,22 +366,24 @@ NNVM_REGISTER_OP(softmax)
// [ ... ,-ynyn + yn]
//
// grad_x =
// [-y1*(ograd1*y1 - 1 + ograd2*y2 + ..., -y2*(ograd1*y1 - 1 + ograd2*y2, ..., ...]]
// [-y1*(ograd1*y1 - ograd1 + ograd2*y2 + ...),
// -y2*(ograd1*y1 - ograd2 + ograd2*y2 + ...),
// ...
// -yn*(ograd1*y1 - ogradn + ograd2*y2 + ...)]
// grad_x = ograd elemwise_mul output
// grad_x = sum(grad_x, keepdim, axis)
// grad_x = grad_x broadcast_mul output
// grad_x = neg grad_x
// grad_x = grad_x + output
// grad_x = grad_x +
ograd elemwise_mul
output
const
SoftmaxParam
&
param
=
nnvm
::
get
<
SoftmaxParam
>
(
n
->
attrs
.
parsed
);
NodeEntry
output
=
NodeEntry
{
n
,
0
,
0
};
NodeEntry
sub0
=
MakeNode
(
"elemwise_mul"
,
n
->
attrs
.
name
+
"_grad_sub0"
,
{
ograds
[
0
],
output
});
NodeEntry
sub1
=
MakeNode
(
"sum"
,
n
->
attrs
.
name
+
"_grad_sub1"
,
{
sub0
},
{{
"axis"
,
std
::
to_string
(
param
.
axis
)},
{
"keepdims"
,
"true"
}});
NodeEntry
sub2
=
MakeNode
(
"broadcast_mul"
,
n
->
attrs
.
name
+
"_grad_sub2"
,
{
sub1
,
output
});
NodeEntry
sub3
=
MakeNode
(
"negative"
,
n
->
attrs
.
name
+
"_grad_sub3"
,
{
sub2
});
return
std
::
vector
<
NodeEntry
>
{
MakeNode
(
"elemwise_
add
"
,
n
->
attrs
.
name
+
"_grad"
,
{
sub
3
,
output
})
MakeNode
(
"elemwise_
sub
"
,
n
->
attrs
.
name
+
"_grad"
,
{
sub
0
,
sub2
})
};
});
...
...
@@ -414,31 +416,33 @@ NNVM_REGISTER_OP(log_softmax)
.
set_attr
<
FGradient
>
(
"FGradient"
,
[](
const
NodePtr
&
n
,
const
std
::
vector
<
NodeEntry
>&
ograds
)
{
// grad_x = grad_y dot jacobian of softmax
// grad_x = grad_y dot jacobian of
log
softmax
//
// jacobian of softmax
// jacobian of
log
softmax
// [-y1 + 1, -y2, ... ]
// [ ... , -y2 + 1, ... ]
// [ ... ... ]
// [ ... ,-yn + 1]
//
// grad_x =
// [-(ograd1*y1 - 1 + ograd2*y2 + ..., -(ograd1*y1 - 1 + ograd2*y2, ..., ...]]
// grad_x = ograd elemwise_mul output
// grad_x = sum(grad_x, keepdim, axis)
// [ograd1 - exp(y1)*(ograd1 + ... + ogradn),
// ograd2 - exp(y2)*(ograd1 + ... + ogradn),
// ...
// ogradn - exp(yn)*(ograd1 + ... + ogradn)]
// grad_x = sum(ograd, keepdim, axis)
// sigma = exp(output)
// grad_x = grad_x elemwise_mul sigma
// grad_x = neg grad_x
// grad_x = grad_x + o
nes_like(
grad
_x)
// grad_x = grad_x + ograd
const
SoftmaxParam
&
param
=
nnvm
::
get
<
SoftmaxParam
>
(
n
->
attrs
.
parsed
);
NodeEntry
output
=
NodeEntry
{
n
,
0
,
0
};
NodeEntry
sub0
=
MakeNode
(
"elemwise_mul"
,
n
->
attrs
.
name
+
"_grad_sub0"
,
{
ograds
[
0
],
output
});
NodeEntry
sub1
=
MakeNode
(
"sum"
,
n
->
attrs
.
name
+
"_grad_sub1"
,
{
sub0
},
NodeEntry
sub0
=
MakeNode
(
"sum"
,
n
->
attrs
.
name
+
"_grad_sub0"
,
{
ograds
[
0
]},
{{
"axis"
,
std
::
to_string
(
param
.
axis
)},
{
"keepdims"
,
"true"
}});
NodeEntry
sub2
=
MakeNode
(
"full_like"
,
n
->
attrs
.
name
+
"_grad_sub2"
,
{
n
->
inputs
[
0
]},
{{
"fill_value"
,
"-1"
}});
NodeEntry
sub3
=
MakeNode
(
"broadcast_mul"
,
n
->
attrs
.
name
+
"_grad_sub3"
,
{
sub1
,
sub2
});
NodeEntry
sub1
=
MakeNode
(
"exp"
,
n
->
attrs
.
name
+
"_grad_sub1"
,
{
output
});
NodeEntry
sub2
=
MakeNode
(
"broadcast_mul"
,
n
->
attrs
.
name
+
"_grad_sub2"
,
{
sub0
,
sub1
});
return
std
::
vector
<
NodeEntry
>
{
MakeNode
(
"elemwise_
add
"
,
n
->
attrs
.
name
+
"_grad"
,
{
sub3
,
ograds
[
0
]})
MakeNode
(
"elemwise_
sub
"
,
n
->
attrs
.
name
+
"_grad"
,
{
ograds
[
0
]
,
sub2
})
};
})
.
set_support_level
(
1
);
...
...
This diff is collapsed.
Click to expand it.
nnvm/tests/python/compiler/test_top_level1.py
+
2
−
2
View file @
a9313787
...
...
@@ -217,7 +217,7 @@ def test_softmax():
dtype
=
"
float32
"
dshape
=
(
10
,
1000
)
inputs
=
[(
'
x
'
,
dshape
,
x
)]
helper
(
y
,
inputs
,
dtype
,
forward
)
,
backward
helper
(
y
,
inputs
,
dtype
,
forward
,
backward
)
def
test_log_softmax
():
...
...
@@ -229,7 +229,7 @@ def test_log_softmax():
def
backward
(
head_grads
,
x
):
y
=
topi
.
testing
.
log_softmax_python
(
x
)
grad
=
head_grads
-
np
.
sum
(
y
*
head_grads
,
axis
=
1
,
keepdims
=
True
)
grad
=
head_grads
-
np
.
exp
(
y
)
*
np
.
sum
(
head_grads
,
axis
=
1
,
keepdims
=
True
)
return
[
grad
]
dtype
=
"
float32
"
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment