Skip to content

Instantly share code, notes, and snippets.

@manasRK
manasRK / padded_rnn.py
Created January 5, 2018 11:45 — forked from MaximumEntropy/padded_rnn.py
Padded RNN PyTorch
import torch
import torch.nn as nn
from torch.autograd import Variable
from torch.nn.utils.rnn import pad_packed_sequence, pack_padded_sequence
x = Variable(torch.randn(10, 20, 30)).cuda()
lens = range(10)
x = pack_padded_sequence(x, lens[::-1], batch_first=True)
@manasRK
manasRK / masked_cross_entropy.py
Created January 5, 2018 11:47 — forked from jihunchoi/masked_cross_entropy.py
PyTorch workaround for masking cross entropy loss
def _sequence_mask(sequence_length, max_len=None):
if max_len is None:
max_len = sequence_length.data.max()
batch_size = sequence_length.size(0)
seq_range = torch.range(0, max_len - 1).long()
seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
seq_range_expand = Variable(seq_range_expand)
if sequence_length.is_cuda:
seq_range_expand = seq_range_expand.cuda()
seq_length_expand = (sequence_length.unsqueeze(1)
@manasRK
manasRK / PyTorch Hack.md
Created February 15, 2018 06:12
Hack for Handling pad_packed_sequence in PyTorch

This is necessitated by the open Issue: pytorch/pytorch#1591

First, the usual pack_padded_sequence and pad_packed_sequence for handling variable length sequences;

seq_len, bsz, n_dims = feats.size()
packed_input = pack_padded_sequence(feats, lengths, batch_first=False)
packed_output, self.hidden = self.lstm(packed_input, self.hidden)
# lstm_out --> seqlen X bsz X hidden_dim
lstm_out, output_lengths = pad_packed_sequence(packed_output, batch_first = False)
@manasRK
manasRK / installing_anaconda_on_mac.md
Created October 17, 2024 20:31 — forked from ryanorsinger/installing_anaconda_on_mac.md
Installing Homebrew and Anaconda

Installing Homebrew and Anaconda on a Mac

Install Homebrew

Run the following command on your terminal to install Homebrew. Homebrew is a package manager for Macs and is used to install useful development tools and software.

/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"

Install Anaconda through Homebrew

  1. Run brew install --cask anaconda to install Anaconda