Gradient descent with Golang

Wolfmib
2 min readJan 8, 2021

Since python is dominated in machine learning area, however, golang is super fast, so i decided to start basic-machine learning from golang myself.

  1. What’s is Gradient descent ? In order to find the min value of the LOSS Function, we need to fins the optimized parameter (w and b …). So Gradient descent is the one of the method to find it
  2. Example F(x) = X*X
the min value is located at X = 0
  1. So let’s start with random point (let’s say… x = 5)
  2. Then we check the slope at this position F’(X) : if the slope > 0, then we move left, if the slope <0 then we move right ? Why ? because we need to move close to the min. point.
  1. learning rate: how far you want to move from step1 to step 2 etc..

2. We set the start point at X=5.1 (in theorem, this function has only one min. point, so no-matter which point you choose, you will get the final training result near Zero !

3. loop 1000 times. Which means we training 1000 times.

4. The f’(x) =df(x)/dx = 2 * x , we use it to calculate each slope as X moving …

5. Finally we get the result X. ~0, Bon!!

the final result is -0.07, which is close to Zero.

Note: The python is script language. So Golang is definitely faster than Python. When facing the biggest CNN structure or mul-ti dimension of Gradient Descent. You will see .. in later post …

--

--

Wolfmib
0 Followers

Senior Gaming Actuary and Python AI Data Scientist and BackEnd Golang Developer !