44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
|
# File 'lib/tensor_stream/math_gradients.rb', line 44
def self._compute_derivative(node, grad)
node.graph.name_scope("#{node.name}_grad") do
x = node.inputs[0] if node.inputs[0]
y = node.inputs[1] if node.inputs[1]
case node.operation
when :add_n
return [grad] * node.inputs.size
when :add
return [grad, grad] if shapes_fully_specified_and_equal(x, y)
sx = tf.shape(x, name: 'add/shape_x')
sy = tf.shape(y, name: 'add/shape_y')
rx, ry = _broadcast_gradient_args(sx, sy)
[tf.reshape(tf.reduce_sum(grad, rx, name: 'add/reduce_sum_x'), sx),
tf.reshape(tf.reduce_sum(grad, ry, name: 'add/reduce_sum_y'), sy)]
when :asin
tf.control_dependencies([grad]) do
x2 = tf.square(x)
one = tf.constant(1, dtype: grad.data_type)
den = tf.sqrt(tf.subtract(one, x2))
inv = tf.reciprocal(den)
grad * inv
end
when :acos
tf.control_dependencies([grad]) do
x2 = tf.square(x)
one = tf.constant(1, dtype: grad.data_type)
den = tf.sqrt(tf.subtract(one, x2))
inv = tf.reciprocal(den)
-grad * inv
end
when :sub
return [grad, -grad] if shapes_fully_specified_and_equal(x, y)
sx = tf.shape(x, name: 'sub/shape_x')
sy = tf.shape(y, name: 'sub/shape_y')
rx, ry = _broadcast_gradient_args(sx, sy)
[tf.reshape(tf.reduce_sum(grad, rx, name: 'add/reduce_sub_x'), sx),
-tf.reshape(tf.reduce_sum(grad, ry, name: 'add/reduce_sub_y'), sy)]
when :mul
sx = tf.shape(x)
sy = tf.shape(y)
rx, ry = _broadcast_gradient_args(sx, sy)
[tf.reshape(tf.reduce_sum(tf.mul(grad, y), rx), sx),
tf.reshape(tf.reduce_sum(tf.mul(x, grad), ry), sy)]
when :div
sx = i_op(:shape, x)
sy = i_op(:shape, y)
rx, ry = _broadcast_gradient_args(sx, sy)
[tf.reshape(tf.reduce_sum(tf.div(grad, y), rx), sx),
tf.reshape(tf.reduce_sum(grad * tf.div(tf.div(-x, y), y), ry), sy)]
when :mod
sx = tf.shape(x)
sy = tf.shape(y)
rx, ry = _broadcast_gradient_args(sx, sy)
floor_xy = tf.floor_div(x, y)
gx = tf.reshape(tf.reduce_sum(grad, rx), sx)
gy = tf.reshape(tf.reduce_sum(grad * tf.negative(floor_xy), ry), sy)
[gx, gy]
when :squared_difference
sx = i_op(:shape, x)
sy = i_op(:shape, y)
rx, ry = _broadcast_gradient_args(sx, sy)
x_grad = tf.mul(2.0, grad) * (x - y)
[tf.reshape(tf.reduce_sum(x_grad, rx), sx),
tf.reshape(-tf.reduce_sum(x_grad, ry), sy)]
when :mat_mul
t_a = node.options[:transpose_a]
t_b = node.options[:transpose_b]
if !t_a && !t_b
grad_a = tf.matmul(grad, y, transpose_b: true)
grad_b = tf.matmul(x, grad, transpose_a: true)
elsif !ta && tb
grad_a = tf.matmul(grad, y)
grad_b = tf.matmul(grad, x, transpose_a: true)
elsif t_a && !t_b
grad_a = tf.matmul(y, grad, transpose_b: true)
grad_b = tf.matmul(x, grad)
elsif t_a && t_b
grad_a = tf.matmul(y, grad, transpose_a: true, transpose_b: true)
grad_b = tf.matmul(grad, x, transpose_a: true, transpose_b: true)
end
[grad_a, grad_b]
when :sin
grad * tf.cos(x)
when :tanh
grad * i_op(:tanh_grad, x)
when :pow
z = node
sx = tf.shape(x)
sy = tf.shape(y)
rx, ry = _broadcast_gradient_args(sx, sy)
gx = tf.reduce_sum(grad * y * tf.pow(x, y - 1), rx)
log_x = tf.where(x > 0, tf.log(x), tf.zeros_like(x))
gy = tf.reduce_sum(grad * z * log_x, ry)
[gx, gy]
when :abs
grad * tf.sign(x)
when :log
grad * tf.reciprocal(x)
when :cos
-grad * tf.sin(x)
when :max
_min_or_max_grad(node.inputs, grad, ->(x, y) { tf.greater_equal(x, y) } )
when :min
_min_or_max_grad(node.inputs, grad, ->(x, y) { tf.less_equal(x, y) } )
when :tan
secx = tf.reciprocal(tf.cos(x))
secx2 = tf.square(secx)
grad * secx2
when :negate
-grad
when :exp
grad * node
when :identity, :print
grad
when :sign
tf.zeros(tf.shape(x), dtype: x.data_type)
when :sum
_sum_grad(x, y, grad)
when :reciprocal
-grad * (tf.constant(1, dtype: x.dtype) / x**2)
when :sqrt
tf.constant(1, dtype: x.dtype) / (tf.constant(2, dtype: x.dtype) * tf.sqrt(x)) * grad
when :stop_gradient
tf.zeros_like(grad)
when :square
y = tf.constant(2.0, dtype: x.dtype)
tf.multiply(grad, tf.multiply(x, y))
when :where
x_mask = i_op(:where, i_op(:ones_like, x), i_op(:zeros_like, y), pred: node.options[:pred])
y_mask = i_op(:where, i_op(:zeros_like, x), i_op(:ones_like, y), pred: node.options[:pred])
[x_mask * grad, y_mask * grad]
when :cond
x_cond = i_op(:cond, i_op(:ones_like, x), i_op(:zeros_like, y), pred: node.options[:pred])
y_cond = i_op(:cond, i_op(:zeros_like, x), i_op(:ones_like, x), pred: node.options[:pred])
[x_cond * grad, y_cond * grad]
when :mean
sum_grad = _sum_grad(x, y, grad)[0]
input_shape = tf.shape(x)
output_shape = tf.shape(node)
factor = _safe_shape_div(tf.reduce_prod(input_shape), tf.reduce_prod(output_shape))
tf.div(sum_grad, tf.cast(factor, sum_grad.data_type))
when :log1p
grad * tf.reciprocal(i_cons(1, dtype: grad.data_type) + x)
when :sigmoid
i_op(:sigmoid_grad, x, grad)
when :sigmoid_grad
gb = grad * y
[gb - 2.0 * gb * x, i_op(:sigmoid_grad, x, grad)]
when :softmax
i_op(:softmax_grad, x, grad)
when :softmax_cross_entropy_with_logits_v2
[i_op(:softmax_cross_entropy_with_logits_v2_grad, x, y, grad), nil]
when :floor, :ceil
nil
when :zeros_like
nil
when :argmin, :argmax, :floor_div
[nil, nil]
else
raise "no derivative op for #{node.operation}"
end
end
end
|