Skip to content

Instantly share code, notes, and snippets.

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)
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
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)
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_
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
def set_weights(intercept, coef, classes, model=linear_model.SGDClassifier()):
model.intercept_ = intercept
model.coef_ = coef
model.classes_ = classes
return model
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
# 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
@phil8192
phil8192 / wapply.R
Last active April 11, 2019 19:59
roll apply with window of indices
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
@phil8192
phil8192 / waterflow.R
Created November 2, 2018 19:10
R solution to twitter waterflow problem
# 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