You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

Trackable.cs 3.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /*****************************************************************************
  2. Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
  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. http://www.apache.org/licenses/LICENSE-2.0
  7. Unless required by applicable law or agreed to in writing, software
  8. distributed under the License is distributed on an "AS IS" BASIS,
  9. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. See the License for the specific language governing permissions and
  11. limitations under the License.
  12. ******************************************************************************/
  13. using System;
  14. using static Tensorflow.Binding;
  15. namespace Tensorflow.Train
  16. {
  17. public abstract class Trackable
  18. {
  19. protected int _self_update_uid;
  20. /// <summary>
  21. /// Restore-on-create for a variable be saved with this `Checkpointable`.
  22. /// </summary>
  23. /// <returns></returns>
  24. protected virtual IVariableV1 _add_variable_with_custom_getter(string name,
  25. int[] shape,
  26. TF_DataType dtype = TF_DataType.TF_FLOAT,
  27. IInitializer initializer = null,
  28. Func<string, int[], TF_DataType, IInitializer, bool, IVariableV1> getter = null,
  29. bool overwrite = false,
  30. bool trainable = false,
  31. bool use_resource = false,
  32. VariableSynchronization synchronization = VariableSynchronization.Auto,
  33. VariableAggregation aggregation = VariableAggregation.None)
  34. {
  35. ops.init_scope();
  36. IInitializer checkpoint_initializer = null;
  37. if (tf.context.executing_eagerly())
  38. ;
  39. else
  40. checkpoint_initializer = null;
  41. IVariableV1 new_variable;
  42. new_variable = getter(name, shape, dtype, initializer, trainable);
  43. // If we set an initializer and the variable processed it, tracking will not
  44. // assign again. It will add this variable to our dependencies, and if there
  45. // is a non-trivial restoration queued, it will handle that. This also
  46. // handles slot variables.
  47. if (!overwrite || new_variable is RefVariable)
  48. return _track_checkpointable(new_variable, name: name,
  49. overwrite: overwrite);
  50. else
  51. return new_variable;
  52. }
  53. /// <summary>
  54. /// Pop and load any deferred checkpoint restores into `trackable`.
  55. /// </summary>
  56. /// <param name="name"></param>
  57. /// <param name="trackable"></param>
  58. protected void _handle_deferred_dependencies(string name, IVariableV1 trackable)
  59. {
  60. _maybe_initialize_trackable();
  61. // TODO
  62. }
  63. protected IVariableV1 _track_checkpointable(IVariableV1 checkpointable, string name, bool overwrite = false)
  64. {
  65. return checkpointable;
  66. }
  67. /// <summary>
  68. /// Initialize dependency management.
  69. /// </summary>
  70. protected void _maybe_initialize_trackable()
  71. {
  72. // _self_unconditional_checkpoint_dependencies = []
  73. _self_update_uid = -1;
  74. }
  75. }
  76. }