33using System . Linq ;
44using System . Text ;
55using Tensorflow . Framework ;
6+ using Tensorflow . Train ;
67using static Tensorflow . Python ;
78
89namespace Tensorflow
@@ -13,7 +14,7 @@ namespace Tensorflow
1314 /// class directly, but instead instantiate one of its subclasses such as
1415 /// `GradientDescentOptimizer`, `AdagradOptimizer`, or `MomentumOptimizer`.
1516 /// </summary>
16- public abstract class Optimizer
17+ public abstract class Optimizer : Trackable
1718 {
1819 // Values for gate_gradients.
1920 public static int GATE_NONE = 0 ;
@@ -27,6 +28,7 @@ public abstract class Optimizer
2728 public Dictionary < string , Dictionary < string , RefVariable > > _slots ;
2829 public Dictionary < string , RefVariable > _non_slot_dict ;
2930 public Dictionary < string , object > _deferred_slot_restorations ;
31+ SlotCreator slot_creator = new SlotCreator ( ) ;
3032
3133 public Optimizer ( float learning_rate , bool use_locking , string name = null )
3234 {
@@ -187,9 +189,49 @@ public Operation apply_gradients(Tuple<Tensor, RefVariable>[] grads_and_vars, Re
187189 } ) ;
188190 }
189191
190- private void _create_slots ( RefVariable [ ] var_list )
192+ /// <summary>
193+ /// Create the beta1 and beta2 accumulators on the same device as the first
194+ /// variable. Sort the var_list to make sure this device is consistent across
195+ /// workers (these need to go on the same PS, otherwise some updates are
196+ /// silently ignored).
197+ /// </summary>
198+ /// <param name="var_list"></param>
199+ protected virtual void _create_slots ( RefVariable [ ] var_list )
191200 {
201+
202+ }
192203
204+ /// <summary>
205+ /// Add an extra variable, not associated with a slot.
206+ /// </summary>
207+ /// <param name="initial_value"></param>
208+ /// <param name="name"></param>
209+ /// <param name="colocate_with"></param>
210+ protected RefVariable _create_non_slot_variable ( float initial_value , string name , RefVariable colocate_with )
211+ {
212+ // Recommendation: Use OptimizerV2 if your optimizer uses non-slot variables.
213+ var graph = colocate_with . graph ;
214+ var key = $ "{ name } .{ graph . graph_key } ";
215+ var v = _non_slot_dict . ContainsKey ( key ) ? _non_slot_dict [ key ] : null ;
216+ if ( v == null )
217+ {
218+ _maybe_initialize_trackable ( ) ;
219+ v = variable_scope . default_variable_creator (
220+ initial_value , name : name , trainable : false ,
221+ use_resource : resource_variable_ops . is_resource_variable (
222+ colocate_with ) ) ;
223+
224+ // Restore this variable by name if necessary, but don't add a
225+ // Trackable dependency. Optimizers return the current graph's
226+ // non-slot variables from _checkpoint_dependencies explicitly rather
227+ // than unconditionally adding dependencies (since there may be multiple
228+ // non-slot variables with the same name in different graphs, trying to
229+ // save all of them would result in errors).
230+ _handle_deferred_dependencies ( name , v ) ;
231+ _non_slot_dict [ key ] = v ;
232+ }
233+
234+ return v ;
193235 }
194236
195237 public virtual Operation _finish ( Operation [ ] update_ops , string name_scope )
@@ -341,5 +383,33 @@ protected T _call_if_callable<T>(T param)
341383 {
342384 return param ;
343385 }
386+
387+ /// <summary>
388+ /// Find or create a slot initialized with 0.0.
389+ /// </summary>
390+ /// <param name="var"></param>
391+ /// <param name="slot_name"></param>
392+ /// <param name="op_name"></param>
393+ /// <returns></returns>
394+ protected RefVariable _zero_slot ( RefVariable var , string slot_name , string op_name )
395+ {
396+ var named_slots = _slot_dict ( slot_name ) ;
397+ if ( ! named_slots . ContainsKey ( _var_key ( var ) ) )
398+ {
399+ var new_slot_variable = slot_creator . create_zeros_slot ( var , op_name ) ;
400+ }
401+ return named_slots [ _var_key ( var ) ] ;
402+ }
403+
404+ protected Dictionary < string , RefVariable > _slot_dict ( string slot_name )
405+ {
406+ var named_slots = _slots . ContainsKey ( slot_name ) ? _slots [ slot_name ] : null ;
407+ if ( named_slots == null )
408+ {
409+ _slots [ slot_name ] = new Dictionary < string , RefVariable > ( ) ;
410+ }
411+
412+ return named_slots ;
413+ }
344414 }
345415}
0 commit comments