@@ -37,7 +37,7 @@ Import TF.NET.
3737using Tensorflow ;
3838```
3939
40- Add two constants.
40+ Add two constants:
4141``` cs
4242// Create a Constant op
4343var a = tf .constant (4 . 0 f );
@@ -50,7 +50,7 @@ using (var sess = tf.Session())
5050}
5151```
5252
53- Feed placeholder.
53+ Feed placeholder:
5454``` cs
5555// Create a placeholder op
5656var a = tf .placeholder (tf .float32 );
@@ -59,27 +59,86 @@ var c = tf.add(a, b);
5959
6060using (var sess = tf .Session ())
6161{
62- var feed_dict = new Dictionary <Tensor , object >();
63- feed_dict .Add (a , 3 . 0 f );
64- feed_dict .Add (b , 2 . 0 f );
65-
66- var o = sess .run (c , feed_dict );
62+ var o = sess .run (c , new FeedItem (a , 3 . 0 f ), new FeedItem (b , 2 . 0 f ));
6763}
6864```
6965
66+ Linear Regression:
67+
68+ ``` c#
69+ // We can set a fixed init value in order to debug
70+ var W = tf .Variable (- 0 . 06 f , name : " weight" );
71+ var b = tf .Variable (- 0 . 73 f , name : " bias" );
72+
73+ // Construct a linear model
74+ var pred = tf .add (tf .multiply (X , W ), b );
75+
76+ // Mean squared error
77+ var cost = tf .reduce_sum (tf .pow (pred - Y , 2 . 0 f )) / (2 . 0 f * n_samples );
78+
79+ // Gradient descent
80+ // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
81+ var optimizer = tf .train .GradientDescentOptimizer (learning_rate ).minimize (cost );
82+
83+ // Initialize the variables (i.e. assign their default value)
84+ var init = tf .global_variables_initializer ();
85+
86+ // Start training
87+ with (tf .Session (), sess =>
88+ {
89+ // Run the initializer
90+ sess .run (init );
91+
92+ // Fit all training data
93+ for (int epoch = 0 ; epoch < training_epochs ; epoch ++ )
94+ {
95+ foreach (var (x , y ) in zip <float >(train_X , train_Y ))
96+ sess .run (optimizer , new FeedItem (X , x ), new FeedItem (Y , y ));
97+
98+ // Display logs per epoch step
99+ if ((epoch + 1 ) % display_step == 0 )
100+ {
101+ var c = sess .run (cost , new FeedItem (X , train_X ), new FeedItem (Y , train_Y ));
102+ Console .WriteLine ($" Epoch: {epoch + 1 } cost={c } " + $" W={sess .run (W )} b={sess .run (b )}" );
103+ }
104+
105+ Console .WriteLine (" Optimization Finished!" );
106+ var training_cost = sess .run (cost , new FeedItem (X , train_X ), new FeedItem (Y , train_Y ));
107+ Console .WriteLine ($" Training cost={training_cost } W={sess .run (W )} b={sess .run (b )}" );
108+
109+ // Testing example
110+ var test_X = np .array (6 . 83 f , 4 . 668 f , 8 . 9 f , 7 . 91 f , 5 . 7 f , 8 . 7 f , 3 . 1 f , 2 . 1 f );
111+ var test_Y = np .array (1 . 84 f , 2 . 273 f , 3 . 2 f , 2 . 831 f , 2 . 92 f , 3 . 24 f , 1 . 35 f , 1 . 03 f );
112+ Console .WriteLine (" Testing... (Mean square loss Comparison)" );
113+
114+ var testing_cost = sess .run (tf .reduce_sum (tf .pow (pred - Y , 2 . 0 f )) / (2 . 0 f * test_X .shape [0 ]), new FeedItem (X , test_X ), new FeedItem (Y , test_Y ));
115+ Console .WriteLine ($" Testing cost={testing_cost }" );
116+
117+ var diff = Math .Abs ((float )training_cost - (float )testing_cost );
118+ Console .WriteLine ($" Absolute mean square loss difference: {diff }" );
119+ }
120+ });
121+ ```
122+
123+
124+
70125Read the docs & book [ The Definitive Guide to Tensorflow.NET] ( https://tensorflownet.readthedocs.io/en/latest/FrontCover.html ) .
71126
72127### More examples:
73128
74129* [ Hello World] ( test/TensorFlowNET.Examples/HelloWorld.cs )
75130* [ Basic Operations] ( test/TensorFlowNET.Examples/BasicOperations.cs )
76- * [ Image Recognition] ( test/TensorFlowNET.Examples/ImageRecognition.cs )
77131* [ Linear Regression] ( test/TensorFlowNET.Examples/LinearRegression.cs )
78132* [ Logistic Regression] ( test/TensorFlowNET.Examples/LogisticRegression.cs )
79133* [ Nearest Neighbor] ( test/TensorFlowNET.Examples/NearestNeighbor.cs )
134+ * [ Naive Bayes Classification] ( test/TensorFlowNET.Examples/NaiveBayesClassifier.cs )
135+ * [ Image Recognition] ( test/TensorFlowNET.Examples/ImageRecognition.cs )
136+ * [ K-means Clustering] ( test/TensorFlowNET.Examples/KMeansClustering.cs )
137+ * [ NN XOR] ( test/TensorFlowNET.Examples/NeuralNetXor.cs )
138+ * [ Object Detection] ( test/TensorFlowNET.Examples/ObjectDetection.cs )
80139* [ Text Classification] ( test/TensorFlowNET.Examples/TextClassificationWithMovieReviews.cs )
81140* [ CNN Text Classification] ( test/TensorFlowNET.Examples/CnnTextClassification.cs )
82- * [ Naive Bayes Classification ] ( test/TensorFlowNET.Examples/NaiveBayesClassifier.cs )
141+
83142* [ Named Entity Recognition] ( test/TensorFlowNET.Examples/NamedEntityRecognition.cs )
84143
85144### Contribute:
@@ -94,7 +153,7 @@ You can:
94153* Debug one of the unit tests that is marked as Ignored to get it to work (can be challenging)
95154* Debug one of the not yet working examples and get it to work (hard)
96155
97- How to debug unit tests:
156+ ### How to debug unit tests:
98157
99158The best way to find out why a unit test is failing is to single step it in C# and its pendant Python at the same time to see where the flow of execution digresses or where variables exhibit different values. Good Python IDEs like PyCharm let you single step into the tensorflow library code.
100159
0 commit comments