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.

SaveOptions.cs 2.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4. namespace Tensorflow.ModelSaving
  5. {
  6. /// <summary>
  7. /// Options for saving to SavedModel.
  8. /// </summary>
  9. public class SaveOptions
  10. {
  11. public bool save_debug_info = false;
  12. public IList<string>? namespace_white_list { get; set; } = null;
  13. public IDictionary<string, object>? function_aliases { get; set; } = null;
  14. public string? experimental_io_device { get; set; } = null;
  15. // TODO: experimental
  16. public VariablePolicy experimental_variable_policy { get; set; } = VariablePolicy.None;
  17. public bool experimental_custom_gradients { get; set; } = true;
  18. public SaveOptions(bool save_debug_info = false)
  19. {
  20. this.save_debug_info = save_debug_info;
  21. }
  22. }
  23. public class VariablePolicy
  24. {
  25. public string Policy { get; }
  26. private VariablePolicy(string policy)
  27. {
  28. Policy = policy;
  29. }
  30. public static VariablePolicy None = new(null);
  31. public static VariablePolicy SAVE_VARIABLE_DEVICES = new("save_variable_devices");
  32. public static VariablePolicy EXPAND_DISTRIBUTED_VARIABLES = new("expand_distributed_variables");
  33. public bool save_variable_devices()
  34. {
  35. return this != VariablePolicy.None;
  36. }
  37. /// <summary>
  38. /// Tries to convert `obj` to a VariablePolicy instance.
  39. /// </summary>
  40. /// <param name="obj"></param>
  41. /// <returns></returns>
  42. public static VariablePolicy from_obj(object obj)
  43. {
  44. if (obj is null) return VariablePolicy.None;
  45. if (obj is VariablePolicy) return (VariablePolicy)obj;
  46. var key = obj.ToString().ToLower();
  47. return key switch
  48. {
  49. null => VariablePolicy.None,
  50. "save_variable_devices" => VariablePolicy.SAVE_VARIABLE_DEVICES,
  51. "expand_distributed_variables" => VariablePolicy.EXPAND_DISTRIBUTED_VARIABLES,
  52. _ => throw new ValueError($"Received invalid VariablePolicy value: {obj}.")
  53. };
  54. }
  55. }
  56. }