diff --git a/src/KerasNET.Core/Model.cs b/src/KerasNET.Core/Model.cs
index b1e6de57..d1d05159 100644
--- a/src/KerasNET.Core/Model.cs
+++ b/src/KerasNET.Core/Model.cs
@@ -115,7 +115,7 @@ namespace Keras
var init = tf.global_variables_initializer();
float loss_value = 0;
- with(tf.Session(graph), sess =>
+ using (var sess = tf.Session(graph))
{
sess.run(init);
var step = 0;
@@ -133,7 +133,7 @@ namespace Keras
Console.WriteLine($"Step {step} loss: {loss_value}");
}
Console.WriteLine($"Final loss: {loss_value}");
- });
+ }
return loss_value;
}
diff --git a/src/TensorFlowNET.Core/APIs/tf.nn.cs b/src/TensorFlowNET.Core/APIs/tf.nn.cs
index 0bc9d0e5..67efe726 100644
--- a/src/TensorFlowNET.Core/APIs/tf.nn.cs
+++ b/src/TensorFlowNET.Core/APIs/tf.nn.cs
@@ -136,7 +136,7 @@ namespace Tensorflow
public static Tensor bias_add(Tensor value, RefVariable bias, string data_format = null, string name = null)
{
- return Python.with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
+ return Python.tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
{
name = scope;
return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name);
@@ -169,7 +169,7 @@ namespace Tensorflow
///
public static Tensor softmax_cross_entropy_with_logits(Tensor labels, Tensor logits, int dim = -1, string name = null)
{
- with(ops.name_scope(name, "softmax_cross_entropy_with_logits_sg", new { logits, labels }), scope =>
+ tf_with(ops.name_scope(name, "softmax_cross_entropy_with_logits_sg", new { logits, labels }), scope =>
{
name = scope;
labels = array_ops.stop_gradient(labels, name: "labels_stop_gradient");
diff --git a/src/TensorFlowNET.Core/Buffers/Buffer.cs b/src/TensorFlowNET.Core/Buffers/Buffer.cs
index 753ad508..dbe576b8 100644
--- a/src/TensorFlowNET.Core/Buffers/Buffer.cs
+++ b/src/TensorFlowNET.Core/Buffers/Buffer.cs
@@ -66,7 +66,7 @@ namespace Tensorflow
return buffer.Data;
}
- protected override void DisposeUnManagedState()
- => c_api.TF_DeleteBuffer(_handle);
+ protected override void DisposeUnManagedState(IntPtr handle)
+ => c_api.TF_DeleteBuffer(handle);
}
}
diff --git a/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs b/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs
index a6253520..8112708f 100644
--- a/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs
+++ b/src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs
@@ -68,7 +68,7 @@ namespace Tensorflow.Clustering
private Tensor _initialize()
{
- return with(ops.control_dependencies(new Operation[]
+ return tf_with(ops.control_dependencies(new Operation[]
{
check_ops.assert_positive(_num_remaining)
}), delegate
diff --git a/src/TensorFlowNET.Core/DisposableObject.cs b/src/TensorFlowNET.Core/DisposableObject.cs
index b59e6aa0..7e416e6d 100644
--- a/src/TensorFlowNET.Core/DisposableObject.cs
+++ b/src/TensorFlowNET.Core/DisposableObject.cs
@@ -34,38 +34,27 @@ namespace Tensorflow
_handle = handle;
}
- private bool disposedValue = false; // To detect redundant calls
-
protected virtual void DisposeManagedState()
{
}
- protected abstract void DisposeUnManagedState();
+ protected abstract void DisposeUnManagedState(IntPtr handle);
protected virtual void Dispose(bool disposing)
{
- if (!disposedValue)
+ if (disposing)
{
- if (disposing)
+ // free unmanaged resources (unmanaged objects) and override a finalizer below.
+ if (_handle != IntPtr.Zero)
{
// dispose managed state (managed objects).
DisposeManagedState();
- }
- // free unmanaged resources (unmanaged objects) and override a finalizer below.
- /*IntPtr h = IntPtr.Zero;
- lock (this)
- {
- h = _handle;
- _handle = IntPtr.Zero;
- }*/
- if (_handle != IntPtr.Zero)
- DisposeUnManagedState();
-
- // set large fields to null.
- _handle = IntPtr.Zero;
+ // set large fields to null.
+ DisposeUnManagedState(_handle);
- disposedValue = true;
+ _handle = IntPtr.Zero;
+ }
}
}
diff --git a/src/TensorFlowNET.Core/Framework/importer.py.cs b/src/TensorFlowNET.Core/Framework/importer.py.cs
index 577d41aa..0c405be9 100644
--- a/src/TensorFlowNET.Core/Framework/importer.py.cs
+++ b/src/TensorFlowNET.Core/Framework/importer.py.cs
@@ -42,7 +42,7 @@ namespace Tensorflow
string prefix = "";
var graph = ops.get_default_graph();
- with(ops.name_scope(name, "import", input_map.Values), scope =>
+ tf_with(ops.name_scope(name, "import", input_map.Values), scope =>
{
prefix = scope;
/*if (!string.IsNullOrEmpty(prefix))
diff --git a/src/TensorFlowNET.Core/Gradients/gradients_util.cs b/src/TensorFlowNET.Core/Gradients/gradients_util.cs
index 95f083da..43247fa4 100644
--- a/src/TensorFlowNET.Core/Gradients/gradients_util.cs
+++ b/src/TensorFlowNET.Core/Gradients/gradients_util.cs
@@ -55,7 +55,7 @@ namespace Tensorflow
**/
var grads = new Dictionary>>();
- with(ops.name_scope(name, "gradients",
+ tf_with(ops.name_scope(name, "gradients",
values: ys.Concat(xs).Concat(stop_gradients).Concat(grad_ys)), scope =>
{
string grad_scope = scope;
@@ -141,7 +141,7 @@ namespace Tensorflow
}
}
- with(ops.name_scope(op.name + "_grad"), scope1 =>
+ tf_with(ops.name_scope(op.name + "_grad"), scope1 =>
{
string name1 = scope1;
if (grad_fn != null)
diff --git a/src/TensorFlowNET.Core/Gradients/math_grad.cs b/src/TensorFlowNET.Core/Gradients/math_grad.cs
index a84185f3..a5ac79ba 100644
--- a/src/TensorFlowNET.Core/Gradients/math_grad.cs
+++ b/src/TensorFlowNET.Core/Gradients/math_grad.cs
@@ -90,7 +90,7 @@ namespace Tensorflow.Gradients
{
var grad = grads[0];
var y = op.outputs[0]; // y = e^x
- return with(ops.control_dependencies(new Operation[] { grad }), dp => {
+ return tf_with(ops.control_dependencies(new Operation[] { grad }), dp => {
y = math_ops.conj(y);
return new Tensor[] { math_ops.mul_no_nan(y, grad) };
});
@@ -107,7 +107,7 @@ namespace Tensorflow.Gradients
{
var grad = grads[0];
var x = op.inputs[0];
- return with(ops.control_dependencies(new Operation[] { grad }), dp => {
+ return tf_with(ops.control_dependencies(new Operation[] { grad }), dp => {
x = math_ops.conj(x);
return new Tensor[] { grad * math_ops.digamma(x) };
});
@@ -118,7 +118,7 @@ namespace Tensorflow.Gradients
{
var grad = grads[0];
var x = op.inputs[0];
- return with(ops.control_dependencies(new Operation[] { grad }), dp => {
+ return tf_with(ops.control_dependencies(new Operation[] { grad }), dp => {
x = math_ops.conj(x);
return new Tensor[] { grad * math_ops.reciprocal(x) };
});
@@ -431,7 +431,7 @@ namespace Tensorflow.Gradients
var grad = grads[0];
var y = op.outputs[0];
- return with(ops.control_dependencies(grads), delegate
+ return tf_with(ops.control_dependencies(grads), delegate
{
y = math_ops.conj(y);
return new Tensor[] { gen_math_ops.sigmoid_grad(y, grad) };
@@ -453,7 +453,7 @@ namespace Tensorflow.Gradients
var grad = grads[0];
var x = op.inputs[0];
- return with(ops.control_dependencies(grads), delegate
+ return tf_with(ops.control_dependencies(grads), delegate
{
x = math_ops.conj(x);
var y = constant_op.constant(2.0f, dtype: x.dtype);
@@ -467,7 +467,7 @@ namespace Tensorflow.Gradients
var grad = grads[0];
var y = op.outputs[0];
- return with(ops.control_dependencies(grads), delegate
+ return tf_with(ops.control_dependencies(grads), delegate
{
y = math_ops.conj(y);
return new Tensor[] { gen_math_ops.tanh_grad(y, grad) };
diff --git a/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs b/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs
index 0dbe5eb3..156cc8c9 100644
--- a/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs
+++ b/src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs
@@ -207,7 +207,7 @@ namespace Tensorflow.Keras.Layers
public Tensor _assign_moving_average(RefVariable variable, Tensor value, Tensor momentum)
{
- return Python.with(ops.name_scope(null, "AssignMovingAvg", new { variable, value, momentum }), scope =>
+ return Python.tf_with(ops.name_scope(null, "AssignMovingAvg", new { variable, value, momentum }), scope =>
{
// var cm = ops.colocate_with(variable);
var decay = ops.convert_to_tensor(1.0f - momentum, name: "decay");
diff --git a/src/TensorFlowNET.Core/Keras/Layers/Layer.cs b/src/TensorFlowNET.Core/Keras/Layers/Layer.cs
index f380ce78..d96c1f14 100644
--- a/src/TensorFlowNET.Core/Keras/Layers/Layer.cs
+++ b/src/TensorFlowNET.Core/Keras/Layers/Layer.cs
@@ -125,7 +125,7 @@ namespace Tensorflow.Keras.Layers
// Symbolic execution on symbolic tensors. We will attempt to build
// the corresponding TF subgraph inside `backend.get_graph()`
var graph = backend.get_graph().as_default();
- with(ops.name_scope(_name_scope()), delegate
+ tf_with(ops.name_scope(_name_scope()), delegate
{
// Build layer if applicable (if the `build` method has been
// overridden).
diff --git a/src/TensorFlowNET.Core/Layers/Layer.cs b/src/TensorFlowNET.Core/Layers/Layer.cs
index 57c71e92..961952a6 100644
--- a/src/TensorFlowNET.Core/Layers/Layer.cs
+++ b/src/TensorFlowNET.Core/Layers/Layer.cs
@@ -72,7 +72,7 @@ namespace Tensorflow.Layers
}
Tensor outputs = null;
- with(scope_context_manager, scope2 =>
+ tf_with(scope_context_manager, scope2 =>
{
_current_scope = scope2;
// Actually call layer
@@ -136,12 +136,12 @@ namespace Tensorflow.Layers
_set_scope();
var reuse = built || (_reuse != null && _reuse.Value);
- return with(tf.variable_scope(_scope,
+ return tf_with(tf.variable_scope(_scope,
reuse: reuse,
auxiliary_name_scope: false), scope =>
{
_current_scope = scope;
- return with(ops.name_scope(_name_scope()), delegate
+ return tf_with(ops.name_scope(_name_scope()), delegate
{
var variable = base.add_weight(name,
shape,
@@ -183,7 +183,7 @@ namespace Tensorflow.Layers
}
else
{
- with(tf.variable_scope(scope, default_name: _base_name), captured_scope =>
+ tf_with(tf.variable_scope(scope, default_name: _base_name), captured_scope =>
{
// convert variable_scope to VariableScope
_scope = captured_scope;
diff --git a/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs b/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs
index c6b7d24d..136c9e3b 100644
--- a/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs
+++ b/src/TensorFlowNET.Core/Operations/ControlFlows/CondContext.cs
@@ -122,7 +122,7 @@ namespace Tensorflow.Operations
_external_values[result.name] = result;
}
- with(ops.control_dependencies(null), ctrl =>
+ tf_with(ops.control_dependencies(null), ctrl =>
{
var results = control_flow_ops._SwitchRefOrTensor(result, _pred);
result = results[_branch];
diff --git a/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs b/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs
index 19a1266b..69affeea 100644
--- a/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs
+++ b/src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs
@@ -58,7 +58,7 @@ namespace Tensorflow
private Tensor _call_log_prob (Tensor value, string name)
{
- return with(ops.name_scope(name, "moments", new { value }), scope =>
+ return tf_with(ops.name_scope(name, "moments", new { value }), scope =>
{
try
{
diff --git a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs
index 2aa15063..f4f4b4bf 100644
--- a/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs
+++ b/src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs
@@ -50,9 +50,9 @@ namespace Tensorflow
parameters.Add("validate_args", validate_args);
parameters.Add("allow_nan_stats", allow_nan_stats);
- with(ops.name_scope(name, "", new { loc, scale }), scope =>
+ tf_with(ops.name_scope(name, "", new { loc, scale }), scope =>
{
- with(ops.control_dependencies(validate_args ? new Operation[] { scale.op} : new Operation[] { }), cd =>
+ tf_with(ops.control_dependencies(validate_args ? new Operation[] { scale.op} : new Operation[] { }), cd =>
{
this._loc = array_ops.identity(loc, name);
this._scale = array_ops.identity(scale, name);
diff --git a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs
index 5334b378..c5a6ad22 100644
--- a/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs
+++ b/src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs
@@ -24,7 +24,7 @@ namespace Tensorflow
public Tensor compute_weighted_loss(Tensor losses, Tensor weights = null, string scope = null,
string loss_collection = ops.GraphKeys.LOSSES, string reduction = Reduction.SUM_BY_NONZERO_WEIGHTS)
{
- return with(ops.name_scope(scope, default_name: "weighted_loss", (losses, weights)), delegate
+ return tf_with(ops.name_scope(scope, default_name: "weighted_loss", (losses, weights)), delegate
{
// Save the `reduction` argument for loss normalization when distributing
// to multiple replicas. Used only for estimator + v1 optimizer flow.
@@ -77,7 +77,7 @@ namespace Tensorflow
public Tensor _num_present(Tensor losses, Tensor weights, bool per_batch = false)
{
- return with(ops.name_scope(null, default_name: "num_present", (losses, weights)), name_scope =>
+ return tf_with(ops.name_scope(null, default_name: "num_present", (losses, weights)), name_scope =>
{
string scope = name_scope;
weights = math_ops.cast(weights, dtype: dtypes.float32);
@@ -104,7 +104,7 @@ namespace Tensorflow
string loss_collection= ops.GraphKeys.LOSSES,
string reduction = Reduction.SUM_BY_NONZERO_WEIGHTS)
{
- return with(ops.name_scope(scope,
+ return tf_with(ops.name_scope(scope,
"sparse_softmax_cross_entropy_loss",
(logits, labels, weights)),
name_scope =>
diff --git a/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs b/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs
index 9d1e5726..b385f9c8 100644
--- a/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs
+++ b/src/TensorFlowNET.Core/Operations/NnOps/MaxPoolFunction.cs
@@ -30,7 +30,7 @@ namespace Tensorflow.Operations
string data_format = "NHWC",
string name = null)
{
- return with(ops.name_scope(name, "MaxPool", value), scope =>
+ return tf_with(ops.name_scope(name, "MaxPool", value), scope =>
{
name = scope;
value = ops.convert_to_tensor(value, name: "input");
diff --git a/src/TensorFlowNET.Core/Operations/NnOps/rnn.cs b/src/TensorFlowNET.Core/Operations/NnOps/rnn.cs
index 7d08a3c6..4a9e5de4 100644
--- a/src/TensorFlowNET.Core/Operations/NnOps/rnn.cs
+++ b/src/TensorFlowNET.Core/Operations/NnOps/rnn.cs
@@ -30,7 +30,7 @@ namespace Tensorflow.Operations
TF_DataType dtype = TF_DataType.DtInvalid,
int? parallel_iterations = null, bool swap_memory = false, bool time_major = false)
{
- with(tf.variable_scope("rnn"), scope =>
+ tf_with(tf.variable_scope("rnn"), scope =>
{
VariableScope varscope = scope;
var flat_input = nest.flatten(inputs_tensor);
@@ -140,7 +140,7 @@ namespace Tensorflow.Operations
var time = array_ops.constant(0, dtype: dtypes.int32, name: "time");
string base_name = null;
- with(ops.name_scope("dynamic_rnn"), scope => base_name = scope);
+ tf_with(ops.name_scope("dynamic_rnn"), scope => base_name = scope);
Func _create_ta = (name, element_shape, dtype_) =>
{
diff --git a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs
index 033731b0..ab34a320 100644
--- a/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs
+++ b/src/TensorFlowNET.Core/Operations/OpDefLibrary.cs
@@ -58,7 +58,7 @@ namespace Tensorflow
var input_types = new List();
object values = null;
- return with(ops.name_scope(name), scope =>
+ return tf_with(ops.name_scope(name), scope =>
{
var inferred_from = new Dictionary();
var base_types = new List();
diff --git a/src/TensorFlowNET.Core/Operations/RNNCell.cs b/src/TensorFlowNET.Core/Operations/RNNCell.cs
index 57f46e7b..1b260981 100644
--- a/src/TensorFlowNET.Core/Operations/RNNCell.cs
+++ b/src/TensorFlowNET.Core/Operations/RNNCell.cs
@@ -82,7 +82,7 @@ namespace Tensorflow
{
Tensor output = null;
var state_size = this.state_size;
- with(ops.name_scope($"{this.GetType().Name}ZeroState", values: new { batch_size }), delegate
+ tf_with(ops.name_scope($"{this.GetType().Name}ZeroState", values: new { batch_size }), delegate
{
output = _zero_state_tensors(state_size, batch_size, dtype);
});
diff --git a/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs b/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs
index b4619c05..bbeee929 100644
--- a/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs
+++ b/src/TensorFlowNET.Core/Operations/_GraphTensorArray.cs
@@ -66,7 +66,7 @@ namespace Tensorflow.Operations
_element_shape = new List { };
}
- with(ops.name_scope(name, "", new { handle, size, flow }), scope =>
+ tf_with(ops.name_scope(name, "", new { handle, size, flow }), scope =>
{
if(handle != null)
{
diff --git a/src/TensorFlowNET.Core/Operations/array_ops.py.cs b/src/TensorFlowNET.Core/Operations/array_ops.py.cs
index f04675f6..f563a61f 100644
--- a/src/TensorFlowNET.Core/Operations/array_ops.py.cs
+++ b/src/TensorFlowNET.Core/Operations/array_ops.py.cs
@@ -43,7 +43,7 @@ namespace Tensorflow
public static Tensor zeros(TensorShape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null)
{
dtype = dtype.as_base_dtype();
- return with(ops.name_scope(name, "zeros", shape), scope =>
+ return tf_with(ops.name_scope(name, "zeros", shape), scope =>
{
name = scope;
switch (dtype)
@@ -67,7 +67,7 @@ namespace Tensorflow
public static Tensor zeros(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null)
{
dtype = dtype.as_base_dtype();
- return with(ops.name_scope(name, "zeros", shape), scope =>
+ return tf_with(ops.name_scope(name, "zeros", shape), scope =>
{
name = scope;
switch (dtype)
@@ -140,7 +140,7 @@ namespace Tensorflow
{
var must_pack = false;
var converted_elems = new List