Logistic regression in Julia using Optim.jl

后端 未结 2 1950
攒了一身酷
攒了一身酷 2021-02-09 10:55

I\'m trying to implement a simple regularized logistic regression algorithm in Julia. I\'d like to use Optim.jl library to minimize my cost function, but I can\'t get it to work

2条回答
  •  南方客
    南方客 (楼主)
    2021-02-09 11:29

    Here is an example of unregularized logistic regression that uses the autodifferentiation functionality of Optim.jl. It might help you with your own implementation.

    using Optim
    
    const X = rand(100, 3)
    const true_β = [5,2,4]
    const true_y =  1 ./ (1 + exp(-X*true_β))
    
    function objective(β)
        y = 1 ./ (1 + exp(-X*β))
        return sum((y - true_y).^2)  # Use SSE, non-standard for log. reg.
    end
    
    println(optimize(objective, [3.0,3.0,3.0],
                    autodiff=true, method=LBFGS()))
    

    Which gives me

    Results of Optimization Algorithm
     * Algorithm: L-BFGS
     * Starting Point: [3.0,3.0,3.0]
     * Minimizer: [4.999999945789497,1.9999999853962256,4.0000000047769495]
     * Minimum: 0.000000
     * Iterations: 14
     * Convergence: true
       * |x - x'| < 1.0e-32: false
       * |f(x) - f(x')| / |f(x)| < 1.0e-08: false
       * |g(x)| < 1.0e-08: true
       * Exceeded Maximum Number of Iterations: false
     * Objective Function Calls: 53
     * Gradient Call: 53
    

提交回复
热议问题