Skip to content

Commit

Permalink
corrected SGT functionality, added list comprehension to the objectiv…
Browse files Browse the repository at this point in the history
…e functions
  • Loading branch information
JoKoum committed Nov 26, 2021
1 parent 5741494 commit 26e3e3d
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 54 deletions.
61 changes: 19 additions & 42 deletions StochasticGradientTree.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,40 +101,37 @@ def transformFeatures(self, X):
fx[fx.columns[i]] = np.array(fx[fx.columns[i]].values, dtype=np.int64)
return fx

def fit(self, X, y):
pass

def predict(self, X):
pass

def predict_proba(self, X):
pass
def _train(self, x, y):


class StochasticGradientTreeClassifier(StochasticGradientTree):
def __init__(self, objective=SoftmaxCrossEntropy(), bins=64, batch_size=200, epochs=20, m_lambda=0.1, gamma=1.0):
super().__init__(objective, bins, batch_size, epochs, m_lambda, gamma)
pred = [self.tree.predict(x)]
gradHess = self.objective.computeDerivatives([y], pred)
self.tree.update(x, gradHess[0])

def fit(self, X, y):

X, featureInfo = self.createFeatures(X)
self.tree = StreamingGradientTree(featureInfo, self.options)

X = X.values

try:
y = y.values
except:
pass

for _ in range(self.epochs):

pred = [self.tree.predict(x) for x in X]
target = np.float64(y)

gradHess = self.objective.computeDerivatives(target,pred)

[self.tree.update(x, gh) for x, gh in zip(X, gradHess)]
[[self._train(x, yi) for x, yi in zip(X,y)] for _ in range(self.epochs)]

def predict(self, X):
pass

def predict_proba(self, X):
pass


class StochasticGradientTreeClassifier(StochasticGradientTree):
def __init__(self, objective=SoftmaxCrossEntropy(), bins=64, batch_size=200, epochs=20, m_lambda=0.1, gamma=1.0):
super().__init__(objective, bins, batch_size, epochs, m_lambda, gamma)
self._estimator_type = 'classifier'

def predict(self, X):

Expand All @@ -159,27 +156,7 @@ def predict_proba(self, X):
class StochasticGradientTreeRegressor(StochasticGradientTree):
def __init__(self, objective=SquaredError(), bins=64, batch_size=200, epochs=20, m_lambda=0.1, gamma=1.0):
super().__init__(objective, bins, batch_size, epochs, m_lambda, gamma)

def fit(self, X, y):

X, featureInfo = self.createFeatures(X)
self.tree = StreamingGradientTree(featureInfo, self.options)

X = X.values

try:
y = y.values
except:
pass

for _ in range(self.epochs):

pred = [self.tree.predict(x) for x in X]
target = np.float64(y)

gradHess = self.objective.computeDerivatives(target,pred)

[self.tree.update(x, gh) for x, gh in zip(X, gradHess)]
self._estimator_type = 'regressor'

def predict(self, X):

Expand Down
10 changes: 5 additions & 5 deletions utils/SoftmaxCrossEntropy.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,13 @@

class SoftmaxCrossEntropy:
def computeDerivatives(self, groundTruth, raw):
result = [GradHess() for _ in range(len(raw))]
predictions = self.transfer(raw)

for i in range(len(result)):
result[i] = GradHess(predictions[i] - groundTruth[i], predictions[i] * (1.0 - predictions[i]))

return result
return [
GradHess(
predictions[i] - groundTruth[i],
predictions[i] * (1.0 - predictions[i]))
for i in range(len(raw))]

@staticmethod
def transfer(raw):
Expand Down
12 changes: 5 additions & 7 deletions utils/SquaredError.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from utils.GradHess import GradHess
import numpy as np

class SquaredError:
@staticmethod
def computeDerivatives(groundTruth, raw):
result = [GradHess() for _ in range(len(raw))]

for i in range(len(result)):
result[i] = GradHess(raw[i] - groundTruth[i], 1.0 )

return result
def computeDerivatives(groundTruth, raw):
return [
GradHess(raw[i] - groundTruth[i], 1.0 )
for i in range(len(raw))]

0 comments on commit 26e3e3d

Please sign in to comment.