Skip to content

Instantly share code, notes, and snippets.

View neubig's full-sized avatar

Graham Neubig neubig

View GitHub Profile
@neubig
neubig / dynet-tagger.py
Last active May 21, 2018 06:01
A small sequence labeler in DyNet
"""
DyNet implementation of a sequence labeler (POS taggger).
This is a translation of this tagger in PyTorch: https://gist.github.com/hal3/8c170c4400576eb8d0a8bd94ab231232
Basic architecture:
- take words
- run though bidirectional GRU
- predict labels one word at a time (left to right), using a recurrent neural network "decoder"
The decoder updates hidden state based on:
- most recent word
@neubig
neubig / linalg-calcfunction.py
Last active October 19, 2022 09:50
Examples of linear algebra in numpy
import numpy as np
import sys
################# Explanation ##################
# This is a function to calculate house prices h(x) = -40 + 0.25x
# The first term (-40) is the base price, and "x" is the number of square feet in the house
################################################
# Set up the function
my_function = np.array([-40, 0.25])
@neubig
neubig / lstm-lm.py
Last active August 23, 2017 09:18
This is a minimal implementation of training for a language model using long short-term memory (LSTM) neural networks
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# This is a simplified implementation of the LSTM language model (by Graham Neubig)
#
# LSTM Neural Networks for Language Modeling
# Martin Sundermeyer, Ralf Schlüter, Hermann Ney
# InterSpeech 2012
#
# The structure of the model is extremely simple. At every time step we
@neubig
neubig / plot-gp.py
Created November 19, 2014 02:25
A simple program to sample functions from a Gaussian process and plot them
#!/usr/bin/python
from math import exp
import numpy as np
import matplotlib.pyplot as plt
def rbf_kernel(x1, x2, variance = 1):
return exp(-1 * ((x1-x2) ** 2) / (2*variance))
def gram_matrix(xs):
@neubig
neubig / pequalnp.py
Last active August 29, 2015 13:57
This is a program that answers the age-old problem of whether P=NP
#!/usr/bin/python
################################################################################
# pequalnp.py
# Graham Neubig
# 4/1/2014
#
# This is a program that provides an answer for the age-old problem of whether
# the class of problems that can be verified in polynomial time (NP) is equal
# to the class of problems for which an answer can be provided in polynomial
@neubig
neubig / crf.py
Created November 7, 2013 10:59
This is a script to train conditional random fields. It is written to minimize the number of lines of code, with no regard for efficiency.
#!/usr/bin/python
# crf.py (by Graham Neubig)
# This script trains conditional random fields (CRFs)
# stdin: A corpus of WORD_POS WORD_POS WORD_POS sentences
# stdout: Feature vectors for emission and transition properties
from collections import defaultdict
from math import log, exp
import sys
@neubig
neubig / train-rbmlm.py
Created April 30, 2013 02:30
This is a an example of how to train a restricted Boltzmann machine language model
#!/usr/bin/python
# This code implements the training part of the Restricted Boltzmann Machine
# language model described by:
# Three New Graphical Models for Statistical Language Modeling
# Andriy Mnih and Geoffrey Hinton
# ICML 2007
# http://www.gatsby.ucl.ac.uk/~amnih/papers/threenew.pdf
#
# Usage: train-rbmlm.py training-file.txt
@neubig
neubig / solve-3sat.py
Created April 1, 2013 01:07
This is a program to answer the question of whether a boolean satisfiability problem in conjunctive normal form with 3 variables per clause (3SAT) is true or false in linear time.
#!/usr/bin/python
# ***************************
# * solve-3sat.py
# * by Graham Neubig
# * 4/1/2013
# ***************************
#
# This is a Python program to provide an answer for satisfiability problems
# in conjunctive normal form with 3 variables per clause (3SAT) in LINEAR time.
#
@neubig
neubig / find-tweets.py
Created September 20, 2012 06:04
A python program to find tweets from a tab-separated file with a regular expression
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import re
import datetime
pattern = ur'電力.*供給'
@neubig
neubig / string-rewriting-kernel.py
Created September 10, 2012 02:41
An example implementation of the String Rewriting Kernel in python
#!/usr/bin/python
# A python implementation of the string rewriting kernel
# by Graham Neubig
#
# Reference:
# Fan Bu, Hang Li, Xiaoyan Zhu. "String Rewriting Kernel". ACL 2012
# http://aclweb.org/anthology-new/P/P12/P12-1047.pdf
from math import factorial