1- using Newtonsoft . Json ;
2- using NumSharp . Core ;
1+ using NumSharp . Core ;
32using System ;
43using System . Collections . Generic ;
54using System . Text ;
@@ -13,64 +12,44 @@ namespace TensorFlowNET.Examples
1312 /// </summary>
1413 public class LinearRegression : Python , IExample
1514 {
16- private NumPyRandom rng = np . random ;
15+ NumPyRandom rng = np . random ;
16+
17+ // Parameters
18+ float learning_rate = 0.01f ;
19+ int training_epochs = 1000 ;
20+ int display_step = 50 ;
1721
1822 public void Run ( )
1923 {
20- var graph = tf . Graph ( ) . as_default ( ) ;
21-
22- // Parameters
23- float learning_rate = 0.01f ;
24- int training_epochs = 1000 ;
25- int display_step = 10 ;
26-
2724 // Training Data
2825 var train_X = np . array ( 3.3f , 4.4f , 5.5f , 6.71f , 6.93f , 4.168f , 9.779f , 6.182f , 7.59f , 2.167f ,
2926 7.042f , 10.791f , 5.313f , 7.997f , 5.654f , 9.27f , 3.1f ) ;
3027 var train_Y = np . array ( 1.7f , 2.76f , 2.09f , 3.19f , 1.694f , 1.573f , 3.366f , 2.596f , 2.53f , 1.221f ,
3128 2.827f , 3.465f , 1.65f , 2.904f , 2.42f , 2.94f , 1.3f ) ;
3229 var n_samples = train_X . shape [ 0 ] ;
3330
31+ var graph = tf . Graph ( ) . as_default ( ) ;
32+
3433 // tf Graph Input
3534 var X = tf . placeholder ( tf . float32 ) ;
3635 var Y = tf . placeholder ( tf . float32 ) ;
3736
3837 // Set model weights
39- //var rnd1 = rng.randn<float>();
40- //var rnd2 = rng.randn<float>();
38+ // We can set a fixed init value in order to debug
39+ // var rnd1 = rng.randn<float>();
40+ // var rnd2 = rng.randn<float>();
4141 var W = tf . Variable ( - 0.06f , name : "weight" ) ;
4242 var b = tf . Variable ( - 0.73f , name : "bias" ) ;
4343
44- var mul = tf . multiply ( X , W ) ;
45- var pred = tf . add ( mul , b ) ;
44+ // Construct a linear model
45+ var pred = tf . add ( tf . multiply ( X , W ) , b ) ;
4646
4747 // Mean squared error
48- var sub = pred - Y ;
49- var pow = tf . pow ( sub , 2.0f ) ;
50-
51- var reduce = tf . reduce_sum ( pow ) ;
52- var cost = reduce / ( 2.0f * n_samples ) ;
48+ var cost = tf . reduce_sum ( tf . pow ( pred - Y , 2.0f ) ) / ( 2.0f * n_samples ) ;
5349
5450 // radient descent
5551 // Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
56- var grad = tf . train . GradientDescentOptimizer ( learning_rate ) ;
57- var optimizer = grad . minimize ( cost ) ;
58-
59- //tf.train.export_meta_graph(filename: "linear_regression.meta.bin");
60- // import meta
61- // var new_saver = tf.train.import_meta_graph("linear_regression.meta.bin");
62- var text = JsonConvert . SerializeObject ( graph , new JsonSerializerSettings
63- {
64- Formatting = Formatting . Indented
65- } ) ;
66-
67- /*var cost = graph.OperationByName("truediv").output;
68- var pred = graph.OperationByName("Add").output;
69- var optimizer = graph.OperationByName("GradientDescent");
70- var X = graph.OperationByName("Placeholder").output;
71- var Y = graph.OperationByName("Placeholder_1").output;
72- var W = graph.OperationByName("weight").output;
73- var b = graph.OperationByName("bias").output;*/
52+ var optimizer = tf . train . GradientDescentOptimizer ( learning_rate ) . minimize ( cost ) ;
7453
7554 // Initialize the variables (i.e. assign their default value)
7655 var init = tf . global_variables_initializer ( ) ;
@@ -89,22 +68,33 @@ public void Run()
8968 sess . run ( optimizer ,
9069 new FeedItem ( X , x ) ,
9170 new FeedItem ( Y , y ) ) ;
92- var rW = sess . run ( W ) ;
9371 }
9472
9573 // Display logs per epoch step
96- /* if ((epoch + 1) % display_step == 0)
74+ if ( ( epoch + 1 ) % display_step == 0 )
9775 {
9876 var c = sess . run ( cost ,
9977 new FeedItem ( X , train_X ) ,
10078 new FeedItem ( Y , train_Y ) ) ;
101- var rW = sess.run(W);
102- Console.WriteLine($"Epoch: {epoch + 1} cost={c} " +
103- $"W={rW} b={sess.run(b)}");
104- }*/
79+ Console . WriteLine ( $ "Epoch: { epoch + 1 } cost={ c } " + $ "W={ sess . run ( W ) } b={ sess . run ( b ) } ") ;
80+ }
10581 }
10682
10783 Console . WriteLine ( "Optimization Finished!" ) ;
84+ var training_cost = sess . run ( cost ,
85+ new FeedItem ( X , train_X ) ,
86+ new FeedItem ( Y , train_Y ) ) ;
87+ Console . WriteLine ( $ "Training cost={ training_cost } W={ sess . run ( W ) } b={ sess . run ( b ) } ") ;
88+
89+ // Testing example
90+ var test_X = np . array ( 6.83f , 4.668f , 8.9f , 7.91f , 5.7f , 8.7f , 3.1f , 2.1f ) ;
91+ var test_Y = np . array ( 1.84f , 2.273f , 3.2f , 2.831f , 2.92f , 3.24f , 1.35f , 1.03f ) ;
92+ Console . WriteLine ( "Testing... (Mean square loss Comparison)" ) ;
93+ var testing_cost = sess . run ( tf . reduce_sum ( tf . pow ( pred - Y , 2.0f ) ) / ( 2.0f * test_X . shape [ 0 ] ) ,
94+ new FeedItem ( X , test_X ) ,
95+ new FeedItem ( Y , test_Y ) ) ;
96+ Console . WriteLine ( $ "Testing cost={ testing_cost } ") ;
97+ Console . WriteLine ( $ "Absolute mean square loss difference: { Math . Abs ( ( float ) training_cost - ( float ) testing_cost ) } ") ;
10898 } ) ;
10999 }
110100 }
0 commit comments