This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import functools | |
def log1(func): | |
@functools.warp(func) | |
def wrapper(*args, **kw): | |
print("call %s():", func.__name__) | |
return func(*args, **kw) | |
return wrapper | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_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] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): | |
""" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__方法 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
''' | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /bin/bash | |
# constants definition | |
baseDir=$(cd `dirname "$0"`;pwd) #返回文件当路径 | |
home=$baseDir/.. | |
registry=abc.com | |
name=$registry/dev | |
# functions | |
funWithReturn(){ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |