Skip to content

Instantly share code, notes, and snippets.

View khaotik's full-sized avatar
💫
gradient descending

khaotik

💫
gradient descending
View GitHub Profile
@khaotik
khaotik / codingstyle_py
Last active July 31, 2017 03:04
Collections of my python coding style
Collections of my python coding style
[General]
- Rules are recommended but not strict, and not recommnaded for code golfing.
- Name global constant like C macros, FOR_EXAMPLE
- Name global variable (non-constant) with prefix "g_"
- function arguments names always with suffix "_", to avoid clashes like fn( .., dtype=dtype ), it's weird and sometime cause misunderstanding of code
def example_function(arg1_, arg2_):
local1 = arg1_+arg2_
return local1
@khaotik
khaotik / pyutil.py
Created September 3, 2016 03:29
Random small and useful utilities for python
'''
A collection of small python snippets that help code looking better
'''
class given(object):
'''
make a closure with local variables using with statement
usage example:
@khaotik
khaotik / scan_test.py
Last active November 29, 2016 17:08
for investigating theano scan overhead
from __future__ import print_function
from time import time
import theano as th
import theano.tensor as T
import numpy as np
USE_PROFILER = True
if not USE_PROFILER:
@khaotik
khaotik / th_scan_profiling_1.md
Last active November 30, 2016 12:01
Theano scan investigation 1

The snippet for scan test is:

x = T.matrix()
y, _ = th.scan(fn=lambda x : T.sum(x*x), sequences=x)
fn_sum_scan = theano.function([x], y)
xval = np.random.randn(5000,10000).astype(np.float32)
timeit(fn(xval))
@khaotik
khaotik / test_circconv.py
Created March 14, 2017 10:19
circular convolution with generalized elemwise op
from itertools import product
from random import randint
from time import time
import numpy as np
import theano as th
T = th.tensor
from theano.tensor.padding import idx, at_idx
from theano.tensor.signal import conv
N = 256
@khaotik
khaotik / test_roll.py
Created March 14, 2017 10:49
roll using generalized elemwise op
from random import randint
from time import time
import numpy as np
import theano as th
import theano.tensor as T
from theano.tensor.padding import idx, at_idx
N = 256
x = T.matrix()
@khaotik
khaotik / method_1.cpp
Last active September 5, 2018 07:19
Receive packed array via WSTP
#include <stdlib.h>
#include <vector>
#include <string>
#include "wstp.h"
using namespace std;
int main() {
int err;
auto env_p = WSInitialize((void*)0);
@khaotik
khaotik / ezumount.py
Created January 8, 2019 05:01
ezumount - easily unmount and power off plugged USB disks from linux CLI
#!/usr/bin/env python3
import os
import sys
import subprocess as subp
if len(sys.argv) != 2:
appname = sys.argv[0]
print('%s - unmount USB directory and power it off' % appname, file=sys.stderr)
print('Usage: %s <directory>' % appname, file=sys.stderr)
sys.exit(-1)
@khaotik
khaotik / accum.py
Created January 26, 2019 11:14
binary carry accumulator
# binary carry accumulator with O(log(N)) space requirement
# better numerical stability when adding lots of small numbers
class Accumulator:
__slots__ = ('fn', 'data', 'index', 'init')
def __init__(self, size=31, init=0., fn='add'):
if fn == 'mean':
fn=lambda x,wx,y,wy:(x*wx+y*wy)/(wx+wy)
elif fn == 'add':
fn=lambda x,wx,y,wy:x+y
self.fn = fn
@khaotik
khaotik / tarfile-min-example.py
Created May 2, 2021 11:53
Minimal python tarfile module usage
#!/usr/bin/env python
import io
import tarfile
a_content = '''
content:aaa
content:111
'''
b_content = '''
content:bbb
content:222