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
ModelZoo
ResNet50_tensorflow
Commits
ad254209
Unverified
Commit
ad254209
authored
Oct 10, 2018
by
Reed
Committed by
GitHub
Oct 10, 2018
Browse files
Improve perf by converting sparse grads to dense. (#5470)
parent
75d592e9
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
26 additions
and
0 deletions
+26
-0
official/recommendation/neumf_model.py
official/recommendation/neumf_model.py
+26
-0
No files found.
official/recommendation/neumf_model.py
View file @
ad254209
...
@@ -44,6 +44,31 @@ from official.recommendation import constants as rconst
...
@@ -44,6 +44,31 @@ from official.recommendation import constants as rconst
from
official.recommendation
import
stat_utils
from
official.recommendation
import
stat_utils
def
_sparse_to_dense_grads
(
grads_and_vars
):
"""Convert sparse gradients to dense gradients.
All sparse gradients, which are represented as instances of tf.IndexedSlices,
are converted to dense Tensors. Dense gradients, which are represents as
Tensors, are unchanged.
The purpose of this conversion is that for small embeddings, which are used by
this model, applying dense gradients with the AdamOptimizer is faster than
applying sparse gradients.
Args
grads_and_vars: A list of (gradient, variable) tuples. Each gradient can
be a Tensor or an IndexedSlices. Tensors are unchanged, and IndexedSlices
are converted to dense Tensors.
Returns:
The same list of (gradient, variable) as `grads_and_vars`, except each
IndexedSlices gradient is converted to a Tensor.
"""
# Calling convert_to_tensor changes IndexedSlices into Tensors, and leaves
# Tensors unchanged.
return
[(
tf
.
convert_to_tensor
(
g
),
v
)
for
g
,
v
in
grads_and_vars
]
def
neumf_model_fn
(
features
,
labels
,
mode
,
params
):
def
neumf_model_fn
(
features
,
labels
,
mode
,
params
):
"""Model Function for NeuMF estimator."""
"""Model Function for NeuMF estimator."""
if
params
.
get
(
"use_seed"
):
if
params
.
get
(
"use_seed"
):
...
@@ -94,6 +119,7 @@ def neumf_model_fn(features, labels, mode, params):
...
@@ -94,6 +119,7 @@ def neumf_model_fn(features, labels, mode, params):
tvars
=
tf
.
trainable_variables
()
tvars
=
tf
.
trainable_variables
()
gradients
=
optimizer
.
compute_gradients
(
gradients
=
optimizer
.
compute_gradients
(
loss
,
tvars
,
colocate_gradients_with_ops
=
True
)
loss
,
tvars
,
colocate_gradients_with_ops
=
True
)
gradients
=
_sparse_to_dense_grads
(
gradients
)
minimize_op
=
optimizer
.
apply_gradients
(
minimize_op
=
optimizer
.
apply_gradients
(
gradients
,
global_step
=
global_step
,
name
=
"train"
)
gradients
,
global_step
=
global_step
,
name
=
"train"
)
update_ops
=
tf
.
get_collection
(
tf
.
GraphKeys
.
UPDATE_OPS
)
update_ops
=
tf
.
get_collection
(
tf
.
GraphKeys
.
UPDATE_OPS
)
...
...
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