Skip to content

Instantly share code, notes, and snippets.

@movefast
movefast / Seq2SeqAttnWithBeam.py
Created April 21, 2018 06:37
Seq2Seq rnn with beam search
class Seq2SeqAttnRNN(nn.Module):
def __init__(self, vecs_enc, itos_enc, em_sz_enc, vecs_dec, itos_dec, em_sz_dec, nh, out_sl, nl=2):
super().__init__()
self.emb_enc = create_emb(vecs_enc, itos_enc, em_sz_enc)
self.nl,self.nh,self.out_sl = nl,nh,out_sl
self.gru_enc = nn.GRU(em_sz_enc, nh, num_layers=nl, dropout=0.25)
self.out_enc = nn.Linear(nh, em_sz_dec, bias=False)
self.emb_dec = create_emb(vecs_dec, itos_dec, em_sz_dec)
self.gru_dec = nn.GRU(em_sz_dec, em_sz_dec, num_layers=nl, dropout=0.1)
self.emb_enc_drop = nn.Dropout(0.15)
@movefast
movefast / WeightedLossSampler.py
Last active December 12, 2018 10:29
A random sampler weighted on prev batch losses using fastai library
from torch.utils.data.sampler import Sampler
from torch.utils.data.sampler import RandomSampler
class WeightedLossSampler(Sampler, Callback):
def __init__(self, data_source, replacement=True, bs=64, init_fac=1, ls_init_fac=1e-2):
self.num_samples = len(data_source)
self.weights = to_gpu(torch.ones(self.num_samples)*init_fac)
self.replacement = replacement
self.i = 0
@movefast
movefast / ctags.setup
Created February 18, 2018 21:34 — forked from nazgob/ctags.setup
ctags setup on mac
# you have ctags but it does not work...
$ ctags -R --exclude=.git --exclude=log *
ctags: illegal option -- R
usage: ctags [-BFadtuwvx] [-f tagsfile] file ...
#you need to get new ctags, i recommend homebrew but anything will work
$ brew install ctags
#alias ctags if you used homebrew
$ alias ctags="`brew --prefix`/bin/ctags"
public class Solution {
public static int uniquePathsWithObstacles(int[][] obstacleGrid) {
if(obstacleGrid.length == 0 || obstacleGrid[0].length == 0) return 0;
int[][] res = new int[obstacleGrid.length][obstacleGrid[0].length];
for(int i = 0; i < obstacleGrid.length; i++) {
for(int j = 0; j < obstacleGrid[0].length;j++) {
if(obstacleGrid[i][j] == 1) res[i][j] = 0;
else if(i == 0 && j == 0) res[0][0] = 1;
else if (i == 0) res[i][j] = res[i][j-1];
else if (j == 0) res[i][j] = res[i-1][j];