Skip to content

Commit f3eb31f

Browse files
committed
Built-in numpy api.
1 parent 8048b62 commit f3eb31f

File tree

166 files changed

+2087
-1265
lines changed

Some content is hidden

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

166 files changed

+2087
-1265
lines changed

src/TensorFlowNET.Console/MemoryBasicTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NumSharp;
1+
using Tensorflow.Numpy;
22
using System;
33
using Tensorflow.Keras.ArgsDefinition;
44
using Tensorflow.Keras.Engine.DataAdapters;

src/TensorFlowNET.Console/MemoryFuncGraphTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NumSharp;
1+
using Tensorflow.Numpy;
22
using System;
33
using System.Collections.Generic;
44
using System.Text;

src/TensorFlowNET.Console/MemoryKerasTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NumSharp;
1+
using Tensorflow.Numpy;
22
using System;
33
using static Tensorflow.Binding;
44
using static Tensorflow.KerasApi;

src/TensorFlowNET.Console/MemoryMonitor.cs

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
using System.Diagnostics;
33
using System.Threading;
44
using System.Threading.Tasks;
5-
using NumSharp;
5+
using Tensorflow.Numpy;
66
using static Tensorflow.Binding;
77
using static Tensorflow.KerasApi;
88

@@ -12,12 +12,17 @@ public class MemoryMonitor
1212
{
1313
public void WarmUp()
1414
{
15+
while (true)
16+
{
17+
var ones = np.ones((128, 128));
18+
}
19+
1520
TensorShape shape = (1, 32, 32, 3);
1621
np.arange(shape.size).astype(np.float32).reshape(shape.dims);
1722

1823
print($"tensorflow native version: v{tf.VERSION}");
1924
tf.Context.ensure_initialized();
20-
var a = tf.constant(np.ones(10, 10));
25+
var a = tf.constant(np.ones((10, 10)));
2126
var b = tf.Variable(a);
2227
var c = tf.Variable(b);
2328
var d = b * c;

src/TensorFlowNET.Console/Tensorflow.Console.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
</PropertyGroup>
1616

1717
<ItemGroup>
18-
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.5.0" />
18+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.6.0-rc0" />
1919
</ItemGroup>
2020

2121
<ItemGroup>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17-
using NumSharp;
17+
using Tensorflow.Numpy;
1818
using System.Collections.Generic;
1919
using System.Diagnostics;
2020
using System.Linq;

src/TensorFlowNET.Core/Binding.Util.cs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,7 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17-
using NumSharp;
18-
using NumSharp.Utilities;
17+
using Tensorflow.Numpy;
1918
using System;
2019
using System.Collections;
2120
using System.Collections.Generic;
@@ -90,12 +89,12 @@ private static string _tostring(object obj)
9089
switch (obj)
9190
{
9291
case NDArray nd:
93-
return nd.ToString(false);
94-
case Array arr:
92+
return nd.ToString();
93+
/*case Array arr:
9594
if (arr.Rank != 1 || arr.GetType().GetElementType()?.IsArray == true)
9695
arr = Arrays.Flatten(arr);
9796
var objs = toObjectArray(arr);
98-
return $"[{string.Join(", ", objs.Select(_tostring))}]";
97+
return $"[{string.Join(", ", objs.Select(_tostring))}]";*/
9998
default:
10099
return obj?.ToString() ?? "null";
101100
}
@@ -166,7 +165,7 @@ public static int len(object a)
166165
case ICollection arr:
167166
return arr.Count;
168167
case NDArray ndArray:
169-
return ndArray.ndim == 0 ? 1 : ndArray.shape[0];
168+
return ndArray.ndim == 0 ? 1 : (int)ndArray.dims[0];
170169
case IEnumerable enumerable:
171170
return enumerable.OfType<object>().Count();
172171
case TensorShape arr:
@@ -272,10 +271,11 @@ public static float time()
272271
public static IEnumerable<(T, T)> zip<T>(NDArray t1, NDArray t2)
273272
where T : unmanaged
274273
{
275-
var a = t1.AsIterator<T>();
274+
/*var a = t1.AsIterator<T>();
276275
var b = t2.AsIterator<T>();
277276
while (a.HasNext() && b.HasNext())
278-
yield return (a.MoveNext(), b.MoveNext());
277+
yield return (a.MoveNext(), b.MoveNext());*/
278+
throw new NotImplementedException("");
279279
}
280280

281281
public static IEnumerable<(T1, T2)> zip<T1, T2>(IList<T1> t1, IList<T2> t2)
@@ -296,8 +296,9 @@ public static float time()
296296
{
297297
var a = t1.AsIterator<T1>();
298298
var b = t2.AsIterator<T2>();
299-
while (a.HasNext() && b.HasNext())
300-
yield return (a.MoveNext(), b.MoveNext());
299+
//while (a.HasNext() && b.HasNext())
300+
//yield return (a.MoveNext(), b.MoveNext());
301+
throw new NotImplementedException("");
301302
}
302303

303304
public static IEnumerable<(T1, T2)> zip<T1, T2>(IEnumerable<T1> e1, IEnumerable<T2> e2)

src/TensorFlowNET.Core/Buffers/Buffer.cs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ You may obtain a copy of the License at
1414
limitations under the License.
1515
******************************************************************************/
1616

17-
using NumSharp.Backends.Unmanaged;
1817
using System;
18+
using System.IO;
1919
using System.Runtime.CompilerServices;
2020
using Tensorflow.Util;
2121
using static Tensorflow.c_api;
@@ -43,12 +43,12 @@ private unsafe ref readonly TF_Buffer DangerousBuffer
4343
///
4444
/// <inheritdoc cref="SafeHandleLease" path="/devdoc/usage"/>
4545
/// </remarks>
46-
public unsafe UnmanagedMemoryBlock<byte> DangerousMemoryBlock
46+
public unsafe MemoryStream DangerousMemoryBlock
4747
{
4848
get
4949
{
5050
ref readonly TF_Buffer buffer = ref DangerousBuffer;
51-
return new UnmanagedMemoryBlock<byte>((byte*)buffer.data.ToPointer(), (long)buffer.length);
51+
return new MemoryStream(ToArray());
5252
}
5353
}
5454

@@ -90,17 +90,19 @@ private static SafeBufferHandle _toBuffer(byte[] data)
9090
/// <summary>
9191
/// Copies this buffer's contents onto a <see cref="byte"/> array.
9292
/// </summary>
93-
public byte[] ToArray()
93+
public unsafe byte[] ToArray()
9494
{
9595
using (Handle.Lease())
9696
{
97-
var block = DangerousMemoryBlock;
98-
var len = block.Count;
99-
if (len == 0)
100-
return Array.Empty<byte>();
97+
ref readonly TF_Buffer buffer = ref DangerousBuffer;
98+
99+
if (buffer.length == 0)
100+
return new byte[0];
101+
102+
var data = new byte[DangerousBuffer.length];
103+
fixed (byte* dst = data)
104+
System.Buffer.MemoryCopy(buffer.data.ToPointer(), dst, buffer.length, buffer.length);
101105

102-
var data = new byte[len];
103-
block.CopyTo(data, 0);
104106
return data;
105107
}
106108
}

src/TensorFlowNET.Core/Data/DataSetBase.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NumSharp;
1+
using Tensorflow.Numpy;
22

33
namespace Tensorflow
44
{

src/TensorFlowNET.Core/Data/DatasetManager.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using NumSharp;
1+
using Tensorflow.Numpy;
22
using System.Collections.Generic;
33
using Tensorflow.Data;
44

0 commit comments

Comments
 (0)