Skip to content

Instantly share code, notes, and snippets.

View cosmozhang's full-sized avatar
🏠
Working from home

Cosmo Zhang cosmozhang

🏠
Working from home
View GitHub Profile
@cosmozhang
cosmozhang / masked_word_prediction_bert.py
Created August 15, 2023 17:30 — forked from yuchenlin/masked_word_prediction_bert.py
A simple example script for predicting masked words in a sentence using BERT.
import torch
from transformers import BertTokenizer, BertModel, BertForMaskedLM
import logging
logging.basicConfig(level=logging.INFO)# OPTIONAL
tokenizer = BertTokenizer.from_pretrained('bert-base-uncased')
model = BertForMaskedLM.from_pretrained('bert-base-uncased')
model.eval()
@cosmozhang
cosmozhang / betareg.py
Created October 20, 2020 21:41 — forked from brentp/betareg.py
beta regression in statsmodels
# -*- coding: utf-8 -*-
u"""
Beta regression for modeling rates and proportions.
References
----------
Grün, Bettina, Ioannis Kosmidis, and Achim Zeileis. Extended beta regression
in R: Shaken, stirred, mixed, and partitioned. No. 2011-22. Working Papers in
Economics and Statistics, 2011.
@cosmozhang
cosmozhang / parser.py
Created January 16, 2019 15:59 — forked from BarclayII/parser.py
PyTorch neural parser based on DyNet implementation
'''
Original implementation
https://github.com/clab/dynet_tutorial_examples/blob/master/tutorial_parser.ipynb
The code structure and variable names are similar for better reference.
Not for serious business, just for some comparison between PyTorch and DyNet
(and I still prefer PyTorch)
'''
import torch as T
@cosmozhang
cosmozhang / hrn.py
Created February 24, 2018 21:26
implementation of highway recurrent networks
import torch
from torch import nn
import torch.nn.functional as F
import torch.nn.utils
from torch.autograd import Variable
from torch.nn import Parameter, init
from torch.nn._functions.rnn import variable_recurrent_factory, StackedRNN
from torch.nn.modules.rnn import RNNCellBase
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence, PackedSequence
import numpy as np
@cosmozhang
cosmozhang / pad_packed_demo.py
Created January 31, 2018 15:41 — forked from Tushar-N/pad_packed_demo.py
How to use pad_packed_sequence in pytorch
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
import torch.nn.functional as F
import numpy as np
import itertools
def flatten(l):
return list(itertools.chain.from_iterable(l))
sudo ldconfig /usr/local/cuda-7.0/lib64
import edu.stanford.nlp.sentiment.BuildBinarizedDataset;
public class BinarizeTrees {
public static void main(String[] args) {
String textFile = (args.length > 1) ? args[1] : args[0];
System.out.println("Generating binarized trees from file "+textFile);
String[] input = {"-input", textFile};
@cosmozhang
cosmozhang / nltkparser.py
Created November 30, 2015 02:44
NLTK stanford parser
import os
from nltk.parse import stanford
os.environ['STANFORD_PARSER'] = '/path/to/standford/jars'
os.environ['STANFORD_MODELS'] = '/path/to/standford/jars'
parser = stanford.StanfordParser(model_path="/location/of/the/englishPCFG.ser.gz")
sentences = parser.raw_parse_sents(("Hello, My name is Melroy.", "What is your name?"))
print sentences
# GUI