data_flow_grad.py revision 854f49bd43588c062b046384f239f64a3d819702
1# Copyright 2015 Google Inc. All Rights Reserved.
2#
3# Licensed under the Apache License, Version 2.0 (the "License");
4# you may not use this file except in compliance with the License.
5# You may obtain a copy of the License at
6#
7#     http://www.apache.org/licenses/LICENSE-2.0
8#
9# Unless required by applicable law or agreed to in writing, software
10# distributed under the License is distributed on an "AS IS" BASIS,
11# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ==============================================================================
15
16"""Gradients for operators defined in data_flow_ops.py."""
17from __future__ import absolute_import
18from __future__ import division
19from __future__ import print_function
20
21from six.moves import xrange  # pylint: disable=redefined-builtin
22from tensorflow.python.framework import dtypes
23from tensorflow.python.framework import ops
24from tensorflow.python.ops import array_ops
25from tensorflow.python.ops import constant_op
26from tensorflow.python.ops import data_flow_ops
27from tensorflow.python.ops import gen_data_flow_ops
28from tensorflow.python.ops import math_ops
29
30
31@ops.RegisterGradient("DynamicStitch")
32def _DynamicStitchGrads(op, grad):
33  """Gradients for DynamicStitch."""
34
35  num_values = len(op.inputs) // 2
36  indices_grad = [None] * num_values
37
38  def AsInt32(x):
39    return (x if op.inputs[0].dtype == dtypes.int32 else
40            math_ops.cast(x, dtypes.int32))
41  inputs = [AsInt32(op.inputs[i]) for i in xrange(num_values)]
42  if isinstance(grad, ops.IndexedSlices):
43    output_shape = array_ops.shape(op.outputs[0])
44    output_rows = output_shape[0]
45    grad = math_ops.unsorted_segment_sum(grad.values, grad.indices, output_rows)
46  values_grad = [array_ops.gather(grad, inp) for inp in inputs]
47  return indices_grad + values_grad
48
49
50ops.NoGradient("Queue")
51ops.NoGradient("QueueEnqueue")
52ops.NoGradient("QueueEnqueueMany")
53ops.NoGradient("QueueDequeue")
54ops.NoGradient("QueueDequeueMany")
55ops.NoGradient("QueueClose")
56ops.NoGradient("QueueSize")
57
58ops.NoGradient("Stack")
59ops.NoGradient("StackPush")
60ops.NoGradient("StackPop")
61ops.NoGradient("StackClose")
62