Skip to content

Commit b15b98f

Browse files
authored
Merge pull request SciSharp#187 from PppBr/master
implemented class nn
2 parents a4e52ab + 5e8908e commit b15b98f

6 files changed

Lines changed: 60 additions & 27 deletions

File tree

‎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(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
}

‎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

‎test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
<Reference Include="Newtonsoft.Json">
2121
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
2222
</Reference>
23+
<Reference Include="NumSharp.Core">
24+
<HintPath>C:\Users\bpeng\Desktop\BoloReborn\NumSharp\src\NumSharp.Core\bin\Debug\netstandard2.0\NumSharp.Core.dll</HintPath>
25+
</Reference>
2326
</ItemGroup>
2427

2528
</Project>

0 commit comments

Comments
 (0)