Skip to content

Instantly share code, notes, and snippets.

@shreydesai
shreydesai / span_viz.py
Created March 20, 2020 01:17
Visualization for span-based outputs
"""
Model output visualization script for span-based data. Requires
matplotlib for color maps.
Usage:
python3 span_viz.py > index.html
open index.html
"""
import argparse
@shreydesai
shreydesai / preprocess.py
Created June 26, 2019 04:40
Preprocessing rules for Twitter data
def preprocess(raw):
if '>' in raw:
raw = raw.replace('>','>')
if '<' in raw:
raw = raw.replace('&lt;','<')
if '&amp;' in raw:
raw = raw.replace('&amp;','&')
if '”' in raw or '“' in raw:
raw = raw.replace('“','"')
raw = raw.replace('”','"')
@shreydesai
shreydesai / text_cnn.py
Last active May 15, 2019 16:31
Text CNN baseline
import torch
import torch.nn as nn
device = 'cuda' is torch.cuda.is_available() else 'cpu'
class NgramModule(nn.Module):
def __init__(self, seq_len, kernel, channels):
super().__init__()
self.conv = nn.Conv2d(1, channels, kernel)
@shreydesai
shreydesai / glove.py
Created February 17, 2019 20:08
Loading GloVe vectors
import pickle
import numpy as np
# load embeddings
glove = np.loadtxt('glove.6B.300d.txt', dtype='str', comments=None)
words = {w:i for i,w in enumerate(glove[:,0])}
vecs = glove[:,1:].astype('float')
# cache embeddings
pickle.dump((words, vecs), open('glove', 'wb'))
@shreydesai
shreydesai / dotproduct_attention.py
Created February 4, 2019 22:10
PyTorch Scaled Dot Product Attention
import torch
import torch.nn as nn
import numpy as np
class DotProductAttention(nn.Module):
def __init__(self, query_dim, key_dim, value_dim):
super().__init__()
self.scale = 1.0/np.sqrt(query_dim)
@shreydesai
shreydesai / additive_attention.py
Last active October 7, 2024 22:59
PyTorch Additive Attention
import torch
import torch.nn as nn
class AdditiveAttention(nn.Module):
def __init__(self, hidden_dim):
super().__init__()
self.hidden_dim = hidden_dim
@shreydesai
shreydesai / linkedlist.go
Created May 25, 2018 17:11
Singly linked list in Go
package main
import "fmt"
// Node data structure
type Node struct {
data int
next *Node
}
@shreydesai
shreydesai / dracula.vim
Created January 30, 2018 21:05
Dracula Vim Theme
set background=dark
highlight clear
if exists("syntax_on")
syntax reset
endif
let g:colors_name = "dracula"
hi Cursor ctermfg=17 ctermbg=231 cterm=NONE guifg=#282a36 guibg=#f8f8f0 gui=NONE
@shreydesai
shreydesai / .vimrc
Created January 30, 2018 21:04
Vim Config File
colorscheme dracula
" Only do this part when compiled with support for autocommands.
if has("autocmd")
" Use filetype detection and file-based automatic indenting.
filetype plugin indent on
" Use actual tab chars in Makefiles.
autocmd FileType make set tabstop=8 shiftwidth=8 softtabstop=0 noexpandtab
endif
@shreydesai
shreydesai / .tmux.conf
Created January 25, 2018 20:22
Tmux Config File
# Reload config
bind r source-file ~/.tmux.conf; display ' Reloaded'
# Colors
set -g default-terminal "screen-256color"
# Remaps leader
set-option -g prefix C-a
bind-key C-a last-window