1- /*****************************************************************************
2- Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3-
4- Licensed under the Apache License, Version 2.0 (the "License");
5- you may not use this file except in compliance with the License.
6- You may obtain a copy of the License at
7-
8- http://www.apache.org/licenses/LICENSE-2.0
9-
10- Unless required by applicable law or agreed to in writing, software
11- distributed under the License is distributed on an "AS IS" BASIS,
12- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13- See the License for the specific language governing permissions and
14- limitations under the License.
1+ /*****************************************************************************
2+ Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+ Licensed under the Apache License, Version 2.0 (the "License");
5+ you may not use this file except in compliance with the License.
6+ You may obtain a copy of the License at
7+
8+ http://www.apache.org/licenses/LICENSE-2.0
9+
10+ Unless required by applicable law or agreed to in writing, software
11+ distributed under the License is distributed on an "AS IS" BASIS,
12+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ See the License for the specific language governing permissions and
14+ limitations under the License.
1515******************************************************************************/
1616
1717using System ;
1818using System . Linq ;
1919using Tensorflow . Operations ;
20+ using static Tensorflow . Binding ;
2021
2122namespace Tensorflow . Gradients
2223{
2324 /// <summary>
2425 /// Gradients for operators defined in control_flow_ops.py.cs
2526 /// </summary>
27+ [ RegisterGradient ( "control_flow_grad" ) ]
2628 public class control_flow_grad
2729 {
2830 /// <summary>
@@ -33,6 +35,7 @@ public class control_flow_grad
3335 /// on the second visit. A next_iteration is also added on second visit.
3436 /// </summary>
3537 /// <returns></returns>
38+ [ RegisterGradient ( "Switch" ) ]
3639 public Tensor [ ] _SwitchGrad ( Tensor op , Tensor [ ] grads )
3740 {
3841 throw new NotImplementedException ( "_SwitchGrad" ) ;
@@ -83,68 +86,68 @@ public Tensor[] _SwitchGrad(Tensor op, Tensor[] grads)
8386 // false_grad = switch(grad[0], op.inputs[1])[0]
8487 // true_grad = switch(grad[1], op.inputs[1])[1]
8588 // return merge([false_grad, true_grad])[0], None
86- }
87-
89+ }
90+
8891 /// <summary>
8992 /// Gradients for a Merge op are calculated using a Switch op.
9093 /// </summary>
91- [ RegisterGradient ( "Merge" ) ]
94+ [ RegisterGradient ( "Merge" ) ]
9295 public static Tensor [ ] _MergeGrad ( Operation op , Tensor [ ] grads )
9396 {
9497 var grad = grads [ 0 ] ;
95- var _ = grads [ 1 ] ;
9698 var input_op = op . inputs [ 0 ] . op ;
9799 var graph = ops . get_default_graph ( ) ;
98100 var op_ctxt = control_flow_util . GetOutputContext ( input_op ) ;
99101 var grad_ctxt = graph . _get_control_flow_context ( ) ;
100102 switch ( op_ctxt )
101103 {
102- case WhileContext cwhile :
103- {
104- return control_flow_ops . _SwitchRefOrTensor ( grad , grad_ctxt . pivot ) ;
104+ case WhileContext cwhile :
105+ {
106+ return control_flow_ops . _SwitchRefOrTensor ( grad , grad_ctxt . pivot ) ;
105107 }
106- case CondContext ccond :
107- {
108- var pred = ccond . pred ;
109- if ( grad_ctxt != null && grad_ctxt . grad_state != null )
110- {
111- //# This Merge node is part of a cond within a loop.
112- //# The backprop needs to have the value of this predicate for every
113- //# iteration. So we must have its values accumulated in the forward, and
114- //# use the accumulated values as the predicate for this backprop switch.
115- var grad_state = grad_ctxt . grad_state ;
116- var real_pred = grad_state . history_map [ pred . name ] as Tensor ;
117- if ( real_pred == null )
118- {
119- //# Remember the value of pred for every iteration.
120- grad_ctxt = grad_state . grad_context ;
121- grad_ctxt . Exit ( ) ;
122- var history_pred = grad_state . AddForwardAccumulator ( pred ) ;
123- grad_ctxt . Enter ( ) ;
124-
125- //# Add the stack pop op. If pred.op is in a (outer) CondContext,
126- //# the stack pop will be guarded with a switch.
127- real_pred = grad_state . AddBackpropAccumulatedValue ( history_pred , pred ) ;
128- grad_state . history_map [ pred . name ] = real_pred ;
129- }
130- pred = real_pred ;
131- }
132- var results = control_flow_ops . _SwitchRefOrTensor ( grad , pred , name : "cond_grad" ) ;
133- return results ;
108+ case CondContext ccond :
109+ {
110+ var pred = ccond . pred ;
111+ if ( grad_ctxt != null && grad_ctxt . grad_state != null )
112+ {
113+ //# This Merge node is part of a cond within a loop.
114+ //# The backprop needs to have the value of this predicate for every
115+ //# iteration. So we must have its values accumulated in the forward, and
116+ //# use the accumulated values as the predicate for this backprop switch.
117+ var grad_state = grad_ctxt . grad_state ;
118+ var real_pred = grad_state . history_map [ pred . name ] as Tensor ;
119+ if ( real_pred == null )
120+ {
121+ //# Remember the value of pred for every iteration.
122+ grad_ctxt = grad_state . grad_context ;
123+ grad_ctxt . Exit ( ) ;
124+ var history_pred = grad_state . AddForwardAccumulator ( pred ) ;
125+ grad_ctxt . Enter ( ) ;
126+
127+ //# Add the stack pop op. If pred.op is in a (outer) CondContext,
128+ //# the stack pop will be guarded with a switch.
129+ real_pred = grad_state . AddBackpropAccumulatedValue ( history_pred , pred ) ;
130+ grad_state . history_map [ pred . name ] = real_pred ;
131+ }
132+ pred = real_pred ;
133+ }
134+ var results = control_flow_ops . _SwitchRefOrTensor ( grad , pred , name : "cond_grad" ) ;
135+ return results ;
134136 }
135- default :
136- {
137- var num_inputs = op . inputs . Length ;
138- var cond = new Tensor [ num_inputs ] ;
139- for ( int i = 0 ; i < num_inputs ; i ++ )
140- cond [ i ] = math_ops . equal ( op . outputs [ 1 ] , i ) ;
141- var result = cond . Select ( t => control_flow_ops . _SwitchRefOrTensor ( grad , t ) [ 1 ] ) . ToArray ( ) ;
142- return result ;
137+ default :
138+ {
139+ var num_inputs = op . inputs . Length ;
140+ var cond = new Tensor [ num_inputs ] ;
141+ for ( int i = 0 ; i < num_inputs ; i ++ )
142+ cond [ i ] = math_ops . equal ( op . outputs [ 1 ] , i ) ;
143+ var result = cond . Select ( t => control_flow_ops . _SwitchRefOrTensor ( grad , t ) [ 1 ] ) . ToArray ( ) ;
144+ return result ;
143145 }
144146 }
145147
146148 }
147149
150+ [ RegisterGradient ( "RefMerge" ) ]
148151 public Tensor [ ] _RefMergeGrad ( Operation op , Tensor [ ] grads )
149152 {
150153 return _MergeGrad ( op , grads ) ;
@@ -153,6 +156,7 @@ public Tensor[] _RefMergeGrad(Operation op, Tensor[] grads)
153156 /// <summary>
154157 /// Gradients for an exit op are calculated using an Enter op.
155158 /// </summary>
159+ [ RegisterGradient ( "Exit" ) ]
156160 public Tensor [ ] _ExitGrad ( Operation op , Tensor [ ] grads )
157161 {
158162 throw new NotImplementedException ( "_ExitGrad" ) ;
@@ -197,14 +201,16 @@ public Tensor[] _ExitGrad(Operation op, Tensor[] grads)
197201 ///
198202 /// Note that the backprop next_iteration is added in switch grad.
199203 /// </summary>
200- public ( object , Tensor [ ] ) _NextIterationGrad ( object _ , Tensor [ ] grad )
204+ [ RegisterGradient ( "NextIteration" ) ]
205+ public Tensor [ ] _NextIterationGrad ( object _ , Tensor [ ] grad )
201206 {
202- return ( _ , grad ) ;
207+ return grad ;
203208 }
204209
205- public ( object , Tensor [ ] ) _RefNextIterationGrad ( object _ , Tensor [ ] grad )
210+ [ RegisterGradient ( "RefNextIteration" ) ]
211+ public Tensor [ ] _RefNextIterationGrad ( object _ , Tensor [ ] grad )
206212 {
207- return ( _ , grad ) ;
213+ return grad ;
208214 }
209215
210216 /// <summary>
@@ -213,7 +219,8 @@ public Tensor[] _ExitGrad(Operation op, Tensor[] grads)
213219 /// For loop variables, grad is the gradient so just add an exit.
214220 /// For loop invariants, we need to add an accumulator loop.
215221 /// </summary>
216- public ( object , Tensor [ ] ) _EnterGrad ( Tensor op , Tensor [ ] grad )
222+ [ RegisterGradient ( "Enter" ) ]
223+ public Tensor [ ] _EnterGrad ( Tensor op , Tensor [ ] grad )
217224 {
218225 throw new NotImplementedException ( "_EnterGrad" ) ;
219226 // graph = ops.get_default_graph()
@@ -242,18 +249,21 @@ public Tensor[] _ExitGrad(Operation op, Tensor[] grads)
242249 // return result
243250 }
244251
245- public ( object , Tensor [ ] ) _RefEnterGrad ( Tensor op , Tensor [ ] grad )
252+
253+ [ RegisterGradient ( "RefEnter" ) ]
254+ public Tensor [ ] _RefEnterGrad ( Tensor op , Tensor [ ] grad )
246255 {
247256 return _EnterGrad ( op , grad ) ;
248257 }
249258
250259 /// <summary>
251260 /// Stop backprop for the predicate of a while loop.
252261 /// </summary>
253- public object _LoopCondGrad ( object _ )
262+ [ RegisterGradient ( "LoopCond" ) ]
263+ public Tensor [ ] _LoopCondGrad ( Tensor op , Tensor [ ] grad )
254264 {
255265 return null ;
256- }
257-
258- }
266+ }
267+
268+ }
259269}
0 commit comments