Skip to content

Commit 14c0a45

Browse files
committed
implementing NB classifier
1 parent a1853a9 commit 14c0a45

7 files changed

Lines changed: 110 additions & 82 deletions

File tree

TensorFlow.NET.sln

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\T
1111
EndProject
1212
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Visualization", "src\TensorFlowNET.Visualization\TensorFlowNET.Visualization.csproj", "{0254BFF9-453C-4FE0-9609-3644559A79CE}"
1313
EndProject
14+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "NumSharp.Core", "..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj", "{A4428462-1147-45E9-8403-4E6860FF6EED}"
15+
EndProject
1416
Global
1517
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1618
Debug|Any CPU = Debug|Any CPU
@@ -33,6 +35,10 @@ Global
3335
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Debug|Any CPU.Build.0 = Debug|Any CPU
3436
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.ActiveCfg = Release|Any CPU
3537
{0254BFF9-453C-4FE0-9609-3644559A79CE}.Release|Any CPU.Build.0 = Release|Any CPU
38+
{A4428462-1147-45E9-8403-4E6860FF6EED}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
39+
{A4428462-1147-45E9-8403-4E6860FF6EED}.Debug|Any CPU.Build.0 = Debug|Any CPU
40+
{A4428462-1147-45E9-8403-4E6860FF6EED}.Release|Any CPU.ActiveCfg = Release|Any CPU
41+
{A4428462-1147-45E9-8403-4E6860FF6EED}.Release|Any CPU.Build.0 = Release|Any CPU
3642
EndGlobalSection
3743
GlobalSection(SolutionProperties) = preSolution
3844
HideSolutionNode = FALSE

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,9 @@ public static Tensor _clip_by_value(Tensor t, Tensor clip_value_min, Tensor clip
152152
public static Tensor sub(Tensor a, Tensor b)
153153
=> gen_math_ops.sub(a, b);
154154

155+
public static Tensor divide(Tensor a, Tensor b)
156+
=> gen_math_ops.real_div(a, b);
157+
155158
public static Tensor sqrt(Tensor a, string name = null)
156159
=> gen_math_ops.sqrt(a, name);
157160

src/TensorFlowNET.Core/Operations/Distributions/distribution.py.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ public Tensor log_prob(Tensor value, string name = "log_prob")
4343

4444
private Tensor _call_log_prob (Tensor value, string name)
4545
{
46-
with(ops.name_scope(name, "moments", new { value }), scope =>
46+
return with(ops.name_scope(name, "moments", new { value }), scope =>
4747
{
4848
try
4949
{
@@ -60,10 +60,9 @@ private Tensor _call_log_prob (Tensor value, string name)
6060
}
6161
}
6262
});
63-
return null;
6463
}
6564

66-
private Tensor _log_prob(Tensor value)
65+
protected virtual Tensor _log_prob(Tensor value)
6766
{
6867
throw new NotImplementedException();
6968
}

src/TensorFlowNET.Core/Operations/Distributions/normal.py.cs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -79,25 +79,32 @@ public Tensor _batch_shape()
7979
return array_ops.broadcast_static_shape(new Tensor(_loc.shape), new Tensor(_scale.shape));
8080
}
8181

82-
private Tensor _log_prob(Tensor x)
82+
protected override Tensor _log_prob(Tensor x)
8383
{
84-
return _log_unnormalized_prob(_z(x)) -_log_normalization();
84+
var log_prob = _log_unnormalized_prob(_z(x));
85+
var log_norm = _log_normalization();
86+
return log_prob - log_norm;
8587
}
8688

8789
private Tensor _log_unnormalized_prob (Tensor x)
8890
{
8991
return -0.5 * math_ops.square(_z(x));
9092
}
91-
93+
/// <summary>
94+
/// Standardize input `x` to a unit normal.
95+
/// </summary>
96+
/// <param name="x"></param>
97+
/// <returns></returns>
9298
private Tensor _z (Tensor x)
9399
{
94-
return (x - this._loc) / this._scale;
100+
return tf.divide(tf.sub(x, this._loc), this._scale);
95101
}
96102

97103
private Tensor _log_normalization()
98104
{
99-
Tensor t = new Tensor(Math.Log(2.0 * Math.PI));
100-
return 0.5 * t + math_ops.log(scale());
105+
Tensor t1 = ops.convert_to_tensor(Math.Log(2.0 * Math.PI), TF_DataType.TF_FLOAT);
106+
Tensor t2 = tf.multiply(ops.convert_to_tensor(0.5, TF_DataType.TF_FLOAT), t1);
107+
return tf.add(t2, math_ops.log(this._scale));
101108
}
102109
}
103110
}

src/TensorFlowNET.Core/TensorFlowNET.Core.csproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,4 +58,8 @@ Bug memory leak issue when allocating Tensor.</PackageReleaseNotes>
5858
<Folder Include="Keras\Initializers\" />
5959
</ItemGroup>
6060

61+
<ItemGroup>
62+
<ProjectReference Include="..\..\..\NumSharp\src\NumSharp.Core\NumSharp.Core.csproj" />
63+
</ItemGroup>
64+
6165
</Project>

0 commit comments

Comments
 (0)