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.

variables.py.cs 2.6 kB

6 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. namespace Tensorflow
  6. {
  7. public class variables
  8. {
  9. /// <summary>
  10. /// Returns all variables created with `trainable=True`
  11. /// </summary>
  12. /// <returns></returns>
  13. public static object trainable_variables()
  14. {
  15. return ops.get_collection(ops.GraphKeys.TRAINABLE_VARIABLES);
  16. }
  17. /// <summary>
  18. /// Returns all variables and `SaveableObject`s that must be checkpointed.
  19. /// </summary>
  20. /// <param name="scope"></param>
  21. /// <returns></returns>
  22. public static RefVariable[] _all_saveable_objects(string scope = "")
  23. {
  24. var all = new List<RefVariable>();
  25. var collection = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES, scope);
  26. if(collection != null)
  27. all.AddRange(collection as List<RefVariable>);
  28. collection = ops.get_collection(ops.GraphKeys.SAVEABLE_OBJECTS, scope);
  29. if (collection != null)
  30. all.AddRange(collection as List<RefVariable>);
  31. return all.ToArray();
  32. }
  33. /// <summary>
  34. /// Returns global variables.
  35. /// </summary>
  36. /// <param name="scope">
  37. /// (Optional.) A string. If supplied, the resulting list is filtered
  38. /// to include only items whose `name` attribute matches `scope` using
  39. /// `re.match`. Items without a `name` attribute are never returned if a
  40. /// scope is supplied. The choice of `re.match` means that a `scope` without
  41. /// special tokens filters by prefix.
  42. /// </param>
  43. /// <returns>A list of `Variable` objects.</returns>
  44. public static List<RefVariable> global_variables(string scope = "")
  45. {
  46. var result = ops.get_collection(ops.GraphKeys.GLOBAL_VARIABLES, scope);
  47. return result as List<RefVariable>;
  48. }
  49. /// <summary>
  50. /// Returns an Op that initializes a list of variables.
  51. /// </summary>
  52. /// <param name="var_list">List of `Variable` objects to initialize.</param>
  53. /// <param name="name">Optional name for the returned operation.</param>
  54. /// <returns>An Op that run the initializers of all the specified variables.</returns>
  55. public static Operation variables_initializer(RefVariable[] var_list, string name = "init")
  56. {
  57. return control_flow_ops.group(var_list.Select(x => x.initializer).ToArray(), name);
  58. }
  59. }
  60. }

tensorflow框架的.NET版本,提供了丰富的特性和API,可以借此很方便地在.NET平台下搭建深度学习训练与推理流程。