Browse Source

IEnumerable<NDArray>

tags/v0.60-tf.numpy
Oceania2018 4 years ago
parent
commit
1478a2c2dc
12 changed files with 49 additions and 382 deletions
  1. +2
    -2
      src/TensorFlowNET.Core/Binding.Util.cs
  2. +2
    -0
      src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs
  3. +3
    -0
      src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs
  4. +3
    -0
      src/TensorFlowNET.Core/NumPy/Numpy.Math.cs
  5. +11
    -10
      src/TensorFlowNET.Core/Numpy/NDArray.cs
  6. +0
    -47
      src/TensorFlowNET.Core/Numpy/NDIterator.Generic.cs
  7. +0
    -24
      src/TensorFlowNET.Core/Numpy/NDIterator.cs
  8. +6
    -0
      src/TensorFlowNET.Core/Numpy/Shape.cs
  9. +1
    -1
      src/TensorFlowNET.Core/Operations/array_ops.cs
  10. +6
    -3
      test/TensorFlowNET.UnitTest/Basics/SessionTest.cs
  11. +15
    -0
      test/TensorFlowNET.UnitTest/NumPy/Array.Indexing.Test.cs
  12. +0
    -295
      test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs

+ 2
- 2
src/TensorFlowNET.Core/Binding.Util.cs View File

@@ -299,8 +299,8 @@ namespace Tensorflow
where T1 : unmanaged where T1 : unmanaged
where T2 : unmanaged where T2 : unmanaged
{ {
var a = t1.AsIterator<T1>();
var b = t2.AsIterator<T2>();
//var a = t1.AsIterator<T1>();
//var b = t2.AsIterator<T2>();
//while (a.HasNext() && b.HasNext()) //while (a.HasNext() && b.HasNext())
//yield return (a.MoveNext(), b.MoveNext()); //yield return (a.MoveNext(), b.MoveNext());
throw new NotImplementedException(""); throw new NotImplementedException("");


+ 2
- 0
src/TensorFlowNET.Core/NumPy/NumPy.Logical.cs View File

@@ -9,6 +9,8 @@ namespace Tensorflow.NumPy
{ {
public partial class np public partial class np
{ {
[AutoNumPy]
public static NDArray any(NDArray a, Axis axis = null) => throw new NotImplementedException("");
[AutoNumPy] [AutoNumPy]
public static NDArray logical_or(NDArray x1, NDArray x2) => new NDArray(tf.logical_or(x1, x2)); public static NDArray logical_or(NDArray x1, NDArray x2) => new NDArray(tf.logical_or(x1, x2));




+ 3
- 0
src/TensorFlowNET.Core/NumPy/Numpy.Manipulation.cs View File

@@ -17,6 +17,9 @@ namespace Tensorflow.NumPy
[AutoNumPy] [AutoNumPy]
public static NDArray squeeze(NDArray x1, Axis? axis = null) => new NDArray(array_ops.squeeze(x1, axis)); 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] [AutoNumPy]
public static NDArray dstack(params NDArray[] tup) => throw new NotImplementedException(""); public static NDArray dstack(params NDArray[] tup) => throw new NotImplementedException("");
} }


+ 3
- 0
src/TensorFlowNET.Core/NumPy/Numpy.Math.cs View File

@@ -12,6 +12,9 @@ namespace Tensorflow.NumPy
[AutoNumPy] [AutoNumPy]
public static NDArray exp(NDArray x) => new NDArray(tf.exp(x)); public static NDArray exp(NDArray x) => new NDArray(tf.exp(x));


[AutoNumPy]
public static NDArray floor(NDArray x) => new NDArray(tf.floor(x));

[AutoNumPy] [AutoNumPy]
public static NDArray log(NDArray x) => new NDArray(tf.log(x)); public static NDArray log(NDArray x) => new NDArray(tf.log(x));




+ 11
- 10
src/TensorFlowNET.Core/Numpy/NDArray.cs View File

@@ -15,29 +15,21 @@
******************************************************************************/ ******************************************************************************/


using System; using System;
using System.Collections;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Text; using System.Text;
using Tensorflow.Eager;
using static Tensorflow.Binding; using static Tensorflow.Binding;


namespace Tensorflow.NumPy namespace Tensorflow.NumPy
{ {
public partial class NDArray : Tensor
public partial class NDArray : Tensor, IEnumerable<NDArray>
{ {
public IntPtr data => TensorDataPointer; public IntPtr data => TensorDataPointer;


public NDArray[] GetNDArrays()
=> throw new NotImplementedException("");

public ValueType GetValue(params int[] indices) public ValueType GetValue(params int[] indices)
=> throw new NotImplementedException(""); => throw new NotImplementedException("");


public NDIterator<T> AsIterator<T>(bool autoreset = false) where T : unmanaged
=> throw new NotImplementedException("");

public bool HasNext() => throw new NotImplementedException("");
public T MoveNext<T>() => throw new NotImplementedException("");
[AutoNumPy] [AutoNumPy]
public NDArray reshape(Shape newshape) => new NDArray(tf.reshape(this, newshape)); public NDArray reshape(Shape newshape) => new NDArray(tf.reshape(this, newshape));
public NDArray astype(TF_DataType dtype) => new NDArray(math_ops.cast(this, dtype)); public NDArray astype(TF_DataType dtype) => new NDArray(math_ops.cast(this, dtype));
@@ -46,5 +38,14 @@ namespace Tensorflow.NumPy
public Array ToMuliDimArray<T>() => throw new NotImplementedException(""); public Array ToMuliDimArray<T>() => throw new NotImplementedException("");
public byte[] ToByteArray() => BufferToArray(); public byte[] ToByteArray() => BufferToArray();
public override string ToString() => NDArrayRender.ToString(this); public override string ToString() => NDArrayRender.ToString(this);

public IEnumerator<NDArray> GetEnumerator()
{
for (int i = 0; i < dims[0]; i++)
yield return this[i];
}

IEnumerator IEnumerable.GetEnumerator()
=> GetEnumerator();
} }
} }

+ 0
- 47
src/TensorFlowNET.Core/Numpy/NDIterator.Generic.cs View File

@@ -1,47 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Tensorflow.NumPy
{
public partial class NDIterator<TOut> : NDIterator, IEnumerable<TOut>, 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<bool> HasNext => throw new NotImplementedException();

public Action Reset => throw new NotImplementedException();

public void Dispose()
{
throw new NotImplementedException();
}

public IEnumerator GetEnumerator()
{
throw new NotImplementedException();
}

public Func<T> MoveNext<T>() where T : unmanaged
=> throw new NotImplementedException();

public MoveNextReferencedDelegate<T> MoveNextReference<T>() where T : unmanaged
{
throw new NotImplementedException();
}

IEnumerator<TOut> IEnumerable<TOut>.GetEnumerator()
{
throw new NotImplementedException();
}
}
}

+ 0
- 24
src/TensorFlowNET.Core/Numpy/NDIterator.cs View File

@@ -1,24 +0,0 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Text;

namespace Tensorflow.NumPy
{
public delegate ref T MoveNextReferencedDelegate<T>() 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<T> MoveNext<T>() where T : unmanaged;
MoveNextReferencedDelegate<T> MoveNextReference<T>() where T : unmanaged;

Func<bool> HasNext { get; }
Action Reset { get; }
}
}

+ 6
- 0
src/TensorFlowNET.Core/Numpy/Shape.cs View File

@@ -96,6 +96,12 @@ namespace Tensorflow
public static implicit operator Shape((long, long, long, long) dims) public static implicit operator Shape((long, long, long, long) dims)
=> new Shape(dims.Item1, dims.Item2, dims.Item3, dims.Item4); => 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) public static implicit operator int[](Shape shape)
=> shape.dims.Select(x => (int)x).ToArray(); => shape.dims.Select(x => (int)x).ToArray();




+ 1
- 1
src/TensorFlowNET.Core/Operations/array_ops.cs View File

@@ -1008,7 +1008,7 @@ namespace Tensorflow
!(paddings_constant is null)) !(paddings_constant is null))
{ {
var new_shape = new List<int>(); var new_shape = new List<int>();
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<int>().Contains(-1)) if (padding is null || dim == -1 || padding.ToArray<int>().Contains(-1))
new_shape.Add(-1); new_shape.Add(-1);


+ 6
- 3
test/TensorFlowNET.UnitTest/Basics/SessionTest.cs View File

@@ -82,7 +82,8 @@ namespace TensorFlowNET.UnitTest
sess.run(tf.global_variables_initializer()); sess.run(tf.global_variables_initializer());
var ret = sess.run(op, feed_dict: (input, np.array(1, 2, 3, 4, 5, 6))); 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.dtype);
print(ret); print(ret);
} }
@@ -110,7 +111,8 @@ namespace TensorFlowNET.UnitTest
sess.run(tf.global_variables_initializer()); 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)); 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.dtype);
print(ret); print(ret);
} }
@@ -124,7 +126,8 @@ namespace TensorFlowNET.UnitTest
sess.run(tf.global_variables_initializer()); 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)); 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.dtype);
print(ret); print(ret);
} }


+ 15
- 0
test/TensorFlowNET.UnitTest/NumPy/Array.Indexing.Test.cs View File

@@ -101,5 +101,20 @@ namespace TensorFlowNET.UnitTest.NumPy
var shape3 = ShapeHelper.GetShape(x.shape, Slice.All, new Slice(0, isIndex: true)); var shape3 = ShapeHelper.GetShape(x.shape, Slice.All, new Slice(0, isIndex: true));
Assert.AreEqual(shape3, (4, 3, 2)); 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++;
}
}
} }
} }

+ 0
- 295
test/TensorFlowNET.UnitTest/Utilities/FluentExtension.cs View File

@@ -232,301 +232,6 @@ namespace TensorFlowNET.UnitTest
return new AndConstraint<NDArrayAssertions>(this); return new AndConstraint<NDArrayAssertions>(this);
} }


public AndConstraint<NDArrayAssertions> 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<bool>();
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<byte>();
/*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<short>();
/*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<ushort>();
/*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<int>();
/*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<uint>();
/*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<long>();
/*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<ulong>();
/*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<char>();
/*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<double>();
/*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<float>();
/*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<NDArrayAssertions>(this);
}

public AndConstraint<NDArrayAssertions> AllValuesBe(object val) public AndConstraint<NDArrayAssertions> AllValuesBe(object val)
{ {




Loading…
Cancel
Save