Skip to content

Commit 5a7d28c

Browse files
committed
simplify with function.
1 parent 9160887 commit 5a7d28c

29 files changed

Lines changed: 79 additions & 72 deletions

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Tensorflow
99
{
10-
public class importer
10+
public class importer : Python
1111
{
1212
public static ITensorOrOperation[] import_graph_def(GraphDef graph_def,
1313
Dictionary<string, Tensor> input_map = null,
@@ -26,7 +26,7 @@ public static ITensorOrOperation[] import_graph_def(GraphDef graph_def,
2626

2727
string prefix = "";
2828
var graph = ops.get_default_graph();
29-
Python.with<ops.name_scope>(new ops.name_scope(name, "import", input_map.Values), scope =>
29+
with(new ops.name_scope(name, "import", input_map.Values), scope =>
3030
{
3131
prefix = scope;
3232
/*if (!string.IsNullOrEmpty(prefix))

‎src/TensorFlowNET.Core/Gradients/gradients_impl.py.cs‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
namespace Tensorflow
99
{
10-
public class gradients_impl
10+
public class gradients_impl : Python
1111
{
1212
public static Tensor[] gradients(Tensor[] ys,
1313
Tensor[] xs,
@@ -58,7 +58,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
5858
**/
5959
var grads = new Dictionary<string, Tensor[][]>();
6060

61-
Python.with<ops.name_scope>(new ops.name_scope(name, "gradients", values: all), scope =>
61+
with(new ops.name_scope(name, "gradients", values: all), scope =>
6262
{
6363
string grad_scope = scope;
6464
// Get a uid for this call to gradients that can be used to help
@@ -131,7 +131,7 @@ public static Tensor[] _GradientsHelper(Tensor[] ys,
131131
// for ops that do not have gradients.
132132
var grad_fn = ops.get_gradient_function(op);
133133

134-
Python.with<ops.name_scope>(new ops.name_scope(op.name + "_grad"), scope1 =>
134+
with(new ops.name_scope(op.name + "_grad"), scope1 =>
135135
{
136136
string name1 = scope1;
137137
if (grad_fn != null)

‎src/TensorFlowNET.Core/Operations/Losses/losses_impl.py.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public Tensor sparse_softmax_cross_entropy(Tensor labels,
1212
string scope = "",
1313
string loss_collection= "losses")
1414
{
15-
with<ops.name_scope>(new ops.name_scope(scope,
15+
with(new ops.name_scope(scope,
1616
"sparse_softmax_cross_entropy_loss",
1717
(logits, labels, weights)),
1818
namescope =>

‎src/TensorFlowNET.Core/Operations/OpDefLibrary.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
namespace Tensorflow
1212
{
13-
public class OpDefLibrary
13+
public class OpDefLibrary : Python
1414
{
1515
public Operation _apply_op_helper(string op_type_name, string name = null, dynamic args = null)
1616
{
@@ -44,7 +44,7 @@ public Operation _apply_op_helper(string op_type_name, string name = null, dynam
4444
var input_types = new List<TF_DataType>();
4545
dynamic values = null;
4646

47-
return Python.with<ops.name_scope, Operation>(new ops.name_scope(name), scope =>
47+
return with(new ops.name_scope(name), scope =>
4848
{
4949
var inferred_from = new Dictionary<string, object>();
5050
var base_types = new List<TF_DataType>();

‎src/TensorFlowNET.Core/Operations/array_ops.py.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,14 @@
55

66
namespace Tensorflow
77
{
8-
public class array_ops
8+
public class array_ops : Python
99
{
1010
public static Tensor placeholder_with_default<T>(T input, int[] shape, string name = null) => gen_array_ops.placeholder_with_default(input, shape, name);
1111

1212
public static Tensor zeros(Shape shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null)
1313
{
1414
dtype = dtype.as_base_dtype();
15-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "zeros", shape), scope =>
15+
return with(new ops.name_scope(name, "zeros", shape), scope =>
1616
{
1717
name = scope;
1818
switch (dtype)
@@ -68,7 +68,7 @@ public static Tensor ones_like<T>(T tensor, TF_DataType dtype = TF_DataType.DtIn
6868

6969
private static Tensor ones_like_impl<T>(T tensor, TF_DataType dtype, string name, bool optimize = true)
7070
{
71-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "ones_like", new { tensor }), scope =>
71+
return with(new ops.name_scope(name, "ones_like", new { tensor }), scope =>
7272
{
7373
name = scope;
7474
var tensor1 = ops.convert_to_tensor(tensor, name: "tensor");
@@ -84,7 +84,7 @@ private static Tensor ones_like_impl<T>(T tensor, TF_DataType dtype, string name
8484
public static Tensor ones(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null)
8585
{
8686
dtype = dtype.as_base_dtype();
87-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "ones", new { shape }), scope =>
87+
return with(new ops.name_scope(name, "ones", new { shape }), scope =>
8888
{
8989
name = scope;
9090
var output = gen_array_ops.fill(shape, constant_op.constant(1.0f, dtype: dtype), name: name);
@@ -130,7 +130,7 @@ public static Tensor size(Tensor input, string name = null, bool optimize = true
130130

131131
private static Tensor shape_internal(Tensor input, string name = null, bool optimize = true, TF_DataType out_type = TF_DataType.TF_INT32)
132132
{
133-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "Shape", new { input }), scope =>
133+
return with(new ops.name_scope(name, "Shape", new { input }), scope =>
134134
{
135135
name = scope;
136136

@@ -151,7 +151,7 @@ private static Tensor shape_internal(Tensor input, string name = null, bool opti
151151

152152
private static Tensor size_internal(Tensor input, string name = null, bool optimize = true, TF_DataType out_type = TF_DataType.TF_INT32)
153153
{
154-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "Size", new Tensor[] { input }), scope =>
154+
return with(new ops.name_scope(name, "Size", new Tensor[] { input }), scope =>
155155
{
156156
name = scope;
157157

@@ -182,7 +182,7 @@ private static Tensor size_internal(Tensor input, string name = null, bool optim
182182

183183
public static Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true)
184184
{
185-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope =>
185+
return with(new ops.name_scope(name, "zeros_like", new Tensor[] { tensor }), scope =>
186186
{
187187
name = scope;
188188
tensor = ops.convert_to_tensor(tensor, name: "tensor");

‎src/TensorFlowNET.Core/Operations/control_flow_ops.py.cs‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ public class control_flow_ops : Python
99
{
1010
public static Operation group<T>(T[] inputs, string name = null) where T : ITensorOrOperation
1111
{
12-
return with<ops.name_scope, Operation>(new ops.name_scope(name, "group_deps", inputs), scope =>
12+
return with(new ops.name_scope(name, "group_deps", inputs), scope =>
1313
{
1414
name = scope;
1515

@@ -39,7 +39,7 @@ public static Operation group<T>(T[] inputs, string name = null) where T : ITens
3939

4040
private static Operation _GroupControlDeps(string dev, Operation[] deps, string name = null)
4141
{
42-
return Python.with<_ControlDependenciesController, Operation>(ops.control_dependencies(deps), ctl =>
42+
return with(ops.control_dependencies(deps), ctl =>
4343
{
4444
if (dev == null)
4545
{
@@ -83,7 +83,7 @@ private static bool IsLoopExit(Operation op)
8383

8484
public static Tensor[] tuple(Tensor[] tensors, string name = null, Operation[] control_inputs = null)
8585
{
86-
return Python.with<ops.name_scope, Tensor[]>(new ops.name_scope(name, "tuple", tensors), scope =>
86+
return with(new ops.name_scope(name, "tuple", tensors), scope =>
8787
{
8888
name = scope;
8989
var gating_ops = tensors.Select(x => x.op).ToList();
@@ -115,11 +115,11 @@ public static Tensor with_dependencies(Operation[] dependencies, Tensor output_t
115115
values.AddRange(dependencies);
116116
values.Add(output_tensor);
117117

118-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "control_dependency", values), scope =>
118+
return with(new ops.name_scope(name, "control_dependency", values), scope =>
119119
{
120120
name = scope;
121121

122-
return Python.with<_ControlDependenciesController, Tensor>(ops.control_dependencies(dependencies), ctl =>
122+
return with(ops.control_dependencies(dependencies), ctl =>
123123
{
124124
output_tensor = ops.convert_to_tensor_or_composite(output_tensor);
125125
return _Identity(output_tensor, name: name);

‎src/TensorFlowNET.Core/Operations/math_ops.py.cs‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ public static Tensor cast(Tensor x, TF_DataType dtype = TF_DataType.DtInvalid, s
1414
if(base_type == x.dtype)
1515
return x;
1616

17-
return with<ops.name_scope, Tensor>(new ops.name_scope(name, "Cast", new { x }), scope =>
17+
return with(new ops.name_scope(name, "Cast", new { x }), scope =>
1818
{
1919
x = ops.convert_to_tensor(x, name: "x");
2020
if (x.dtype.as_base_dtype() != base_type)
@@ -141,7 +141,7 @@ public static Tensor range(object start, object limit = null, object delta = nul
141141
if (delta == null)
142142
delta = 1;
143143

144-
return with<ops.name_scope, Tensor>(new ops.name_scope(name, "Range", new object[] { start, limit, delta }), scope =>
144+
return with(new ops.name_scope(name, "Range", new object[] { start, limit, delta }), scope =>
145145
{
146146
name = scope;
147147
var start1 = ops.convert_to_tensor(start, name: "start");
@@ -154,15 +154,15 @@ public static Tensor range(object start, object limit = null, object delta = nul
154154

155155
public static Tensor floordiv(Tensor x, Tensor y, string name = null)
156156
{
157-
return with<ops.name_scope, Tensor>(new ops.name_scope(name, "floordiv", new { x, y }), scope =>
157+
return with(new ops.name_scope(name, "floordiv", new { x, y }), scope =>
158158
{
159159
return gen_math_ops.floor_div(x, y, scope);
160160
});
161161
}
162162

163163
public static Tensor rank_internal(Tensor input, string name = null, bool optimize = true)
164164
{
165-
return with<ops.name_scope, Tensor>(new ops.name_scope(name, "Rank", new List<Tensor> { input }), scope =>
165+
return with(new ops.name_scope(name, "Rank", new List<Tensor> { input }), scope =>
166166
{
167167
name = scope;
168168
var input_tensor = ops.convert_to_tensor(input);
@@ -182,7 +182,7 @@ public static Tensor matmul(Tensor a, Tensor b,
182182
{
183183
Tensor result = null;
184184

185-
Python.with<ops.name_scope>(new ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope =>
185+
with(new ops.name_scope(name, "MatMul", new Tensor[] { a, b }), scope =>
186186
{
187187
name = scope;
188188

@@ -212,7 +212,7 @@ public static Tensor conj(Tensor x, string name = null)
212212
if (dt.is_floating() || dt.is_integer())
213213
return x;
214214

215-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "Conj", new List<Tensor> { x }), scope =>
215+
return with(new ops.name_scope(name, "Conj", new List<Tensor> { x }), scope =>
216216
{
217217

218218
return x;

‎src/TensorFlowNET.Core/Operations/nn_impl.py.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ public Tuple<Tensor, Tensor> moments(Tensor x,
2020
bool keep_dims = false)
2121
{
2222
Tuple<Tensor, Tensor> t = null;
23-
with<ops.name_scope>(new ops.name_scope(name, "moments", new { x, axes }), scope =>
23+
with(new ops.name_scope(name, "moments", new { x, axes }), scope =>
2424
{
2525
// The dynamic range of fp16 is too limited to support the collection of
2626
// sufficient statistics. As a workaround we simply perform the operations

‎src/TensorFlowNET.Core/Operations/random_ops.py.cs‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static Tensor random_normal(int[] shape,
2323
int? seed = null,
2424
string name = null)
2525
{
26-
return Python.with<ops.name_scope, Tensor>(new ops.name_scope(name, "random_normal", new object[] { shape, mean, stddev }), scope =>
26+
return with(new ops.name_scope(name, "random_normal", new { shape, mean, stddev }), scope =>
2727
{
2828
var shape_tensor = _ShapeTensor(shape);
2929
var mean_tensor = ops.convert_to_tensor(mean, dtype: dtype, name: "mean");
@@ -53,7 +53,7 @@ public static Tensor random_uniform(int[] shape,
5353
int? seed = null,
5454
string name = null)
5555
{
56-
return with<ops.name_scope, Tensor>(new ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope =>
56+
return with(new ops.name_scope(name, "random_uniform", new { shape, minval, maxval }), scope =>
5757
{
5858
name = scope;
5959
var tensorShape = _ShapeTensor(shape);

‎src/TensorFlowNET.Core/Python.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,12 +43,12 @@ public static void with(IPython py, Action<IPython> action)
4343
}
4444
}
4545

46-
public static void with<T>(IPython py, Action<T> action) where T : IPython
46+
public static void with<T>(T py, Action<T> action) where T : IPython
4747
{
4848
try
4949
{
5050
py.__enter__();
51-
action((T)py);
51+
action(py);
5252
}
5353
catch (Exception ex)
5454
{
@@ -62,12 +62,12 @@ public static void with<T>(IPython py, Action<T> action) where T : IPython
6262
}
6363
}
6464

65-
public static TOut with<TIn, TOut>(IPython py, Func<TIn, TOut> action) where TIn : IPython
65+
public static TOut with<TIn, TOut>(TIn py, Func<TIn, TOut> action) where TIn : IPython
6666
{
6767
try
6868
{
6969
py.__enter__();
70-
return action((TIn)py);
70+
return action(py);
7171
}
7272
catch (Exception ex)
7373
{

0 commit comments

Comments
 (0)