Skip to content

Instantly share code, notes, and snippets.

View yzhangcs's full-sized avatar
:octocat:
Focusing

Yu Zhang yzhangcs

:octocat:
Focusing
View GitHub Profile
@yzhangcs
yzhangcs / timeout.py
Created October 19, 2018 09:45
Context-manager/Decorator that prevents function execution from timeout
# -*- coding: utf-8 -*-
import functools
import signal
class timeout(object):
r"""Context-manager that prevents function execution from timeout.
Also functions as a decorator.
@yzhangcs
yzhangcs / bash.reg
Last active May 5, 2020 09:19
Adds a "Bash" context menu option in Windows Explorer
Windows Registry Editor Version 5.00
[HKEY_CLASSES_ROOT\Directory\Background\shell\Bash]
@="Bash here"
"Icon"="C:\\Program Files\\WindowsApps\\CanonicalGroupLimited.UbuntuonWindows_2004.2020.424.0_x64__79rhkp1fndgsc\\ubuntu.exe"
[HKEY_CLASSES_ROOT\Directory\Background\shell\Bash\command]
@="C:\\Program Files\\WindowsApps\\CanonicalGroupLimited.UbuntuonWindows_2004.2020.424.0_x64__79rhkp1fndgsc\\ubuntu.exe -c bash"
@yzhangcs
yzhangcs / bilstm.py
Created May 25, 2019 02:07
Deep Highway BiLSTM
# -*- coding: utf-8 -*-
from parser.modules.dropout import SharedDropout
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.utils.rnn import PackedSequence
@yzhangcs
yzhangcs / transformer.py
Created June 4, 2019 08:53
partition transformer
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
class Transformer(nn.Module):
def __init__(self, n_layers, n_heads, n_model, n_embed, n_inner,
input_dropout=0.1, attn_dropout=0.1, ffn_dropout=0.1):
@yzhangcs
yzhangcs / treecrf.py
Created October 16, 2019 09:09
The implementation of inside&outside and eisner algorithms
# -*- coding: utf-8 -*-
import torch
from torch.nn.utils.rnn import pad_sequence
@torch.enable_grad()
def crf(scores, mask, target=None, partial=False):
lens = mask.sum(1)
total = lens.sum()
@yzhangcs
yzhangcs / full2half.py
Last active November 17, 2024 05:01
Convert full-width characters to half-width ones.
# -*- coding: utf-8 -*-
import argparse
import unicodedata
# FF00-FF5F -> 0020-007E
MAP = {' ': ' ', '!': '!', '"': '"', '#': '#', '$': '$', '%': '%', '&': '&',
''': "'", '(': '(', ')': ')', '*': '*', '+': '+', ',': ',', '-': '-',
'.': '.', '/': '/',
'0': '0', '1': '1', '2': '2', '3': '3', '4': '4', '5': '5', '6': '6',
@yzhangcs
yzhangcs / eval.py
Last active November 30, 2019 10:33
Script used for outputting some metrics w.r.t. the predicted results.
# -*- coding: utf-8 -*-
import argparse
import os
import unicodedata
import torch
def ispunct(token):
@yzhangcs
yzhangcs / tarjan.py
Created April 19, 2020 06:30
Tarjan's Algorithm
def tarjan(tree):
tree[0] = -1
# record the search order, i.e., the timestep
dfn = [-1] * len(tree)
# record the the smallest timestep in a SCC
low = [-1] * len(tree)
# push the visited into the stack
stack, onstack = [], [False] * len(tree)
def connect(i, timestep):
@yzhangcs
yzhangcs / matrix_tree.py
Created July 7, 2020 10:24
Matrix Tree Theorem
# -*- coding: utf-8 -*-
import torch
import torch.autograd as autograd
import torch.nn as nn
class MatrixTree(nn.Module):
def __init__(self, *args, **kwargs):
@yzhangcs
yzhangcs / gat.py
Created July 7, 2020 14:21
Graph Attention Networks
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
class GAT(nn.Module):
def __init__(self, n_input, n_inner, n_layers, alpha=0.1, dropout=0.5):
super(GAT, self).__init__()