Browse Source

Graph.Operation-OperationByName: Added guard, docs and exception when IntPtr.Zero is returned

tags/v0.12
Eli Belash 6 years ago
parent
commit
e6640b5674
1 changed files with 15 additions and 1 deletions
  1. +15
    -1
      src/TensorFlowNET.Core/Graphs/Graph.Operation.cs

+ 15
- 1
src/TensorFlowNET.Core/Graphs/Graph.Operation.cs View File

@@ -59,14 +59,28 @@ namespace Tensorflow
return return_opers;
}

/// <summary>
/// Get operation with given <paramref name="operName"/>
/// </summary>
/// <exception cref="ValueError">When <paramref name="operName"/> is not found current graph.</exception>
/// <exception cref="RuntimeError">When tf.get_default_graph() is not current graph.</exception>
/// <example>
/// graph.GetOperationByName("CustomInputName");
/// </example>
public Operation OperationByName(string operName)
{
if (operName == null)
throw new ArgumentNullException(nameof(operName));

var handle = c_api.TF_GraphOperationByName(_handle, operName);
if (handle == IntPtr.Zero)
throw new ValueError($"Could not find operation \"{operName}\" inside graph \"{_graph_key}\".");

var defaultKey = tf.get_default_graph().graph_key;
if (graph_key != defaultKey)
{
//Console.WriteLine($"Current graph is not default graph.");
throw new ValueError($"Current graph is not default graph. Default Graph Key: {defaultKey}, Current Graph Key: {graph_key}");
throw new RuntimeError($"Current graph is not default graph. Default Graph Key: {defaultKey}, Current Graph Key: {graph_key}");
}

return new Operation(handle, g: this);


Loading…
Cancel
Save