Browse Source

clean code.

tags/TimeSeries
Oceania2018 3 years ago
parent
commit
ebfb3275cf
13 changed files with 54 additions and 226 deletions
  1. +7
    -35
      src/TensorFlowNET.Core/Binding.Util.cs
  2. +7
    -6
      src/TensorFlowNET.Core/Framework/random_seed.cs
  3. +0
    -32
      src/TensorFlowNET.Core/Graphs/NullContextmanager.cs
  4. +1
    -1
      src/TensorFlowNET.Core/Sessions/Session.cs
  5. +1
    -1
      src/TensorFlowNET.Core/Tensorflow.Binding.csproj
  6. +0
    -64
      src/TensorFlowNET.Core/ops._DefaultStack.cs
  7. +5
    -4
      src/TensorFlowNET.Core/ops.name_scope.cs
  8. +12
    -10
      src/TensorFlowNET.Keras/Engine/Layer.Apply.cs
  9. +19
    -18
      src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs
  10. +1
    -1
      src/TensorFlowNET.Keras/Tensorflow.Keras.csproj
  11. +0
    -25
      test/TensorFlowNET.Graph.UnitTest/PythonTest.cs
  12. +1
    -4
      test/TensorFlowNET.UnitTest/Basics/TrainSaverTest.cs
  13. +0
    -25
      test/TensorFlowNET.UnitTest/PythonTest.cs

+ 7
- 35
src/TensorFlowNET.Core/Binding.Util.cs View File

@@ -203,49 +203,21 @@ namespace Tensorflow
yield return values[i];
}

[DebuggerStepThrough]
public static void tf_with(ITensorFlowObject py, Action<ITensorFlowObject> action)
{
try
{
py.__enter__();
action(py);
}
finally
{
py.__exit__();
py.Dispose();
}
}

[DebuggerStepThrough]
public static void tf_with<T>(T py, Action<T> action) where T : ITensorFlowObject
{
try
{
py.__enter__();
action(py);
}
finally
{
py.__exit__();
py.Dispose();
}
py.__enter__();
action(py);
py.__exit__();
}

[DebuggerStepThrough]
public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : ITensorFlowObject
{
try
{
py.__enter__();
return action(py);
}
finally
{
py.__exit__();
py.Dispose();
}
py.__enter__();
var result = action(py);
py.__exit__();
return result;
}

public static float time()


+ 7
- 6
src/TensorFlowNET.Core/Framework/random_seed.cs View File

@@ -60,21 +60,22 @@ namespace Tensorflow
var (seed, seed2) = get_seed(op_seed);
Tensor _seed, _seed2;
if (seed is null)
_seed = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed");
_seed = constant_op.constant(0L, name: "seed");
else
_seed = constant_op.constant(seed.Value, dtype: TF_DataType.TF_INT64, name: "seed");
_seed = constant_op.constant((long)seed.Value, name: "seed");

if (seed2 is null)
_seed2 = constant_op.constant(0, dtype: TF_DataType.TF_INT64, name: "seed2");
_seed2 = constant_op.constant(0L, name: "seed2");
else
{
_seed2 = tf_with(ops.name_scope("seed2"), scope =>
{
_seed2 = constant_op.constant(seed2.Value, dtype: TF_DataType.TF_INT64);
_seed2 = constant_op.constant((long)seed2.Value);
return array_ops.where_v2(
math_ops.logical_and(
math_ops.equal(_seed, 0l), math_ops.equal(_seed2, 0l)),
constant_op.constant(2^31 - 1, dtype: dtypes.int64),
math_ops.equal(_seed, 0L),
math_ops.equal(_seed2, 0L)),
constant_op.constant(2^31L - 1),
_seed2,
name: scope);
});


+ 0
- 32
src/TensorFlowNET.Core/Graphs/NullContextmanager.cs View File

@@ -1,32 +0,0 @@
using System;

namespace Tensorflow
{
public class NullContextmanager : ITensorFlowObject
{
public void __init__()
{
throw new NotImplementedException();
}

public void __enter__()
{
throw new NotImplementedException();
}

public void __del__()
{
throw new NotImplementedException();
}

public void __exit__()
{
throw new NotImplementedException();
}

public void Dispose()
{
throw new NotImplementedException();
}
}
}

+ 1
- 1
src/TensorFlowNET.Core/Sessions/Session.cs View File

@@ -21,7 +21,7 @@ using Tensorflow.Util;

namespace Tensorflow
{
public class Session : BaseSession, ITensorFlowObject
public class Session : BaseSession
{
public Session(string target = "", Graph g = null) : base(target, g, null)
{ }


+ 1
- 1
src/TensorFlowNET.Core/Tensorflow.Binding.csproj View File

@@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>netstandard2.0</TargetFramework>
<AssemblyName>TensorFlow.NET</AssemblyName>
<AssemblyName>Tensorflow.Binding</AssemblyName>
<RootNamespace>Tensorflow</RootNamespace>
<TargetTensorFlow>2.2.0</TargetTensorFlow>
<Version>0.70.0</Version>


+ 0
- 64
src/TensorFlowNET.Core/ops._DefaultStack.cs View File

@@ -1,64 +0,0 @@
/*****************************************************************************
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
******************************************************************************/

using System;
using System.Collections.Generic;

namespace Tensorflow
{
public partial class ops
{
_DefaultStack _default_session_stack = new _DefaultStack();

public class _DefaultStack : ITensorFlowObject
{
Stack<object> stack;
#pragma warning disable CS0414 // The field 'ops._DefaultStack._enforce_nesting' is assigned but its value is never used
bool _enforce_nesting = true;
#pragma warning restore CS0414 // The field 'ops._DefaultStack._enforce_nesting' is assigned but its value is never used

public _DefaultStack()
{
stack = new Stack<object>();
}

public void __enter__()
{

}

public void __exit__()
{

}

public void Dispose()
{
throw new NotImplementedException();
}

public void __init__()
{

}

public void __del__()
{

}
}
}
}

+ 5
- 4
src/TensorFlowNET.Core/ops.name_scope.cs View File

@@ -99,15 +99,16 @@ namespace Tensorflow
[DebuggerStepThrough]
public void Dispose()
{
if (tf.Context.executing_eagerly())
tf.Context.ScopeName = old_scope_name;
else
get_default_graph()._name_stack = old_scope_name;

}

[DebuggerStepThrough]
public void __exit__()
{
if (tf.Context.executing_eagerly())
tf.Context.ScopeName = old_scope_name;
else
get_default_graph()._name_stack = old_scope_name;
}

[DebuggerNonUserCode]


+ 12
- 10
src/TensorFlowNET.Keras/Engine/Layer.Apply.cs View File

@@ -33,18 +33,20 @@ namespace Tensorflow.Keras.Engine
else
nameScope = _name_scope();

tf_with(ops.name_scope(nameScope), scope =>
{
if (!built)
MaybeBuild(inputs);
var scope = ops.name_scope(nameScope);
scope.__enter__();

if (!built)
MaybeBuild(inputs);

outputs = Call(inputs, state: state, training: training);

outputs = Call(inputs, state: state, training: training);
// memory leak
// _set_connectivity_metadata_(inputs, outputs);
_handle_activity_regularization(inputs, outputs);
_set_mask_metadata(inputs, outputs, null);

// memory leak
// _set_connectivity_metadata_(inputs, outputs);
_handle_activity_regularization(inputs, outputs);
_set_mask_metadata(inputs, outputs, null);
});
scope.__exit__();

return outputs;
}


+ 19
- 18
src/TensorFlowNET.Keras/Engine/Layer.FunctionalConstructionCall.cs View File

@@ -26,26 +26,27 @@ namespace Tensorflow.Keras.Engine
var graph = keras.backend.get_graph();
graph.as_default();

tf_with(ops.name_scope(_name_scope()), scope =>
{
MaybeBuild(inputs);

// Wrapping `call` function in autograph to allow for dynamic control
// flow and control dependencies in call. We are limiting this to
// subclassed layers as autograph is strictly needed only for
// subclassed layers and models.
// tf_convert will respect the value of autograph setting in the
// enclosing tf.function, if any.
if (!dynamic)
throw new NotImplementedException("");

outputs = Call(inputs);
var scope = ops.name_scope(_name_scope());
scope.__enter__();

MaybeBuild(inputs);

// Wrapping `call` function in autograph to allow for dynamic control
// flow and control dependencies in call. We are limiting this to
// subclassed layers as autograph is strictly needed only for
// subclassed layers and models.
// tf_convert will respect the value of autograph setting in the
// enclosing tf.function, if any.
if (!dynamic)
throw new NotImplementedException("");

outputs = Call(inputs);
_set_connectivity_metadata_(inputs, outputs);
_handle_activity_regularization(inputs, outputs);
_set_mask_metadata(inputs, outputs, null);
});
_set_connectivity_metadata_(inputs, outputs);
_handle_activity_regularization(inputs, outputs);
_set_mask_metadata(inputs, outputs, null);

scope.__exit__();
graph.Exit();

return outputs;


+ 1
- 1
src/TensorFlowNET.Keras/Tensorflow.Keras.csproj View File

@@ -60,7 +60,7 @@ Keras is an API designed for human beings, not machines. Keras follows best prac
</PropertyGroup>

<ItemGroup>
<PackageReference Include="HDF5-CSharp" Version="1.12.4" />
<PackageReference Include="HDF5-CSharp" Version="1.12.5" />
<PackageReference Include="MethodBoundaryAspect.Fody" Version="2.0.144" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.1" />
<PackageReference Include="SharpZipLib" Version="1.3.3" />


+ 0
- 25
test/TensorFlowNET.Graph.UnitTest/PythonTest.cs View File

@@ -252,35 +252,10 @@ namespace TensorFlowNET.UnitTest
//else
//{
s = self._create_session(graph, config, force_gpu);
self._constrain_devices_and_set_default(s, use_gpu, force_gpu);
//}
return s.as_default();
}

private ITensorFlowObject _constrain_devices_and_set_default(Session sess, bool useGpu, bool forceGpu)
{
//def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu):
//"""Set the session and its graph to global default and constrain devices."""
//if context.executing_eagerly():
// yield None
//else:
// with sess.graph.as_default(), sess.as_default():
// if force_gpu:
// # Use the name of an actual device if one is detected, or
// # '/device:GPU:0' otherwise
// gpu_name = gpu_device_name()
// if not gpu_name:
// gpu_name = "/device:GPU:0"
// with sess.graph.device(gpu_name):
// yield sess
// elif use_gpu:
// yield sess
// else:
// with sess.graph.device("/device:CPU:0"):
// yield sess
return sess;
}

// See session() for details.
private Session _create_session(Graph graph, object cfg, bool forceGpu)
{


+ 1
- 4
test/TensorFlowNET.UnitTest/Basics/TrainSaverTest.cs View File

@@ -42,10 +42,7 @@ namespace TensorFlowNET.UnitTest.Basics

public void ImportSavedModel()
{
tf_with(Session.LoadFromSavedModel("mobilenet"), sess =>
{

});
Session.LoadFromSavedModel("mobilenet");
}

public void ImportGraphDefFromPbFile()


+ 0
- 25
test/TensorFlowNET.UnitTest/PythonTest.cs View File

@@ -266,35 +266,10 @@ namespace TensorFlowNET.UnitTest
//else
//{
s = self._create_session(graph, config, force_gpu);
self._constrain_devices_and_set_default(s, use_gpu, force_gpu);
//}
return s.as_default();
}

private ITensorFlowObject _constrain_devices_and_set_default(Session sess, bool useGpu, bool forceGpu)
{
//def _constrain_devices_and_set_default(self, sess, use_gpu, force_gpu):
//"""Set the session and its graph to global default and constrain devices."""
//if context.executing_eagerly():
// yield None
//else:
// with sess.graph.as_default(), sess.as_default():
// if force_gpu:
// # Use the name of an actual device if one is detected, or
// # '/device:GPU:0' otherwise
// gpu_name = gpu_device_name()
// if not gpu_name:
// gpu_name = "/device:GPU:0"
// with sess.graph.device(gpu_name):
// yield sess
// elif use_gpu:
// yield sess
// else:
// with sess.graph.device("/device:CPU:0"):
// yield sess
return sess;
}

// See session() for details.
private Session _create_session(Graph graph, object cfg, bool forceGpu)
{


Loading…
Cancel
Save