Skip to content

Commit 2705179

Browse files
committed
implemented nn class
1 parent a2fdf69 commit 2705179

8 files changed

Lines changed: 72 additions & 33 deletions

File tree

‎TensorFlow.NET.sln‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Utility", "sr
1313
EndProject
1414
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}"
1515
EndProject
16+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{268BF0B6-0AA9-4FD3-A245-7AF336F1E3E9}"
17+
EndProject
1618
Global
1719
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1820
Debug|Any CPU = Debug|Any CPU
@@ -39,6 +41,10 @@ Global
3941
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Debug|Any CPU.Build.0 = Debug|Any CPU
4042
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.ActiveCfg = Release|Any CPU
4143
{4BB2ABD1-635E-41E4-B534-CB5B6A2D754D}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{268BF0B6-0AA9-4FD3-A245-7AF336F1E3E9}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{268BF0B6-0AA9-4FD3-A245-7AF336F1E3E9}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{268BF0B6-0AA9-4FD3-A245-7AF336F1E3E9}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{268BF0B6-0AA9-4FD3-A245-7AF336F1E3E9}.Release|Any CPU.Build.0 = Release|Any CPU
4248
EndGlobalSection
4349
GlobalSection(SolutionProperties) = preSolution
4450
HideSolutionNode = FALSE

‎src/TensorFlowNET.Core/APIs/tf.nn.cs‎

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ namespace Tensorflow
66
{
77
public static partial class tf
88
{
9-
public static nn_impl nn => new nn_impl();
9+
public static class nn
10+
{
11+
public static (Tensor, Tensor) moments(Tensor x,
12+
int[] axes,
13+
string name = null,
14+
bool keep_dims = false) => nn_impl.moments(x, axes, name: name, keep_dims: keep_dims);
15+
}
1016
}
1117
}

‎src/TensorFlowNET.Core/Operations/gen_math_ops.cs‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ public static class gen_math_ops
2222
/// <returns> A `Tensor`. Has the same type as `input`.</returns>
2323
public static Tensor mean(Tensor input, Tensor axis, bool keep_dims= false, string name = null)
2424
{
25-
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, axis });
25+
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims });
2626

2727
return _op.outputs[0];
2828
}

‎src/TensorFlowNET.Core/Operations/math_ops.py.cs‎

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool ke
3939
{
4040
var r = _ReductionDims(input_tensor, new Tensor(axis));
4141
var m = gen_math_ops.mean(input_tensor, r);
42-
return _may_reduce_to_scalar(keepdims, m);
42+
return _may_reduce_to_scalar(keepdims,axis, m);
4343
}
4444
/// <summary>
4545
/// Returns (x - y)(x - y) element-wise.
@@ -117,6 +117,12 @@ private static Tensor _may_reduce_to_scalar(bool keepdims, Tensor output)
117117
return output;
118118
}
119119

120+
private static Tensor _may_reduce_to_scalar(bool keepdims, int[] axos, Tensor output)
121+
{
122+
output.shape = new long[0];
123+
return output;
124+
}
125+
120126
private static Tensor _ReductionDims(Tensor x, Tensor axis)
121127
{
122128
if (axis != null)
@@ -130,6 +136,24 @@ private static Tensor _ReductionDims(Tensor x, Tensor axis)
130136
}
131137
}
132138

139+
private static int[] _ReductionDims(Tensor x, int[] axis)
140+
{
141+
if (axis != null)
142+
{
143+
return axis;
144+
}
145+
else
146+
{
147+
var rank = array_ops.rank(x);
148+
if (rank != null)
149+
{
150+
// return constant_op.constant();
151+
}
152+
// return range(0, rank, 1);
153+
throw new NotFiniteNumberException();
154+
}
155+
}
156+
133157
public static Tensor range(object start, object limit = null, object delta = null, TF_DataType dtype = TF_DataType.DtInvalid, string name = "range" )
134158
{
135159
if(limit == null)

‎src/TensorFlowNET.Core/Operations/nn_impl.py.cs‎

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ public class nn_impl : Python
1414
/// <param name="name"> Name used to scope the operations that compute the moments.</param>
1515
/// <param name="keep_dims"> Produce moments with the same dimensionality as the input.</param>
1616
/// <returns> Two `Tensor` objects: `mean` and `variance`.</returns>
17-
public Tuple<Tensor, Tensor> moments(Tensor x,
17+
public static (Tensor, Tensor) moments(Tensor x,
1818
int[] axes,
1919
string name = null,
2020
bool keep_dims = false)
2121
{
22-
Tuple<Tensor, Tensor> t = null;
23-
with<ops.name_scope>(new ops.name_scope(name, "moments", new { x, axes }), scope =>
22+
return with<ops.name_scope, (Tensor, Tensor)>(new ops.name_scope(name, "moments", new { x, axes }), scope =>
2423
{
2524
// The dynamic range of fp16 is too limited to support the collection of
2625
// sufficient statistics. As a workaround we simply perform the operations
@@ -40,15 +39,10 @@ public Tuple<Tensor, Tensor> moments(Tensor x,
4039
}
4140
// TODO: if x.dtype == dtypes.float16:
4241
if (x.dtype == TF_DataType.TF_FLOAT)
43-
{
44-
t = Tuple.Create(math_ops.cast(mean, x.dtype), math_ops.cast(variance, x.dtype));
45-
return;
46-
}
47-
else {
48-
t = Tuple.Create(mean, variance);
49-
}
42+
return (math_ops.cast(mean, x.dtype), math_ops.cast(variance, x.dtype));
43+
else
44+
return (mean, variance);
5045
});
51-
return t;
5246
}
5347
}
5448
}

‎src/TensorFlowNET.Core/TensorFlowNET.Core.csproj‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ Docs: https://tensorflownet.readthedocs.io</Description>
5151
<Content CopyToOutputDirectory="PreserveNewest" Include="./runtimes/win-x64/native/tensorflow.dll" Link="tensorflow.dll" Pack="true" PackagePath="runtimes/win-x64/native/tensorflow.dll" />
5252
</ItemGroup>
5353

54+
<ItemGroup>
55+
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
56+
</ItemGroup>
57+
5458
</Project>

‎test/TensorFlowNET.Examples/NaiveBayesClassifier.cs‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ public void Run()
1919
// var X = np.array<float[]>(new float[][] { new float[] { 1.0f, 1.0f}, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
2020
var X = np.array<float>(new float[][] { new float[] { 1.0f, 1.0f }, new float[] { 2.0f, 2.0f }, new float[] { -1.0f, -1.0f }, new float[] { -2.0f, -2.0f }, new float[] { 1.0f, -1.0f }, new float[] { 2.0f, -2.0f }, });
2121
var y = np.array<int>(0,0,1,1,2,2);
22-
2322
fit(X, y);
2423
// Create a regular grid and classify each point
2524

@@ -28,41 +27,48 @@ public void Run()
2827
public void fit(NDArray X, NDArray y)
2928
{
3029
NDArray unique_y = y.unique<long>();
31-
32-
Dictionary<long, List<NDArray>> dic = new Dictionary<long, List<NDArray>>();
30+
31+
Dictionary<long, List<List<float>>> dic = new Dictionary<long, List<List<float>>>();
3332
// Init uy in dic
3433
foreach (int uy in unique_y.Data<int>())
3534
{
36-
dic.Add(uy, new List<NDArray>());
35+
dic.Add(uy, new List<List<float>>());
3736
}
3837
// Separate training points by class
3938
// Shape : nb_classes * nb_samples * nb_features
4039
int maxCount = 0;
4140
for (int i = 0; i < y.size; i++)
4241
{
4342
long curClass = (long)y[i];
44-
List<NDArray> l = dic[curClass];
45-
l.Add(X[i] as NDArray);
43+
List<List<float>> l = dic[curClass];
44+
List<float> pair = new List<float>();
45+
pair.Add((float)X[i,0]);
46+
pair.Add((float)X[i, 1]);
47+
l.Add(pair);
4648
if (l.Count > maxCount)
4749
{
4850
maxCount = l.Count;
4951
}
5052
dic[curClass] = l;
5153
}
52-
NDArray points_by_class = np.zeros(new int[] { dic.Count, maxCount, X.shape[1] });
53-
foreach (KeyValuePair<long, List<NDArray>> kv in dic)
54+
float[,,] points = new float[dic.Count, maxCount, X.shape[1]];
55+
foreach (KeyValuePair<long, List<List<float>>> kv in dic)
5456
{
55-
var cls = kv.Value.ToArray();
56-
for (int i = 0; i < dic.Count; i++)
57+
int j = (int) kv.Key;
58+
for (int i = 0; i < maxCount; i++)
5759
{
58-
points_by_class[i] = dic[i];
60+
for (int k = 0; k < X.shape[1]; k++)
61+
{
62+
points[j, i, k] = kv.Value[i][k];
63+
}
5964
}
60-
}
6165

66+
}
67+
NDArray points_by_class = np.array<float>(points);
6268
// estimate mean and variance for each class / feature
6369
// shape : nb_classes * nb_features
6470
var cons = tf.constant(points_by_class);
65-
Tuple<Tensor, Tensor> tup = tf.nn.moments(cons, new int[]{1});
71+
var tup = tf.nn.moments(cons, new int[]{1});
6672
var mean = tup.Item1;
6773
var variance = tup.Item2;
6874
// Create a 3x2 univariate normal distribution with the
Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
<Project Sdk="Microsoft.NET.Sdk">
1+
<Project Sdk="Microsoft.NET.Sdk">
22

33
<PropertyGroup>
44
<OutputType>Exe</OutputType>
55
<TargetFramework>netcoreapp2.2</TargetFramework>
66
</PropertyGroup>
77

88
<ItemGroup>
9-
<PackageReference Include="NumSharp" Version="0.7.3" />
10-
<PackageReference Include="TensorFlow.NET" Version="0.3.0" />
11-
</ItemGroup>
12-
13-
<ItemGroup>
9+
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
1410
<ProjectReference Include="..\..\src\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
1511
<ProjectReference Include="..\..\src\TensorFlowNET.Utility\TensorFlowNET.Utility.csproj" />
1612
</ItemGroup>
@@ -19,6 +15,9 @@
1915
<Reference Include="Newtonsoft.Json">
2016
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
2117
</Reference>
18+
<Reference Include="NumSharp.Core">
19+
<HintPath>C:\Users\bpeng\Desktop\BoloReborn\NumSharp\src\NumSharp.Core\bin\Debug\netstandard2.0\NumSharp.Core.dll</HintPath>
20+
</Reference>
2221
</ItemGroup>
2322

2423
</Project>

0 commit comments

Comments
 (0)