-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improved some of the models (used for ordinal classification)
- Loading branch information
Showing
12 changed files
with
392 additions
and
166 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
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,82 @@ | ||
from keras.models import Model | ||
from keras.layers import Input, Dense | ||
from keras import regularizers | ||
from keras.callbacks import EarlyStopping | ||
from sklearn.base import BaseEstimator, ClassifierMixin | ||
from sklearn.preprocessing import OneHotEncoder | ||
import numpy as np | ||
|
||
# Ordinal network encoding: | ||
# http://orca.st.usm.edu/~zwang/files/rank.pdf | ||
|
||
def create_model(nfeatures, nhidden, l2, K, is_ordinal): | ||
reg = regularizers.l2(l2) if l2 else None | ||
|
||
input_layer = Input([nfeatures]) | ||
hidden = Dense( | ||
nhidden, activation='tanh', kernel_regularizer=reg)(input_layer) | ||
if is_ordinal: | ||
act = 'softmax' | ||
else: | ||
act = 'sigmoid' | ||
output = Dense(K, activation=act)(hidden) | ||
|
||
model = Model(input_layer, output) | ||
model.compile('adam', 'categorical_crossentropy') | ||
#model.summary() | ||
return model | ||
|
||
def class_weight(y): | ||
klasses = np.unique(y) | ||
count = np.bincount(y)[klasses] | ||
return len(y) / (len(klasses)*count) | ||
|
||
|
||
class MultiClassNet(BaseEstimator, ClassifierMixin): | ||
def __init__(self, nhidden, l2=0, balanced=False): | ||
self.nhidden = nhidden | ||
self.l2 = l2 | ||
self.balanced = balanced | ||
|
||
def fit(self, X, y): | ||
self.classes_ = np.unique(y) | ||
K = len(self.classes_) | ||
yy = OneHotEncoder(sparse=False).fit_transform(y[:, np.newaxis]) | ||
self.model = create_model(X.shape[1], self.nhidden, self.l2, K, False) | ||
cb = EarlyStopping('loss', 0.001, 10) | ||
ww = class_weight(y) if self.balanced else None | ||
self.logs = self.model.fit( | ||
X, yy, 128, 10000, 0, callbacks=[cb], class_weight=ww) | ||
return self | ||
|
||
def predict_proba(self, X): | ||
return self.model.predict(X) | ||
|
||
def predict(self, X): | ||
return np.argmax(self.model.predict(X), 1) | ||
|
||
|
||
class OrdinalNet(BaseEstimator, ClassifierMixin): | ||
def __init__(self, nhidden, l2=0, balanced=False): | ||
self.nhidden = nhidden | ||
self.l2 = l2 | ||
self.balanced = balanced | ||
|
||
def fit(self, X, y): | ||
self.classes_ = np.unique(y) | ||
K = len(self.classes_) | ||
yy = np.zeros((len(y), K), int) # ordinal encoding | ||
for i,_y in enumerate(y): | ||
yy[i, 0:_y+1] = 1 | ||
self.model = create_model(X.shape[1], self.nhidden, self.l2, K, True) | ||
cb = EarlyStopping('loss', 0.001, 10) | ||
ww = class_weight(y) if self.balanced else None | ||
self.logs = self.model.fit( | ||
X, yy, 128, 10000, 0, callbacks=[cb], class_weight=ww) | ||
return self | ||
|
||
def predict_proba(self, X): | ||
return self.model.predict(X) | ||
|
||
def predict(self, X): | ||
return np.argmax(self.model.predict(X), 1) |
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,58 @@ | ||
from keras.models import Model | ||
from keras.layers import Input, Dense, Subtract | ||
from keras import regularizers | ||
from keras.callbacks import EarlyStopping | ||
from sklearn.base import BaseEstimator, RegressorMixin | ||
import numpy as np | ||
|
||
def preprocess(X, y): | ||
K = len(np.unique(y)) | ||
N = len(X) | ||
Nk = np.bincount(y) | ||
|
||
X1 = np.repeat(X, N, 0) | ||
X2 = np.tile(X.T, N).T | ||
|
||
y1 = np.repeat(y, N) | ||
y2 = np.tile(y, N) | ||
yy = (y1 > y2) + (y1 == y2)*0.5 | ||
|
||
pairs = K*(K-1) | ||
ww = len(X1) / (pairs * (Nk[y1]*Nk[y2])) | ||
return X1, X2, yy, ww | ||
|
||
|
||
def create_model(nfeatures, nhidden, l2): | ||
reg = regularizers.l2(l2) if l2 else None | ||
|
||
input1 = Input([nfeatures]) | ||
input2 = Input([nfeatures]) | ||
|
||
hidden = Dense(nhidden, activation='tanh', kernel_regularizer=reg) | ||
|
||
output1 = hidden(input1) | ||
output2 = hidden(input2) | ||
diff = Subtract()([output1, output2]) | ||
output = Dense(1, activation='sigmoid')(diff) | ||
|
||
model = Model([input1, input2], output) | ||
model.compile('adam', 'binary_crossentropy') | ||
#model.summary() | ||
return model | ||
|
||
|
||
class RankNet(BaseEstimator, RegressorMixin): | ||
def __init__(self, nhidden, l2=0): | ||
self.nhidden = nhidden | ||
self.l2 = l2 | ||
|
||
def fit(self, X, y): | ||
self.model = create_model(X.shape[1], self.nhidden, self.l2) | ||
X1, X2, yy, ww = preprocess(X, y) | ||
cb = EarlyStopping('loss', 0.001, 10) | ||
self.logs = self.model.fit( | ||
[X1, X2], yy, 128, 10000, 0, callbacks=[cb], sample_weight=ww) | ||
return self | ||
|
||
def predict(self, X): | ||
return self.model.predict([X, np.zeros_like(X)])[:, 0] |
File renamed without changes.
File renamed without changes.
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,26 @@ | ||
from sklearn.base import BaseEstimator, ClassifierMixin | ||
from rank2ordinal.threshold import decide_thresholds | ||
import numpy as np | ||
|
||
|
||
class Rank2Ordinal(BaseEstimator, ClassifierMixin): | ||
def __init__(self, estimator, threshold_strategy='uniform'): | ||
self.estimator = estimator | ||
self.threshold_strategy = threshold_strategy | ||
|
||
def fit(self, X, y): | ||
self.classes_ = np.unique(y) | ||
self.estimator.fit(X, y) | ||
|
||
K = len(self.classes_) | ||
scores = self.estimator.predict(X) | ||
self.ths = decide_thresholds(scores, y, K, self.threshold_strategy) | ||
return self | ||
|
||
# this function passes the ranking score for use by some metrics | ||
def predict_proba(self, X): | ||
return self.estimator.predict(X) | ||
|
||
def predict(self, X): | ||
scores = self.estimator.predict(X) | ||
return np.sum(scores[:, np.newaxis] >= self.ths, 1) |
Oops, something went wrong.