Coder Social home page Coder Social logo

Feature request: Elastic net about flux.jl HOT 2 CLOSED

fluxml avatar fluxml commented on May 5, 2024
Feature request: Elastic net

from flux.jl.

Comments (2)

ylxdzsw avatar ylxdzsw commented on May 5, 2024 1

Since Lasso and Ridge models are linear models with L1 and L2 regularization, and ElasticNet is simply both, they can be implemented by a custom optimizer. To try this, first checkout this branch:

using Flux: Affine, train!, tobatch, Optimizer, WeightDecayConst, WeightDecayRatio, GradDecayFix, Accumulate

truth = Float32[0, 4, 2, 2, -3, 0, -1, 3, 2, -5]'

data = map(1:1024) do i
  x = rand(Float32, 10)
  x, truth * x + rand(Float32)
end

struct ElasticNet
  l1
  l2
end

Lasso(α) = ElasticNet(α, 0)
Ridge(α) = ElasticNet(0, α)

function fit!(m::ElasticNet, data)
  model = Affine(length(data[1][1]), 1)
  train!(model, data, epoch=50, opt=Optimizer([
    Accumulate(length(data) / 8),
    GradDecayFix(.002),
    WeightDecayRatio(m.l2),
    WeightDecayConst(m.l1)
  ]))
end

# train a lasso model
model = fit!(Lasso(5e-5), data)

# see the coefficients (for Lasso models, the 1st and 6th elements should be exactly 0)
@show model.W.x

# do prediction
@show model(tobatch(data[1][1]))[1], data[1][2]

SGD is not the best method to solve Lasso models, but nothing prevent it working. The optimizers currently not work with mxnet or tensorflow backend, but they will very soon.

from flux.jl.

MikeInnes avatar MikeInnes commented on May 5, 2024

Closing this for now as I don't think there's an action item for Flux here; this should be pretty easy to build on top if needed (and of course I'd be happy to have it in the library if it's generally useful).

from flux.jl.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.