Skip to content

Commit da54251

Browse files
committed
add float and double Equal.
1 parent c83f355 commit da54251

10 files changed

Lines changed: 72 additions & 25 deletions

File tree

‎TensorFlow.NET.sln‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,8 @@ Global
2727
GlobalSection(ProjectConfigurationPlatforms) = postSolution
2828
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
2929
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU
30-
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.ActiveCfg = Debug|Any CPU
31-
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.Build.0 = Debug|Any CPU
30+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.ActiveCfg = Debug|x64
31+
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|x64.Build.0 = Debug|x64
3232
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug-Minimal|Any CPU.ActiveCfg = Debug|Any CPU
3333
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug-Minimal|Any CPU.Build.0 = Debug|Any CPU
3434
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug-Minimal|x64.ActiveCfg = Debug|Any CPU

‎src/TensorFlowNET.Core/Eager/EagerTensor.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,14 @@ public EagerTensor(int value, string device_name) : base(value)
1919
{
2020
}
2121

22+
public EagerTensor(float[] value, string device_name) : base(value)
23+
{
24+
}
25+
26+
public EagerTensor(double[] value, string device_name) : base(value)
27+
{
28+
}
29+
2230
public EagerTensor(NDArray value, string device_name) : base(value)
2331
{
2432
}

‎src/TensorFlowNET.Core/TensorFlow.Binding.csproj‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ https://tensorflownet.readthedocs.io</Description>
3737
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|AnyCPU'">
3838
<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
3939
<DefineConstants>TRACE;DEBUG;SERIALIZABLE_</DefineConstants>
40-
<PlatformTarget>AnyCPU</PlatformTarget>
40+
<PlatformTarget>x64</PlatformTarget>
4141
</PropertyGroup>
4242

4343
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">

‎src/TensorFlowNET.Core/Tensors/Tensor.Creation.cs‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public Tensor(IntPtr data_ptr, long[] shape, TF_DataType dType, int num_bytes)
8383
{
8484
unsafe
8585
{
86-
_handle = TF_NewTensor(dType, dims: shape, num_dims: shape.Length, data: data_ptr, len: (UIntPtr) num_bytes);
86+
_handle = TF_NewTensor(dType, dims: shape, num_dims: shape.Length, data: data_ptr, len: (ulong)num_bytes);
8787
AllocationType = TF_TensorData(_handle) == data_ptr ? AllocationType.FromPointer : AllocationType.Tensorflow;
8888
}
8989
}
@@ -99,7 +99,7 @@ public Tensor(IntPtr data_ptr, long[] shape, TF_DataType dType, int num_bytes)
9999
/// <param name="num_bytes">Size of the tensor in memory</param>
100100
public unsafe Tensor(void* data_ptr, long[] shape, TF_DataType dType, int num_bytes)
101101
{
102-
_handle = TF_NewTensor(dType, dims: shape, num_dims: shape.Length, data: data_ptr, len: (UIntPtr) num_bytes);
102+
_handle = TF_NewTensor(dType, dims: shape, num_dims: shape.Length, data: data_ptr, len: (ulong) num_bytes);
103103
AllocationType = TF_TensorData(_handle).ToPointer() == data_ptr ? AllocationType.FromPointer : AllocationType.Tensorflow;
104104
}
105105

@@ -372,9 +372,9 @@ public unsafe Tensor(ulong value, TF_DataType? dType = null)
372372
/// <summary>
373373
/// Create a 1d Tensor from the given linear array and shape
374374
/// </summary>
375-
public Tensor(float[] data, TF_DataType? dType = null)
375+
public Tensor(float[] data)
376376
{
377-
_handle = CreateTensorFromArray(dType ?? dtypes.as_dtype(typeof(float)), new long[] {data.Length}, data, sizeof(float));
377+
_handle = CreateTensorFromArray(TF_DataType.TF_FLOAT, new long[] { data.Length }, data, sizeof(float));
378378
}
379379

380380
/// <summary>
@@ -526,7 +526,7 @@ private unsafe IntPtr CreateTensorFromNDArray(NDArray nd, TF_DataType? given_dty
526526
dims: nd.shape.Select(i => (long) i).ToArray(),
527527
num_dims: nd.ndim,
528528
data: arraySlice.Address,
529-
len: (UIntPtr) (nd.size * nd.dtypesize));
529+
len: (ulong) (nd.size * nd.dtypesize));
530530

531531
//if TF decided not to perform copy, hold reference for given NDArray.
532532
if (TF_TensorData(handle).ToPointer() == arraySlice.Address)
@@ -647,9 +647,9 @@ protected IntPtr CreateTensorFromArray(TF_DataType dt, long[] shape, Array data,
647647
//call NewTensor
648648
IntPtr handle;
649649
if (shape == null || shape.Length == 0)
650-
handle = TF_NewTensor(dt, new long[0], 0, pinnedAddr + start * element_size, (UIntPtr) (count * element_size));
650+
handle = TF_NewTensor(dt, new long[0], 0, pinnedAddr + start * element_size, (ulong) (count * element_size));
651651
else
652-
handle = TF_NewTensor(dt, shape, shape.Length, pinnedAddr + start * element_size, (UIntPtr) (count * element_size));
652+
handle = TF_NewTensor(dt, shape, shape.Length, pinnedAddr + start * element_size, (ulong) (count * element_size));
653653

654654
//Figure if TF decided to clone or not.
655655
if (c_api.TF_TensorData(handle) == pinnedAddr)

‎src/TensorFlowNET.Core/Tensors/Tensor.Value.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,8 @@ public NDArray numpy()
165165
return ToArray<int>();
166166
case TF_DataType.TF_FLOAT:
167167
return ToArray<float>();
168+
case TF_DataType.TF_DOUBLE:
169+
return ToArray<double>();
168170
default:
169171
return BufferToArray();
170172
}

‎src/TensorFlowNET.Core/Tensors/c_api.tensor.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ public partial class c_api
9090
/// <param name="deallocator_arg"></param>
9191
/// <returns></returns>
9292
[DllImport(TensorFlowLibName)]
93-
public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, UIntPtr len, Deallocator deallocator, IntPtr deallocator_arg);
93+
public static extern IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len, Deallocator deallocator, IntPtr deallocator_arg);
9494

9595
/// <summary>
9696
/// Return a new tensor that holds the bytes data[0,len-1]
@@ -103,9 +103,9 @@ public partial class c_api
103103
/// <param name="deallocator"></param>
104104
/// <param name="deallocator_arg"></param>
105105
[MethodImpl(MethodImplOptions.AggressiveInlining)]
106-
public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, UIntPtr len)
106+
public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, IntPtr data, ulong len)
107107
{
108-
return TF_NewTensor(dataType, dims, num_dims, data, len, EmptyDeallocator, DeallocatorArgs.Empty);
108+
return c_api.TF_NewTensor(dataType, dims, num_dims, data, len, EmptyDeallocator, DeallocatorArgs.Empty);
109109
}
110110
/// <summary>
111111
/// Return a new tensor that holds the bytes data[0,len-1]
@@ -118,7 +118,7 @@ public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int
118118
/// <param name="deallocator"></param>
119119
/// <param name="deallocator_arg"></param>
120120
[MethodImpl(MethodImplOptions.AggressiveInlining)]
121-
public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, void* data, UIntPtr len)
121+
public static unsafe IntPtr TF_NewTensor(TF_DataType dataType, long[] dims, int num_dims, void* data, ulong len)
122122
{
123123
return TF_NewTensor(dataType, dims, num_dims, new IntPtr(data), len);
124124
}

‎src/TensorFlowNET.Core/Tensors/constant_op.cs‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,8 @@ private static EagerTensor convert_to_eager_tensor(object value, Context ctx, TF
9393
return new EagerTensor(int32, ctx.device_name);
9494
case float[] float32s:
9595
return new EagerTensor(float32s, ctx.device_name);
96+
case double[] double64s:
97+
return new EagerTensor(double64s, ctx.device_name);
9698
default:
9799
throw new NotImplementedException($"convert_to_eager_tensor {value.GetType()}");
98100
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace TensorFlowNET.UnitTest
6+
{
7+
public class TFNetApiTest
8+
{
9+
public bool Equal(float[] f1, float[] f2)
10+
{
11+
bool ret = false;
12+
var tolerance = .000001f;
13+
for (var i = 0; i < f1.Length; i++)
14+
{
15+
ret = Math.Abs(f1[i] - f2[i]) <= tolerance;
16+
if (!ret)
17+
break;
18+
}
19+
20+
return ret;
21+
}
22+
23+
public bool Equal(double[] d1, double[] d2)
24+
{
25+
bool ret = false;
26+
var tolerance = .000000000000001f;
27+
for (var i = 0; i < d1.Length; i++)
28+
{
29+
ret = Math.Abs(d1[i] - d2[i]) <= tolerance;
30+
if (!ret)
31+
break;
32+
}
33+
34+
return ret;
35+
}
36+
}
37+
}

‎test/TensorFlowNET.UnitTest/math_test/MathOperationTest.cs‎

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace TensorFlowNET.UnitTest.math_test
1010
{
1111
[TestClass]
12-
public class MathOperationTest
12+
public class MathOperationTest : TFNetApiTest
1313
{
1414
// A constant vector of size 6
1515
Tensor a = tf.constant(new float[] { 1.0f, -0.5f, 3.4f, -2.1f, 0.0f, -6.5f });
@@ -18,9 +18,9 @@ public class MathOperationTest
1818
public void Sin()
1919
{
2020
var b = tf.sin(a, name: "Sin");
21-
var expected = new float[] { 0.84147096f, -0.47942555f, -0.2555412f, -0.8632094f /*python output -0.86320937*/, 0f, -0.21511999f };
21+
var expected = new float[] { 0.84147096f, -0.47942555f, -0.2555412f, -0.86320937f, 0f, -0.21511999f };
2222
var actual = b.ToArray<float>();
23-
Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));
23+
Assert.IsTrue(Equal(expected, actual));
2424
}
2525

2626
[TestMethod]
@@ -29,7 +29,7 @@ public void Tan()
2929
var b = tf.tan(a, name: "Tan");
3030
var expected = new float[] { 1.5574077f, -0.5463025f, 0.264317f, 1.709847f, 0f, -0.2202772f };
3131
var actual = b.ToArray<float>();
32-
Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));
32+
Assert.IsTrue(Equal(expected, actual));
3333
}
3434
}
3535
}

‎test/TensorFlowNET.UnitTest/nn_test/ActivationFunctionTest.cs‎

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
namespace TensorFlowNET.UnitTest.nn_test
1010
{
1111
[TestClass]
12-
public class ActivationFunctionTest
12+
public class ActivationFunctionTest : TFNetApiTest
1313
{
1414
// A constant vector of size 6
1515
Tensor a = tf.constant(new float[] { 1.0f, -0.5f, 3.4f, -2.1f, 0.0f, -6.5f });
@@ -18,11 +18,9 @@ public class ActivationFunctionTest
1818
public void Sigmoid()
1919
{
2020
var b = tf.nn.sigmoid(a, name: "sigmoid");
21-
// from python
22-
// [0.7310586f, 0.37754068f, 0.9677046f, 0.10909683f, 0.5f, 0.00150118f]
23-
var expected = new float[] { 0.7310586f, 0.377540678f, 0.9677046f, 0.109096833f, 0.5f, 0.00150118221f };
21+
var expected = new float[] { 0.7310586f, 0.37754068f, 0.9677046f, 0.10909683f, 0.5f, 0.00150118f };
2422
var actual = b.ToArray<float>();
25-
Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));
23+
Assert.IsTrue(Equal(expected, actual));
2624
}
2725

2826
[TestMethod]
@@ -31,7 +29,7 @@ public void ReLU()
3129
var b = tf.nn.relu(a, name: "ReLU");
3230
var expected = new float[] { 1f, 0f, 3.4f, 0f, 0f, 0f };
3331
var actual = b.ToArray<float>();
34-
Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));
32+
Assert.IsTrue(Equal(expected, actual));
3533
}
3634

3735
[TestMethod]
@@ -40,7 +38,7 @@ public void TanH()
4038
var b = tf.nn.tanh(a, name: "TanH");
4139
var expected = new float[] { 0.7615942f, -0.46211717f, 0.9977749f , -0.970452f, 0f, -0.99999547f };
4240
var actual = b.ToArray<float>();
43-
Assert.IsTrue(Enumerable.SequenceEqual(expected, actual));
41+
Assert.IsTrue(Equal(expected, actual));
4442
}
4543
}
4644
}

0 commit comments

Comments
 (0)