Skip to content

Commit a11cb71

Browse files
authored
Merge branch 'master' into ones_like-fix
2 parents bee3a10 + 26a04bd commit a11cb71

File tree

28 files changed

+272
-198
lines changed

28 files changed

+272
-198
lines changed

docs/RELEASE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,25 @@
44

55
This release contains contributions from many people at SciSharp as well as the external contributors.
66

7+
**Release Date 02/06/2021**
8+
9+
### TensorFlow.Binding v0.33.0
10+
11+
* Improve memory usage
12+
* Fix minor bugs
13+
14+
### TensorFlow.Keras v0.4.0
15+
16+
* Add Subtract layer
17+
18+
* Add model.load_weights and model.save_weights
19+
20+
* Fix memory leak issue
21+
22+
* Support to build YOLOv3 object detection model
23+
24+
25+
726
**Release Date 01/09/2021**
827

928
### TensorFlow.Binding v0.32.0

src/TensorFlowNET.Console/MemoryBasicTest.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,31 @@ public Action<int, int> VariableRead
5656
{
5757
var nd = np.zeros(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3);
5858
ResourceVariable variable = tf.Variable(nd);
59-
var nd2 = np.arange(1 * 256 * 256 * 3).astype(np.float32).reshape(1, 256, 256, 3);
60-
variable.assign(nd2);
6159

62-
for (int i = 0; i< 100; i++)
60+
for (int i = 0; i< 10; i++)
6361
{
6462
var v = variable.numpy();
6563
}
6664
};
6765

66+
public Action<int, int> VariableAssign
67+
=> (epoch, iterate) =>
68+
{
69+
ResourceVariable variable = tf.Variable(3112f);
70+
AssignVariable(variable);
71+
for (int i = 0; i < 100; i++)
72+
{
73+
var v = variable.numpy();
74+
if ((float)v != 1984f)
75+
throw new ValueError("");
76+
}
77+
};
78+
79+
void AssignVariable(IVariableV1 v)
80+
{
81+
using var tensor = tf.constant(1984f);
82+
v.assign(tensor);
83+
}
6884

6985
public Action<int, int> MathAdd
7086
=> (epoch, iterate) =>

src/TensorFlowNET.Console/Program.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,10 @@ static void BasicTest(MemoryMonitor mm)
5252
// 100K float variable.
5353
mm.Execute(10, batchSize, basic.Variable);
5454

55+
mm.Execute(10, batchSize, basic.VariableRead);
56+
57+
mm.Execute(10, batchSize, basic.VariableAssign);
58+
5559
// 1 million math.
5660
mm.Execute(10, 100 * batchSize, basic.MathAdd);
5761

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,9 @@ public Tensor stack(object values, int axis = 0, string name = "stack")
215215
public Tensor ones_like(Tensor tensor, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true)
216216
=> array_ops.ones_like(tensor, dtype: dtype, name: name, optimize: optimize);
217217

218+
public Tensor ones_like(NDArray nd, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true)
219+
=> array_ops.ones_like(nd, dtype: dtype, name: name, optimize: optimize);
220+
218221
public Tensor one_hot(Tensor indices, int depth,
219222
Tensor on_value = null,
220223
Tensor off_value = null,
@@ -290,6 +293,9 @@ public Tensor[] unstack(Tensor value, int? num = null, int axis = 0, string name
290293
public Tensor zeros_like(Tensor tensor, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true)
291294
=> array_ops.zeros_like(tensor, dtype: dtype, name: name, optimize: optimize);
292295

296+
public Tensor zeros_like(NDArray nd, TF_DataType dtype = TF_DataType.DtInvalid, string name = null, bool optimize = true)
297+
=> array_ops.zeros_like(nd, dtype: dtype, name: name, optimize: optimize);
298+
293299
/// <summary>
294300
/// Stops gradient computation.
295301
/// </summary>

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,9 @@ public Tensor sinh(Tensor x, string name = null)
118118
public Tensor cos(Tensor x, string name = null)
119119
=> gen_math_ops.cos(x, name);
120120

121+
public Tensor cos(float x, string name = null)
122+
=> gen_math_ops.cos(x, name);
123+
121124
/// <summary>
122125
/// Computes hyperbolic cosine of x element-wise.
123126
/// </summary>

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,8 @@ public static int len(object a)
137137
{
138138
switch (a)
139139
{
140+
case Tensors arr:
141+
return arr.Length;
140142
case Array arr:
141143
return arr.Length;
142144
case IList arr:

src/TensorFlowNET.Core/Contexts/Context.AutoMode.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ namespace Tensorflow.Contexts
2828
/// </summary>
2929
public sealed partial class Context
3030
{
31+
// [DebuggerStepThrough]
3132
public T RunInAutoMode<T>(Func<T> graphAction, Func<T> eagerAction, params object[] args)
3233
{
3334
if (tf.Context.has_graph_arg(args))

src/TensorFlowNET.Core/Operations/array_ops.cs

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -400,9 +400,22 @@ public static Tensor reshape(Tensor tensor, TensorShape shape, string name = nul
400400
public static Tensor reshape(Tensor tensor, object[] shape, string name = null)
401401
=> gen_array_ops.reshape(tensor, shape, name: name);
402402

403+
private static Tensor ones_like_impl<T>(T tensor, TF_DataType dtype, string name, bool optimize = true)
404+
{
405+
return tf_with(ops.name_scope(name, "ones_like", new { tensor }), scope =>
406+
{
407+
name = scope;
408+
var tensor1 = ops.convert_to_tensor(tensor, name: "tensor");
409+
var ones_shape = shape_internal(tensor1, optimize: optimize);
410+
if (dtype == TF_DataType.DtInvalid)
411+
dtype = tensor1.dtype;
412+
var ret = ones(ones_shape, dtype: dtype, name: name);
413+
return ret;
414+
});
415+
}
416+
403417
public static Tensor ones(Tensor shape, TF_DataType dtype = TF_DataType.TF_FLOAT, string name = null)
404418
{
405-
dtype = dtype.as_base_dtype();
406419
return tf_with(ops.name_scope(name, "ones", new { shape }), scope =>
407420
{
408421
name = scope;
@@ -585,11 +598,10 @@ public static Tensor shape_internal(Tensor input, string name = null, bool optim
585598

586599
if (!tf.Context.executing_eagerly())
587600
{
588-
var input_tensor = ops.convert_to_tensor(input);
589-
var input_shape = input_tensor.TensorShape;
590-
if (optimize && input_tensor.NDims > -1 && input_shape.is_fully_defined())
601+
var input_shape = input.TensorShape;
602+
if (optimize && input.NDims > -1 && input_shape.is_fully_defined())
591603
{
592-
var nd = np.array(input_tensor.shape).astype(out_type.as_numpy_dtype());
604+
var nd = np.array(input.shape).astype(out_type.as_numpy_dtype());
593605
return constant_op.constant(nd, name: name);
594606
}
595607
}

src/TensorFlowNET.Core/Operations/gen_math_ops.cs

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,9 @@ public static Tensor div_no_nan(Tensor x, Tensor y, string name = null)
124124
x, y).FirstOrDefault(),
125125
x, y);
126126

127+
public static Tensor mean(Tensor input, int axis, bool keep_dims = false, string name = null)
128+
=> mean(input, ops.convert_to_tensor(axis), keep_dims: keep_dims, name: name);
129+
127130
/// <summary>
128131
/// Computes the mean of elements across dimensions of a tensor.
129132
/// Reduces `input` along the dimensions given in `axis`. Unless
@@ -137,23 +140,30 @@ public static Tensor div_no_nan(Tensor x, Tensor y, string name = null)
137140
/// <param name="keep_dims"> An optional `bool`. Defaults to `False`. If true, retain reduced dimensions with length 1.</param>
138141
/// <param name="name"> A name for the operation (optional).</param>
139142
/// <returns> A `Tensor`. Has the same type as `input`.</returns>
140-
public static Tensor mean<T1, T2>(T1 input, T2 axis, bool keep_dims = false, string name = null)
141-
{
142-
if (tf.Context.executing_eagerly())
143-
{
144-
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
143+
public static Tensor mean(Tensor input, Tensor axis, bool keep_dims = false, string name = null)
144+
=> tf.Context.RunInAutoMode2(
145+
() => tf.OpDefLib._apply_op_helper("Mean", name, new
146+
{
147+
input,
148+
reduction_indices = axis,
149+
keep_dims = keep_dims
150+
}).output,
151+
() => tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
145152
"Mean", name,
146153
null,
147154
input, axis,
148-
"keep_dims", keep_dims);
149-
150-
return results[0];
151-
}
152-
153-
var _op = tf.OpDefLib._apply_op_helper("Mean", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims });
154-
155-
return _op.output;
156-
}
155+
"keep_dims", keep_dims).FirstOrDefault(),
156+
(op) =>
157+
{
158+
var attrs = new object[]
159+
{
160+
"T", op.get_attr<TF_DataType>("T"),
161+
"Tidx", op.get_attr<TF_DataType>("Tidx"),
162+
"keep_dims", op.get_attr<bool>("keep_dims")
163+
};
164+
tf.Runner.RecordGradient("Mean", op.inputs, attrs, op.outputs);
165+
},
166+
new Tensors(input, axis));
157167

158168
public static Tensor mean(Tensor[] inputs, Tensor axis, bool keep_dims = false, string name = null)
159169
{
@@ -376,8 +386,18 @@ public static Tensor sinh(Tensor x, string name = null)
376386
return _op.outputs[0];
377387
}
378388

379-
public static Tensor cos(Tensor x, string name = null)
389+
public static Tensor cos<T>(T x, string name = null)
380390
{
391+
if (tf.executing_eagerly())
392+
{
393+
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
394+
"Cos", name,
395+
null,
396+
x);
397+
398+
return results[0];
399+
}
400+
381401
var _op = tf.OpDefLib._apply_op_helper("Cos", name, args: new { x });
382402

383403
return _op.outputs[0];
@@ -776,20 +796,21 @@ public static Tensor sqrt(Tensor x, string name = null)
776796
}
777797

778798
public static Tensor sub(Tensor x, Tensor y, string name = null)
779-
{
780-
if (tf.Context.executing_eagerly())
781-
{
782-
var results = tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
799+
=> tf.Context.RunInAutoMode2(
800+
() => tf.OpDefLib._apply_op_helper("Sub", name, new { x, y }).output,
801+
() => tf.Runner.TFE_FastPathExecute(tf.Context, tf.Context.DeviceName,
783802
"Sub", name,
784803
null,
785-
x, y);
786-
return results[0];
787-
}
788-
789-
var _op = tf.OpDefLib._apply_op_helper("Sub", name, args: new { x, y });
790-
791-
return _op.output;
792-
}
804+
x, y).FirstOrDefault(),
805+
(op) =>
806+
{
807+
var attrs = new object[]
808+
{
809+
"T", op.get_attr<TF_DataType>("T")
810+
};
811+
tf.Runner.RecordGradient("Sub", op.inputs, attrs, op.outputs);
812+
},
813+
new Tensors(x, y));
793814

794815
public static Tensor sub<Tx, Ty>(Tx x, Ty y, string name = null)
795816
{

src/TensorFlowNET.Core/Operations/math_ops.cs

Lines changed: 7 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -327,31 +327,17 @@ public static Tensor real(Tensor input, string name = null)
327327
public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool keepdims = false, string name = null, int? reduction_indices = null)
328328
{
329329
var r = _ReductionDims(input_tensor, axis);
330-
if (axis == null)
331-
{
332-
var m = gen_math_ops.mean(input_tensor, r, keepdims, name);
333-
return _may_reduce_to_scalar(keepdims, axis, m);
334-
}
335-
else
336-
{
337-
var m = gen_math_ops.mean(input_tensor, axis, keepdims, name);
338-
return _may_reduce_to_scalar(keepdims, axis, m);
339-
}
330+
var axis_tensor = axis == null ? r : ops.convert_to_tensor(axis);
331+
var m = gen_math_ops.mean(input_tensor, axis_tensor, keepdims, name);
332+
return _may_reduce_to_scalar(keepdims, axis_tensor, m);
340333
}
341334

342335
public static Tensor reduce_mean(Tensor[] input_tensors, int? axis = null, bool keepdims = false, string name = null)
343336
{
344-
if (axis == null)
345-
{
346-
var r = _ReductionDims(input_tensors, axis);
347-
var m = gen_math_ops.mean(input_tensors, r, keepdims, name);
348-
return _may_reduce_to_scalar(keepdims, axis, m);
349-
}
350-
else
351-
{
352-
var m = gen_math_ops.mean(input_tensors, axis, keepdims, name);
353-
return _may_reduce_to_scalar(keepdims, axis, m);
354-
}
337+
var r = _ReductionDims(input_tensors, axis);
338+
var axis_tensor = axis == null ? r : ops.convert_to_tensor(axis.Value);
339+
var m = gen_math_ops.mean(input_tensors, axis_tensor, keepdims, name);
340+
return _may_reduce_to_scalar(keepdims, axis, m);
355341
}
356342

357343
/// <summary>

0 commit comments

Comments
 (0)