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.0 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. namespace Tensorflow.Train
  15. {
  16. public abstract class Trackable
  17. {
  18. protected int _self_update_uid;
  19. /// <summary>
  20. /// Restore-on-create for a variable be saved with this `Checkpointable`.
  21. /// </summary>
  22. /// <returns></returns>
  23. protected virtual RefVariable _add_variable_with_custom_getter(string name,
  24. int[] shape,
  25. TF_DataType dtype = TF_DataType.TF_FLOAT,
  26. IInitializer initializer = null,
  27. Func<string, int[], TF_DataType, IInitializer, bool, RefVariable> getter = null,
  28. bool overwrite = false,
  29. bool trainable = false)
  30. {
  31. var checkpoint_initializer = true;
  32. var new_variable = getter(name, shape, dtype, initializer, trainable);
  33. // If we set an initializer and the variable processed it, tracking will not
  34. // assign again. It will add this variable to our dependencies, and if there
  35. // is a non-trivial restoration queued, it will handle that. This also
  36. // handles slot variables.
  37. if (!overwrite || new_variable is RefVariable)
  38. return _track_checkpointable(new_variable, name: name,
  39. overwrite: overwrite);
  40. else
  41. return new_variable;
  42. }
  43. /// <summary>
  44. /// Pop and load any deferred checkpoint restores into `trackable`.
  45. /// </summary>
  46. /// <param name="name"></param>
  47. /// <param name="trackable"></param>
  48. protected void _handle_deferred_dependencies(string name, RefVariable trackable)
  49. {
  50. _maybe_initialize_trackable();
  51. // TODO
  52. }
  53. protected RefVariable _track_checkpointable(RefVariable checkpointable, string name, bool overwrite = false)
  54. {
  55. return checkpointable;
  56. }
  57. /// <summary>
  58. /// Initialize dependency management.
  59. /// </summary>
  60. protected void _maybe_initialize_trackable()
  61. {
  62. // _self_unconditional_checkpoint_dependencies = []
  63. _self_update_uid = -1;
  64. }
  65. }
  66. }