Code Monkey home page Code Monkey logo

Comments (4)

hcho3 avatar hcho3 commented on May 2, 2024

@Sandy4321 It should suffice to replace LogisticLoss() with a squared error:

class SquaredError():
    def __init__(self):
        pass

    def loss(self, y, y_pred):
        return 0.5 * ((y - y_pred) ** 2)

    # gradient w.r.t y_pred
    def gradient(self, y, y_pred):
        return -(y - y_pred)

    # w.r.t y_pred
    def hess(self, y, y_pred):
        return 1

from ml-from-scratch.

Sandy4321 avatar Sandy4321 commented on May 2, 2024

Great thanks
Did you coded regularization
As in original xgboost they use derivation for regularization
As they wrote
Model Complexity¶
We have introduced the training step, but wait, there is one important thing, the regularization term! We need to define the complexity of the tree Ω(f). In order to do so, let us first refine the definition of the tree f(x) as

ft(x)=wq(x),w∈RT,q:Rd→{1,2,⋯,T}.
Here w is the vector of scores on leaves, q is a function assigning each data point to the corresponding leaf, and T is the number of leaves. In XGBoost, we define the complexity as

Ω(f)=γT+12λ∑j=1Tw2j
Of course, there is more than one way to define the complexity, but this one works well in practice. The regularization is one part most tree packages treat less carefully, or simply ignore. This was because the traditional treatment of tree learning only emphasized improving impurity, while the complexity control was left to heuristics. By defining it formally, we can get a better idea of what we are learning and obtain models that perform well in the wild.

from ml-from-scratch.

hcho3 avatar hcho3 commented on May 2, 2024

@Sandy4321 I don't think this example has all the regularization mechanism as XGBoost does, as the example is quite simplified. There are min_samples_split, min_impurity, and max_depth.

from ml-from-scratch.

shorey avatar shorey commented on May 2, 2024

@hcho3 hi, sorry to interrupt. I am trying to learn xgboost by this project. I come up with some problem with function "def _gain(self, y, y_pred):" in supervised_learning/decision_tree.py.

def _gain(self, y, y_pred):
nominator = np.power((y * self.loss.gradient(y, y_pred)).sum(), 2)
denominator = self.loss.hess(y, y_pred).sum()
return 0.5 * (nominator / denominator)

the variable nominator says ((y*self.loss.gradient(y, y_pred).sum())^2, but according to xgboost doc https://xgboost.readthedocs.io/en/latest/tutorials/model.html, shouldn't it be (self.loss.gradient(y, y_pred).sum())^2? I know I am wrong by changing this line to what I thought, because after changing this line the example just got wrong result. But I still don't know why it's like this. Could you explain it to me? thanks

from ml-from-scratch.

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.