Gradient descent vs. random gradient descent (implementation of Python)

Source: Internet
Author: User

1 #-*-coding:cp936-*-2 ImportNumPy as NP3 ImportMatplotlib.pyplot as Plt4 5 6 #Construct Training Data7x = Np.arange (0., 10., 0.2)8m = Len (x)#number of training data points9x0 = Np.full (M, 1.0)                 TenInput_data = Np.vstack ([x0, X]). T#bias B as the first component of a weight vector OneTarget_data = 2 * x + 5 +Np.random.randn (m) A  -  - #two types of termination conditions theLoop_max = 10000#Maximum number of iterations (to prevent a dead loop) -Epsilon = 1e-3 -  - #Initialize weight value + np.random.seed (0) -W = NP.RANDOM.RANDN (2) + #w = Np.zeros (2) A  atAlpha = 0.001#Step Size (Note that the value is too long to cause oscillation, slow convergence rate) -diff =0. -Error = Np.zeros (2)  -Count = 0#Number of Cycles -finish = 0#Stop Flag - #-------------------------------------------stochastic gradient descent algorithm----------------------------------------------------------  in " " - While count < loop_max: to count + = 1 +  - # Traverse Training data set, update weights continuously the For I in range (m): * diff = Np.dot (w, input_data[i])-target_data[i] # Training set generation, calculating error values $ Panax Notoginseng # Use a random gradient descent algorithm to update weights using only one set of training data - w = W-alpha * diff * Input_data[i] the  + #------------------------------termination condition judgment----------------------------------------- A # If not terminated, continue to read the sample for processing, and if all the samples have been read, then the loop resumes reading the sample from the beginning to process.  the  + #----------------------------------termination condition judgment----------------------------------------- - Note: There are several iteration termination conditions, and the position of the judgment statement. Termination judgment can be placed after the weight vector is updated once, or can be placed after the update m times.  $ if Np.linalg.norm (W-error) < epsilon: # termination Condition: The absolute error of weight vectors calculated before and after two times is sufficiently small $ finish = 1 -  Break - Else: the error = W - print ' Loop count =%d '% count, ' \tw:[%f,%f] '% (w[0], w[1])Wuyi " " the  -  Wu #-----------------------------------------------Gradient Descent method-----------------------------------------------------------  -  whileCount <Loop_max: AboutCount + = 1 $  -     #The standard gradient drop is a summary error for all samples prior to the weight update, and the weighted value of the random gradient descent is updated by examining a training sample. -     #in the standard gradient descent, each step of the weight update sums the multiple samples and requires more computation -Sum_m = Np.zeros (2) A      forIinchRange (m): +dif = (Np.dot (w, input_data[i])-target_data[i]) *Input_data[i] theSum_m = Sum_m +dif -  $w = W-alpha * sum_m#Note the value of step alpha, which leads to oscillation the     #w = w-0.005 * sum_m # Alpha takes 0.005 to generate oscillation and requires an alpha to be smaller the      the     #determine if it is convergent the     ifNp.linalg.norm (W-error) <Epsilon: -finish = 1 in          Break the     Else: theError =W About Print 'Loop count =%d'% count,'\tw:[%f,%f]'% (W[0], w[1]) the  the  thePlt.plot (x, Target_data,'k +') +Plt.plot (x, w[1] * x + w[0],'R') -Plt.show ()

Reference: Http://www.tuicool.com/articles/MRbee2i

Gradient descent vs. random gradient descent (implementation of Python)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.