Skip to content

Commit 8dd1eec

Browse files
committed
remove unnecessary variables.
1 parent cb7e017 commit 8dd1eec

16 files changed

Lines changed: 53 additions & 191 deletions

src/TensorFlowNET.Core/Data/MnistModelLoader.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,8 @@ public class MnistModelLoader : IModelLoader<MnistDataSet>
1313
private const string TEST_IMAGES = "t10k-images-idx3-ubyte.gz";
1414
private const string TEST_LABELS = "t10k-labels-idx1-ubyte.gz";
1515

16-
public static async Task<Datasets<MnistDataSet>> LoadAsync(string trainDir, bool oneHot = false, int? trainSize = null, int? validationSize = null, int? testSize = null, bool showProgressInConsole = false)
16+
public async Task<Datasets<MnistDataSet>> LoadAsync(string trainDir, bool oneHot = false, int? trainSize = null, int? validationSize = null, int? testSize = null, bool showProgressInConsole = false)
1717
{
18-
var loader = new MnistModelLoader();
19-
2018
var setting = new ModelLoadSetting
2119
{
2220
TrainDir = trainDir,
@@ -33,7 +31,7 @@ public static async Task<Datasets<MnistDataSet>> LoadAsync(string trainDir, bool
3331
if (testSize.HasValue)
3432
setting.TestSize = testSize.Value;
3533

36-
return await loader.LoadAsync(setting);
34+
return await LoadAsync(setting);
3735
}
3836

3937
public async Task<Datasets<MnistDataSet>> LoadAsync(ModelLoadSetting setting)

src/TensorFlowNET.Core/Eager/EagerRunner.TFE_Execute.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public Tensor[] TFE_ExecuteCancelable(Context ctx,
4242
int num_outputs)
4343
{
4444
var status = tf.Status;
45-
using var op = GetOp(ctx, op_name, status);
45+
var op = GetOp(ctx, op_name, status);
4646
c_api.TFE_OpSetDevice(op, device_name, status.Handle);
4747
if (status.ok())
4848
{

src/TensorFlowNET.Core/Eager/EagerRunner.TFE_FastPathExecute.cs

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,9 @@ namespace Tensorflow.Eager
1515
/// </summary>
1616
public partial class EagerRunner
1717
{
18-
UnorderedMap<Context, SafeOpHandle> thread_local_eager_operation_map = new UnorderedMap<Context, SafeOpHandle>();
18+
UnorderedMap<string, SafeOpHandle> thread_local_eager_operation_map = new UnorderedMap<string, SafeOpHandle>();
19+
public void ClearEagerOperationMap()
20+
=> thread_local_eager_operation_map.Clear();
1921

2022
public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info)
2123
{
@@ -31,7 +33,7 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info)
3133
op_exec_info.run_callbacks = op_exec_info.run_gradient_callback || op_exec_info.run_post_exec_callbacks;
3234

3335
var status = tf.Status;
34-
using var op = GetOp(op_exec_info.ctx, op_exec_info.op_name, status);
36+
var op = GetOp(op_exec_info.ctx, op_exec_info.op_name, status);
3537

3638
var op_def = tf.get_default_graph().GetOpDef(op_exec_info.op_name);
3739

@@ -56,8 +58,8 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info)
5658
}
5759
}
5860

59-
c_api.TFE_OpSetDevice(op, op_exec_info.device_name, status.Handle);
60-
status.Check(true);
61+
// c_api.TFE_OpSetDevice(op, op_exec_info.device_name, status.Handle);
62+
// status.Check(true);
6163

6264
// Add inferred attrs and inputs.
6365
for (int i = 0; i < op_def.InputArg.Count; i++)
@@ -145,7 +147,6 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info)
145147

146148
var flat_result = retVals.Select(x => new EagerTensor(x)).ToArray();
147149

148-
149150
if (op_exec_info.run_callbacks)
150151
{
151152
RunCallbacks(op_exec_info,
@@ -158,19 +159,19 @@ public Tensor[] TFE_FastPathExecute(FastPathOpExecInfo op_exec_info)
158159

159160
SafeOpHandle GetOp(Context ctx, string op_or_function_name, Status status)
160161
{
161-
/*if (thread_local_eager_operation_map.find(ctx, out var op))
162+
if (thread_local_eager_operation_map.find(op_or_function_name, out var op))
162163
c_api.TFE_OpReset(op, op_or_function_name, ctx.DeviceName, status.Handle);
163164
else
164165
{
165166
op = c_api.TFE_NewOp(ctx.Handle, op_or_function_name, status.Handle);
166-
thread_local_eager_operation_map[ctx] = op;
167+
thread_local_eager_operation_map[op_or_function_name] = op;
167168
}
168169

169-
status.Check(true);
170-
return op;*/
171-
var op = c_api.TFE_NewOp(ctx.Handle, op_or_function_name, status.Handle);
172170
status.Check(true);
173171
return op;
172+
/*var op = c_api.TFE_NewOp(ctx.Handle, op_or_function_name, status.Handle);
173+
status.Check(true);
174+
return op;*/
174175
}
175176

176177
bool HasAccumulator()
@@ -268,16 +269,7 @@ void SetOpAttrWithDefaults(Context ctx, SafeOpHandle op, AttrDef attr,
268269

269270
if (attr_value == null)
270271
{
271-
if (is_list != 0)
272-
#pragma warning disable CS0642 // Possible mistaken empty statement
273-
;
274-
#pragma warning restore CS0642 // Possible mistaken empty statement
275-
//SetOpAttrListDefault
276-
else
277-
#pragma warning disable CS0642 // Possible mistaken empty statement
278-
;
279-
#pragma warning restore CS0642 // Possible mistaken empty statement
280-
//SetOpAttrScalarDefault
272+
281273
}
282274
else
283275
{

src/TensorFlowNET.Core/Eager/IEagerRunner.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,5 +39,7 @@ bool RecordGradient(string op_name,
3939
bool MustRecordGradient();
4040

4141
int TapeSetPossibleGradientTypes(params Tensor[] args);
42+
43+
void ClearEagerOperationMap();
4244
}
4345
}

src/TensorFlowNET.Core/Framework/tensor_shape.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,14 +44,14 @@ bool _shape_is_compatible_0dim(Shape _this, Shape _other)
4444
return true;
4545
}
4646

47-
if (other.IsSparseTensor)
47+
if (other is SparseTensor)
4848
{
4949
return self.dtype.is_compatible_with(other.dtype);
5050
}
5151

5252
return self.dtype.is_compatible_with(other.dtype) &&
5353
_shape_is_compatible_0dim(self.shape, other.shape) &&
54-
!self.IsSparseTensor;
54+
!(self is SparseTensor);
5555
}
5656

5757
public static Dimension dimension_at_index(Shape shape, int index)

src/TensorFlowNET.Core/Sessions/BaseSession.cs

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,6 @@ namespace Tensorflow
3030
public class BaseSession : DisposableObject
3131
{
3232
protected Graph _graph;
33-
protected bool _opened;
34-
protected bool _closed;
35-
protected int _current_version;
36-
protected byte[] _target;
3733
public Graph graph => _graph;
3834

3935
public BaseSession(IntPtr handle, Graph g)
@@ -46,18 +42,15 @@ public BaseSession(string target = "", Graph g = null, ConfigProto config = null
4642
{
4743
_graph = g ?? ops.get_default_graph();
4844
if (!_graph.building_function)
49-
_graph.as_default();
50-
_target = Encoding.UTF8.GetBytes(target);
51-
52-
using (var opts = new SessionOptions(target, config))
5345
{
54-
lock (Locks.ProcessWide)
55-
{
56-
status = status ?? new Status();
57-
_handle = c_api.TF_NewSession(_graph, opts.Handle, status.Handle);
58-
status.Check(true);
59-
}
46+
if (ops.get_default_graph() != _graph)
47+
_graph.as_default();
6048
}
49+
50+
using var opts = new SessionOptions(target, config);
51+
status = status ?? tf.Status;
52+
_handle = c_api.TF_NewSession(_graph, opts.Handle, status.Handle);
53+
status.Check(true);
6154
}
6255

6356
public virtual void run(Operation op, params FeedItem[] feed_dict)

src/TensorFlowNET.Core/Tensors/Tensor.Conversions.cs

Lines changed: 0 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -26,80 +26,6 @@ namespace Tensorflow
2626
[SuppressMessage("ReSharper", "InvokeAsExtensionMethod")]
2727
public partial class Tensor
2828
{
29-
public unsafe void CopyTo(NDArray nd)
30-
{
31-
//if (!nd.Shape.IsContiguous)
32-
//throw new ArgumentException("NDArray has to be contiguous (ndarray.Shape.IsContiguous).");
33-
34-
var length = (int)(nd.size * nd.dtypesize);
35-
36-
switch (nd.dtype)
37-
{
38-
/*case NumpyDType.Boolean:
39-
{
40-
CopyTo(new Span<bool>(nd.Address.ToPointer(), length));
41-
break;
42-
}
43-
case NumpyDType.Byte:
44-
{
45-
CopyTo(new Span<byte>(nd.Address.ToPointer(), length));
46-
break;
47-
}
48-
case NumpyDType.Int16:
49-
{
50-
CopyTo(new Span<short>(nd.Address.ToPointer(), length));
51-
break;
52-
}
53-
case NumpyDType.UInt16:
54-
{
55-
CopyTo(new Span<ushort>(nd.Address.ToPointer(), length));
56-
break;
57-
}
58-
case NumpyDType.Int32:
59-
{
60-
CopyTo(new Span<int>(nd.Address.ToPointer(), length));
61-
break;
62-
}
63-
case NumpyDType.UInt32:
64-
{
65-
CopyTo(new Span<uint>(nd.Address.ToPointer(), length));
66-
break;
67-
}
68-
case NumpyDType.Int64:
69-
{
70-
CopyTo(new Span<long>(nd.Address.ToPointer(), length));
71-
break;
72-
}
73-
case NumpyDType.UInt64:
74-
{
75-
CopyTo(new Span<ulong>(nd.Address.ToPointer(), length));
76-
break;
77-
}
78-
case NumpyDType.Char:
79-
{
80-
CopyTo(new Span<char>(nd.Address.ToPointer(), length));
81-
break;
82-
}
83-
case NumpyDType.Double:
84-
{
85-
CopyTo(new Span<double>(nd.Address.ToPointer(), length));
86-
break;
87-
}
88-
case NumpyDType.Single:
89-
{
90-
CopyTo(new Span<float>(nd.Address.ToPointer(), length));
91-
break;
92-
}*/
93-
default:
94-
throw new NotSupportedException();
95-
}
96-
}
97-
98-
public void CopyTo<T>(Span<T> destination) where T : unmanaged
99-
{
100-
throw new NotImplementedException("");
101-
}
102-
10329
public TensorSpec ToTensorSpec()
10430
=> new TensorSpec(shape, dtype, name);
10531
}

src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public partial class Tensor
3232

3333
public Tensor()
3434
{
35-
isCreatedInGraphMode = !tf.executing_eagerly();
35+
_isCreatedInGraphMode = !tf.executing_eagerly();
3636
}
3737

3838
/// <summary>
@@ -45,7 +45,7 @@ public unsafe Tensor(SafeTensorHandle handle, bool clone = false)
4545
if (clone && handle != null)
4646
_handle = TF_NewTensor(shape, dtype, data: TensorDataPointer.ToPointer());
4747

48-
isCreatedInGraphMode = !tf.executing_eagerly();
48+
_isCreatedInGraphMode = !tf.executing_eagerly();
4949
}
5050

5151
/// <summary>
@@ -59,13 +59,13 @@ public unsafe Tensor(SafeTensorHandle handle, bool clone = false)
5959
public unsafe Tensor(IntPtr data_ptr, Shape shape, TF_DataType dtype)
6060
{
6161
_handle = TF_NewTensor(shape, dtype, data: data_ptr.ToPointer());
62-
isCreatedInGraphMode = !tf.executing_eagerly();
62+
_isCreatedInGraphMode = !tf.executing_eagerly();
6363
}
6464

6565
public unsafe Tensor(NDArray nd)
6666
{
6767
_handle = TF_NewTensor(nd.shape, nd.dtype, nd.data.ToPointer());
68-
isCreatedInGraphMode = !tf.executing_eagerly();
68+
_isCreatedInGraphMode = !tf.executing_eagerly();
6969
}
7070

7171
#region scala
@@ -107,13 +107,13 @@ public Tensor(Operation op, int value_index, TF_DataType dtype)
107107
_value_index = value_index;
108108
_override_dtype = dtype;
109109
_id = ops.uid();
110-
isCreatedInGraphMode = !tf.executing_eagerly();
110+
_isCreatedInGraphMode = !tf.executing_eagerly();
111111
}
112112

113113
protected unsafe void InitTensor(Shape shape, TF_DataType dtype)
114114
{
115115
_handle = TF_NewTensor(shape, dtype, null);
116-
isCreatedInGraphMode = !tf.executing_eagerly();
116+
_isCreatedInGraphMode = !tf.executing_eagerly();
117117
}
118118

119119
protected unsafe void InitTensor(Shape shape, byte[] bytes, TF_DataType dtype)
@@ -122,12 +122,12 @@ protected unsafe void InitTensor(Shape shape, byte[] bytes, TF_DataType dtype)
122122
_handle = StringTensor(new byte[][] { bytes }, Shape.Scalar);
123123
else
124124
_handle = TF_NewTensor(bytes, shape, dtype);
125-
isCreatedInGraphMode = !tf.executing_eagerly();
125+
_isCreatedInGraphMode = !tf.executing_eagerly();
126126
}
127127

128128
protected unsafe void InitTensor(Array array, Shape? shape = null)
129129
{
130-
isCreatedInGraphMode = !tf.executing_eagerly();
130+
_isCreatedInGraphMode = !tf.executing_eagerly();
131131

132132
shape = shape ?? array.GetShape();
133133
var dtype = array.GetDataType();

src/TensorFlowNET.Core/Tensors/Tensor.cs

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,8 @@ limitations under the License.
1717
using Tensorflow.NumPy;
1818
using System;
1919
using System.Diagnostics.CodeAnalysis;
20-
using System.Globalization;
2120
using System.Linq;
22-
using System.Runtime.InteropServices;
2321
using Tensorflow.Eager;
24-
using Tensorflow.Framework;
2522
using Tensorflow.Keras.Engine;
2623
using static Tensorflow.Binding;
2724

@@ -97,12 +94,9 @@ public partial class Tensor : DisposableObject,
9794
/// </summary>
9895
public SafeTensorHandleHandle EagerTensorHandle => _eagerTensorHandle;
9996

100-
protected bool isCreatedInGraphMode;
97+
protected bool _isCreatedInGraphMode;
10198

102-
public bool IsCreatedInGraphMode => isCreatedInGraphMode;
103-
public bool IsSparseTensor => this is SparseTensor;
104-
105-
public Tensor TensorShape => tf.shape(this);
99+
public bool IsCreatedInGraphMode => _isCreatedInGraphMode;
106100

107101
/// <summary>
108102
/// Returns the shape of a tensor.
@@ -157,7 +151,6 @@ public int[] _shape_tuple()
157151
/// Keras History: (Layer, (node_index, tensor_index))
158152
/// </summary>
159153
public KerasHistory KerasHistory { get; set; }
160-
public Tensor KerasMask { get; set; }
161154

162155
/// <summary>
163156
/// Updates the shape of this tensor.

src/TensorFlowNET.Core/ops.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,9 @@ public static int uid_layer()
383383
public static void reset_uid()
384384
{
385385
uid_number = -1;
386+
graph_uid_number = -1;
387+
uid_number_for_function = 0;
388+
uid_number_for_layer = 0;
386389
}
387390

388391
public static void colocate_with(bool ignore_existing = false)

0 commit comments

Comments
 (0)