Skip to content

Commit 28155aa

Browse files
committed
Makina Commit 1
1 parent baaff63 commit 28155aa

18 files changed

Lines changed: 648 additions & 2 deletions

TensorFlow.NET.sln

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11

22
Microsoft Visual Studio Solution File, Format Version 12.00
3-
# Visual Studio Version 16
4-
VisualStudioVersion = 16.0.28803.156
3+
# Visual Studio 15
4+
VisualStudioVersion = 15.0.28307.168
55
MinimumVisualStudioVersion = 10.0.40219.1
66
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.UnitTest", "test\TensorFlowNET.UnitTest\TensorFlowNET.UnitTest.csproj", "{029A8CF1-CF95-4DCB-98AA-9D3D96A83B3E}"
77
EndProject
88
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Examples", "test\TensorFlowNET.Examples\TensorFlowNET.Examples.csproj", "{1FE60088-157C-4140-91AB-E96B915E4BAE}"
99
EndProject
1010
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "TensorFlowNET.Core", "src\TensorFlowNET.Core\TensorFlowNET.Core.csproj", "{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}"
1111
EndProject
12+
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MakinaNET.Core", "src\MakinaNET.Core\MakinaNET.Core.csproj", "{6FF518EB-523F-4930-919E-05011EFCED6F}"
13+
EndProject
14+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MakinaNET.Example", "src\MakinaNET.Example\MakinaNET.Example.csproj", "{17E1AC16-9E0E-4545-905A-E92C6300C7AF}"
15+
EndProject
16+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "MakinaNET.UnitTest", "src\MakinaNET.Test\MakinaNET.UnitTest.csproj", "{02F54D9F-617B-4B15-9D21-E21AF66693B1}"
17+
EndProject
1218
Global
1319
GlobalSection(SolutionConfigurationPlatforms) = preSolution
1420
Debug|Any CPU = Debug|Any CPU
@@ -27,6 +33,18 @@ Global
2733
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Debug|Any CPU.Build.0 = Debug|Any CPU
2834
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.ActiveCfg = Release|Any CPU
2935
{FD682AC0-7B2D-45D3-8B0D-C6D678B04144}.Release|Any CPU.Build.0 = Release|Any CPU
36+
{6FF518EB-523F-4930-919E-05011EFCED6F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
37+
{6FF518EB-523F-4930-919E-05011EFCED6F}.Debug|Any CPU.Build.0 = Debug|Any CPU
38+
{6FF518EB-523F-4930-919E-05011EFCED6F}.Release|Any CPU.ActiveCfg = Release|Any CPU
39+
{6FF518EB-523F-4930-919E-05011EFCED6F}.Release|Any CPU.Build.0 = Release|Any CPU
40+
{17E1AC16-9E0E-4545-905A-E92C6300C7AF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
41+
{17E1AC16-9E0E-4545-905A-E92C6300C7AF}.Debug|Any CPU.Build.0 = Debug|Any CPU
42+
{17E1AC16-9E0E-4545-905A-E92C6300C7AF}.Release|Any CPU.ActiveCfg = Release|Any CPU
43+
{17E1AC16-9E0E-4545-905A-E92C6300C7AF}.Release|Any CPU.Build.0 = Release|Any CPU
44+
{02F54D9F-617B-4B15-9D21-E21AF66693B1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
45+
{02F54D9F-617B-4B15-9D21-E21AF66693B1}.Debug|Any CPU.Build.0 = Debug|Any CPU
46+
{02F54D9F-617B-4B15-9D21-E21AF66693B1}.Release|Any CPU.ActiveCfg = Release|Any CPU
47+
{02F54D9F-617B-4B15-9D21-E21AF66693B1}.Release|Any CPU.Build.0 = Release|Any CPU
3048
EndGlobalSection
3149
GlobalSection(SolutionProperties) = preSolution
3250
HideSolutionNode = FALSE

src/MakinaNET.Core/Core.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow;
5+
6+
namespace Makina
7+
{
8+
public static class Makina
9+
{
10+
public static Tensor create_tensor(int[] shape, float mean = 0, float stddev = 1, TF_DataType dtype = TF_DataType.TF_FLOAT, int? seed = null, string name = null)
11+
{
12+
return tf.truncated_normal(shape: shape, mean: mean, stddev: stddev, dtype: dtype, seed: seed, name: name);
13+
}
14+
}
15+
}

src/MakinaNET.Core/IInitializer.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
5+
namespace Makina
6+
{
7+
interface IInitializer
8+
{
9+
}
10+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow;
5+
using Tensorflow.Layers;
6+
7+
namespace Makina.Initializer
8+
{
9+
class BaseInitializer : IInitializer
10+
{
11+
public int seed;
12+
}
13+
}

src/MakinaNET.Core/Layers/Dense.cs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using System.Linq;
5+
using Tensorflow;
6+
using static Makina.Makina;
7+
using Makina;
8+
using NumSharp;
9+
using Tensorflow.Operations.Activation;
10+
11+
namespace Makina.Layers
12+
{
13+
public class Dense : ILayer
14+
{
15+
RefVariable W;
16+
int units;
17+
TensorShape WShape;
18+
string name;
19+
IActivation activation;
20+
21+
public Dense(int units, string name = null, IActivation activation = null)
22+
{
23+
this.activation = activation;
24+
this.units = units;
25+
this.name = (string.IsNullOrEmpty(name) || string.IsNullOrWhiteSpace(name))?this.GetType().Name + "_" + this.GetType().GUID:name;
26+
}
27+
public ILayer __build__(TensorShape input_shape, int seed = 1, float stddev = -1f)
28+
{
29+
Console.WriteLine("Building Layer \"" + name + "\" ...");
30+
if (stddev == -1)
31+
stddev = (float)(1 / Math.Sqrt(2));
32+
var dim = input_shape.Dimensions;
33+
var input_dim = dim[dim.Length - 1];
34+
W = tf.Variable(create_tensor(new int[] { input_dim, units }, seed: seed, stddev: (float)stddev));
35+
WShape = new TensorShape(W.shape);
36+
return this;
37+
}
38+
public Tensor __call__(Tensor x)
39+
{
40+
var dot = tf.matmul(x, W);
41+
if (this.activation != null)
42+
dot = activation.Activate(dot);
43+
Console.WriteLine("Calling Layer \"" + name + "(" + np.array(dot.getShape().Dimensions).ToString() + ")\" ...");
44+
return dot;
45+
}
46+
public TensorShape __shape__()
47+
{
48+
return WShape;
49+
}
50+
public TensorShape output_shape(TensorShape input_shape)
51+
{
52+
var output_shape = input_shape.Dimensions;
53+
output_shape[output_shape.Length - 1] = units;
54+
return new TensorShape(output_shape);
55+
}
56+
}
57+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Text;
4+
using Tensorflow;
5+
using NumSharp;
6+
7+
namespace Makina.Layers
8+
{
9+
public interface ILayer
10+
{
11+
TensorShape __shape__();
12+
ILayer __build__(TensorShape input_shape, int seed = 1, float stddev = -1f);
13+
Tensor __call__(Tensor x);
14+
TensorShape output_shape(TensorShape input_shape);
15+
}
16+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<TargetFramework>netstandard2.0</TargetFramework>
5+
<AssemblyName>Makina</AssemblyName>
6+
<RootNamespace>Makina</RootNamespace>
7+
</PropertyGroup>
8+
9+
<ItemGroup>
10+
<ProjectReference Include="..\TensorFlowNET.Core\TensorFlowNET.Core.csproj" />
11+
</ItemGroup>
12+
13+
</Project>

src/MakinaNET.Core/Model.cs

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using Makina.Layers;
2+
using NumSharp;
3+
using System;
4+
using System.Collections.Generic;
5+
using System.Text;
6+
using Tensorflow;
7+
using static Makina.Makina;
8+
using static Tensorflow.Python;
9+
10+
namespace Makina
11+
{
12+
public class Model
13+
{
14+
public Tensor Flow;
15+
List<ILayer> layer_stack;
16+
17+
public TensorShape InputShape;
18+
19+
public Model()
20+
{
21+
layer_stack = new List<ILayer>();
22+
}
23+
public Model Add(ILayer layer)
24+
{
25+
layer_stack.Add(layer);
26+
return this;
27+
}
28+
public Model Add(IEnumerable<ILayer> layers)
29+
{
30+
layer_stack.AddRange(layers);
31+
return this;
32+
}
33+
public Tensor getFlow()
34+
{
35+
try
36+
{
37+
return Flow;
38+
}
39+
catch (Exception ex)
40+
{
41+
return null;
42+
}
43+
}
44+
public (Operation, Tensor, Tensor) make_graph(Tensor features, Tensor labels)
45+
{
46+
47+
// TODO : Creating Loss Functions And Optimizers.....
48+
49+
#region Model Layers Graph
50+
/*
51+
var stddev = 1 / Math.Sqrt(2);
52+
53+
var d1 = new Dense(num_hidden);
54+
d1.__build__(features.getShape());
55+
var hidden_activations = tf.nn.relu(d1.__call__(features));
56+
57+
var d1_output = d1.output_shape(features.getShape());
58+
59+
60+
var d2 = new Dense(1);
61+
d2.__build__(d1.output_shape(features.getShape()), seed: 17, stddev: (float)(1/ Math.Sqrt(num_hidden)));
62+
var logits = d2.__call__(hidden_activations);
63+
var predictions = tf.sigmoid(tf.squeeze(logits));
64+
*/
65+
#endregion
66+
67+
#region Model Graph Form Layer Stack
68+
var flow_shape = features.getShape();
69+
Flow = features;
70+
for (int i = 0; i < layer_stack.Count; i++)
71+
{
72+
layer_stack[i].__build__(flow_shape);
73+
flow_shape = layer_stack[i].output_shape(flow_shape);
74+
Flow = layer_stack[i].__call__(Flow);
75+
}
76+
var predictions = tf.sigmoid(tf.squeeze(Flow));
77+
78+
#endregion
79+
80+
#region loss and optimizer
81+
var loss = tf.reduce_mean(tf.square(predictions - tf.cast(labels, tf.float32)), name: "loss");
82+
83+
var gs = tf.Variable(0, trainable: false, name: "global_step");
84+
var train_op = tf.train.GradientDescentOptimizer(0.2f).minimize(loss, global_step: gs);
85+
#endregion
86+
87+
return (train_op, loss, gs);
88+
}
89+
public float train(int num_steps, (NDArray, NDArray) training_dataset)
90+
{
91+
var (X, Y) = training_dataset;
92+
var x_shape = X.shape;
93+
var batch_size = x_shape[0];
94+
var graph = tf.Graph().as_default();
95+
96+
var features = tf.placeholder(tf.float32, new TensorShape(batch_size, 2));
97+
var labels = tf.placeholder(tf.float32, new TensorShape(batch_size));
98+
99+
var (train_op, loss, gs) = this.make_graph(features, labels);
100+
101+
var init = tf.global_variables_initializer();
102+
103+
float loss_value = 0;
104+
with(tf.Session(graph), sess =>
105+
{
106+
sess.run(init);
107+
var step = 0;
108+
109+
110+
while (step < num_steps)
111+
{
112+
var result = sess.run(
113+
new ITensorOrOperation[] { train_op, gs, loss },
114+
new FeedItem(features, X),
115+
new FeedItem(labels, Y));
116+
loss_value = result[2];
117+
step = result[1];
118+
if (step % 1000 == 0)
119+
Console.WriteLine($"Step {step} loss: {loss_value}");
120+
}
121+
Console.WriteLine($"Final loss: {loss_value}");
122+
});
123+
124+
return loss_value;
125+
}
126+
}
127+
}

src/MakinaNET.Example/App.config

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<configuration>
3+
<startup>
4+
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
5+
</startup>
6+
<runtime>
7+
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
8+
<dependentAssembly>
9+
<assemblyIdentity name="System.Numerics.Vectors" publicKeyToken="b03f5f7f11d50a3a" culture="neutral" />
10+
<bindingRedirect oldVersion="0.0.0.0-4.1.4.0" newVersion="4.1.4.0" />
11+
</dependentAssembly>
12+
</assemblyBinding>
13+
</runtime>
14+
</configuration>

0 commit comments

Comments
 (0)