From 1478a2c2dc9fcb73639959c9c0846533b1599e1c Mon Sep 17 00:00:00 2001 From: Oceania2018 Date: Sun, 25 Jul 2021 23:19:30 -0500 Subject: [PATCH] IEnumerable --- src/TensorFlowNET.Core/Binding.Util.cs | 4 +- src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs | 2 + .../NumPy/Numpy.Manipulation.cs | 3 + src/TensorFlowNET.Core/NumPy/Numpy.Math.cs | 3 + src/TensorFlowNET.Core/Numpy/NDArray.cs | 21 +- .../Numpy/NDIterator.Generic.cs | 47 --- src/TensorFlowNET.Core/Numpy/NDIterator.cs | 24 -- src/TensorFlowNET.Core/Numpy/Shape.cs | 6 + .../Operations/array_ops.cs | 2 +- .../Basics/SessionTest.cs | 9 +- .../NumPy/Array.Indexing.Test.cs | 15 + .../Utilities/FluentExtension.cs | 295 ------------------ 12 files changed, 49 insertions(+), 382 deletions(-) delete mode 100644 src/TensorFlowNET.Core/Numpy/NDIterator.Generic.cs delete mode 100644 src/TensorFlowNET.Core/Numpy/NDIterator.cs diff --git a/src/TensorFlowNET.Core/Binding.Util.cs b/src/TensorFlowNET.Core/Binding.Util.cs index f5fbd4e6..c8379565 100644 --- a/src/TensorFlowNET.Core/Binding.Util.cs +++ b/src/TensorFlowNET.Core/Binding.Util.cs @@ -299,8 +299,8 @@ namespace Tensorflow where T1 : unmanaged where T2 : unmanaged { - var a = t1.AsIterator(); - var b = t2.AsIterator(); + //var a = t1.AsIterator(); + //var b = t2.AsIterator(); //while (a.HasNext() && b.HasNext()) //yield return (a.MoveNext(), b.MoveNext()); throw new NotImplementedException(""); diff --git a/src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs b/src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs index 64ef15bb..65417731 100644 --- a/src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs +++ b/src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs @@ -9,6 +9,8 @@ namespace Tensorflow.NumPy { public partial class np { + [AutoNumPy] + public static NDArray any(NDArray a, Axis axis = null) => throw new NotImplementedException(""); [AutoNumPy] public static NDArray logical_or(NDArray x1, NDArray x2) => new NDArray(tf.logical_or(x1, x2)); diff --git a/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs b/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs index e75343db..a1a3d79b 100644 --- a/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs +++ b/src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs @@ -17,6 +17,9 @@ namespace Tensorflow.NumPy [AutoNumPy] public static NDArray squeeze(NDArray x1, Axis? axis = null) => new NDArray(array_ops.squeeze(x1, axis)); + [AutoNumPy] + public static NDArray stack(NDArray arrays, Axis axis = null) => new NDArray(array_ops.stack(arrays, axis ?? 0)); + [AutoNumPy] public static NDArray dstack(params NDArray[] tup) => throw new NotImplementedException(""); } diff --git a/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs b/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs index d82ab15f..9d78137f 100644 --- a/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs +++ b/src/TensorFlowNET.Core/NumPy/Numpy.Math.cs @@ -12,6 +12,9 @@ namespace Tensorflow.NumPy [AutoNumPy] public static NDArray exp(NDArray x) => new NDArray(tf.exp(x)); + [AutoNumPy] + public static NDArray floor(NDArray x) => new NDArray(tf.floor(x)); + [AutoNumPy] public static NDArray log(NDArray x) => new NDArray(tf.log(x)); diff --git a/src/TensorFlowNET.Core/Numpy/NDArray.cs b/src/TensorFlowNET.Core/Numpy/NDArray.cs index 1adf7c0f..dfc67e0d 100644 --- a/src/TensorFlowNET.Core/Numpy/NDArray.cs +++ b/src/TensorFlowNET.Core/Numpy/NDArray.cs @@ -15,29 +15,21 @@ ******************************************************************************/ using System; +using System.Collections; using System.Collections.Generic; using System.Linq; using System.Text; -using Tensorflow.Eager; using static Tensorflow.Binding; namespace Tensorflow.NumPy { - public partial class NDArray : Tensor + public partial class NDArray : Tensor, IEnumerable { public IntPtr data => TensorDataPointer; - public NDArray[] GetNDArrays() - => throw new NotImplementedException(""); - public ValueType GetValue(params int[] indices) => throw new NotImplementedException(""); - public NDIterator AsIterator(bool autoreset = false) where T : unmanaged - => throw new NotImplementedException(""); - - public bool HasNext() => throw new NotImplementedException(""); - public T MoveNext() => throw new NotImplementedException(""); [AutoNumPy] public NDArray reshape(Shape newshape) => new NDArray(tf.reshape(this, newshape)); public NDArray astype(TF_DataType dtype) => new NDArray(math_ops.cast(this, dtype)); @@ -46,5 +38,14 @@ namespace Tensorflow.NumPy public Array ToMuliDimArray() => throw new NotImplementedException(""); public byte[] ToByteArray() => BufferToArray(); public override string ToString() => NDArrayRender.ToString(this); + + public IEnumerator GetEnumerator() + { + for (int i = 0; i < dims[0]; i++) + yield return this[i]; + } + + IEnumerator IEnumerable.GetEnumerator() + => GetEnumerator(); } } diff --git a/src/TensorFlowNET.Core/Numpy/NDIterator.Generic.cs b/src/TensorFlowNET.Core/Numpy/NDIterator.Generic.cs deleted file mode 100644 index 2f1a5ff6..00000000 --- a/src/TensorFlowNET.Core/Numpy/NDIterator.Generic.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow.NumPy -{ - public partial class NDIterator : NDIterator, IEnumerable, IDisposable where TOut : unmanaged - { - public IMemoryBlock Block => throw new NotImplementedException(); - - public IteratorType Type => throw new NotImplementedException(); - - public Shape Shape => throw new NotImplementedException(); - - public Shape BroadcastedShape => throw new NotImplementedException(); - - public bool AutoReset => throw new NotImplementedException(); - - public Func HasNext => throw new NotImplementedException(); - - public Action Reset => throw new NotImplementedException(); - - public void Dispose() - { - throw new NotImplementedException(); - } - - public IEnumerator GetEnumerator() - { - throw new NotImplementedException(); - } - - public Func MoveNext() where T : unmanaged - => throw new NotImplementedException(); - - public MoveNextReferencedDelegate MoveNextReference() where T : unmanaged - { - throw new NotImplementedException(); - } - - IEnumerator IEnumerable.GetEnumerator() - { - throw new NotImplementedException(); - } - } -} diff --git a/src/TensorFlowNET.Core/Numpy/NDIterator.cs b/src/TensorFlowNET.Core/Numpy/NDIterator.cs deleted file mode 100644 index 36862e30..00000000 --- a/src/TensorFlowNET.Core/Numpy/NDIterator.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Collections; -using System.Collections.Generic; -using System.Text; - -namespace Tensorflow.NumPy -{ - public delegate ref T MoveNextReferencedDelegate() where T : unmanaged; - - public interface NDIterator : IEnumerable - { - IMemoryBlock Block { get; } - IteratorType Type { get; } - Shape Shape { get; } //TODO! is there a performance difference if this shape is readonly or not? - Shape? BroadcastedShape { get; } - bool AutoReset { get; } - - Func MoveNext() where T : unmanaged; - MoveNextReferencedDelegate MoveNextReference() where T : unmanaged; - - Func HasNext { get; } - Action Reset { get; } - } -} diff --git a/src/TensorFlowNET.Core/Numpy/Shape.cs b/src/TensorFlowNET.Core/Numpy/Shape.cs index 263550e3..dd2981e7 100644 --- a/src/TensorFlowNET.Core/Numpy/Shape.cs +++ b/src/TensorFlowNET.Core/Numpy/Shape.cs @@ -96,6 +96,12 @@ namespace Tensorflow public static implicit operator Shape((long, long, long, long) dims) => new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4); + public static implicit operator Shape((int, int, int, int, int) dims) + => new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4, dims.Item5); + + public static implicit operator Shape((long, long, long, long, long) dims) + => new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4, dims.Item5); + public static implicit operator int[](Shape shape) => shape.dims.Select(x => (int)x).ToArray(); diff --git a/src/TensorFlowNET.Core/Operations/array_ops.cs b/src/TensorFlowNET.Core/Operations/array_ops.cs index 26bbd042..e821dfb0 100644 --- a/src/TensorFlowNET.Core/Operations/array_ops.cs +++ b/src/TensorFlowNET.Core/Operations/array_ops.cs @@ -1008,7 +1008,7 @@ namespace Tensorflow !(paddings_constant is null)) { var new_shape = new List(); - foreach ((NDArray padding, int dim) in zip(paddings_constant.GetNDArrays(), np.array(input_shape.dims).GetNDArrays())) + foreach ((NDArray padding, int dim) in zip(paddings_constant, input_shape.as_int_list())) { if (padding is null || dim == -1 || padding.ToArray().Contains(-1)) new_shape.Add(-1); diff --git a/test/TensorFlowNET.UnitTest/Basics/SessionTest.cs b/test/TensorFlowNET.UnitTest/Basics/SessionTest.cs index ade47aae..40047b2b 100644 --- a/test/TensorFlowNET.UnitTest/Basics/SessionTest.cs +++ b/test/TensorFlowNET.UnitTest/Basics/SessionTest.cs @@ -82,7 +82,8 @@ namespace TensorFlowNET.UnitTest sess.run(tf.global_variables_initializer()); var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6))); - ret.Should().BeShaped(2, 3).And.BeOfValues(1, 2, 3, 4, 5, 6); + Assert.AreEqual(ret.shape, (2, 3)); + Assert.AreEqual(ret, new[] { 1, 2, 3, 4, 5, 6 }); print(ret.dtype); print(ret); } @@ -110,7 +111,8 @@ namespace TensorFlowNET.UnitTest sess.run(tf.global_variables_initializer()); var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6).astype(np.float32) + 0.1f)); - ret.Should().BeShaped(2, 3).And.BeOfValues(1, 2, 3, 4, 5, 6); + Assert.AreEqual(ret.shape, (2, 3)); + Assert.AreEqual(ret, new[] { 1, 2, 3, 4, 5, 6 }); print(ret.dtype); print(ret); } @@ -124,7 +126,8 @@ namespace TensorFlowNET.UnitTest sess.run(tf.global_variables_initializer()); var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6).astype(np.float32) + 0.1f)); - ret.Should().BeShaped(2, 3).And.BeOfValues(1, 2, 3, 4, 5, 6); + Assert.AreEqual(ret.shape, (2, 3)); + Assert.AreEqual(ret, new[] { 1, 2, 3, 4, 5, 6 }); print(ret.dtype); print(ret); } diff --git a/test/TensorFlowNET.UnitTest/NumPy/Array.Indexing.Test.cs b/test/TensorFlowNET.UnitTest/NumPy/Array.Indexing.Test.cs index b46b4872..7c1a6d15 100644 --- a/test/TensorFlowNET.UnitTest/NumPy/Array.Indexing.Test.cs +++ b/test/TensorFlowNET.UnitTest/NumPy/Array.Indexing.Test.cs @@ -101,5 +101,20 @@ namespace TensorFlowNET.UnitTest.NumPy var shape3 = ShapeHelper.GetShape(x.shape, Slice.All, new Slice(0, isIndex: true)); Assert.AreEqual(shape3, (4, 3, 2)); } + + [TestMethod] + public void iterating() + { + var array = np.array(new[,] { { 0, 3 }, { 2, 2 }, { 3, 1 } }); + int i = 0; + foreach(var x in array) + { + if (i == 0) + Assert.AreEqual(x, new[] { 0, 3 }); + else + Assert.AreEqual(x, array[i]); + i++; + } + } } } diff --git a/test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs b/test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs index 3ac4a90a..49d32261 100644 --- a/test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs +++ b/test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs @@ -232,301 +232,6 @@ namespace TensorFlowNET.UnitTest return new AndConstraint(this); } - public AndConstraint BeOfValues(params object[] values) - { - if (values == null) - throw new ArgumentNullException(nameof(values)); - - Subject.size.Should().Be((ulong)values.Length, "the method BeOfValues also confirms the sizes are matching with given values."); - -#if _REGEN - #region Compute - switch (Subject.typecode) - { - %foreach supported_dtypes,supported_dtypes_lowercase% - case NPTypeCode.#1: - { - var iter = Subject.AsIterator<#2>(); - var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.To#1(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: #1).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - } - break; - } - % - default: - throw new NotSupportedException(); - } - #endregion -#else - - #region Compute - - switch (Subject.dtype) - { - case TF_DataType.TF_BOOL: - { - var iter = Subject.AsIterator(); - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToBoolean(values[i]); - /*var nextval = iter.MoveNext(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Boolean).\n------- Subject -------\n{Subject}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i);*/ - } - - break; - } - - case TF_DataType.TF_INT8: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToByte(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Byte).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_INT16: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToInt16(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Int16).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_UINT16: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToUInt16(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: UInt16).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_INT32: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToInt32(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Int32).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_UINT32: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToUInt32(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: UInt32).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_INT64: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToInt64(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Int64).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_UINT64: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToUInt64(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: UInt64).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_UINT8: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToChar(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Char).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_DOUBLE: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToDouble(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Double).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - - case TF_DataType.TF_FLOAT: - { - var iter = Subject.AsIterator(); - /*var next = iter.MoveNext; - var hasnext = iter.HasNext; - for (int i = 0; i < values.Length; i++) - { - Execute.Assertion - .ForCondition(hasnext()) - .FailWith($"Expected the NDArray to have atleast {values.Length} but in fact it has size of {i}."); - - var expected = Convert.ToSingle(values[i]); - var nextval = next(); - - Execute.Assertion - .ForCondition(expected == nextval) - .FailWith($"Expected NDArray's {{2}}th value to be {{0}}, but found {{1}} (dtype: Single).\n------- Subject -------\n{Subject.ToString(false)}\n------- Expected -------\n[{string.Join(", ", values.Select(v => v.ToString()))}]", expected, nextval, i); - }*/ - - break; - } - default: - throw new NotSupportedException(); - } - - #endregion - -#endif - - - return new AndConstraint(this); - } - public AndConstraint AllValuesBe(object val) {