Skip to content

Instantly share code, notes, and snippets.

@liangfu
liangfu / sorting.py
Created January 28, 2012 05:37
general sorting algorithms in python
#!/usr/bin/env python
from math import sqrt
import random
def bubble_sort(a):
"""
basic sorting method -- bubble sort
"""
s = len(a)
@liangfu
liangfu / QSound2.h
Created July 13, 2012 02:23
QSound interface with SDL_mixer implementation
/**
* @file QSound2.h
* @author Liangfu Chen <chenclf@gmail.com>
* @date Tue Jul 10 14:39:25 2012
*
* @brief QSound interface with SDL_mixer implementation
*
* Phonon is not the default option while compile Qt4.
* This class is designed to enable brief sound playback in
* the Linux environment.
#include <cstring>
#include <numeric>
#include <functional>
/**
* Hackers Delight: http://www.hackersdelight.org/HDcode/nlz.c.txt
*/
int pop(unsigned x) {
x = x - ((x >> 1) & 0x55555555);
x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
@liangfu
liangfu / cvquantile.cpp
Last active April 27, 2016 09:19
Compute quantile of input matrix for given value range from 0.0 to 1.0
/**
* compute quantile of input matrix for given value range from 0.0 to 1.0
*/
float cvQuantile(CvMat * src, float p)
{
p=MIN(1,MAX(0,p)); // eliminate overflow
int nr = src->rows, nc = src->cols;
CvMat * converted = cvCreateMat(nr,nc,CV_32F);
CvMat * reshaped = cvCreateMat(1,nr*nc,CV_32F);
CvMat * sorted = cvCreateMat(1,nr*nc,CV_32F);
@liangfu
liangfu / gist:eebbdb27c6a1fd616751
Created March 27, 2016 16:35 — forked from karpathy/gist:587454dc0146a6ae21fc
An efficient, batched LSTM.
"""
This is a batched LSTM forward and backward pass
"""
import numpy as np
import code
class LSTM:
@staticmethod
def init(input_size, hidden_size, fancy_forget_bias_init = 3):
@liangfu
liangfu / min-char-rnn.py
Created March 28, 2016 01:31 — forked from karpathy/min-char-rnn.py
Minimal character-level language model with a Vanilla Recurrent Neural Network, in Python/numpy
"""
Minimal character-level Vanilla RNN model. Written by Andrej Karpathy (@karpathy)
BSD License
"""
import numpy as np
# data I/O
data = open('input.txt', 'r').read() # should be simple plain text file
chars = list(set(data))
data_size, vocab_size = len(data), len(chars)
@liangfu
liangfu / MLPdemo.m
Last active April 29, 2016 07:02
demonstration of multi-layer perceptron with gradient checkings enabled
function MLPdemo()
net.alpha=0.002;
net.maxiter=200;
% hyperbolic - input layer
net.layer{1}.forward = @tanh_forward;
net.layer{1}.backward = @tanh_backward;
net.layer{1}.alpha=net.alpha;
@liangfu
liangfu / hash.c
Created May 24, 2016 16:05
simple hash string code in c
char num2char(size_t num){
char res = 0; int val = num%62;
if (val<10){ res = val+48;
}else if (val>=10&&val<36){ res = val+65-10;
}else if (val>=36&&val<62){ res = val+97-36;
}return res;
}
void swap(char & a, char & b){char t=b;b=a;a=t;}
void hash(char * src, char * dst, int sz){
memcpy(dst,src,32);
@liangfu
liangfu / pg-pong.py
Created June 1, 2016 02:20 — forked from karpathy/pg-pong.py
Training a Neural Network ATARI Pong agent with Policy Gradients from raw pixels
""" Trains an agent with (stochastic) Policy Gradients on Pong. Uses OpenAI Gym. """
import numpy as np
import cPickle as pickle
import gym
# hyperparameters
H = 200 # number of hidden layer neurons
batch_size = 10 # every how many episodes to do a param update?
learning_rate = 1e-4
gamma = 0.99 # discount factor for reward
@liangfu
liangfu / utility.h
Created August 25, 2016 01:45
Get current time in formatted string with cross-platform C/C++, while avoid using c++11 classes.
#include <string>
#include <ctime>
#if defined WIN32
#include <windows.h>
#include <stdint.h>
static int gettimeofday(struct timeval * tp, struct timezone * tzp)
{
// Note: some broken versions only have 8 trailing zero's, the correct epoch has 9 trailing zero's
static const uint64_t EPOCH = ((uint64_t) 116444736000000000ULL);
SYSTEMTIME system_time;