Skip to content

Commit 226720d

Browse files
committed
Merge Tensorflow.Python into Tensorflow.Binding.
1 parent 67a58b3 commit 226720d

89 files changed

Lines changed: 177 additions & 135 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/TensorFlowNET.Core/APIs/tf.nn.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ limitations under the License.
1616

1717
using Tensorflow.Operations;
1818
using Tensorflow.Operations.Activation;
19-
using static Tensorflow.Python;
19+
using static Tensorflow.Binding;
2020

2121
namespace Tensorflow
2222
{
@@ -141,7 +141,7 @@ public Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string name =
141141

142142
public Tensor bias_add(Tensor value, RefVariable bias, string data_format = null, string name = null)
143143
{
144-
return Python.tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
144+
return tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
145145
{
146146
name = scope;
147147
return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name);
Lines changed: 20 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using NumSharp;
18-
using NumSharp.Backends;
1918
using System;
2019
using System.Collections;
2120
using System.Collections.Generic;
@@ -26,18 +25,15 @@ limitations under the License.
2625
namespace Tensorflow
2726
{
2827
/// <summary>
29-
/// Mapping C# functions to Python
28+
/// Binding utilities to mimic python functions.
3029
/// </summary>
31-
public static class Python
30+
public static partial class Binding
3231
{
3332
public static void print(object obj)
3433
{
3534
Console.WriteLine(obj.ToString());
3635
}
3736

38-
//protected int len<T>(IEnumerable<T> a)
39-
// => a.Count();
40-
4137
public static int len(object a)
4238
{
4339
switch (a)
@@ -66,15 +62,15 @@ public static IEnumerable<int> range(int start, int end)
6662
return Enumerable.Range(start, end - start);
6763
}
6864

69-
public static T New<T>() where T : IPyClass, new()
65+
public static T New<T>() where T : IObjectLife, new()
7066
{
7167
var instance = new T();
72-
instance.__init__(instance);
68+
instance.__init__();
7369
return instance;
7470
}
7571

7672
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
77-
public static void tf_with(IPython py, Action<IPython> action)
73+
public static void tf_with(IObjectLife py, Action<IObjectLife> action)
7874
{
7975
try
8076
{
@@ -94,7 +90,7 @@ public static void tf_with(IPython py, Action<IPython> action)
9490
}
9591

9692
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
97-
public static void tf_with<T>(T py, Action<T> action) where T : IPython
93+
public static void tf_with<T>(T py, Action<T> action) where T : IObjectLife
9894
{
9995
try
10096
{
@@ -114,7 +110,7 @@ public static void tf_with<T>(T py, Action<T> action) where T : IPython
114110
}
115111

116112
[DebuggerNonUserCode()] // with "Just My Code" enabled this lets the debugger break at the origin of the exception
117-
public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : IPython
113+
public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : IObjectLife
118114
{
119115
try
120116
{
@@ -124,7 +120,6 @@ public static TOut tf_with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn
124120
catch (Exception ex)
125121
{
126122
Console.WriteLine(ex.ToString());
127-
throw;
128123
return default(TOut);
129124
}
130125
finally
@@ -276,7 +271,9 @@ public static bool hasattr(object obj, string key)
276271
var __memberobject__ = __type__.GetMember(key);
277272
return (__memberobject__.Length > 0) ? true : false;
278273
}
274+
279275
public delegate object __object__(params object[] args);
276+
280277
public static __object__ getattr(object obj, string key, params Type[] ___parameter_type__)
281278
{
282279
var __dyn_obj__ = obj.GetType().GetMember(key);
@@ -288,7 +285,7 @@ public static __object__ getattr(object obj, string key, params Type[] ___parame
288285
try
289286
{
290287
var __method__ = (___parameter_type__.Length > 0) ? obj.GetType().GetMethod(key, ___parameter_type__) : obj.GetType().GetMethod(key);
291-
return (__object__)((object[] args) => __method__.Invoke(obj, args));
288+
return (object[] args) => __method__.Invoke(obj, args);
292289
}
293290
catch (System.Reflection.AmbiguousMatchException ex)
294291
{
@@ -297,16 +294,17 @@ public static __object__ getattr(object obj, string key, params Type[] ___parame
297294
}
298295
else if (__type__.MemberType == System.Reflection.MemberTypes.Field)
299296
{
300-
var __field__ = (object)obj.GetType().GetField(key).GetValue(obj);
301-
return (__object__)((object[] args) => { return __field__; });
297+
var __field__ = obj.GetType().GetField(key).GetValue(obj);
298+
return (object[] args) => { return __field__; };
302299
}
303300
else if (__type__.MemberType == System.Reflection.MemberTypes.Property)
304301
{
305-
var __property__ = (object)obj.GetType().GetProperty(key).GetValue(obj);
306-
return (__object__)((object[] args) => { return __property__; });
302+
var __property__ = obj.GetType().GetProperty(key).GetValue(obj);
303+
return (object[] args) => { return __property__; };
307304
}
308-
return (__object__)((object[] args) => { return "NaN"; });
305+
return (object[] args) => { return "NaN"; };
309306
}
307+
310308
public static IEnumerable TupleToEnumerable(object tuple)
311309
{
312310
Type t = tuple.GetType();
@@ -315,18 +313,20 @@ public static IEnumerable TupleToEnumerable(object tuple)
315313
var flds = t.GetFields();
316314
for(int i = 0; i < flds.Length;i++)
317315
{
318-
yield return ((object)flds[i].GetValue(tuple));
316+
yield return flds[i].GetValue(tuple);
319317
}
320318
}
321319
else
322320
{
323321
throw new System.Exception("Expected Tuple.");
324322
}
325323
}
324+
326325
public static bool isinstance(object Item1, Type Item2)
327326
{
328-
return (Item1.GetType() == Item2);
327+
return Item1.GetType() == Item2;
329328
}
329+
330330
public static bool isinstance(object Item1, object tuple)
331331
{
332332
var tup = TupleToEnumerable(tuple);
@@ -338,11 +338,4 @@ public static bool isinstance(object Item1, object tuple)
338338
return false;
339339
}
340340
}
341-
342-
public interface IPython : IDisposable
343-
{
344-
void __enter__();
345-
346-
void __exit__();
347-
}
348341
}

src/TensorFlowNET.Core/Binding.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44

55
namespace Tensorflow
66
{
7-
public static class Binding
7+
public static partial class Binding
88
{
9-
public static tensorflow tf { get; } = Python.New<tensorflow>();
9+
public static tensorflow tf { get; } = New<tensorflow>();
1010
}
1111
}

src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ limitations under the License.
1515
******************************************************************************/
1616

1717
using System.Linq;
18-
using static Tensorflow.Python;
18+
using static Tensorflow.Binding;
1919

2020
namespace Tensorflow.Clustering
2121
{

src/TensorFlowNET.Core/Framework/graph_util_impl.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ limitations under the License.
1818
using System;
1919
using System.Collections.Generic;
2020
using System.Linq;
21-
using static Tensorflow.Python;
21+
using static Tensorflow.Binding;
2222

2323
namespace Tensorflow
2424
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ limitations under the License.
1919
using System.Collections.Generic;
2020
using System.Linq;
2121
using static Tensorflow.OpDef.Types;
22-
using static Tensorflow.Python;
22+
using static Tensorflow.Binding;
2323

2424
namespace Tensorflow
2525
{

src/TensorFlowNET.Core/Gradients/array_grad.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
using System.Collections.Generic;
1818
using System.Linq;
1919
using Tensorflow.Framework;
20-
using static Tensorflow.Python;
2120
using static Tensorflow.Binding;
2221

2322
namespace Tensorflow.Gradients

src/TensorFlowNET.Core/Gradients/gradients_util.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ limitations under the License.
1717
using System;
1818
using System.Collections.Generic;
1919
using System.Linq;
20-
using static Tensorflow.Python;
20+
using static Tensorflow.Binding;
2121

2222
namespace Tensorflow
2323
{

src/TensorFlowNET.Core/Gradients/math_grad.cs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ limitations under the License.
1717
using System;
1818
using System.Linq;
1919
using Tensorflow.Operations;
20-
using static Tensorflow.Python;
2120
using static Tensorflow.Binding;
2221

2322
namespace Tensorflow.Gradients

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
using System.Collections.Generic;
2020
using System.Linq;
2121
using System.Runtime.InteropServices;
22+
using static Tensorflow.Binding;
2223

2324
namespace Tensorflow
2425
{
@@ -247,7 +248,7 @@ public unsafe Operation create_op(string op_type, Tensor[] inputs, TF_DataType[]
247248
if (inputs == null)
248249
inputs = new Tensor[0];
249250

250-
foreach ((int idx, Tensor a) in Python.enumerate(inputs))
251+
foreach ((int idx, Tensor a) in enumerate(inputs))
251252
{
252253

253254
}

0 commit comments

Comments
 (0)