Skip to content

Instantly share code, notes, and snippets.

View badbye's full-sized avatar
Dreaming

yalei badbye

Dreaming
  • China
View GitHub Profile
library(shiny)
library(shinyjs)
library(wordcloud2)
if (!require(EBImage)) {
source("https://bioconductor.org/biocLite.R")
biocLite("EBImage")
}
library(EBImage)
codeOut = StringIO.StringIO()
codeErr = StringIO.StringIO()
sys.stdout = codeOut
sys.stderr = codeErr
exec code
sys.stdout = sys.__stdout__
sys.stderr = sys.__stderr__
error_output = codeErr.getvalue().strip()
normal_output = codeOut.getvalue().strip()
codeOut.close()
@badbye
badbye / profileit.py
Created June 8, 2016 07:21
python profile decorator
import cProfile
def profileit(name):
def inner(func):
def wrapper(*args, **kwargs):
prof = cProfile.Profile()
retval = prof.runcall(func, *args, **kwargs)
# Note use of name from outer scope
prof.dump_stats(name)
return retval
logger <- mx.metric.logger$new()
mx.callback.plot.train.metric <- function(period, logger=NULL) {
function(iteration, nbatch, env, verbose=TRUE) {
if (nbatch %% period == 0 && !is.null(env$metric)) {
N = env$end.round
result <- env$metric$get(env$train.metric)
plot(c(0.5,1)~c(0,N), col=NA, ylab = paste0("Train-", result$name),xlab = "")
logger$train <- c(logger$train, result$value)
lines(logger$train, lwd = 3, col="red")
@badbye
badbye / learnRcpp.cpp
Created May 31, 2016 11:16
Learn to write cpp in R. [High performance functions with Rcpp](http://adv-r.had.co.nz/Rcpp.html)
#include <Rcpp.h>
using namespace Rcpp;
//[[Rcpp::export]]
bool allC(LogicalVector x){
int n = x.size();
for (int i = 0; i < n; i++){
if (x[i] == false){
return false;
}
# 最后一个问题当时给我的印象特别深,让人不由得赞叹怎么可以构造出这么离奇却又合理的概率命题。
# 想象现在有两个盒子,两个盒子里各有整数,我们只知道它们不相同,没有其它任何进一步的信息。
# 现在游戏主办方允许我们打开一个箱子,然后猜另一个里面的数字比我们看到这个数大还是小。初看之
# 下,似乎没有任何办法帮我们来确知另一个盒子里究竟是个啥。但现在,只要你有一个随机数发生器,
# 你就可以改变命运。不妨假设我们有一个均匀随机转盘,转盘转到的角度服从上的均匀分布。我们按如
# 下规则猜测:从两个盒子中随便挑一个数字,然后转动转盘。如果比我们猜的这个数大,就猜大,反之
# 则猜小。
#
# 作者:猪月
# 链接:https://www.zhihu.com/question/41408857/answer/90886492
# encoding: utf-8
'''
Created on 2016.04.21
@author: yalei
'''
import mxnet as mx
def cnn_text_network(num_class = 3, input_shape=(20, 300), conv_kernels = [3, 4, 5], num_filter = 100, drop_prob = 0.5):
'''
library(Rcpp)
cppFunction('NumericVector replace_na(NumericVector x) {
int n = x.size();
NumericVector out(n);
int temp = 0;
for(int i = 0; i < n; ++i) {
if (R_IsNA(x[i])){
out[i] = temp;
}else{
temp = x[i];
@badbye
badbye / punctuation.txt
Created April 26, 2016 09:43
punctuations(Chinese&English)
"#$%&'()*+,-/:;<=>@[\]^_`{|}~⦅⦆「」、 、〃〈〉《》「」『』【】〔〕〖〗〘〙〚〛〜〝〞〟〰〾〿–—‘’‛“”„‟…‧﹏﹑﹔·!?。。'!"#$%&\'()*+,-./:;<=>?@[\\]^_`{|}~'
@badbye
badbye / nnet_vs_mxnet.R
Last active April 21, 2016 02:42
求调试
## nnet
library(nnet)
n = nnet(Species ~ ., data = iris, size=2, maxit = 500, abstol=1e-6)
mean(predict(n, iris[, 1:4], type='class') == iris[, 5])
# 0.66 or 0.99
library(devtools)
source_url('https://gist.githubusercontent.com/fawda123/7471137/raw/466c1474d0a505ff044412703516c34f1a4684a5/nnet_plot_update.r')
plot.nnet(n)
## mxnet