Skip to content

Commit ebfb327

Browse files
committed
clean code.
1 parent a22e92d commit ebfb327

File tree

13 files changed

+54
-226
lines changed

13 files changed

+54
-226
lines changed

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 7 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -203,49 +203,21 @@ public static IEnumerable<T> reversed<T>(IList<T> values)
203203
yield return values[i];
204204
}
205205

206-
[DebuggerStepThrough]
207-
public static void tf_with(ITensorFlowObject py, Action<ITensorFlowObject> action)
208-
{
209-
try
210-
{
211-
py.__enter__();
212-
action(py);
213-
}
214-
finally
215-
{
216-
py.__exit__();
217-
py.Dispose();
218-
}
219-
}
220-
221206
[DebuggerStepThrough]
222207
public static void tf_with<T>(T py, Action<T> action) where T : ITensorFlowObject
223208
{
224-
try
225-
{
226-
py.__enter__();
227-
action(py);
228-
}
229-
finally
230-
{
231-
py.__exit__();
232-
py.Dispose();
233-
}
209+
py.__enter__();
210+
action(py);
211+
py.__exit__();
234212
}
235213

236214
[DebuggerStepThrough]
237215
public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : ITensorFlowObject
238216
{
239-
try
240-
{
241-
py.__enter__();
242-
return action(py);
243-
}
244-
finally
245-
{
246-
py.__exit__();
247-
py.Dispose();
248-
}
217+
py.__enter__();
218+
var result = action(py);
219+
py.__exit__();
220+
return result;
249221
}
250222

251223
public static float time()

src/TensorFlowNET.Core/Framework/random_seed.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,22 @@ public static (Tensor, Tensor) get_seed_tensor(int? op_seed = null)
6060
var (seed, seed2) = get_seed(op_seed);
6161
Tensor _seed, _seed2;
6262
if (seed is null)
63-
_seed = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed");
63+
_seed = constant_op.constant(0L, name: "seed");
6464
else
65-
_seed = constant_op.constant(seed.Value, dtype: TF_DataType.TF_INT64, name: "seed");
65+
_seed = constant_op.constant((long)seed.Value, name: "seed");
6666

6767
if (seed2 is null)
68-
_seed2 = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed2");
68+
_seed2 = constant_op.constant(0L, name: "seed2");
6969
else
7070
{
7171
_seed2 = tf_with(ops.name_scope("seed2"), scope =>
7272
{
73-
_seed2 = constant_op.constant(seed2.Value, dtype: TF_DataType.TF_INT64);
73+
_seed2 = constant_op.constant((long)seed2.Value);
7474
return array_ops.where_v2(
7575
math_ops.logical_and(
76-
math_ops.equal(_seed, 0l), math_ops.equal(_seed2, 0l)),
77-
constant_op.constant(2^31 - 1, dtype: dtypes.int64),
76+
math_ops.equal(_seed, 0L),
77+
math_ops.equal(_seed2, 0L)),
78+
constant_op.constant(2^31L - 1),
7879
_seed2,
7980
name: scope);
8081
});

src/TensorFlowNET.Core/Graphs/NullContextmanager.cs

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/TensorFlowNET.Core/Sessions/Session.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ limitations under the License.
2121

2222
namespace Tensorflow
2323
{
24-
public class Session : BaseSession, ITensorFlowObject
24+
public class Session : BaseSession
2525
{
2626
public Session(string target = "", Graph g = null) : base(target, g, null)
2727
{ }

src/TensorFlowNET.Core/Tensorflow.Binding.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
<PropertyGroup>
44
<TargetFramework>netstandard2.0</TargetFramework>
5-
<AssemblyName>TensorFlow.NET</AssemblyName>
5+
<AssemblyName>Tensorflow.Binding</AssemblyName>
66
<RootNamespace>Tensorflow</RootNamespace>
77
<TargetTensorFlow>2.2.0</TargetTensorFlow>
88
<Version>0.70.0</Version>

src/TensorFlowNET.Core/ops._DefaultStack.cs

Lines changed: 0 additions & 64 deletions
This file was deleted.

src/TensorFlowNET.Core/ops.name_scope.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -99,15 +99,16 @@ public void __enter__()
9999
[DebuggerStepThrough]
100100
public void Dispose()
101101
{
102-
if (tf.Context.executing_eagerly())
103-
tf.Context.ScopeName = old_scope_name;
104-
else
105-
get_default_graph()._name_stack = old_scope_name;
102+
106103
}
107104

108105
[DebuggerStepThrough]
109106
public void __exit__()
110107
{
108+
if (tf.Context.executing_eagerly())
109+
tf.Context.ScopeName = old_scope_name;
110+
else
111+
get_default_graph()._name_stack = old_scope_name;
111112
}
112113

113114
[DebuggerNonUserCode]

src/TensorFlowNET.Keras/Engine/Layer.Apply.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,18 +33,20 @@ public Tensors Apply(Tensors inputs, Tensor state = null, bool training = false)
3333
else
3434
nameScope = _name_scope();
3535

36-
tf_with(ops.name_scope(nameScope), scope =>
37-
{
38-
if (!built)
39-
MaybeBuild(inputs);
36+
var scope = ops.name_scope(nameScope);
37+
scope.__enter__();
38+
39+
if (!built)
40+
MaybeBuild(inputs);
41+
42+
outputs = Call(inputs, state: state, training: training);
4043

41-
outputs = Call(inputs, state: state, training: training);
44+
// memory leak
45+
// _set_connectivity_metadata_(inputs, outputs);
46+
_handle_activity_regularization(inputs, outputs);
47+
_set_mask_metadata(inputs, outputs, null);
4248

43-
// memory leak
44-
// _set_connectivity_metadata_(inputs, outputs);
45-
_handle_activity_regularization(inputs, outputs);
46-
_set_mask_metadata(inputs, outputs, null);
47-
});
49+
scope.__exit__();
4850

4951
return outputs;
5052
}

src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -26,26 +26,27 @@ Tensors FunctionalConstructionCall(Tensors inputs)
2626
var graph = keras.backend.get_graph();
2727
graph.as_default();
2828

29-
tf_with(ops.name_scope(_name_scope()), scope =>
30-
{
31-
MaybeBuild(inputs);
32-
33-
// Wrapping `call` function in autograph to allow for dynamic control
34-
// flow and control dependencies in call. We are limiting this to
35-
// subclassed layers as autograph is strictly needed only for
36-
// subclassed layers and models.
37-
// tf_convert will respect the value of autograph setting in the
38-
// enclosing tf.function, if any.
39-
if (!dynamic)
40-
throw new NotImplementedException("");
41-
42-
outputs = Call(inputs);
29+
var scope = ops.name_scope(_name_scope());
30+
scope.__enter__();
31+
32+
MaybeBuild(inputs);
33+
34+
// Wrapping `call` function in autograph to allow for dynamic control
35+
// flow and control dependencies in call. We are limiting this to
36+
// subclassed layers as autograph is strictly needed only for
37+
// subclassed layers and models.
38+
// tf_convert will respect the value of autograph setting in the
39+
// enclosing tf.function, if any.
40+
if (!dynamic)
41+
throw new NotImplementedException("");
42+
43+
outputs = Call(inputs);
4344

44-
_set_connectivity_metadata_(inputs, outputs);
45-
_handle_activity_regularization(inputs, outputs);
46-
_set_mask_metadata(inputs, outputs, null);
47-
});
45+
_set_connectivity_metadata_(inputs, outputs);
46+
_handle_activity_regularization(inputs, outputs);
47+
_set_mask_metadata(inputs, outputs, null);
4848

49+
scope.__exit__();
4950
graph.Exit();
5051

5152
return outputs;

src/TensorFlowNET.Keras/Tensorflow.Keras.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ Keras is an API designed for human beings, not machines. Keras follows best prac
6060
</PropertyGroup>
6161

6262
<ItemGroup>
63-
<PackageReference Include="HDF5-CSharp" Version="1.12.4" />
63+
<PackageReference Include="HDF5-CSharp" Version="1.12.5" />
6464
<PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.144" />
6565
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
6666
<PackageReference Include="SharpZipLib" Version="1.3.3" />

0 commit comments

Comments
 (0)