Browse Source

Graph: Added tf.peak_default_graph which just returns null if no default exists.

tags/v0.12
Eli Belash 6 years ago
parent
commit
067e62bed2
2 changed files with 29 additions and 4 deletions
  1. +14
    -2
      src/TensorFlowNET.Core/APIs/tf.graph.cs
  2. +15
    -2
      src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs

+ 14
- 2
src/TensorFlowNET.Core/APIs/tf.graph.cs View File

@@ -29,7 +29,19 @@ namespace Tensorflow
return ops.get_default_graph(); return ops.get_default_graph();
} }


public Graph Graph()
/// <summary>
/// Equivalent to <see cref="get_default_graph"/> but does not create a new graph if it there is none.
/// </summary>
public Graph peak_default_graph()
{
return ops.default_graph_stack.peak_controller();
}

/// <summary>
/// Creates a new graph.
/// </summary>
///<remarks>Has no interaction with graph defaulting. Equivalent to new Graph();</remarks>
public Graph Graph()
=> new Graph(); => new Graph();
} }
}
}

+ 15
- 2
src/TensorFlowNET.Core/Graphs/DefaultGraphStack.cs View File

@@ -21,11 +21,10 @@ using static Tensorflow.Binding;


namespace Tensorflow namespace Tensorflow
{ {

/// <summary> /// <summary>
/// Serves as a stack for determining current default graph. /// Serves as a stack for determining current default graph.
/// </summary> /// </summary>
public class DefaultGraphStack
public class DefaultGraphStack
{ {
private readonly List<StackModel> _stack = new List<StackModel>(); private readonly List<StackModel> _stack = new List<StackModel>();


@@ -52,6 +51,20 @@ namespace Tensorflow
throw new TensorflowException("Unable to find a default graph"); throw new TensorflowException("Unable to find a default graph");
} }


public Graph peak_controller()
{
if (_stack.Count == 0 || _stack.Count(x => x.IsDefault) == 0)
return null;
for (var i = _stack.Count - 1; i >= 0; i--)
{
var x = _stack[i];
if (x.IsDefault)
return x.Graph;
}

return null;
}

public bool remove(Graph g) public bool remove(Graph g)
{ {
if (_stack.Count == 0) if (_stack.Count == 0)


Loading…
Cancel
Save