Previous Story, we used the golang to implement simple gradient descent example.
This time, we will extend to the linear regress problem. How to do it with golang ? Instead of Python or Other tools !
First, we need to build up a problem and get some background knowledge.
Suppose we have 100 samples { x0,x1,x2,x3,….x99 } and {y0,y1,y2,….y99}
Assume the final best-fit function f(x) = Y = betaZero + betaOne * X
Now we need to find (betaZero, betaOne) {two parameters} But How ?
Well… there is a lot of way to doing this, but the most common method is the following: we compare the distance between the sample and target-fit-function.
1. the smaller distance means better fit-function !
Let’s sum all the distance between sample’s value (Yi) and expected value with the function Y(Xi) :
For point 1: Delta_Distance_One = Y0-betaZero-betaOne * X0
For point 2: Delta_Distance_Two = Y1-betaZero-betaOne * X1
……
For point 100: Delta_Distance_100 = Y99-betaZero-betaOne*X99
Then, we sum it up :
Delta_Distance_One + Delta_Distance_Two + …. + Delta_Distance_100
Wait… we need to prevent the minus-value, cause we are caring the distance !
Let’s make it square and sum up !
Final_Loss =
(Delta_Distance_One)* (Delta_Distance_One) +
(Delta_Distance_Two)*(Delta_Distance_Two) +
(Delta_Distance_Three)*(Delta_Distance_Three)+
….
(Delta_Distance_100)*(Delta_Distance_100)
The Loss Function which includes two parameters. Loss_F=F(BetaZero,BetaOne) , similar to the previous lecture f(x) is one dimension. Now , we only need to handle two dimensions , easy right ?
- The slope in BetaZero’s direction:
2. The slope in BetaOne’s direction:
Using F(x) = 3X + 2 to generate the similar data. So after training we shall get the result of (BetaZero, BetaOne) ~ (3, 2)
Generating data: Let’s make sample with X range from -1 to 1 :
Then, we do the Shuffle the x_value for these samples :
Then, using the following formula to make Y (variance: mean=0.0, std = 0.05)
Setting Initial Value of Beta0 , Beta1 and learning rate.
If you need the code, please clap and make the comment, I will make it when I had time to do that… Merci beaucoup !
#JA #Johnny #MachineLearning #Golang #GradientDescent #LinearRegression