forked from SciSharp/TensorFlow.NET
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBasicLinearModel.cs
More file actions
64 lines (55 loc) · 2.32 KB
/
Copy pathBasicLinearModel.cs
File metadata and controls
64 lines (55 loc) · 2.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System;
using Tensorflow;
using static Tensorflow.Binding;
namespace TensorFlowNET.UnitTest.Training
{
[TestClass]
public class BasicLinearModel
{
/// <summary>
/// Linear Regression without tf.train.Optimizer
/// https://www.tensorflow.org/tutorials/customization/custom_training
/// </summary>
[TestMethod]
public void LinearRegression()
{
// Initialize the weights to `5.0` and the bias to `0.0`
// In practice, these should be initialized to random values (for example, with `tf.random.normal`)
var W = tf.Variable(5.0f);
var b = tf.Variable(0.0f);
// Define linear model
Func<Tensor, Tensor> model = (x) => W * x + b;
// Define the loss function
Func<Tensor, Tensor, Tensor> loss = (target_y, predicted_y)
=> tf.reduce_mean(tf.square(target_y - predicted_y));
int NUM_EXAMPLES = 1000;
float TRUE_W = 3.0f;
float TRUE_b = 2.0f;
var inputs = tf.random.normal(shape: NUM_EXAMPLES);
var noise = tf.random.normal(shape: NUM_EXAMPLES);
var outputs = inputs * TRUE_W + TRUE_b + noise;
Tensor init_loss = loss(model(inputs), outputs);
// print($"Current loss: {init_loss.numpy()}");
// Define a training loop
Func<Tensor, Tensor, float, Tensor> train = (inputs, outputs, learning_rate)
=>
{
using var t = tf.GradientTape();
var current_loss = loss(outputs, model(inputs));
var (dW, db) = t.gradient(current_loss, (W, b));
W.assign_sub(learning_rate * dW);
b.assign_sub(learning_rate * db);
return current_loss;
};
var epochs = range(10);
foreach (var epoch in epochs)
{
var current_loss = train(inputs, outputs, 0.1f);
print($"Epoch {epoch}: W={(float)W.numpy()} b={(float)b.numpy()}, loss={(float)current_loss.numpy()}");
if (epoch > 0) // skip first epoch
Assert.IsTrue((bool)(current_loss < init_loss));
}
}
}
}