Skip to content

Commit 723de52

Browse files
authored
Merge pull request #28 from nhirschey/nh
Add LinearRegressionEager.fs, Ignore FunctionApproximation.fs for now
2 parents bc4eea0 + ec4a412 commit 723de52

File tree

6 files changed

+119
-35
lines changed

6 files changed

+119
-35
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,3 +329,5 @@ ASALocalRun/
329329
.mfractor/
330330
.vscode/tasks.json
331331
/src/tensorflow2.x-python-tutorial/my_model
332+
333+
.ionide

README.md

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -30,27 +30,27 @@ dotnet TensorFlowNET.Examples.dll -ex "MNIST CNN (Eager)"
3030

3131
Example runner will download all the required files like training data and model pb files.
3232

33-
* [Hello World](src/TensorFlowNET.Examples/HelloWorld.cs)
34-
* [Basic Operations](src/TensorFlowNET.Examples/BasicOperations.cs)
35-
* [Linear Regression](src/TensorFlowNET.Examples/BasicModels/LinearRegression.cs) in Graph mode
36-
* [Linear Regression](src/TensorFlowNET.Examples/BasicModels/LinearRegressionEager.cs) in Eager mode
37-
* [Logistic Regression](src/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs) in Graph mode
38-
* [Logistic Regression](src/TensorFlowNET.Examples/BasicModels/LogisticRegressionEager.cs) in Eager mode
39-
* [Nearest Neighbor](src/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs)
40-
* [Naive Bayes Classification](src/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs)
41-
* [Full Connected Neural Network](src/TensorFlowNET.Examples/\NeuralNetworks/FullyConnectedEager.cs) in Eager mode
42-
* [K-means Clustering](src/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs)
43-
* [NN XOR](src/TensorFlowNET.Examples/NeuralNetworks/NeuralNetXor.cs)
44-
* [Object Detection](src/TensorFlowNET.Examples/ObjectDetection/DetectInMobilenet.cs) in MobileNet
45-
* [Binary Text Classification](src/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
46-
* [CNN Text Classification](src/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
47-
* [MNIST FNN](src/TensorFlowNET.Examples/ImageProcessing/MnistFnnKerasFunctional.cs) in Keras Functional API
48-
* [MNIST CNN](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs) in Graph mode
49-
* [MNIST RNN](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRNN.cs)
50-
* [MNIST LSTM](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionLSTM.cs)
51-
* [Named Entity Recognition](src/TensorFlowNET.Examples/TextProcessing/NER)
52-
* [Transfer Learning for Image Classification](src/TensorFlowNET.Examples/ImageProcessing/TransferLearningWithInceptionV3.cs) in InceptionV3
53-
* [CNN In Your Own Dataset](src/TensorFlowNET.Examples/ImageProcessing/CnnInYourOwnData.cs)
33+
* Hello World [C#](src/TensorFlowNET.Examples/HelloWorld.cs)
34+
* Basic Operations [C#](src/TensorFlowNET.Examples/BasicOperations.cs)
35+
* Linear Regression in Graph mode [C#](src/TensorFlowNET.Examples/BasicModels/LinearRegression.cs)
36+
* Linear Regression in Eager mode [C#](src/TensorFlowNET.Examples/BasicModels/LinearRegressionEager.cs), [F#](src/TensorFlowNET.Examples.FSharp/BasicModels/LinearRegressionEager.fs)
37+
* Logistic Regression in Graph mode [C#](src/TensorFlowNET.Examples/BasicModels/LogisticRegression.cs)
38+
* Logistic Regression in Eager mode [C#](src/TensorFlowNET.Examples/BasicModels/LogisticRegressionEager.cs)
39+
* Nearest Neighbor [C#](src/TensorFlowNET.Examples/BasicModels/NearestNeighbor.cs)
40+
* Naive Bayes Classification [C#](src/TensorFlowNET.Examples/BasicModels/NaiveBayesClassifier.cs)
41+
* Full Connected Neural Network in Eager mode [C#](src/TensorFlowNET.Examples/\NeuralNetworks/FullyConnectedEager.cs)
42+
* K-means Clustering [C#](src/TensorFlowNET.Examples/BasicModels/KMeansClustering.cs)
43+
* NN XOR [C#](src/TensorFlowNET.Examples/NeuralNetworks/NeuralNetXor.cs)
44+
* Object Detection in MobileNet [C#](src/TensorFlowNET.Examples/ObjectDetection/DetectInMobilenet.cs)
45+
* Binary Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/BinaryTextClassification.cs)
46+
* CNN Text Classification [C#](src/TensorFlowNET.Examples/TextProcessing/cnn_models/VdCnn.cs)
47+
* MNIST FNN in Keras Functional API [C#](src/TensorFlowNET.Examples/ImageProcessing/MnistFnnKerasFunctional.cs)
48+
* MNIST CNN in Graph mode [C#](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionCNN.cs)
49+
* MNIST RNN [C#](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionRNN.cs)
50+
* MNIST LSTM [C#](src/TensorFlowNET.Examples/ImageProcessing/DigitRecognitionLSTM.cs)
51+
* Named Entity Recognition [C#](src/TensorFlowNET.Examples/TextProcessing/NER)
52+
* Transfer Learning for Image Classification in InceptionV3 [C#](src/TensorFlowNET.Examples/ImageProcessing/TransferLearningWithInceptionV3.cs)
53+
* CNN In Your Own Dataset [C#](src/TensorFlowNET.Examples/ImageProcessing/CnnInYourOwnData.cs)
5454

5555

5656
### Welcome to PR your example to us.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
(******************************************************************************
2+
Copyright 2018 The TensorFlow.NET Authors. All Rights Reserved.
3+
4+
Licensed under the Apache License, Version 2.0 (the "License");
5+
you may not use this file except in compliance with the License.
6+
You may obtain a copy of the License at
7+
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
10+
Unless required by applicable law or agreed to in writing, software
11+
distributed under the License is distributed on an "AS IS" BASIS,
12+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
See the License for the specific language governing permissions and
14+
limitations under the License.
15+
******************************************************************************)
16+
17+
namespace TensorFlow.NET.Examples.FSharp
18+
19+
open NumSharp
20+
open Tensorflow
21+
open type Tensorflow.Binding
22+
open type Tensorflow.KerasApi
23+
24+
/// <summary>
25+
/// A linear regression learning algorithm example using TensorFlow library.
26+
/// https://github.com/aymericdamien/TensorFlow-Examples/blob/master/tensorflow_v2/notebooks/2_BasicModels/linear_regression.ipynb
27+
/// </summary>
28+
29+
module LinearRegressionEager =
30+
let run() =
31+
let tf = New<tensorflow>()
32+
tf.enable_eager_execution()
33+
34+
let training_steps = 1000
35+
let learning_rate = 0.01f
36+
let display_step = 100
37+
38+
let train_X =
39+
np.array(3.3f, 4.4f, 5.5f, 6.71f, 6.93f, 4.168f, 9.779f, 6.182f, 7.59f, 2.167f,
40+
7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f)
41+
let train_Y =
42+
np.array(1.7f, 2.76f, 2.09f, 3.19f, 1.694f, 1.573f, 3.366f, 2.596f, 2.53f, 1.221f,
43+
2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f)
44+
let n_samples = train_X.shape.[0]
45+
46+
// We can set a fixed init value in order to demo
47+
let W = tf.Variable(-0.06f,name = "weight")
48+
let b = tf.Variable(-0.73f, name = "bias")
49+
let optimizer = keras.optimizers.SGD(learning_rate)
50+
51+
// Run training for the given number of steps.
52+
for step = 1 to (training_steps + 1) do
53+
// Run the optimization to update W and b values.
54+
// Wrap computation inside a GradientTape for automatic differentiation.
55+
use g = tf.GradientTape()
56+
// Linear regression (Wx + b).
57+
let pred = W * train_X + b
58+
// Mean square error.
59+
let loss = tf.reduce_sum(tf.pow(pred - train_Y,2)) / (2 * n_samples)
60+
// should stop recording
61+
// compute gradients
62+
let gradients = g.gradient(loss,struct (W,b))
63+
64+
// Update W and b following gradients.
65+
optimizer.apply_gradients(zip(gradients, struct (W,b)))
66+
67+
if (step % display_step) = 0 then
68+
let pred = W * train_X + b
69+
let loss = tf.reduce_sum(tf.pow(pred-train_Y,2)) / (2 * n_samples)
70+
printfn $"step: {step}, loss: {loss.numpy()}, W: {W.numpy()}, b: {b.numpy()}"
71+
72+

src/TensorFlowNET.Examples.FSharp/FunctionApproximation.fs

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,32 +40,33 @@ let run()=
4040
let n_hidden_layer_2 = 25 // Hidden layer 2
4141

4242
let tf = Binding.New<tensorflow>()
43+
tf.enable_eager_execution()
4344
let x = tf.placeholder(tf.float64, new TensorShape(N_points,n_input))
4445
let y = tf.placeholder(tf.float64, new TensorShape(n_output))
4546

4647

4748
let weights = dict[
48-
"hidden_layer_1", tf.Variable(tf.random_normal([|n_input; n_hidden_layer_1|],dtype=tf.float64))
49-
"hidden_layer_2", tf.Variable(tf.random_normal([|n_hidden_layer_1; n_hidden_layer_2|],dtype=tf.float64))
50-
"out", tf.Variable(tf.random_normal([|n_hidden_layer_2; n_output|],dtype=tf.float64))
49+
"hidden_layer_1", tf.Variable(tf.random.normal(TensorShape [|n_input; n_hidden_layer_1|],dtype=tf.float64))
50+
"hidden_layer_2", tf.Variable(tf.random.normal(TensorShape [|n_hidden_layer_1; n_hidden_layer_2|],dtype=tf.float64))
51+
"out", tf.Variable(tf.random.normal(TensorShape [|n_hidden_layer_2; n_output|],dtype=tf.float64))
5152
]
5253
let biases = dict[
53-
"hidden_layer_1", tf.Variable(tf.random_normal([|n_hidden_layer_1|],dtype=tf.float64))
54-
"hidden_layer_2", tf.Variable(tf.random_normal([|n_hidden_layer_2|],dtype=tf.float64))
55-
"out", tf.Variable(tf.random_normal([|n_output|],dtype=tf.float64))
54+
"hidden_layer_1", tf.Variable(tf.random.normal(TensorShape [|n_hidden_layer_1|],dtype=tf.float64))
55+
"hidden_layer_2", tf.Variable(tf.random.normal(TensorShape [|n_hidden_layer_2|],dtype=tf.float64))
56+
"out", tf.Variable(tf.random.normal(TensorShape [|n_output|],dtype=tf.float64))
5657
]
5758

5859

5960
// Hidden layer with RELU activation
6061

61-
let layer_1 = tf.add(tf.matmul(x, weights.["hidden_layer_1"]._AsTensor()),biases.["hidden_layer_1"])
62+
let layer_1 = tf.add(tf.matmul(x, weights.["hidden_layer_1"].AsTensor()),biases.["hidden_layer_1"])
6263
let layer_1 = tf.nn.relu(layer_1)
6364

64-
let layer_2 = tf.add(tf.matmul(layer_1, weights.["hidden_layer_2"]._AsTensor()),biases.["hidden_layer_2"])
65+
let layer_2 = tf.add(tf.matmul(layer_1, weights.["hidden_layer_2"].AsTensor()),biases.["hidden_layer_2"])
6566
let layer_2 = tf.nn.relu(layer_2)
6667

6768
// Output layer with linear activation
68-
let ops = tf.add(tf.matmul(layer_2, weights.["out"]._AsTensor()), biases.["out"])
69+
let ops = tf.add(tf.matmul(layer_2, weights.["out"].AsTensor()), biases.["out"])
6970

7071
// Define loss and optimizer
7172
let cost = tf.reduce_mean(tf.square(tf.squeeze(ops)-y))
@@ -83,7 +84,7 @@ let run()=
8384
for epoch in [0..training_epochs] do
8485
// Run optimization process (backprop) and cost function (to get loss value)
8586

86-
let result=sess.run([|optimizer:>ITensorOrOperation; gs._AsTensor():>ITensorOrOperation; cost:>ITensorOrOperation|], new FeedItem(x, X_train), new FeedItem(y, y_train))
87+
let result=sess.run([|optimizer:>ITensorOrOperation; gs.AsTensor():>ITensorOrOperation; cost:>ITensorOrOperation|], new FeedItem(x, X_train), new FeedItem(y, y_train))
8788

8889

8990
let loss_value = (double) result.[2];
Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Learn more about F# at http://fsharp.org
22

33
open System
4+
open TensorFlow.NET.Examples.FSharp
45

56
[<EntryPoint>]
67
let main argv =
7-
FunctionApproximation.run()
8+
//FunctionApproximation.run() // Still Needs updates
9+
LinearRegressionEager.run()
810
0 // return an integer exit code

src/TensorFlowNET.Examples.FSharp/TensorFlowNET.Examples.FSharp.fsproj

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,24 @@
77
</PropertyGroup>
88

99
<ItemGroup>
10+
<Compile Include="BasicModels\LinearRegressionEager.fs" />
1011
<Compile Include="FunctionApproximation.fs" />
1112
<Compile Include="Program.fs" />
1213
</ItemGroup>
1314

1415
<ItemGroup>
15-
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="1.15.0" />
16-
<PackageReference Include="TensorFlow.NET" Version="0.12.0" />
16+
<PackageReference Include="Colorful.Console" Version="1.2.11" />
17+
<PackageReference Include="Newtonsoft.Json" Version="12.0.3" />
18+
<PackageReference Include="OpenCvSharp4.runtime.win" Version="4.4.0.20200915" />
19+
<PackageReference Include="SciSharp.TensorFlow.Redist" Version="2.3.1" />
20+
<PackageReference Include="SharpCV" Version="0.6.0" />
21+
<PackageReference Include="SharpZipLib" Version="1.3.0" />
22+
<PackageReference Include="System.Drawing.Common" Version="4.7.0" />
23+
<PackageReference Include="TensorFlow.Keras" Version="0.1.0" />
1724
</ItemGroup>
1825

1926
<ItemGroup>
20-
<PackageReference Update="FSharp.Core" Version="4.7.2" />
27+
<PackageReference Update="FSharp.Core" Version="5.0" />
2128
</ItemGroup>
2229

2330
</Project>

0 commit comments

Comments
 (0)