Skip to content

Commit a89c9ab

Browse files
committed
rename with to tf_with, only use to construct graph purpose.
1 parent 40da3d7 commit a89c9ab

87 files changed

Lines changed: 478 additions & 494 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

src/KerasNET.Core/Model.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,7 @@ public float train(int num_steps, (NDArray, NDArray) training_dataset)
115115
var init = tf.global_variables_initializer();
116116

117117
float loss_value = 0;
118-
with(tf.Session(graph), sess =>
118+
using (var sess = tf.Session(graph))
119119
{
120120
sess.run(init);
121121
var step = 0;
@@ -133,7 +133,7 @@ public float train(int num_steps, (NDArray, NDArray) training_dataset)
133133
Console.WriteLine($"Step {step} loss: {loss_value}");
134134
}
135135
Console.WriteLine($"Final loss: {loss_value}");
136-
});
136+
}
137137

138138
return loss_value;
139139
}

src/TensorFlowNET.Core/APIs/tf.nn.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ public static Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string
136136

137137
public static Tensor bias_add(Tensor value, RefVariable bias, string data_format = null, string name = null)
138138
{
139-
return Python.with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
139+
return Python.tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
140140
{
141141
name = scope;
142142
return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name);
@@ -169,7 +169,7 @@ public static Tensor sparse_softmax_cross_entropy_with_logits(Tensor labels = nu
169169
/// <returns></returns>
170170
public static Tensor softmax_cross_entropy_with_logits(Tensor labels, Tensor logits, int dim = -1, string name = null)
171171
{
172-
with(ops.name_scope(name, "softmax_cross_entropy_with_logits_sg", new { logits, labels }), scope =>
172+
tf_with(ops.name_scope(name, "softmax_cross_entropy_with_logits_sg", new { logits, labels }), scope =>
173173
{
174174
name = scope;
175175
labels = array_ops.stop_gradient(labels, name: "labels_stop_gradient");

src/TensorFlowNET.Core/Buffers/Buffer.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ public static implicit operator byte[](Buffer buffer)
6666
return buffer.Data;
6767
}
6868

69-
protected override void DisposeUnManagedState()
70-
=> c_api.TF_DeleteBuffer(_handle);
69+
protected override void DisposeUnManagedState(IntPtr handle)
70+
=> c_api.TF_DeleteBuffer(handle);
7171
}
7272
}

src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public _InitializeClustersOpFactory(Tensor[] inputs,
6868

6969
private Tensor _initialize()
7070
{
71-
return with(ops.control_dependencies(new Operation[]
71+
return tf_with(ops.control_dependencies(new Operation[]
7272
{
7373
check_ops.assert_positive(_num_remaining)
7474
}), delegate

src/TensorFlowNET.Core/DisposableObject.cs

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -34,38 +34,27 @@ public DisposableObject(IntPtr handle)
3434
_handle = handle;
3535
}
3636

37-
private bool disposedValue = false; // To detect redundant calls
38-
3937
protected virtual void DisposeManagedState()
4038
{
4139
}
4240

43-
protected abstract void DisposeUnManagedState();
41+
protected abstract void DisposeUnManagedState(IntPtr handle);
4442

4543
protected virtual void Dispose(bool disposing)
4644
{
47-
if (!disposedValue)
45+
if (disposing)
4846
{
49-
if (disposing)
47+
// free unmanaged resources (unmanaged objects) and override a finalizer below.
48+
if (_handle != IntPtr.Zero)
5049
{
5150
// dispose managed state (managed objects).
5251
DisposeManagedState();
53-
}
5452

55-
// free unmanaged resources (unmanaged objects) and override a finalizer below.
56-
/*IntPtr h = IntPtr.Zero;
57-
lock (this)
58-
{
59-
h = _handle;
60-
_handle = IntPtr.Zero;
61-
}*/
62-
if (_handle != IntPtr.Zero)
63-
DisposeUnManagedState();
64-
65-
// set large fields to null.
66-
_handle = IntPtr.Zero;
53+
// set large fields to null.
54+
DisposeUnManagedState(_handle);
6755

68-
disposedValue = true;
56+
_handle = IntPtr.Zero;
57+
}
6958
}
7059
}
7160

src/TensorFlowNET.Core/Framework/importer.py.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static ITensorOrOperation[] import_graph_def(GraphDef graph_def,
4242

4343
string prefix = "";
4444
var graph = ops.get_default_graph();
45-
with(ops.name_scope(name, "import", input_map.Values), scope =>
45+
tf_with(ops.name_scope(name, "import", input_map.Values), scope =>
4646
{
4747
prefix = scope;
4848
/*if (!string.IsNullOrEmpty(prefix))

src/TensorFlowNET.Core/Gradients/gradients_util.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
5555
**/
5656
var grads = new Dictionary<string, List<List<Tensor>>>();
5757

58-
with(ops.name_scope(name, "gradients",
58+
tf_with(ops.name_scope(name, "gradients",
5959
values: ys.Concat(xs).Concat(stop_gradients).Concat(grad_ys)), scope =>
6060
{
6161
string grad_scope = scope;
@@ -141,7 +141,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
141141
}
142142
}
143143

144-
with(ops.name_scope(op.name + "_grad"), scope1 =>
144+
tf_with(ops.name_scope(op.name + "_grad"), scope1 =>
145145
{
146146
string name1 = scope1;
147147
if (grad_fn != null)

src/TensorFlowNET.Core/Gradients/math_grad.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public static Tensor[] _ExpGrad(Operation op, Tensor[] grads)
9090
{
9191
var grad = grads[0];
9292
var y = op.outputs[0]; // y = e^x
93-
return with(ops.control_dependencies(new Operation[] { grad }), dp => {
93+
return tf_with(ops.control_dependencies(new Operation[] { grad }), dp => {
9494
y = math_ops.conj(y);
9595
return new Tensor[] { math_ops.mul_no_nan(y, grad) };
9696
});
@@ -107,7 +107,7 @@ public static Tensor[] _LgammaGrad(Operation op, Tensor[] grads)
107107
{
108108
var grad = grads[0];
109109
var x = op.inputs[0];
110-
return with(ops.control_dependencies(new Operation[] { grad }), dp => {
110+
return tf_with(ops.control_dependencies(new Operation[] { grad }), dp => {
111111
x = math_ops.conj(x);
112112
return new Tensor[] { grad * math_ops.digamma(x) };
113113
});
@@ -118,7 +118,7 @@ public static Tensor[] _LogGrad(Operation op, Tensor[] grads)
118118
{
119119
var grad = grads[0];
120120
var x = op.inputs[0];
121-
return with(ops.control_dependencies(new Operation[] { grad }), dp => {
121+
return tf_with(ops.control_dependencies(new Operation[] { grad }), dp => {
122122
x = math_ops.conj(x);
123123
return new Tensor[] { grad * math_ops.reciprocal(x) };
124124
});
@@ -431,7 +431,7 @@ public static Tensor[] _SigmoidGrad(Operation op, Tensor[] grads)
431431
var grad = grads[0];
432432
var y = op.outputs[0];
433433

434-
return with(ops.control_dependencies(grads), delegate
434+
return tf_with(ops.control_dependencies(grads), delegate
435435
{
436436
y = math_ops.conj(y);
437437
return new Tensor[] { gen_math_ops.sigmoid_grad(y, grad) };
@@ -453,7 +453,7 @@ public static Tensor[] _SquareGrad(Operation op, Tensor[] grads)
453453
var grad = grads[0];
454454
var x = op.inputs[0];
455455

456-
return with(ops.control_dependencies(grads), delegate
456+
return tf_with(ops.control_dependencies(grads), delegate
457457
{
458458
x = math_ops.conj(x);
459459
var y = constant_op.constant(2.0f, dtype: x.dtype);
@@ -467,7 +467,7 @@ public static Tensor[] _TanhGrad(Operation op, Tensor[] grads)
467467
var grad = grads[0];
468468
var y = op.outputs[0];
469469

470-
return with(ops.control_dependencies(grads), delegate
470+
return tf_with(ops.control_dependencies(grads), delegate
471471
{
472472
y = math_ops.conj(y);
473473
return new Tensor[] { gen_math_ops.tanh_grad(y, grad) };

src/TensorFlowNET.Core/Keras/Layers/BatchNormalization.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ private Tensor _fused_batch_norm(Tensor inputs, Tensor training)
207207

208208
public Tensor _assign_moving_average(RefVariable variable, Tensor value, Tensor momentum)
209209
{
210-
return Python.with(ops.name_scope(null, "AssignMovingAvg", new { variable, value, momentum }), scope =>
210+
return Python.tf_with(ops.name_scope(null, "AssignMovingAvg", new { variable, value, momentum }), scope =>
211211
{
212212
// var cm = ops.colocate_with(variable);
213213
var decay = ops.convert_to_tensor(1.0f - momentum, name: "decay");

src/TensorFlowNET.Core/Keras/Layers/Layer.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ public Tensor __call__(Tensor[] inputs,
125125
// Symbolic execution on symbolic tensors. We will attempt to build
126126
// the corresponding TF subgraph inside `backend.get_graph()`
127127
var graph = backend.get_graph().as_default();
128-
with(ops.name_scope(_name_scope()), delegate
128+
tf_with(ops.name_scope(_name_scope()), delegate
129129
{
130130
// Build layer if applicable (if the `build` method has been
131131
// overridden).

0 commit comments

Comments
 (0)