From 5a7d28c200fd247b63fc8c74f0b1a139f660bfb5 Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 3 Mar 2019 20:08:48 -0600 Subject: [PATCH] simplify with function. --- src/TensorFlowNET.Core/Framework/importer.py.cs | 4 ++-- .../Gradients/gradients_impl.py.cs | 6 +++--- .../Operations/Losses/losses_impl.py.cs | 2 +- src/TensorFlowNET.Core/Operations/OpDefLibrary.cs | 4 ++-- src/TensorFlowNET.Core/Operations/array_ops.py.cs | 14 +++++++------- .../Operations/control_flow_ops.py.cs | 10 +++++----- src/TensorFlowNET.Core/Operations/math_ops.py.cs | 12 ++++++------ src/TensorFlowNET.Core/Operations/nn_impl.py.cs | 2 +- src/TensorFlowNET.Core/Operations/random_ops.py.cs | 4 ++-- src/TensorFlowNET.Core/Python.cs | 8 ++++---- src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs | 2 +- src/TensorFlowNET.Core/Tensors/Tensor.cs | 2 +- src/TensorFlowNET.Core/Train/Optimizer.cs | 6 +++--- .../Train/Saving/BaseSaverBuilder.cs | 4 ++-- .../Variables/RefVariable.Operators.cs | 2 +- src/TensorFlowNET.Core/Variables/RefVariable.cs | 4 ++-- src/TensorFlowNET.Core/Variables/VariableScope.cs | 4 ++-- src/TensorFlowNET.Core/ops.py.cs | 4 ++-- test/TensorFlowNET.Examples/ImageRecognition.cs | 6 +++--- .../InceptionArchGoogLeNet.cs | 6 +++--- test/TensorFlowNET.Examples/LinearRegression.cs | 2 +- test/TensorFlowNET.Examples/MetaGraph.cs | 2 +- test/TensorFlowNET.UnitTest/CApiTest.cs | 2 +- test/TensorFlowNET.UnitTest/ConstantTest.cs | 10 +++++----- test/TensorFlowNET.UnitTest/NameScopeTest.cs | 4 ++-- test/TensorFlowNET.UnitTest/PlaceholderTest.cs | 4 ++-- test/TensorFlowNET.UnitTest/SessionTest.cs | 2 +- test/TensorFlowNET.UnitTest/TrainSaverTest.cs | 6 +++--- test/TensorFlowNET.UnitTest/VariableTest.cs | 13 ++++++++++--- 29 files changed, 79 insertions(+), 72 deletions(-) diff --git a/src/TensorFlowNET.Core/Framework/importer.py.cs b/src/TensorFlowNET.Core/Framework/importer.py.cs index a0a45ca0..9070d27b 100644 --- a/src/TensorFlowNET.Core/Framework/importer.py.cs +++ b/src/TensorFlowNET.Core/Framework/importer.py.cs @@ -7,7 +7,7 @@ using static Tensorflow.OpDef.Types; namespace Tensorflow { - public class importer + public class importer : Python { public static ITensorOrOperation[] import_graph_def(GraphDef graph_def, Dictionary input_map = null, @@ -26,7 +26,7 @@ namespace Tensorflow string prefix = ""; var graph = ops.get_default_graph(); - Python.with(new ops.name_scope(name, "import", input_map.Values), scope => + with(new ops.name_scope(name, "import", input_map.Values), scope => { prefix = scope; /*if (!string.IsNullOrEmpty(prefix)) diff --git a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs index e4993b5e..a4af9caa 100644 --- a/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs +++ b/src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs @@ -7,7 +7,7 @@ using System.Threading; namespace Tensorflow { - public class gradients_impl + public class gradients_impl : Python { public static Tensor[] gradients(Tensor[] ys, Tensor[] xs, @@ -58,7 +58,7 @@ namespace Tensorflow **/ var grads = new Dictionary(); - Python.with(new ops.name_scope(name, "gradients", values: all), scope => + with(new ops.name_scope(name, "gradients", values: all), scope => { string grad_scope = scope; // Get a uid for this call to gradients that can be used to help @@ -131,7 +131,7 @@ namespace Tensorflow // for ops that do not have gradients. var grad_fn = ops.get_gradient_function(op); - Python.with(new ops.name_scope(op.name + "_grad"), scope1 => + with(new ops.name_scope(op.name + "_grad"), scope1 => { string name1 = scope1; if (grad_fn != null) diff --git a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs index e45074b0..02d7f180 100644 --- a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs @@ -12,7 +12,7 @@ namespace Tensorflow string scope = "", string loss_collection= "losses") { - with(new ops.name_scope(scope, + with(new ops.name_scope(scope, "sparse_softmax_cross_entropy_loss", (logits, labels, weights)), namescope => diff --git a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs index 0c19dea0..8e6bfba0 100644 --- a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs +++ b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs @@ -10,7 +10,7 @@ using static Tensorflow.OpDef.Types; namespace Tensorflow { - public class OpDefLibrary + public class OpDefLibrary : Python { public Operation _apply_op_helper(string op_type_name, string name = null, dynamic args = null) { @@ -44,7 +44,7 @@ namespace Tensorflow var input_types = new List(); dynamic values = null; - return Python.with(new ops.name_scope(name), scope => + return with(new ops.name_scope(name), scope => { var inferred_from = new Dictionary(); var base_types = new List(); diff --git a/src/TensorFlowNET.Core/Operations/array_ops.py.cs b/src/TensorFlowNET.Core/Operations/array_ops.py.cs index 6f8f2f05..cc248e26 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.py.cs @@ -5,14 +5,14 @@ using System.Text; namespace Tensorflow { - public class array_ops + public class array_ops : Python { public static Tensor placeholder_with_default(T input, int[] shape, string name = null) => gen_array_ops.placeholder_with_default(input, shape, name); public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) { dtype = dtype.as_base_dtype(); - return Python.with(new ops.name_scope(name, "zeros", shape), scope => + return with(new ops.name_scope(name, "zeros", shape), scope => { name = scope; switch (dtype) @@ -68,7 +68,7 @@ namespace Tensorflow private static Tensor ones_like_impl(T tensor, TF_DataType dtype, string name, bool optimize = true) { - return Python.with(new ops.name_scope(name, "ones_like", new { tensor }), scope => + return with(new ops.name_scope(name, "ones_like", new { tensor }), scope => { name = scope; var tensor1 = ops.convert_to_tensor(tensor, name: "tensor"); @@ -84,7 +84,7 @@ namespace Tensorflow public static Tensor ones(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null) { dtype = dtype.as_base_dtype(); - return Python.with(new ops.name_scope(name, "ones", new { shape }), scope => + return with(new ops.name_scope(name, "ones", new { shape }), scope => { name = scope; var output = gen_array_ops.fill(shape, constant_op.constant(1.0f, dtype: dtype), name: name); @@ -130,7 +130,7 @@ namespace Tensorflow private static Tensor shape_internal(Tensor input, string name = null, bool optimize = true, TF_DataType out_type = TF_DataType.TF_INT32) { - return Python.with(new ops.name_scope(name, "Shape", new { input }), scope => + return with(new ops.name_scope(name, "Shape", new { input }), scope => { name = scope; @@ -151,7 +151,7 @@ namespace Tensorflow private static Tensor size_internal(Tensor input, string name = null, bool optimize = true, TF_DataType out_type = TF_DataType.TF_INT32) { - return Python.with(new ops.name_scope(name, "Size", new Tensor[] { input }), scope => + return with(new ops.name_scope(name, "Size", new Tensor[] { input }), scope => { name = scope; @@ -182,7 +182,7 @@ namespace Tensorflow public static Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true) { - return Python.with(new ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope => + return with(new ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope => { name = scope; tensor = ops.convert_to_tensor(tensor, name: "tensor"); diff --git a/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs b/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs index c9220868..303104b5 100644 --- a/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs @@ -9,7 +9,7 @@ namespace Tensorflow { public static Operation group(T[] inputs, string name = null) where T : ITensorOrOperation { - return with(new ops.name_scope(name, "group_deps", inputs), scope => + return with(new ops.name_scope(name, "group_deps", inputs), scope => { name = scope; @@ -39,7 +39,7 @@ namespace Tensorflow private static Operation _GroupControlDeps(string dev, Operation[] deps, string name = null) { - return Python.with<_ControlDependenciesController, Operation>(ops.control_dependencies(deps), ctl => + return with(ops.control_dependencies(deps), ctl => { if (dev == null) { @@ -83,7 +83,7 @@ namespace Tensorflow public static Tensor[] tuple(Tensor[] tensors, string name = null, Operation[] control_inputs = null) { - return Python.with(new ops.name_scope(name, "tuple", tensors), scope => + return with(new ops.name_scope(name, "tuple", tensors), scope => { name = scope; var gating_ops = tensors.Select(x => x.op).ToList(); @@ -115,11 +115,11 @@ namespace Tensorflow values.AddRange(dependencies); values.Add(output_tensor); - return Python.with(new ops.name_scope(name, "control_dependency", values), scope => + return with(new ops.name_scope(name, "control_dependency", values), scope => { name = scope; - return Python.with<_ControlDependenciesController, Tensor>(ops.control_dependencies(dependencies), ctl => + return with(ops.control_dependencies(dependencies), ctl => { output_tensor = ops.convert_to_tensor_or_composite(output_tensor); return _Identity(output_tensor, name: name); diff --git a/src/TensorFlowNET.Core/Operations/math_ops.py.cs b/src/TensorFlowNET.Core/Operations/math_ops.py.cs index ed952e53..8e87e388 100644 --- a/src/TensorFlowNET.Core/Operations/math_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/math_ops.py.cs @@ -14,7 +14,7 @@ namespace Tensorflow if(base_type == x.dtype) return x; - return with(new ops.name_scope(name, "Cast", new { x }), scope => + return with(new ops.name_scope(name, "Cast", new { x }), scope => { x = ops.convert_to_tensor(x, name: "x"); if (x.dtype.as_base_dtype() != base_type) @@ -141,7 +141,7 @@ namespace Tensorflow if (delta == null) delta = 1; - return with(new ops.name_scope(name, "Range", new object[] { start, limit, delta }), scope => + return with(new ops.name_scope(name, "Range", new object[] { start, limit, delta }), scope => { name = scope; var start1 = ops.convert_to_tensor(start, name: "start"); @@ -154,7 +154,7 @@ namespace Tensorflow public static Tensor floordiv(Tensor x, Tensor y, string name = null) { - return with(new ops.name_scope(name, "floordiv", new { x, y }), scope => + return with(new ops.name_scope(name, "floordiv", new { x, y }), scope => { return gen_math_ops.floor_div(x, y, scope); }); @@ -162,7 +162,7 @@ namespace Tensorflow public static Tensor rank_internal(Tensor input, string name = null, bool optimize = true) { - return with(new ops.name_scope(name, "Rank", new List { input }), scope => + return with(new ops.name_scope(name, "Rank", new List { input }), scope => { name = scope; var input_tensor = ops.convert_to_tensor(input); @@ -182,7 +182,7 @@ namespace Tensorflow { Tensor result = null; - Python.with(new ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope => + with(new ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope => { name = scope; @@ -212,7 +212,7 @@ namespace Tensorflow if (dt.is_floating() || dt.is_integer()) return x; - return Python.with(new ops.name_scope(name, "Conj", new List { x }), scope => + return with(new ops.name_scope(name, "Conj", new List { x }), scope => { return x; diff --git a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs index 3005863c..1e6ff3be 100644 --- a/src/TensorFlowNET.Core/Operations/nn_impl.py.cs +++ b/src/TensorFlowNET.Core/Operations/nn_impl.py.cs @@ -20,7 +20,7 @@ namespace Tensorflow bool keep_dims = false) { Tuple t = null; - with(new ops.name_scope(name, "moments", new { x, axes }), scope => + with(new ops.name_scope(name, "moments", new { x, axes }), scope => { // The dynamic range of fp16 is too limited to support the collection of // sufficient statistics. As a workaround we simply perform the operations diff --git a/src/TensorFlowNET.Core/Operations/random_ops.py.cs b/src/TensorFlowNET.Core/Operations/random_ops.py.cs index 00f1aa00..c6ea3aa9 100644 --- a/src/TensorFlowNET.Core/Operations/random_ops.py.cs +++ b/src/TensorFlowNET.Core/Operations/random_ops.py.cs @@ -23,7 +23,7 @@ namespace Tensorflow int? seed = null, string name = null) { - return Python.with(new ops.name_scope(name, "random_normal", new object[] { shape, mean, stddev }), scope => + return with(new ops.name_scope(name, "random_normal", new { shape, mean, stddev }), scope => { var shape_tensor = _ShapeTensor(shape); var mean_tensor = ops.convert_to_tensor(mean, dtype: dtype, name: "mean"); @@ -53,7 +53,7 @@ namespace Tensorflow int? seed = null, string name = null) { - return with(new ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope => + return with(new ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope => { name = scope; var tensorShape = _ShapeTensor(shape); diff --git a/src/TensorFlowNET.Core/Python.cs b/src/TensorFlowNET.Core/Python.cs index 7ea80a90..d40c2cc0 100644 --- a/src/TensorFlowNET.Core/Python.cs +++ b/src/TensorFlowNET.Core/Python.cs @@ -43,12 +43,12 @@ namespace Tensorflow } } - public static void with(IPython py, Action action) where T : IPython + public static void with(T py, Action action) where T : IPython { try { py.__enter__(); - action((T)py); + action(py); } catch (Exception ex) { @@ -62,12 +62,12 @@ namespace Tensorflow } } - public static TOut with(IPython py, Func action) where TIn : IPython + public static TOut with(TIn py, Func action) where TIn : IPython { try { py.__enter__(); - return action((TIn)py); + return action(py); } catch (Exception ex) { diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs index 89a8aa17..a357e5d5 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.Operators.cs @@ -42,7 +42,7 @@ namespace Tensorflow dtype = tr.dtype.as_base_dtype(); var namescope = new ops.name_scope(null, name, new { x, y }); - return Python.with(namescope, scope => + return with(namescope, scope => { Tensor result = null; var x1 = ops.convert_to_tensor(x, dtype: dtype, name: "x"); diff --git a/src/TensorFlowNET.Core/Tensors/Tensor.cs b/src/TensorFlowNET.Core/Tensors/Tensor.cs index 76db062e..2b3db534 100644 --- a/src/TensorFlowNET.Core/Tensors/Tensor.cs +++ b/src/TensorFlowNET.Core/Tensors/Tensor.cs @@ -12,7 +12,7 @@ namespace Tensorflow /// A tensor is a generalization of vectors and matrices to potentially higher dimensions. /// Internally, TensorFlow represents tensors as n-dimensional arrays of base datatypes. /// - public partial class Tensor : IDisposable, ITensorOrOperation + public partial class Tensor : Python, IDisposable, ITensorOrOperation { private readonly IntPtr _handle; diff --git a/src/TensorFlowNET.Core/Train/Optimizer.cs b/src/TensorFlowNET.Core/Train/Optimizer.cs index 25c443f3..407315e2 100644 --- a/src/TensorFlowNET.Core/Train/Optimizer.cs +++ b/src/TensorFlowNET.Core/Train/Optimizer.cs @@ -12,7 +12,7 @@ namespace Tensorflow /// class directly, but instead instantiate one of its subclasses such as /// `GradientDescentOptimizer`, `AdagradOptimizer`, or `MomentumOptimizer`. /// - public abstract class Optimizer + public abstract class Optimizer : Python { // Values for gate_gradients. public static int GATE_NONE = 0; @@ -87,7 +87,7 @@ namespace Tensorflow _create_slots(var_list); var update_ops = new List(); - return Python.with(new ops.name_scope(name, Name), scope => + return with(new ops.name_scope(name, Name), scope => { name = scope; _prepare(); @@ -98,7 +98,7 @@ namespace Tensorflow continue; var scope_name = var.op.name; - Python.with(new ops.name_scope("update_" + scope_name), scope2 => + with(new ops.name_scope("update_" + scope_name), scope2 => { update_ops.Add(processor.update_op(this, grad)); }); diff --git a/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs b/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs index 06a1fb49..876bf856 100644 --- a/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs +++ b/src/TensorFlowNET.Core/Train/Saving/BaseSaverBuilder.cs @@ -5,7 +5,7 @@ using System.Text; namespace Tensorflow { - public class BaseSaverBuilder + public class BaseSaverBuilder : Python { protected SaverDef.Types.CheckpointFormatVersion _write_version; @@ -79,7 +79,7 @@ namespace Tensorflow Tensor save_tensor = null; Operation restore_op = null; - return Python.with(new ops.name_scope(name, "save", saveables.Select(x => x.op).ToArray()), scope => + return with(new ops.name_scope(name, "save", saveables.Select(x => x.op).ToArray()), scope => { name = scope; diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs b/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs index 5bd4120d..3b2f67c4 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.Operators.cs @@ -17,7 +17,7 @@ namespace Tensorflow private static Tensor op_helper(string default_name, RefVariable x, T y) { var tensor1 = x.value(); - return with(new ops.name_scope(null, default_name, new { tensor1, y }), scope => { + return with(new ops.name_scope(null, default_name, new { tensor1, y }), scope => { var tensor2 = ops.convert_to_tensor(y, tensor1.dtype.as_base_dtype(), "y"); return gen_math_ops.add(tensor1, tensor2, scope); }); diff --git a/src/TensorFlowNET.Core/Variables/RefVariable.cs b/src/TensorFlowNET.Core/Variables/RefVariable.cs index ea7069e4..5eb03d3f 100644 --- a/src/TensorFlowNET.Core/Variables/RefVariable.cs +++ b/src/TensorFlowNET.Core/Variables/RefVariable.cs @@ -118,7 +118,7 @@ namespace Tensorflow ops.init_scope(); var values = init_from_fn ? new object[0] : new object[] { initial_value }; - with(new ops.name_scope(name, "Variable", values), scope => + with(new ops.name_scope(name, "Variable", values), scope => { name = scope; if (init_from_fn) @@ -132,7 +132,7 @@ namespace Tensorflow List = new AttrValue.Types.ListValue() }; attr.List.S.Add(ByteString.CopyFromUtf8($"loc:{true_name}")); - with(new ops.name_scope("Initializer"), scope2 => + with(new ops.name_scope("Initializer"), scope2 => { _initial_value = (initial_value as Func)(); _initial_value = ops.convert_to_tensor(_initial_value, name: "initial_value", dtype: dtype); diff --git a/src/TensorFlowNET.Core/Variables/VariableScope.cs b/src/TensorFlowNET.Core/Variables/VariableScope.cs index d5e16504..7b924ce6 100644 --- a/src/TensorFlowNET.Core/Variables/VariableScope.cs +++ b/src/TensorFlowNET.Core/Variables/VariableScope.cs @@ -7,7 +7,7 @@ namespace Tensorflow /// /// Variable scope object to carry defaults to provide to `get_variable` /// - public class VariableScope + public class VariableScope : Python { public bool use_resource { get; set; } private _ReuseMode _reuse; @@ -38,7 +38,7 @@ namespace Tensorflow VariableAggregation aggregation= VariableAggregation.NONE) { string full_name = !string.IsNullOrEmpty(this.name) ? this.name + "/" + name : name; - return Python.with(new ops.name_scope(null), scope => + return with(new ops.name_scope(null), scope => { if (dtype == TF_DataType.DtInvalid) dtype = _dtype; diff --git a/src/TensorFlowNET.Core/ops.py.cs b/src/TensorFlowNET.Core/ops.py.cs index 6f2408db..c993ee8c 100644 --- a/src/TensorFlowNET.Core/ops.py.cs +++ b/src/TensorFlowNET.Core/ops.py.cs @@ -12,7 +12,7 @@ using System.ComponentModel; namespace Tensorflow { - public partial class ops + public partial class ops : Python { public static void add_to_collection(string name, T value) { @@ -216,7 +216,7 @@ namespace Tensorflow // inner_device_stack = default_graph._device_function_stack // var outer_context = default_graph.as_default; - Python.with(ops.control_dependencies(null), delegate + with(ops.control_dependencies(null), delegate { var outer_graph = get_default_graph(); // outer_device_stack = None diff --git a/test/TensorFlowNET.Examples/ImageRecognition.cs b/test/TensorFlowNET.Examples/ImageRecognition.cs index 73bf0ab1..a4bf05f4 100644 --- a/test/TensorFlowNET.Examples/ImageRecognition.cs +++ b/test/TensorFlowNET.Examples/ImageRecognition.cs @@ -39,7 +39,7 @@ namespace TensorFlowNET.Examples var idx = 0; float propability = 0; - with(tf.Session(graph), sess => + with(tf.Session(graph), sess => { var results = sess.run(output_operation.outputs[0], new FeedItem(input_operation.outputs[0], tensor)); var probabilities = results.Data(); @@ -63,7 +63,7 @@ namespace TensorFlowNET.Examples int input_mean = 117, int input_std = 1) { - return with(tf.Graph().as_default(), graph => + return with(tf.Graph().as_default(), graph => { var file_reader = tf.read_file(file_name, "file_reader"); var decodeJpeg = tf.image.decode_jpeg(file_reader, channels: 3, name: "DecodeJpeg"); @@ -74,7 +74,7 @@ namespace TensorFlowNET.Examples var sub = tf.subtract(bilinear, new float[] { input_mean }); var normalized = tf.divide(sub, new float[] { input_std }); - return with(tf.Session(graph), sess => sess.run(normalized)); + return with(tf.Session(graph), sess => sess.run(normalized)); }); } diff --git a/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs b/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs index 3cd0dbda..aaae7c1a 100644 --- a/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs +++ b/test/TensorFlowNET.Examples/InceptionArchGoogLeNet.cs @@ -46,7 +46,7 @@ namespace TensorFlowNET.Examples var input_operation = graph.get_operation_by_name(input_name); var output_operation = graph.get_operation_by_name(output_name); - var results = with(tf.Session(graph), + var results = with(tf.Session(graph), sess => sess.run(output_operation.outputs[0], new FeedItem(input_operation.outputs[0], nd))); @@ -68,7 +68,7 @@ namespace TensorFlowNET.Examples int input_mean = 0, int input_std = 255) { - return with(tf.Graph().as_default(), graph => + return with(tf.Graph().as_default(), graph => { var file_reader = tf.read_file(file_name, "file_reader"); var image_reader = tf.image.decode_jpeg(file_reader, channels: 3, name: "jpeg_reader"); @@ -79,7 +79,7 @@ namespace TensorFlowNET.Examples var sub = tf.subtract(bilinear, new float[] { input_mean }); var normalized = tf.divide(sub, new float[] { input_std }); - return with(tf.Session(graph), sess => sess.run(normalized)); + return with(tf.Session(graph), sess => sess.run(normalized)); }); } diff --git a/test/TensorFlowNET.Examples/LinearRegression.cs b/test/TensorFlowNET.Examples/LinearRegression.cs index e1f81f5d..35d64e5c 100644 --- a/test/TensorFlowNET.Examples/LinearRegression.cs +++ b/test/TensorFlowNET.Examples/LinearRegression.cs @@ -53,7 +53,7 @@ namespace TensorFlowNET.Examples var init = tf.global_variables_initializer(); // Start training - with(tf.Session(), sess => + with(tf.Session(), sess => { // Run the initializer sess.run(init); diff --git a/test/TensorFlowNET.Examples/MetaGraph.cs b/test/TensorFlowNET.Examples/MetaGraph.cs index bb272a2c..7ce74ffc 100644 --- a/test/TensorFlowNET.Examples/MetaGraph.cs +++ b/test/TensorFlowNET.Examples/MetaGraph.cs @@ -16,7 +16,7 @@ namespace TensorFlowNET.Examples private void ImportMetaGraph(string dir) { - with(tf.Session(), sess => + with(tf.Session(), sess => { var new_saver = tf.train.import_meta_graph(dir + "my-model-10000.meta"); new_saver.restore(sess, dir + "my-model-10000"); diff --git a/test/TensorFlowNET.UnitTest/CApiTest.cs b/test/TensorFlowNET.UnitTest/CApiTest.cs index dedb88b3..4f0be55d 100644 --- a/test/TensorFlowNET.UnitTest/CApiTest.cs +++ b/test/TensorFlowNET.UnitTest/CApiTest.cs @@ -6,7 +6,7 @@ using Tensorflow; namespace TensorFlowNET.UnitTest { - public class CApiTest + public class CApiTest : Python { protected TF_Code TF_OK = TF_Code.TF_OK; protected TF_DataType TF_FLOAT = TF_DataType.TF_FLOAT; diff --git a/test/TensorFlowNET.UnitTest/ConstantTest.cs b/test/TensorFlowNET.UnitTest/ConstantTest.cs index 7b0768e0..ee496e7c 100644 --- a/test/TensorFlowNET.UnitTest/ConstantTest.cs +++ b/test/TensorFlowNET.UnitTest/ConstantTest.cs @@ -10,7 +10,7 @@ using Tensorflow; namespace TensorFlowNET.UnitTest { [TestClass] - public class ConstantTest + public class ConstantTest : Python { Status status = new Status(); @@ -27,7 +27,7 @@ namespace TensorFlowNET.UnitTest { string str = "Hello, TensorFlow.NET!"; var tensor = tf.constant(str); - Python.with(tf.Session(), sess => + with(tf.Session(), sess => { var result = sess.run(tensor); Assert.IsTrue(result.Data()[0] == str); @@ -39,7 +39,7 @@ namespace TensorFlowNET.UnitTest { // small size var tensor = tf.zeros(new Shape(3, 2), TF_DataType.TF_INT32, "small"); - Python.with(tf.Session(), sess => + with(tf.Session(), sess => { var result = sess.run(tensor); @@ -50,7 +50,7 @@ namespace TensorFlowNET.UnitTest // big size tensor = tf.zeros(new Shape(200, 100), TF_DataType.TF_INT32, "big"); - Python.with(tf.Session(), sess => + with(tf.Session(), sess => { var result = sess.run(tensor); @@ -74,7 +74,7 @@ namespace TensorFlowNET.UnitTest }); var tensor = tf.constant(nd); - Python.with(tf.Session(), sess => + with(tf.Session(), sess => { var result = sess.run(tensor); var data = result.Data(); diff --git a/test/TensorFlowNET.UnitTest/NameScopeTest.cs b/test/TensorFlowNET.UnitTest/NameScopeTest.cs index 64b2bd1f..01a5f031 100644 --- a/test/TensorFlowNET.UnitTest/NameScopeTest.cs +++ b/test/TensorFlowNET.UnitTest/NameScopeTest.cs @@ -15,7 +15,7 @@ namespace TensorFlowNET.UnitTest [TestMethod] public void NestedNameScope() { - with(new ops.name_scope("scope1"), scope1 => + with(new ops.name_scope("scope1"), scope1 => { name = scope1; Assert.AreEqual("scope1", g._name_stack); @@ -24,7 +24,7 @@ namespace TensorFlowNET.UnitTest var const1 = tf.constant(1.0); Assert.AreEqual("scope1/Const:0", const1.name); - with(new ops.name_scope("scope2"), scope2 => + with(new ops.name_scope("scope2"), scope2 => { name = scope2; Assert.AreEqual("scope1/scope2", g._name_stack); diff --git a/test/TensorFlowNET.UnitTest/PlaceholderTest.cs b/test/TensorFlowNET.UnitTest/PlaceholderTest.cs index 3bc6a892..189a9c69 100644 --- a/test/TensorFlowNET.UnitTest/PlaceholderTest.cs +++ b/test/TensorFlowNET.UnitTest/PlaceholderTest.cs @@ -7,7 +7,7 @@ using Tensorflow; namespace TensorFlowNET.UnitTest { [TestClass] - public class PlaceholderTest + public class PlaceholderTest : Python { [TestMethod] public void placeholder() @@ -15,7 +15,7 @@ namespace TensorFlowNET.UnitTest var x = tf.placeholder(tf.int32); var y = x * 3; - Python.with(tf.Session(), sess => + with(tf.Session(), sess => { var result = sess.run(y, new FeedItem(x, 2)); diff --git a/test/TensorFlowNET.UnitTest/SessionTest.cs b/test/TensorFlowNET.UnitTest/SessionTest.cs index e93e4444..37206426 100644 --- a/test/TensorFlowNET.UnitTest/SessionTest.cs +++ b/test/TensorFlowNET.UnitTest/SessionTest.cs @@ -82,7 +82,7 @@ namespace TensorFlowNET.UnitTest var a = constant_op.constant(np.array(3.0).reshape(1, 1)); var b = constant_op.constant(np.array(2.0).reshape(1, 1)); var c = math_ops.matmul(a, b, name: "matmul"); - Python.with(tf.Session(), delegate + with(tf.Session(), delegate { var result = c.eval(); Assert.AreEqual(6, result.Data()[0]); diff --git a/test/TensorFlowNET.UnitTest/TrainSaverTest.cs b/test/TensorFlowNET.UnitTest/TrainSaverTest.cs index f5aec32b..3ea4dfc8 100644 --- a/test/TensorFlowNET.UnitTest/TrainSaverTest.cs +++ b/test/TensorFlowNET.UnitTest/TrainSaverTest.cs @@ -19,7 +19,7 @@ namespace TensorFlowNET.UnitTest public void ImportGraph() { - with(tf.Session(), sess => + with(tf.Session(), sess => { var new_saver = tf.train.import_meta_graph("C:/tmp/my-model.meta"); }); @@ -44,7 +44,7 @@ namespace TensorFlowNET.UnitTest public void ImportSavedModel() { - with(Session.LoadFromSavedModel("mobilenet"), sess => + with(Session.LoadFromSavedModel("mobilenet"), sess => { }); @@ -65,7 +65,7 @@ namespace TensorFlowNET.UnitTest // Add ops to save and restore all the variables. var saver = tf.train.Saver(); - with(tf.Session(), sess => + with(tf.Session(), sess => { sess.run(init_op); diff --git a/test/TensorFlowNET.UnitTest/VariableTest.cs b/test/TensorFlowNET.UnitTest/VariableTest.cs index 26351a84..0964aaa0 100644 --- a/test/TensorFlowNET.UnitTest/VariableTest.cs +++ b/test/TensorFlowNET.UnitTest/VariableTest.cs @@ -32,9 +32,10 @@ namespace TensorFlowNET.UnitTest /// /// https://www.tensorflow.org/api_docs/python/tf/variable_scope + /// how to create a new variable /// [TestMethod] - public void VarCreation1() + public void VarCreation() { with(tf.variable_scope("foo"), delegate { @@ -46,6 +47,12 @@ namespace TensorFlowNET.UnitTest }); } + [TestMethod] + public void ReenterVariableScope() + { + + } + [TestMethod] public void ScalarVar() { @@ -65,7 +72,7 @@ namespace TensorFlowNET.UnitTest [TestMethod] public void Assign1() { - with(tf.Graph().as_default(), graph => + with(tf.Graph().as_default(), graph => { var variable = tf.Variable(31, name: "tree"); var init = tf.global_variables_initializer(); @@ -91,7 +98,7 @@ namespace TensorFlowNET.UnitTest // Add an op to initialize the variables. var init_op = tf.global_variables_initializer(); - with(tf.Session(), sess => + with(tf.Session(), sess => { sess.run(init_op); // o some work with the model.