Skip to content

Commit 7263694

Browse files
committed
update docs for Linear Regression
1 parent 07e77e1 commit 7263694

9 files changed

Lines changed: 46 additions & 15 deletions

File tree

docs/source/ControlDependency.md

Lines changed: 0 additions & 4 deletions
This file was deleted.

docs/source/LinearRegression.md

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,68 @@
11
# Chapter. Linear Regression
22

3+
### What is linear regression?
4+
5+
Linear regression is a linear approach to modelling the relationship between a scalar response (or dependent variable) and one or more explanatory variables (or independent variables).
6+
7+
Consider the case of a single variable of interest y and a single predictor variable x. The predictor variables are called by many names: covariates, inputs, features; the predicted variable is often called response, output, outcome.
8+
9+
We have some data $D=\{x{\tiny i},y{\tiny i}\}$ and we assume a simple linear model of this dataset with Gaussian noise:
10+
311
```csharp
412
// Prepare training Data
513
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, 7.042f, 10.791f, 5.313f, 7.997f, 5.654f, 9.27f, 3.1f);
614
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, 2.827f, 3.465f, 1.65f, 2.904f, 2.42f, 2.94f, 1.3f);
715
var n_samples = train_X.shape[0];
816
```
17+
![regression dataset](_static/regression-dataset.png)
18+
19+
Based on the given data points, we try to plot a line that models the points the best. The red line can be modelled based on the linear equation: $y = wx + b$. The motive of the linear regression algorithm is to find the best values for $w$ and $b$. Before moving on to the algorithm, le's have a look at two important concepts you must know to better understand linear regression.
20+
21+
### Cost Function
22+
23+
The cost function helps us to figure out the best possible values for $w$ and $b$ which would provide the best fit line for the data points. Since we want the best values for $w$ and $b$, we convert this search problem into a minimization problem where we would like to minimize the error between the predicted value and the actual value.
24+
25+
![minimize-square-cost](_static/minimize-square-cost.png)
26+
27+
We choose the above function to minimize. The difference between the predicted values and ground truth measures the error difference. We square the error difference and sum over all data points and divide that
28+
value by the total number of data points. This provides the average squared error over all the data points. Therefore, this cost function is also known as the Mean Squared Error(MSE) function. Now, using this MSE
29+
function we are going to change the values of $w$ and $b$ such that the MSE value settles at the minima.
930

1031
```csharp
1132
// tf Graph Input
1233
var X = tf.placeholder(tf.float32);
1334
var Y = tf.placeholder(tf.float32);
1435

1536
// Set model weights
16-
// We can set a fixed init value in order to debug
17-
// var rnd1 = rng.randn<float>();
18-
// var rnd2 = rng.randn<float>();
19-
var W = tf.Variable(-0.06f, name: "weight");
20-
var b = tf.Variable(-0.73f, name: "bias");
37+
var W = tf.Variable(rng.randn<float>(), name: "weight");
38+
var b = tf.Variable(rng.randn<float>(), name: "bias");
2139

2240
// Construct a linear model
2341
var pred = tf.add(tf.multiply(X, W), b);
2442

2543
// Mean squared error
2644
var cost = tf.reduce_sum(tf.pow(pred - Y, 2.0f)) / (2.0f * n_samples);
45+
```
46+
47+
### Gradient Descent
48+
49+
The another important concept needed to understand is gradient descent. Gradient descent is a method of updating $w$ and $b$ to minimize the cost function. The idea is that we start with some random values for $w$ and $b$ and then we change these values iteratively to reduce the cost. Gradient descent helps us on how to update the values or which direction we would go next. Gradient descent is also know as **steepest descent**.
50+
51+
![gradient-descent](_static/gradient-descent.png)
2752

53+
To draw an analogy, imagine a pit in the shape of U and you are standing at the topmost point in the pit and your objective is to reach the bottom of the pit. There is a catch, you can only take a discrete number
54+
of steps to reach the bottom. If you decide to take one step at a time you would eventually reach the bottom of the pit but this would take a longer time. If you choose to take longer steps each time, you would
55+
reach sooner but, there is a chance that you could overshoot the bottom of the pit and not exactly at the bottom. In the gradient descent algorithm, the number of steps you take is the learning rate. This
56+
decides on how fast the algorithm converges to the minima.
57+
58+
```csharp
2859
// Gradient descent
2960
// Note, minimize() knows to modify W and b because Variable objects are trainable=True by default
3061
var optimizer = tf.train.GradientDescentOptimizer(learning_rate).minimize(cost);
3162
```
3263

64+
When we visualize the graph in TensorBoard:
65+
66+
![linear-regression](_static/linear-regression-tensor-board.png)
67+
68+
The full example is [here](https://github.com/SciSharp/TensorFlow.NET/blob/master/test/TensorFlowNET.Examples/LinearRegression.cs).
238 KB
Loading
72.7 KB
Loading
15.9 KB
Loading
14 KB
Loading

docs/source/index.rst

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,8 @@ Welcome to TensorFlow.NET's documentation!
2222
Graph
2323
Session
2424
Operation
25-
ControlDependency
2625
Gradient
2726
Train
2827
EagerMode
29-
ImageRecognition
30-
LinearRegression
28+
LinearRegression
29+
ImageRecognition

src/TensorFlowNET.Core/Graphs/Graph.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -196,11 +196,11 @@ public unsafe Operation create_op(string op_type, Tensor[] inputs, TF_DataType[]
196196

197197
_create_op_helper(op, true);
198198

199-
/*Console.Write($"create_op: {op_type} '{node_def.Name}'");
199+
Console.Write($"create_op: {op_type} '{node_def.Name}'");
200200
Console.Write($", inputs: {(inputs.Length == 0 ? "empty" : String.Join(", ", inputs.Select(x => x.name)))}");
201201
Console.Write($", control_inputs: {(control_inputs.Length == 0 ? "empty" : String.Join(", ", control_inputs.Select(x => x.name)))}");
202202
Console.Write($", outputs: {(op.outputs.Length == 0 ? "empty" : String.Join(", ", op.outputs.Select(x => x.name)))}");
203-
Console.WriteLine();*/
203+
Console.WriteLine();
204204

205205
return op;
206206
}

test/TensorFlowNET.Examples/LinearRegression.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ public class LinearRegression : Python, IExample
1616

1717
// Parameters
1818
float learning_rate = 0.01f;
19-
int training_epochs = 1000;
19+
int training_epochs = 10000;
2020
int display_step = 50;
2121

2222
public void Run()

0 commit comments

Comments
 (0)