Gradient descent algorithm won't converge

前端 未结 6 1069
轻奢々
轻奢々 2021-02-06 05:42

I\'m trying to write out a bit of code for the gradient descent algorithm explained in the Stanford Machine Learning lecture (lecture 2 at around 25:00). Below is the implementa

相关标签:
6条回答
  • 2021-02-06 06:10

    If I understand you correctly, your training set only has a non-zero gradient at the edge of a line? Unless you start at the line (actually start exactly at one of your training points) you won't find the line. You are always at a local minimum.

    0 讨论(0)
  • 2021-02-06 06:16

    I have experienced the same problem (albeit in Java) because my learning rate was too big.
    For short, I was using α = 0.001 and I had to push it to 0.000001 to see actual convergence.

    Of course these values are linked to your dataset.

    0 讨论(0)
  • 2021-02-06 06:27

    use backtracking line search to guaranty convergence. It is very simple to implement. See Stephen Boyd, Convex Optimization for reference. You can choose some standard alpha, beta values for backtracking line search, for example 0.3 and 0.8.

    0 讨论(0)
  • 2021-02-06 06:29

    It's not clean from your description what problem you're solving. Also it's very dangerous to post links to external resources - you can be blocked in stackoverflow.

    In any case - gradient descend method and (subgradient descend too) with fixed step size (ML community call it learning rate) should not necesseray converge.

    p.s. Machine Learning community is not interesting in "convergence condition" and "convergence to what" - they are interested in create "something" which pass cross-validation with good result.

    If you're curious about optimization - start to look in convex optimization. Unfortunately it's hard to find job on it, but it append clean vision into what happens in various math optimization things.

    Here is source code which demonstrate it for simple quadratic objective:

    #!/usr/bin/env python
    # Gradiend descend method (without stepping) is not converged for convex         
    # objective
    
    alpha = 0.1
    
    #k = 10.0 # jumping around minimum
    k = 20.0   # diverge
    #k = 0.001  # algorithm converged but gap to the optimal is big
    
    def f(x): return k*x*x
    def g(x): return 2*k*x
    
    x0 = 12
    xNext = x0
    i = 0
    threshold = 0.01
    
    while True:
        i += 1
        xNext = xNext + alpha*(-1)*(g(xNext))
        obj = (xNext)
        print "Iteration: %i, Iterate: %f, Objective: %f, Optimality Gap: %f" % (i, xNext, obj, obj - f(0.0))
    
        if (abs(g(xNext)) < threshold):
            break
        if i > 50:
            break
    
    print "\nYou launched application with x0=%f,threshold=%f" % (x0, threshold)
    
    0 讨论(0)
  • 2021-02-06 06:30

    When your cost function increases or cycles up and down, you usually have too large a value for alpha. What alpha are you using?

    Start out with an alpha = 0.001 and see if that converges? If not try various alphas (0.003, 0.01, 0.03, 0.1, 0.3, 1) and find one that converges quickly.

    Scaling the data (normalization) won't help you with only 1 feature (your theta[1]) as normalization only applies to 2+ features (multivariate linear regression).

    Also bear in mind that for a small number of features you can use the Normal Equation to get the correct answer.

    0 讨论(0)
  • 2021-02-06 06:31

    Your implementation is good. Generally, stochastic gradient descent might diverge when α is too large. What you would do with a large dataset is take a reasonably sized random sample, find α that gives you the best results, and then use it for the rest.

    0 讨论(0)
提交回复
热议问题