Skip to content

Instantly share code, notes, and snippets.

@nsaphra
nsaphra / token_type_counter.py
Created September 20, 2018 15:21
count the type and tokens in a file
import sys
types = set()
token_count = 0
for i, line in enumerate(sys.stdin):
if i % 1000 == 0:
print('.')
line = line.strip().split()
types.update(line)
@nsaphra
nsaphra / lstm_internal_hook.py
Created July 16, 2019 11:23
Rerun an LSTM as a hook, so we can analyze the disassembled gate activations.
"""
Because pytorch does not expose the internal activations of a module,
we must instead rerun the same exact function inside that module.
This is written specifically for a 1 layer LSTM with all default settings.
"""
import torch
import torch.nn as nn
from torch.autograd import Variable