-1

I am beginner in machine learning.ihave problem in gradient descent algo.in the code, mentioned below, my doubt is during

first iteration value of x will be 1

second iteration value of x will be 2

third iteration value of x will be 3

fourth iteration value of x will be 4

fifth iteration value of x will be 5

then what will be the value of x for iterations 6 to 9999???

import numpy as np

import matplotlib.pyplot as plt

%matplotlib inline

def gradient_descent(x,y):

    m_curr = b_curr = 0
    rate = 0.01
    n = len(x)
    plt.scatter(x,y,color='red',marker='+',linewidth='5')
    for i in range(10000):
        y_predicted = m_curr * x + b_curr
        plt.plot(x,y_predicted,color='green')
        md = -(2/n)*sum(x*(y-y_predicted))
        yd = -(2/n)*sum(y-y_predicted)
        m_curr = m_curr - rate * md
        b_curr = b_curr - rate * yd

x = np.array([1,2,3,4,5])

y = np.array([5,7,9,11,13])

gradient_descent(x,y)
Matthieu Brucher
  • 21,634
  • 7
  • 38
  • 62

1 Answers1

0

Three variations for calculating batch gradient

  1. Batch
  2. Stochastic
  3. Mini-batch

I believe you are trying to implement the Batch variation. If that is the case you need to do the following

  1. change the for loop to run until the size of the x or y. i.e. len(x)
  2. call gradient_descent(x,y) in another for-loop with 10000 (or how many number of iterations you want to run). You should probably use a error function to determine this
Aravind Yarram
  • 78,777
  • 46
  • 231
  • 327