Skip to content

Instantly share code, notes, and snippets.

View foowaa's full-sized avatar
:octocat:
thinking

Chunlin TIAN foowaa

:octocat:
thinking
View GitHub Profile
import functools
def log1(func):
@functools.warp(func)
def wrapper(*args, **kw):
print("call %s():", func.__name__)
return func(*args, **kw)
return wrapper
_registry_path = Path(__file__).parent / 'registry.json'
if _registry_path.exists():
with _registry_path.open(encoding='utf-8') as f:
_REGISTRY = json.load(f)
else:
_REGISTRY = {}
def short_name(cls: type) -> str:
"""Returns just a class name (without package and module specification)."""
return cls.__name__.split('.')[-1]
import torch.utils.data as data
class MyDataset(data.Dataset):
'''
Dataset must define __getitem__ and __len__
'''
def __init__(self, others):
pass
def __getitem__(self, index):
"""
def collate_fn(data):
"""
default_collate比较好地实现了对图片等的操作,但是并不支持对文字等不等长序列的操作
dataloader的一个参数
输入data是list of (x, y), list的长度是batch_size
返回xs, ys, lens
(batch_size, x或y的size)的tensor
"""
# Sort a data list
import contextlib
@contextlib.contextmanager
def open_func(file_name):
# __enter__方法
print('open file:', file_name, 'in __enter__')
file_handler = open(file_name, 'r')
yield file_handler
# __exit__方法
@foowaa
foowaa / position_encoding_transformer.py
Last active May 3, 2023 18:46
position encoding of Transformer on numpy
'''
numpy sinusoid position encoding of Transformer model.
params:
n_position(n):number of positions
d_hid(m): dimension of embedding vector
padding_idx:set 0 dimension
return:
sinusoid_table(n*m):numpy array
@foowaa
foowaa / common.sh
Created April 26, 2019 06:28
一般的 shell 脚本
#! /bin/bash
# constants definition
baseDir=$(cd `dirname "$0"`;pwd) #返回文件当路径
home=$baseDir/..
registry=abc.com
name=$registry/dev
# functions
funWithReturn(){
@foowaa
foowaa / dot_example.dot
Last active May 18, 2019 14:42
graphviz dot file example
digraph G {
rankdir="LR";
subgraph cluster_3{
subgraph cluster_2 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
VS -> "top-K"->"sequential \n embeding"
label = "CWSE";
@foowaa
foowaa / dir_natural.m
Last active May 28, 2019 10:00
read all files in natural order by dir in MATLAB
function file=dir_natural(path)
%%% Example:
% file=sortObj('./img/*.bmp*');
file=dir(path);
for i=1:length(file)
A{i}=file(i).name;
end
[~, ind]=natsort(A);
for j=1:length(file)
files(j)=file(ind(j));
@foowaa
foowaa / redis_queue.py
Created July 16, 2019 08:49
redis queue
import redis
class RedisQueue(object):
"""Simple Queue with Redis Backend"""
def __init__(self, name, namespace='queue', **redis_kwargs):
"""The default connection parameters are: host='localhost', port=6379, db=0"""
self.__db = redis.Redis(**redis_kwargs)
self.key = '%s:%s' % (namespace, name)