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 / papers.py
Last active January 20, 2022 07:29
Script for crawling daily CL/ML/CV papers on arxiv
# -*- coding: utf-8 -*-
import re
from datetime import datetime, timedelta
import arxiv
KEYS = ['adversarial', 'algebraic', 'algebratic', 'amr', 'auto-encoding', 'autoencoder', 'autoencoding', 'autoregressive',
'backward', 'bayes', 'bayesian', 'bethe', 'bilexical', 'bipartite', 'bregman', 'carlo', 'chomsky', 'circuit', 'clique',
@yzhangcs
yzhangcs / semiring.py
Created May 15, 2021 15:20
Semirings
# -*- coding: utf-8 -*-
from functools import reduce
import torch
import torch.autograd as autograd
from supar.utils.common import MIN
from torch.autograd import Function
@yzhangcs
yzhangcs / translate.py
Created January 7, 2021 09:57
Script for translation
# -*- coding: utf-8 -*-
import argparse
from functools import partial
from multiprocessing.dummy import Pool
import nltk
from google_trans_new import google_translator
from tqdm import tqdm
@yzhangcs
yzhangcs / paraphrase.py
Last active December 2, 2022 12:54
Paraphrase data via back-translation using Google Translation
# -*- coding: utf-8 -*-
import argparse
from functools import partial
from multiprocessing.dummy import Pool
import nltk
from google_trans_new import google_translator
from tqdm import tqdm
@yzhangcs
yzhangcs / eval.py
Created October 29, 2020 14:36
Script for evaluating conll file
# -*- coding: utf-8 -*-
import argparse
from collections import Counter
def factorize(tags):
spans = []
for i, tag in enumerate(tags):
if tag.startswith(('B', 'S')):
# -*- coding: utf-8 -*-
import argparse
def bio2bioes(fin, fout):
r'''Convert BIO file to BIOES file.
Parameters:
fin (str): the file to convert.
fout (str): the file to save.
@yzhangcs
yzhangcs / affine.py
Created August 9, 2020 15:54
Affine layers
# -*- coding: utf-8 -*-
import torch
import torch.nn as nn
import torch.nn.functional as F
class Biaffine(nn.Module):
"""
Biaffine layer for first-order scoring.
@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__()
@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 / 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):