This file contains hidden or 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
| class Runner: | |
| def __init__(self, X, y): | |
| self.X = X | |
| self.y = y | |
| # called by FedAvg algo. | |
| def optimise(self, intercept_init, coef_init, hyperparameters): | |
| _intercept_init = intercept_init.copy() | |
| _coef_init = coef_init.copy() | |
| model = train_model(_intercept_init, _coef_init, self.X, self.y, **hyperparameters) |
This file contains hidden or 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
| def train_model(intercept_init, coef_init, X, y, epochs, lr, batch_size=None, randomise=True): | |
| if batch_size is None or batch_size <= 0: | |
| batch_size = X.shape[0] | |
| classes = np.unique(y) | |
| model = linear_model.SGDClassifier(loss='log', learning_rate='constant', eta0=lr, verbose=0) | |
| set_weights(intercept_init, coef_init, classes, model) | |
| batch_train(model, X, y, classes, epochs, batch_size, randomise) | |
| return model |
This file contains hidden or 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
| def batch_train(model, X, y, classes, epochs, batch_size, randomise): | |
| for _ in range(0, epochs): | |
| batch_update(model, X, y, classes, batch_size, randomise) |
This file contains hidden or 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
| def batch_update(model, X, y, classes, batch_size, randomise): | |
| if batch_size is None: | |
| batch_size = X.shape[0] | |
| for x_batch, y_batch in batches(X, y, batch_size, randomise): | |
| model.partial_fit(x_batch, y_batch, classes) | |
| return model.intercept_, model.coef_ |
This file contains hidden or 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
| def batches(X, y, batch_size, randomise): | |
| rows = X.shape[0] | |
| i = np.arange(rows) | |
| if randomise: | |
| np.random.shuffle(i) | |
| splits = rows / batch_size | |
| for x_batch, y_batch in zip(np.array_split(X[i, ], splits), | |
| np.array_split(y[i], splits)): | |
| yield x_batch, y_batch |
This file contains hidden or 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
| def set_weights(intercept, coef, classes, model=linear_model.SGDClassifier()): | |
| model.intercept_ = intercept | |
| model.coef_ = coef | |
| model.classes_ = classes | |
| return model |
This file contains hidden or 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
| library(homomorpheR) | |
| key.pair <- PaillierKeyPair$new(modulusBits=1024) | |
| encrypt <- function(x) key.pair$pubkey$encrypt(x) | |
| decrypt <- decrypt <- function(x) key.pair$getPrivateKey()$decrypt(x) | |
| "%+%" <- function(a, b) key.pair$pubkey$add(a, b) | |
| 20 == decrypt(encrypt(1) %+% encrypt(19)) # TRUE |
This file contains hidden or 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
| # convert | |
| # 7, 7, 6, 5, 5, 3, 3, 2, 0, 0 -> | |
| # 1 1 2 3 3 4 4 5 NA NA | |
| x <- c(7,7,6,5,5,3,3,2,0,0) | |
| ifelse(x,cumsum(c(1,abs(sign(diff(x))))),NA) | |
| # [1] 1 1 2 3 3 4 4 5 NA NA |
This file contains hidden or 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
| wapply <- function(x, window=1:10, fun=mean) { | |
| res <- rep(NA, length(x)) | |
| names(res) <- names(x) | |
| i <- max(window) + 1 | |
| while(i <= length(x)) { | |
| res[i] <- fun(x[i - window]) | |
| i <- i + 1 |
This file contains hidden or 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
| # 1 line | |
| function(x) sum(head(tail(pmin(cummax(x),rev(cummax(rev(x))))-x,-1),-1)) | |
| # or with fancy ascii visualisation! | |
| # | |
| # > water(x) | |
| # $water | |
| # [1] 10 | |
| # | |
| # $vis |