-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
7d1d541
commit 29f62db
Showing
9 changed files
with
205 additions
and
58 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
from .tree import Tree | ||
from .linear import Linear | ||
from .krr import KRR | ||
from .base_model import BaseModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
import cunumeric as cn | ||
|
||
from ..utils import solve_singular | ||
from .base_model import BaseModel | ||
|
||
|
||
def l2(X, Y): | ||
XX = cn.einsum("ij,ij->i", X, X)[:, cn.newaxis] | ||
YY = cn.einsum("ij,ij->i", Y, Y) | ||
XY = 2 * cn.dot(X, Y.T) | ||
return XX + YY - XY | ||
|
||
|
||
def rbf_kernel(X, Y, sigma=1.0): | ||
K = l2(X, Y) | ||
return cn.exp(-K / (2 * sigma**2)) | ||
|
||
|
||
class KRR(BaseModel): | ||
def __init__(self, n_components=10, alpha=1.0): | ||
self.num_components = n_components | ||
self.alpha = alpha | ||
|
||
def _fit_components(self, X, g, h) -> "KRR": | ||
# fit with fixed set of components | ||
K = rbf_kernel(X, self.X_train) | ||
num_outputs = g.shape[1] | ||
self.bias_ = cn.zeros(num_outputs) | ||
self.betas_ = cn.zeros((self.X_train.shape[0], num_outputs)) | ||
|
||
for k in range(num_outputs): | ||
W = cn.sqrt(h[:, k]) | ||
Kw = K * W[:, cn.newaxis] | ||
diag = cn.eye(Kw.shape[1]) * self.alpha | ||
KtK = cn.dot(Kw.T, Kw) + diag | ||
yw = W * (-g[:, k] / h[:, k]) | ||
self.betas_[:, k] = solve_singular(KtK, cn.dot(Kw.T, yw)) | ||
return self | ||
|
||
def fit( | ||
self, | ||
X: cn.ndarray, | ||
g: cn.ndarray, | ||
h: cn.ndarray, | ||
) -> "KRR": | ||
usable_num_components = min(X.shape[0], self.num_components) | ||
self.indices = self.random_state.permutation(X.shape[0])[:usable_num_components] | ||
self.X_train = X[self.indices] | ||
return self._fit_components(X, g, h) | ||
|
||
def predict(self, X): | ||
K = rbf_kernel(X, self.X_train) | ||
return K.dot(self.betas_) | ||
|
||
def clear(self) -> None: | ||
self.betas_.fill(0) | ||
|
||
def update( | ||
self, | ||
X: cn.ndarray, | ||
g: cn.ndarray, | ||
h: cn.ndarray, | ||
) -> "KRR": | ||
return self._fit_components(X, g, h) | ||
|
||
def __str__(self) -> str: | ||
return ( | ||
"Components: " | ||
+ str(self.X_train) | ||
+ "\nCoefficients: " | ||
+ str(self.betas_) | ||
+ "\n" | ||
) | ||
|
||
def __eq__(self, other: object) -> bool: | ||
return (other.betas_ == self.betas_).all() and ( | ||
other.X_train == self.X_train | ||
).all() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import numpy as np | ||
import pytest | ||
|
||
import cunumeric as cn | ||
import legateboost as lb | ||
|
||
from ..utils import non_increasing | ||
|
||
|
||
@pytest.mark.parametrize("num_outputs", [1, 5]) | ||
def test_improving_with_components(num_outputs): | ||
rs = cn.random.RandomState(0) | ||
X = rs.random((100, 10)) | ||
g = rs.normal(size=(X.shape[0], num_outputs)) | ||
h = rs.random(g.shape) + 0.1 | ||
X, g, h = cn.array(X), cn.array(g), cn.array(h) | ||
y = -g / h | ||
metrics = [] | ||
for n_components in range(1, 15): | ||
model = ( | ||
lb.models.KRR(n_components=n_components) | ||
.set_random_state(np.random.RandomState(2)) | ||
.fit(X, g, h) | ||
) | ||
predict = model.predict(X) | ||
loss = ((predict - y) ** 2 * h).sum(axis=0) / h.sum(axis=0) | ||
loss = loss.mean() | ||
metrics.append(loss) | ||
|
||
assert non_increasing(metrics) | ||
|
||
|
||
@pytest.mark.parametrize("num_outputs", [1, 5]) | ||
def test_alpha(num_outputs): | ||
# higher alpha hyperparameter should lead to smaller coefficients | ||
rs = cn.random.RandomState(0) | ||
X = rs.random((100, 10)) | ||
g = rs.normal(size=(X.shape[0], num_outputs)) | ||
h = rs.random(g.shape) + 0.1 | ||
X, g, h = cn.array(X), cn.array(g), cn.array(h) | ||
norms = [] | ||
for alpha in np.linspace(0.0, 2.5, 5): | ||
model = ( | ||
lb.models.KRR(alpha=alpha) | ||
.set_random_state(np.random.RandomState(2)) | ||
.fit(X, g, h) | ||
) | ||
norms.append(np.linalg.norm(model.betas_)) | ||
assert non_increasing(norms) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters