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.

Summary.cs 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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.Collections.Generic;
  14. using System.Linq;
  15. using static Tensorflow.Python;
  16. namespace Tensorflow.Summaries
  17. {
  18. public class Summary
  19. {
  20. public FileWriter FileWriter(string logdir, Graph graph,
  21. int max_queue = 10, int flush_secs = 120, string filename_suffix = null,
  22. Session session = null)
  23. => new FileWriter(logdir, graph, max_queue: max_queue,
  24. flush_secs: flush_secs, filename_suffix: filename_suffix,
  25. session: session);
  26. public Tensor histogram(string name, Tensor tensor, string[] collections = null, string family = null)
  27. {
  28. var (tag, scope) = summary_scope(name, family: family, values: new Tensor[] { tensor }, default_name: "HistogramSummary");
  29. var val = gen_logging_ops.histogram_summary(tag: tag, values: tensor, name: scope);
  30. collect(val, collections?.ToList(), new List<string> { ops.GraphKeys.SUMMARIES });
  31. return val;
  32. }
  33. public Tensor merge_all(string key = ops.GraphKeys.SUMMARIES, string scope= null, string name= null)
  34. {
  35. var summary_ops = ops.get_collection(key, scope: scope);
  36. if (summary_ops == null)
  37. return null;
  38. else
  39. return merge((summary_ops as List<ITensorOrOperation>).Select(x => x as Tensor).ToArray(), name: name);
  40. }
  41. /// <summary>
  42. /// Merges summaries.
  43. /// </summary>
  44. /// <param name="inputs"></param>
  45. /// <param name="collections"></param>
  46. /// <param name="name"></param>
  47. /// <returns></returns>
  48. public Tensor merge(Tensor[] inputs, string[] collections = null, string name = null)
  49. {
  50. return tf_with(ops.name_scope(name, "Merge", inputs), delegate
  51. {
  52. var val = gen_logging_ops.merge_summary(inputs: inputs, name: name);
  53. collect(val, collections?.ToList(), new List<string>());
  54. return val;
  55. });
  56. }
  57. public Tensor scalar(string name, Tensor tensor, string[] collections = null, string family = null)
  58. {
  59. var (tag, scope) = summary_scope(name, family: family, values: new Tensor[] { tensor });
  60. var val = gen_logging_ops.scalar_summary(tags: tag, values: tensor, name: scope);
  61. collect(val, collections?.ToList(), new List<string> { ops.GraphKeys.SUMMARIES });
  62. return val;
  63. }
  64. /// <summary>
  65. /// Adds keys to a collection.
  66. /// </summary>
  67. /// <param name="val"The value to add per each key.></param>
  68. /// <param name="collections">A collection of keys to add.</param>
  69. /// <param name="default_collections">Used if collections is None.</param>
  70. public void collect(ITensorOrOperation val, List<string> collections, List<string> default_collections)
  71. {
  72. if (collections == null)
  73. collections = default_collections;
  74. foreach (var key in collections)
  75. ops.add_to_collection(key, val);
  76. }
  77. public (string, string) summary_scope(string name, string family = null, string default_name = null, Tensor[] values = null)
  78. {
  79. string scope_base_name = string.IsNullOrEmpty(family) ? name : $"{family}/{name}";
  80. return tf_with(ops.name_scope(scope_base_name, default_name: default_name, values), scope =>
  81. {
  82. var tag = scope._name_scope;
  83. if (string.IsNullOrEmpty(family))
  84. tag = tag.Remove(tag.Length - 1);
  85. else
  86. tag = $"{family}/{tag.Remove(tag.Length - 1)}";
  87. return (tag, scope._name_scope);
  88. });
  89. }
  90. }
  91. }