Skip to content

Commit 1c8f0a2

Browse files
SanftMonsterOceania2018
authored andcommitted
refactor: gen_nn_ops, gen_math_ops, gen_array_ops and related codes.
1 parent c1b6731 commit 1c8f0a2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

55 files changed

+29617
-1724
lines changed

src/TensorFlowNET.Console/MemoryBasicTest.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ public Action<int, int> Conv2DWithTensor
112112
var strides = new[] { 1, 1, 1, 1 };
113113
var dilations = new[] { 1, 1, 1, 1 };
114114

115-
var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter)
115+
var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "Conv2D", null, input, filter)
116116
{
117117
attrs = ConvertToDict(new
118118
{
@@ -134,7 +134,7 @@ public Action<int, int> Conv2DWithVariable
134134
var strides = new[] { 1, 1, 1, 1 };
135135
var dilations = new[] { 1, 1, 1, 1 };
136136

137-
var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo("Conv2D", null, input, filter)
137+
var results = tf.Runner.TFE_FastPathExecute(new FastPathOpExecInfo(tf.Context, "Conv2D", null, input, filter)
138138
{
139139
attrs = ConvertToDict(new
140140
{

src/TensorFlowNET.Core/APIs/tf.array.cs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@ public partial class tensorflow
4444
/// <param name="name"></param>
4545
/// <returns></returns>
4646
public Tensor batch_to_space_nd<T>(T input, int[] block_shape, int[,] crops, string name = null)
47-
=> gen_array_ops.batch_to_space_nd(input, block_shape, crops, name: name);
47+
=> gen_array_ops.batch_to_space_nd(ops.convert_to_tensor(input), ops.convert_to_tensor(block_shape),
48+
ops.convert_to_tensor(crops), name: name);
4849

4950
/// <summary>
5051
/// Apply boolean mask to tensor.
@@ -91,7 +92,7 @@ public Tensor concat(IEnumerable<Tensor> values, int axis, string name = "concat
9192
});
9293
}
9394

94-
return gen_array_ops.concat_v2(values.ToArray(), axis, name: name);
95+
return gen_array_ops.concat_v2(values.ToArray(), ops.convert_to_tensor(axis), name: name);
9596
}
9697

9798
/// <summary>
@@ -115,7 +116,7 @@ public Tensor expand_dims(Tensor input, int axis = -1, string name = null)
115116
/// <param name="name"></param>
116117
/// <returns></returns>
117118
public Tensor fill<T>(Tensor dims, T value, string name = null)
118-
=> gen_array_ops.fill(dims, value, name: name);
119+
=> gen_array_ops.fill(dims, ops.convert_to_tensor(value), name: name);
119120

120121
public Tensor fill<T>(Shape dims, T value, string name = null)
121122
=> array_ops.fill(dims, value, name: name);
@@ -138,7 +139,7 @@ public Tensor identity(Tensor input, string name = null)
138139
/// <param name="axis"></param>
139140
/// <returns></returns>
140141
public Tensor gather(Tensor @params, Tensor indices, string name = null, int axis = 0)
141-
=> array_ops.gather(@params, indices, name: name, axis: axis);
142+
=> array_ops.gather(@params, indices, name: name, axis: ops.convert_to_tensor(axis));
142143

143144
/// <summary>
144145
/// Return the elements, either from `x` or `y`, depending on the `condition`.
@@ -166,7 +167,7 @@ public Tensor transpose<T1>(T1 a, Axis perm = null, string name = "transpose", b
166167
/// <param name="name"></param>
167168
/// <returns></returns>
168169
public Tensor reverse(Tensor tensor, int[] axis, string name = null)
169-
=> gen_array_ops.reverse(tensor, axis, name: name);
170+
=> gen_array_ops.reverse(tensor, ops.convert_to_tensor(axis), name: name);
170171

171172
public Tensor reverse(Tensor tensor, Tensor axis, string name = null)
172173
=> gen_array_ops.reverse(tensor, axis, name: name);
@@ -189,7 +190,8 @@ public Tensor rank(Tensor input, string name = null)
189190
/// <param name="name">A name for the operation (optional).</param>
190191
/// <returns>A `Tensor` the same type as `input`.</returns>
191192
public Tensor slice<Tb, Ts>(Tensor input, Tb[] begin, Ts[] size, string name = null)
192-
=> array_ops.slice(input, begin, size, name: name);
193+
=> array_ops.slice(input, begin.Select(x => ops.convert_to_tensor(x)).ToArray(),
194+
size.Select(x => ops.convert_to_tensor(x)).ToArray(), name: name);
193195

194196
public Tensor squeeze(Tensor input, int axis, string name = null, int squeeze_dims = -1)
195197
=> array_ops.squeeze(input, new[] { axis }, name);
@@ -255,7 +257,7 @@ public Tensor pad(Tensor tensor, Tensor paddings, string mode = "CONSTANT", stri
255257
/// <param name="name">A name for the operation (optional).</param>
256258
/// <returns>A `Tensor`. Has the same type as `input`.</returns>
257259
public Tensor placeholder_with_default<T>(T input, int[] shape, string name = null)
258-
=> gen_array_ops.placeholder_with_default(input, shape, name: name);
260+
=> gen_array_ops.placeholder_with_default(ops.convert_to_tensor(input), shape, name: name);
259261

260262
/// <summary>
261263
/// Returns the shape of a tensor.

src/TensorFlowNET.Core/APIs/tf.math.cs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ public Tensor add(Tensor a, Tensor b, string name = null)
130130
=> gen_math_ops.add(a, b, name: name);
131131

132132
public Tensor add<Tx, Ty>(Tx a, Ty b, string name = null)
133-
=> gen_math_ops.add(a, b, name: name);
133+
=> gen_math_ops.add(ops.convert_to_tensor(a), ops.convert_to_tensor(b), name: name);
134134

135135
/// <summary>
136136
/// Adds all input tensors element-wise.
@@ -151,10 +151,10 @@ public Tensor atan(Tensor x, string name = null)
151151
=> gen_math_ops.atan(x, name);
152152

153153
public Tensor arg_max(Tensor input, int dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null)
154-
=> gen_math_ops.arg_max(input, dimension, output_type: output_type, name: name);
154+
=> gen_math_ops.arg_max(input, ops.convert_to_tensor(dimension), output_type: output_type, name: name);
155155

156156
public Tensor arg_min(Tensor input, int dimension, TF_DataType output_type = TF_DataType.TF_INT64, string name = null)
157-
=> gen_math_ops.arg_min(input, dimension, output_type: output_type, name: name);
157+
=> gen_math_ops.arg_min(input, ops.convert_to_tensor(dimension), output_type: output_type, name: name);
158158

159159
public Tensor is_finite(Tensor input, string name = null)
160160
=> gen_math_ops.is_finite(input, name);
@@ -199,7 +199,7 @@ public Tensor cos(Tensor x, string name = null)
199199
=> gen_math_ops.cos(x, name);
200200

201201
public Tensor cos(float x, string name = null)
202-
=> gen_math_ops.cos(x, name);
202+
=> gen_math_ops.cos(ops.convert_to_tensor(x), name);
203203

204204
/// <summary>
205205
/// Computes hyperbolic cosine of x element-wise.
@@ -235,7 +235,7 @@ public Tensor floor(Tensor x, string name = null)
235235
/// <param name="name"></param>
236236
/// <returns></returns>
237237
public Tensor greater<Tx, Ty>(Tx x, Ty y, string name = null)
238-
=> gen_math_ops.greater(x, y, name);
238+
=> gen_math_ops.greater(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name);
239239

240240
/// <summary>
241241
/// Returns the truth value of (x >= y) element-wise.
@@ -247,7 +247,7 @@ public Tensor greater<Tx, Ty>(Tx x, Ty y, string name = null)
247247
/// <param name="name"></param>
248248
/// <returns></returns>
249249
public Tensor greater_equal<Tx, Ty>(Tx x, Ty y, string name = null)
250-
=> gen_math_ops.greater_equal(x, y, name);
250+
=> gen_math_ops.greater_equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name);
251251

252252
/// <summary>
253253
/// Returns the truth value of (x &lt; y) element-wise.
@@ -259,7 +259,7 @@ public Tensor greater_equal<Tx, Ty>(Tx x, Ty y, string name = null)
259259
/// <param name="name"></param>
260260
/// <returns></returns>
261261
public Tensor less<Tx, Ty>(Tx x, Ty y, string name = null)
262-
=> gen_math_ops.less(x, y, name);
262+
=> gen_math_ops.less(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name);
263263

264264
/// <summary>
265265
/// Computes the log of the absolute value of `Gamma(x)` element-wise.
@@ -280,7 +280,7 @@ public Tensor lgamma(Tensor x, string name = null)
280280
/// <param name="name"></param>
281281
/// <returns></returns>
282282
public Tensor less_equal<Tx, Ty>(Tx x, Ty y, string name = null)
283-
=> gen_math_ops.less_equal(x, y, name);
283+
=> gen_math_ops.less_equal(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name);
284284

285285
/// <summary>
286286
/// Computes natural logarithm of (1 + x) element-wise.
@@ -292,7 +292,7 @@ public Tensor log1p(Tensor x, string name = null)
292292
=> gen_math_ops.log1p(x, name);
293293

294294
public Tensor logical_and<T>(T x, T y, string name = null)
295-
=> gen_math_ops.logical_and(x, y, name);
295+
=> gen_math_ops.logical_and(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name);
296296

297297
public Tensor logical_not(Tensor x, string name = null)
298298
=> gen_math_ops.logical_not(x, name);
@@ -301,7 +301,10 @@ public Tensor logical_or(Tensor x, Tensor y, string name = null)
301301
=> gen_math_ops.logical_or(x, y, name);
302302

303303
public Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor")
304-
=> gen_math_ops.logical_xor(x, y, name);
304+
{
305+
return gen_math_ops.logical_and(gen_math_ops.logical_or(x, y),
306+
gen_math_ops.logical_not(gen_math_ops.logical_and(x, y)), name);
307+
}
305308

306309
/// <summary>
307310
/// Clips tensor values to a specified min and max.
@@ -312,7 +315,7 @@ public Tensor logical_xor(Tensor x, Tensor y, string name = "LogicalXor")
312315
/// <param name="name"></param>
313316
/// <returns></returns>
314317
public Tensor _clip_by_value(Tensor t, Tensor clip_value_min, Tensor clip_value_max, string name = null)
315-
=> gen_math_ops._clip_by_value(t, clip_value_min, clip_value_max);
318+
=> gen_math_ops.clip_by_value(t, clip_value_min, clip_value_max);
316319

317320
/// <summary>
318321
/// Clips tensor values to a specified min and max.
@@ -345,7 +348,7 @@ public Tensor clip_by_value<T1, T2>(Tensor t, T1 clip_value_min, T2 clip_value_m
345348
=> clip_ops.clip_by_value(t, clip_value_min, clip_value_max, name);
346349

347350
public Tensor sub<Tx, Ty>(Tx a, Ty b, string name = null)
348-
=> gen_math_ops.sub(a, b, name: name);
351+
=> gen_math_ops.sub(ops.convert_to_tensor(a), ops.convert_to_tensor(b), name: name);
349352

350353
public Tensor divide(Tensor a, Tensor b)
351354
=> a / b;
@@ -396,7 +399,7 @@ public Tensor atan2(Tensor y, Tensor x, string name = null)
396399
/// <param name="name"></param>
397400
/// <returns></returns>
398401
public Tensor max<Tx, Ty>(Tx input, Ty axis, bool keep_dims = false, string name = null)
399-
=> gen_math_ops._max(input, axis, keep_dims: keep_dims, name: name);
402+
=> gen_math_ops.max(ops.convert_to_tensor(input), ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name);
400403

401404
/// <summary>
402405
/// Computes the minimum of elements across dimensions of a tensor.
@@ -409,7 +412,7 @@ public Tensor max<Tx, Ty>(Tx input, Ty axis, bool keep_dims = false, string name
409412
/// <param name="name"></param>
410413
/// <returns></returns>
411414
public Tensor min<Tx, Ty>(Tx input, Ty axis, bool keep_dims = false, string name = null)
412-
=> gen_math_ops._min(input, axis, keep_dims: keep_dims, name: name);
415+
=> gen_math_ops.min(ops.convert_to_tensor(input), ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name);
413416

414417
/// <summary>
415418
/// Returns the max of x and y (i.e. x > y ? x : y) element-wise.
@@ -421,7 +424,7 @@ public Tensor min<Tx, Ty>(Tx input, Ty axis, bool keep_dims = false, string name
421424
/// <param name="name"></param>
422425
/// <returns></returns>
423426
public Tensor maximum<T1, T2>(T1 x, T2 y, string name = null)
424-
=> gen_math_ops.maximum(x, y, name: name);
427+
=> gen_math_ops.maximum(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name);
425428

426429
/// <summary>
427430
/// Returns the min of x and y (i.e. x &lt; y ? x : y) element-wise.
@@ -433,7 +436,7 @@ public Tensor maximum<T1, T2>(T1 x, T2 y, string name = null)
433436
/// <param name="name"></param>
434437
/// <returns></returns>
435438
public Tensor minimum<T1, T2>(T1 x, T2 y, string name = null)
436-
=> gen_math_ops.minimum(x, y, name: name);
439+
=> gen_math_ops.minimum(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name);
437440

438441
public Tensor multiply(Tensor x, Tensor y, string name = null)
439442
=> gen_math_ops.mul(x, y, name: name);
@@ -448,7 +451,7 @@ public Tensor multiply(Tensor x, Tensor y, string name = null)
448451
/// <param name="name"></param>
449452
/// <returns></returns>
450453
public Tensor multiply<Tx, Ty>(Tx x, Ty y, string name = null)
451-
=> gen_math_ops.mul(x, y, name: name);
454+
=> gen_math_ops.mul(ops.convert_to_tensor(x), ops.convert_to_tensor(y), name: name);
452455

453456
public Tensor negative(Tensor x, string name = null)
454457
=> gen_math_ops.neg(x, name);
@@ -577,7 +580,7 @@ public Tensor sigmoid<T>(T x, string name = null)
577580
=> math_ops.sigmoid(x, name: name);
578581

579582
public Tensor sum(Tensor input, int axis, bool keep_dims = false, string name = null)
580-
=> gen_math_ops._sum(input, axis, keep_dims: keep_dims, name: name);
583+
=> gen_math_ops.sum(input, ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name);
581584

582585
public Tensor reduce_mean(Tensor input_tensor, Axis? axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
583586
=> math_ops.reduce_mean(input_tensor, axis: axis, keepdims: keepdims, name: name, reduction_indices: reduction_indices);

src/TensorFlowNET.Core/APIs/tf.nn.cs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -29,21 +29,8 @@ public class nn_internal
2929
public Tensor conv2d(Tensor input, Tensor filter, int[] strides, string padding, bool use_cudnn_on_gpu = true,
3030
string data_format = "NHWC", int[] dilations = null, string name = null)
3131
{
32-
var parameters = new Conv2dParams
33-
{
34-
Input = input,
35-
Filter = filter,
36-
Strides = strides,
37-
Padding = padding,
38-
UseCudnnOnGpu = use_cudnn_on_gpu,
39-
DataFormat = data_format,
40-
Name = name
41-
};
42-
43-
if (dilations != null)
44-
parameters.Dilations = dilations;
45-
46-
return gen_nn_ops.conv2d(parameters);
32+
return gen_nn_ops.conv2d(input, filter, strides, padding, use_cudnn_on_gpu,
33+
data_format: data_format, dilations: dilations, name: name);
4734
}
4835

4936
public Tensor[] ctc_greedy_decoder(Tensor inputs, Tensor sequence_length, bool merge_repeated = true, string name = null)
@@ -118,7 +105,7 @@ public Tensor embedding_lookup(Tensor @params,
118105

119106
public IActivation softmax() => new softmax();
120107
public Tensor tanh(Tensor x, string name = null)
121-
=> gen_nn_ops.tanh(x, name);
108+
=> gen_math_ops.tanh(x, name);
122109

123110
public Tensor relu(Tensor features, string name = null)
124111
=> gen_nn_ops.relu(features, name);
@@ -146,14 +133,14 @@ public Tensor in_top_k(Tensor predictions, Tensor targets, int k, string name =
146133
=> nn_ops.in_top_k(predictions, targets, k, name);
147134

148135
public Tensor[] top_k(Tensor input, int k = 1, bool sorted = true, string name = null)
149-
=> gen_nn_ops.top_kv2(input, k: k, sorted: sorted, name: name);
136+
=> gen_nn_ops.top_kv2(input, k: ops.convert_to_tensor(k), sorted: sorted, name: name);
150137

151138
public Tensor bias_add(Tensor value, IVariableV1 bias, string data_format = null, string name = null)
152139
{
153140
return tf_with(ops.name_scope(name, "BiasAdd", new { value, bias }), scope =>
154141
{
155142
name = scope;
156-
return gen_nn_ops.bias_add(value, bias, data_format: data_format, name: name);
143+
return gen_nn_ops.bias_add(value, ops.convert_to_tensor(bias), data_format: data_format, name: name);
157144
});
158145
}
159146

@@ -172,7 +159,7 @@ public Tensor l2_loss(Tensor t, string name = null)
172159
/// <returns></returns>
173160
public Tensor lrn(Tensor input, int depth_radius = 5, int bias = 1,
174161
int alpha = 1, float beta = 0.5f, string name = null)
175-
=> gen_nn_ops.local_response_normalization(input, depth_radius: depth_radius, bias: bias,
162+
=> gen_nn_ops.lrn(input, depth_radius: depth_radius, bias: bias,
176163
alpha: alpha, beta: beta, name: name);
177164

178165
public Tensor leaky_relu(Tensor features, float alpha = 0.2f, string name = null)

src/TensorFlowNET.Core/APIs/tf.reshape.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,6 @@ public Tensor reshape(Tensor tensor,
3131
public Tensor reshape(Tensor tensor,
3232
object[] shape,
3333
string name = null)
34-
=> gen_array_ops.reshape(tensor, shape, name);
34+
=> gen_array_ops.reshape(tensor, ops.convert_to_tensor(shape), name);
3535
}
3636
}

src/TensorFlowNET.Core/APIs/tf.tensor.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -46,10 +46,10 @@ public Tensor strided_slice<T>(Tensor input, T[] begin, T[] end, T[] strides = n
4646
int ellipsis_mask = 0,
4747
int new_axis_mask = 0,
4848
int shrink_axis_mask = 0,
49-
string name = null) => gen_array_ops.strided_slice(input: input,
50-
begin: begin,
51-
end: end,
52-
strides: strides,
49+
string name = null) => array_ops.strided_slice(input,
50+
begin: ops.convert_to_tensor(begin),
51+
end: ops.convert_to_tensor(end),
52+
strides: ops.convert_to_tensor(strides),
5353
begin_mask: begin_mask,
5454
end_mask: end_mask,
5555
ellipsis_mask: ellipsis_mask,

src/TensorFlowNET.Core/APIs/tf.tile.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public Tensor tile(Tensor input, Tensor multiples, string name = null)
2323
=> gen_array_ops.tile(input, multiples, name);
2424

2525
public Tensor tile(Tensor input, object[] multiples, string name = null)
26-
=> gen_array_ops.tile(input, multiples, name);
26+
=> gen_array_ops.tile(input, ops.convert_to_tensor(multiples), name);
2727

2828
public Tensor tile(Tensor input, Shape multiples, string name = null)
2929
{

src/TensorFlowNET.Core/Attributes/c_api.ops.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ public partial class c_api
5757
[DllImport(TensorFlowLibName)]
5858
public static extern int TF_OperationGetAttrValueProto(IntPtr oper, string attr_name, SafeBufferHandle output_attr_value, SafeStatusHandle status);
5959

60+
[DllImport(TensorFlowLibName)]
61+
public static extern void TF_OperationGetAttrType(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status);
62+
63+
[DllImport(TensorFlowLibName)]
64+
public static extern void TF_OperationGetAttrInt(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status);
65+
66+
[DllImport(TensorFlowLibName)]
67+
public static extern void TF_OperationGetAttrFloat(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status);
68+
69+
[DllImport(TensorFlowLibName)]
70+
public static extern void TF_OperationGetAttrBool(IntPtr oper, string attr_name, IntPtr value, SafeStatusHandle status);
71+
72+
[DllImport(TensorFlowLibName)]
73+
public static extern void TF_OperationGetAttrShape(IntPtr oper, string attr_name, long[] value, int num_dims, SafeStatusHandle status);
74+
6075
[DllImport(TensorFlowLibName)]
6176
public static extern void TF_SetAttrBool(IntPtr desc, string attr_name, bool value);
6277

src/TensorFlowNET.Core/Clustering/_InitializeClustersOpFactory.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ private Tensor _initialize()
8888

8989
public Tensor op()
9090
{
91-
var x = control_flow_ops.cond(gen_math_ops.equal(_num_remaining, 0),
91+
var x = control_flow_ops.cond(gen_math_ops.equal(_num_remaining, ops.convert_to_tensor(0)),
9292
() =>
9393
{
9494
return check_ops.assert_equal(_cluster_centers_initialized, true);

0 commit comments

Comments
 (0)