using System; using System.Collections.Generic; using System.Linq; using System.Text; using static Tensorflow.Python; namespace Tensorflow.Summaries { public class Summary { public Tensor merge_all(string key = ops.GraphKeys.SUMMARIES, string scope= null, string name= null) { var summary_ops = ops.get_collection(key, scope: scope); if (summary_ops == null) return null; else return merge((summary_ops as List).Select(x => x as Tensor).ToArray(), name: name); } /// /// Merges summaries. /// /// /// /// /// public Tensor merge(Tensor[] inputs, string[] collections = null, string name = null) { return with(ops.name_scope(name, "Merge", inputs), delegate { var val = gen_logging_ops.merge_summary(inputs: inputs, name: name); collect(val, collections?.ToList(), new List()); return val; }); } public Tensor scalar(string name, Tensor tensor, string[] collections = null, string family = null) { var (tag, scope) = summary_scope(name, family: family, values: new Tensor[] { tensor }); var val = gen_logging_ops.scalar_summary(tags: tag, values: tensor, name: scope); collect(val, collections?.ToList(), new List { ops.GraphKeys.SUMMARIES }); return val; } /// /// Adds keys to a collection. /// /// /// A collection of keys to add. /// Used if collections is None. public void collect(ITensorOrOperation val, List collections, List default_collections) { if (collections == null) collections = default_collections; foreach (var key in collections) ops.add_to_collection(key, val); } public (string, string) summary_scope(string name, string family = null, string default_name = null, Tensor[] values = null) { string scope_base_name = string.IsNullOrEmpty(family) ? name : $"{family}/{name}"; return with(ops.name_scope(scope_base_name, default_name: default_name, values), scope => { var tag = scope._name_scope; if (string.IsNullOrEmpty(family)) tag = tag.Remove(tag.Length - 1); else tag = $"{family}/{tag.Remove(tag.Length - 1)}"; return (tag, scope._name_scope); }); } } }