Skip to content

Instantly share code, notes, and snippets.

View zhpmatrix's full-sized avatar
🎯
Focusing

张大千 zhpmatrix

🎯
Focusing
View GitHub Profile
def dropout(x, level, noise_shape=None, seed=None):
"""Sets entries in `x` to zero at random,
while scaling the entire tensor.
# Arguments
x: tensor
level: fraction of the entries in the tensor
that will be set to 0.
noise_shape: shape for randomly generated keep/drop flags,
must be broadcastable to the shape of `x`
@zhpmatrix
zhpmatrix / Coding Tricks from Ng
Created September 15, 2017 07:14
So many cool coding tricks from course 《Deep Learning》of Andrew Ng
# initialize the parameters of logistic regression
def initialize_with_zeros(dim):
"""
This function creates a vector of zeros of shape (dim, 1) for w and initializes b to 0.
Argument:
dim -- size of the w vector we want (or number of parameters in this case)
Returns:
@zhpmatrix
zhpmatrix / 代码笔记
Created September 6, 2017 13:35
代码笔记
返回Logistic Regression中的预测错误的样本:
float(sum( labels != (preds > 0.0) ) ) / len(labels)
核心逻辑:对于Sigmoid函数,y = 1 / 1 + exp(-x),当x > 0 时,y = 1, 当 x < 0 时, y = 0.
@zhpmatrix
zhpmatrix / custom_loss.py
Created June 29, 2017 08:39
对数似然损失,没有化简的情形
def custom_loss(y_pre,D_label): #别人的自定义损失函数
label=D_label.get_label()
penalty=2.0
grad=-label/y_pre+penalty*(1-label)/(1-y_pre) #梯度
hess=label/(y_pre**2)+penalty*(1-label)/(1-y_pre)**2 #2阶导
return grad,hess
@zhpmatrix
zhpmatrix / XGBRegressor.py
Created June 29, 2017 08:26
XGBOOST plot_importance
from sklearn.model_selection import train_test_split,GridSearchCV
from sklearn.preprocessing import OneHotEncoder
from sklearn.metrics import mean_squared_error
from sklearn.metrics import r2_score
import pandas as pd
import scipy as sp
import xgboost as xgb
import matplotlib.pyplot as plt
def f1_error(preds,dtrain):
label=dtrain.get_label()
preds = 1.0/(1.0+np.exp(-preds))
pred = [int(i >= 0.5) for i in preds]
tp = sum([int(i == 1 and j == 1) for i,j in zip(pred,label)])
precision=float(tp)/sum(pred)
recall=float(tp)/sum(label)
return 'f1-score',2 * ( precision*recall/(precision+recall) )
@zhpmatrix
zhpmatrix / custom_objective.py
Created June 29, 2017 07:00
custom objective and evaluation metric
#!/usr/bin/python
import numpy as np
import xgboost as xgb
###
# advanced: customized loss function
#
print ('start running example to used customized objective function')
dtrain = xgb.DMatrix('agaricus.txt.train')
dtest = xgb.DMatrix('agaricus.txt.test')
@zhpmatrix
zhpmatrix / exchange data.c
Created June 20, 2017 06:07
a simple demo to example data with diff thread
#include"mpi.h"
int main(int argc,char *argv[])
{
char message[20]="";
int myrank;
MPI_Status status;
MPI_Init(&argc,&argv);
MPI_Comm_rank(MPI_COMM_WORLD,&myrank);
if(myrank==0)
{/*先将字符串拷贝到发送缓冲区message中,然后调用MPI_Send语句将它发出,用
@zhpmatrix
zhpmatrix / tuple.cpp
Created May 15, 2017 07:24
boost 1.55 tuple
#include <iostream>
#include <boost/tuple/tuple.hpp>
#include <boost/tuple/tuple_comparison.hpp>
#include <boost/tuple/tuple_io.hpp>
using namespace boost;
int main(){
tuple<int, char, float> t(2, 'a', 0.9);
  std::cout << t << std::endl;
  return 0;
}
@zhpmatrix
zhpmatrix / group.py
Created May 11, 2017 02:20
how to decompose problems?
#################################################################
#desc: find the number of ways to divide series into diff sets
#author: zhpmatrix
#date: 2017-05-11
#################################################################
from itertools import combinations
def c(n,k):
return len([val for val in combinations(range(n),k)])