Skip to content

Instantly share code, notes, and snippets.

View zjf's full-sized avatar

Jianfeng Zhu zjf

  • Baidu, IDL
  • Beijing
View GitHub Profile
@zjf
zjf / parallel.py
Created December 10, 2018 12:44 — forked from thomwolf/parallel.py
Data Parallelism in PyTorch for modules and losses
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
## Created by: Hang Zhang, Rutgers University, Email: [email protected]
## Modified by Thomas Wolf, HuggingFace Inc., Email: [email protected]
## Copyright (c) 2017-2018
##
## This source code is licensed under the MIT-style license found in the
## LICENSE file in the root directory of this source tree
##+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
"""Encoding Data Parallel"""
@zjf
zjf / selecting_cmp.R
Created March 28, 2014 02:53
data.table - boosting performance on selecting rows based on specific column values.
library(data.table)
library(rbenchmark)
library(doMC)
registerDoMC(4)
set.seed(42)
nrows = 1e5
nsearch = 1e3
d = data.frame(a = sample(letters, nrows, rep = T), b = sample(1:nrows, rep = T), c = runif(nrows))
d.dt = data.table(d, key = 'a,b')
@zjf
zjf / lazy_man_svm.R
Last active December 20, 2015 17:48
Lazy Man's SVM | add bias term and regulation term, based on http://www.weibo.com/1459604443/A3x1VtIQn
set.seed(1001)
x = rbind(cbind(rnorm(50, 1, 0.3), rnorm(50, 2, 0.3)), cbind(rnorm(50, 2, 0.3), rnorm(50, 1, 0.3)))
y = c(rep(1, 50), rep(-1, 50))
h <- function(param, C = 1){
b = param[length(param)]
w = param[-length(param)]
g = y * (x %*% w + b)
0.5 * w %*% w + C * sum(1 - g[g < 1])
}
@zjf
zjf / c_which.R
Last active December 16, 2015 06:39
An equivalent which() function in Rcpp
cppFunction('
std::vector<int> c_which(NumericVector x, Function f, List args){
std::vector<int> ind = as< std::vector<int> >(f(x, args));
std::vector<int> out(ind);
std::vector<int>::iterator it;
int j = 0;
it = std::find(ind.begin(), ind.end(), 1);
while(it++ != ind.end()){
out[j++] = it - ind.begin();
it = std::find(it, ind.end(), 1);
@zjf
zjf / bg.R
Last active December 14, 2015 06:39
Backpropogation Algorithm
## Demo
set.seed(12321)
W_all_test1 = list(matrix(rnorm(1, 0, 0.5), nc = 1), matrix(10, nc = 1))
b_all_test1 = list(matrix(rnorm(1, 0, 0.5), nc = 1), matrix(-5, nc = 1))
X = matrix(rnorm(200, 0, 2), ncol = 1)
Y = apply(X, 1, function(x){forward_propagation(x, W_all_test1, b_all_test1)})
X.train = X[1:150,]
Y.train = Y[1:150]
X.test = X[151:200,]
Y.test = Y[151:200]