Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion src/TensorFlowNET.Core/APIs/tf.nn.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@ namespace Tensorflow
{
public static partial class tf
{
public static nn_impl nn => new nn_impl();
public static class nn
{
public static (Tensor, Tensor) moments(Tensor x,
int[] axes,
string name = null,
bool keep_dims = false) => nn_impl.moments(x, axes, name: name, keep_dims: keep_dims);
}
}
}
2 changes: 1 addition & 1 deletion src/TensorFlowNET.Core/Operations/gen_math_ops.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public static class gen_math_ops
/// <returns> A `Tensor`. Has the same type as `input`.</returns>
public static Tensor mean(Tensor input, Tensor axis, bool keep_dims= false, string name = null)
{
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, axis });
var _op = _op_def_lib._apply_op_helper("Mean", name, args: new { input, reduction_indices = axis, keep_dims = keep_dims });

return _op.outputs[0];
}
Expand Down
26 changes: 25 additions & 1 deletion src/TensorFlowNET.Core/Operations/math_ops.py.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public static Tensor reduce_mean(Tensor input_tensor, int[] axis = null, bool ke
{
var r = _ReductionDims(input_tensor, new Tensor(axis));
var m = gen_math_ops.mean(input_tensor, r);
return _may_reduce_to_scalar(keepdims, m);
return _may_reduce_to_scalar(keepdims,axis, m);
}
/// <summary>
/// Returns (x - y)(x - y) element-wise.
Expand Down Expand Up @@ -117,6 +117,12 @@ private static Tensor _may_reduce_to_scalar(bool keepdims, Tensor output)
return output;
}

private static Tensor _may_reduce_to_scalar(bool keepdims, int[] axos, Tensor output)
{
output.shape = new long[0];
return output;
}

private static Tensor _ReductionDims(Tensor x, Tensor axis)
{
if (axis != null)
Expand All @@ -130,6 +136,24 @@ private static Tensor _ReductionDims(Tensor x, Tensor axis)
}
}

private static int[] _ReductionDims(Tensor x, int[] axis)
{
if (axis != null)
{
return axis;
}
else
{
var rank = array_ops.rank(x);
if (rank != null)
{
// return constant_op.constant();
}
// return range(0, rank, 1);
throw new NotFiniteNumberException();
}
}

public static Tensor range(object start, object limit = null, object delta = null, TF_DataType dtype = TF_DataType.DtInvalid, string name = "range" )
{
if(limit == null)
Expand Down
16 changes: 5 additions & 11 deletions src/TensorFlowNET.Core/Operations/nn_impl.py.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,12 @@ public class nn_impl : Python
/// <param name="name"> Name used to scope the operations that compute the moments.</param>
/// <param name="keep_dims"> Produce moments with the same dimensionality as the input.</param>
/// <returns> Two `Tensor` objects: `mean` and `variance`.</returns>
public Tuple<Tensor, Tensor> moments(Tensor x,
public static (Tensor, Tensor) moments(Tensor x,
int[] axes,
string name = null,
bool keep_dims = false)
{
Tuple<Tensor, Tensor> t = null;
with(new ops.name_scope(name, "moments", new { x, axes }), scope =>
return with<ops.name_scope, (Tensor, Tensor)>(new ops.name_scope(name, "moments", new { x, axes }), scope =>
{
// The dynamic range of fp16 is too limited to support the collection of
// sufficient statistics. As a workaround we simply perform the operations
Expand All @@ -40,15 +39,10 @@ public Tuple<Tensor, Tensor> moments(Tensor x,
}
// TODO: if x.dtype == dtypes.float16:
if (x.dtype == TF_DataType.TF_FLOAT)
{
t = Tuple.Create(math_ops.cast(mean, x.dtype), math_ops.cast(variance, x.dtype));
return;
}
else {
t = Tuple.Create(mean, variance);
}
return (math_ops.cast(mean, x.dtype), math_ops.cast(variance, x.dtype));
else
return (mean, variance);
});
return t;
}
}
}
32 changes: 19 additions & 13 deletions test/TensorFlowNET.Examples/NaiveBayesClassifier.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ public void Run()
// 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 }, });
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 }, });
var y = np.array<int>(0,0,1,1,2,2);

fit(X, y);
// Create a regular grid and classify each point

Expand All @@ -28,41 +27,48 @@ public void Run()
public void fit(NDArray X, NDArray y)
{
NDArray unique_y = y.unique<long>();
Dictionary<long, List<NDArray>> dic = new Dictionary<long, List<NDArray>>();

Dictionary<long, List<List<float>>> dic = new Dictionary<long, List<List<float>>>();
// Init uy in dic
foreach (int uy in unique_y.Data<int>())
{
dic.Add(uy, new List<NDArray>());
dic.Add(uy, new List<List<float>>());
}
// Separate training points by class
// Shape : nb_classes * nb_samples * nb_features
int maxCount = 0;
for (int i = 0; i < y.size; i++)
{
long curClass = (long)y[i];
List<NDArray> l = dic[curClass];
l.Add(X[i] as NDArray);
List<List<float>> l = dic[curClass];
List<float> pair = new List<float>();
pair.Add((float)X[i,0]);
pair.Add((float)X[i, 1]);
l.Add(pair);
if (l.Count > maxCount)
{
maxCount = l.Count;
}
dic[curClass] = l;
}
NDArray points_by_class = np.zeros(new int[] { dic.Count, maxCount, X.shape[1] });
foreach (KeyValuePair<long, List<NDArray>> kv in dic)
float[,,] points = new float[dic.Count, maxCount, X.shape[1]];
foreach (KeyValuePair<long, List<List<float>>> kv in dic)
{
var cls = kv.Value.ToArray();
for (int i = 0; i < dic.Count; i++)
int j = (int) kv.Key;
for (int i = 0; i < maxCount; i++)
{
points_by_class[i] = dic[i];
for (int k = 0; k < X.shape[1]; k++)
{
points[j, i, k] = kv.Value[i][k];
}
}
}

}
NDArray points_by_class = np.array<float>(points);
// estimate mean and variance for each class / feature
// shape : nb_classes * nb_features
var cons = tf.constant(points_by_class);
Tuple<Tensor, Tensor> tup = tf.nn.moments(cons, new int[]{1});
var tup = tf.nn.moments(cons, new int[]{1});
var mean = tup.Item1;
var variance = tup.Item2;
// Create a 3x2 univariate normal distribution with the
Expand Down
3 changes: 3 additions & 0 deletions test/TensorFlowNET.Examples/TensorFlowNET.Examples.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
<Reference Include="Newtonsoft.Json">
<HintPath>C:\Program Files\dotnet\sdk\NuGetFallbackFolder\newtonsoft.json\9.0.1\lib\netstandard1.0\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="NumSharp.Core">
<HintPath>C:\Users\bpeng\Desktop\BoloReborn\NumSharp\src\NumSharp.Core\bin\Debug\netstandard2.0\NumSharp.Core.dll</HintPath>
</Reference>
</ItemGroup>

</Project>