From 605ab06bafcce1af8da747d9e3eb356291e9eede Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Fri, 17 Nov 2017 20:34:44 +0400 Subject: [PATCH 1/2] add DefaultGraphStack --- TensorFlowSharp/Tensorflow.cs | 34 +++++++++++++++++++++++++++++++++- 1 file changed, 33 insertions(+), 1 deletion(-) diff --git a/TensorFlowSharp/Tensorflow.cs b/TensorFlowSharp/Tensorflow.cs index 0f361989..a4970903 100644 --- a/TensorFlowSharp/Tensorflow.cs +++ b/TensorFlowSharp/Tensorflow.cs @@ -108,6 +108,36 @@ internal static void CheckSize () } } + internal class DefaultGraphStack + { + [ThreadStatic] + private static IList stack; + private static object lockObj = new object(); + + internal TFGraph Instance => stack.Last(); + + internal static void SetGraph(TFGraph graph) + { + if (stack == null) + { + lock (lockObj) + { + if (stack == null) + { + stack = new List(); + } + } + } + + stack.Add(graph); + } + + internal static void RemoveGraph(TFGraph graph) + { + stack.Remove(graph); + } + } + /// /// Base class for many TensorFlow data types that provides a common idiom to dispose and /// release resources associated with the native data types. Generally, you do not need to use this. @@ -473,12 +503,13 @@ public partial class TFGraph : TFDisposable /// /// Initializes a new instance of the class. /// - public TFGraph () : base (TF_NewGraph ()) + public TFGraph () : this (TF_NewGraph ()) { } internal TFGraph (IntPtr handle) : base (handle) { + DefaultGraphStack.SetGraph(this); } // extern void TF_DeleteGraph (TF_Graph *); @@ -486,6 +517,7 @@ internal TFGraph (IntPtr handle) : base (handle) static extern unsafe void TF_DeleteGraph (TF_Graph graph); internal override void NativeDispose (IntPtr handle) { + DefaultGraphStack.RemoveGraph(this); TF_DeleteGraph (handle); } From 54f9de9c2e0ec6b2b0370b3be845b6d44acd0c87 Mon Sep 17 00:00:00 2001 From: alexpantyukhin Date: Mon, 20 Nov 2017 11:35:29 +0400 Subject: [PATCH 2/2] add implementing of operations plus, minus, multiplication, division for TFOutput. --- TensorFlowSharp/Tensorflow.cs | 46 ++++++++++++++++++++++++++++++++++- 1 file changed, 45 insertions(+), 1 deletion(-) diff --git a/TensorFlowSharp/Tensorflow.cs b/TensorFlowSharp/Tensorflow.cs index a4970903..259b39ff 100644 --- a/TensorFlowSharp/Tensorflow.cs +++ b/TensorFlowSharp/Tensorflow.cs @@ -114,7 +114,7 @@ internal class DefaultGraphStack private static IList stack; private static object lockObj = new object(); - internal TFGraph Instance => stack.Last(); + internal static TFGraph Instance => stack.Last(); internal static void SetGraph(TFGraph graph) { @@ -3172,6 +3172,50 @@ public override string ToString () { return string.Format ("[{3} Index={1} Operation={2} (0x{0:X})]", (long) LLOperation, Index, Operation, OutputType); } + + /// + /// Plus operation. + /// + /// First output + /// Second output + /// + public static TFOutput operator + (TFOutput o1, TFOutput o2) + { + return DefaultGraphStack.Instance.Add (o1, o2); + } + + /// + /// Multiplication operation. + /// + /// First output + /// Second output + /// + public static TFOutput operator * (TFOutput o1, TFOutput o2) + { + return DefaultGraphStack.Instance.Mul (o1, o2); + } + + /// + /// Division operation. + /// + /// First output + /// Second output + /// + public static TFOutput operator / (TFOutput o1, TFOutput o2) + { + return DefaultGraphStack.Instance.Div (o1, o2); + } + + /// + /// Subtraction operation. + /// + /// First output + /// Second output + /// + public static TFOutput operator - (TFOutput o1, TFOutput o2) + { + return DefaultGraphStack.Instance.Sub (o1, o2); + } } ///