Skip to content

Instantly share code, notes, and snippets.

View joelgrus's full-sized avatar
🤷‍♀️
I'm probably arguing on Twitter

Joel Grus joelgrus

🤷‍♀️
I'm probably arguing on Twitter
View GitHub Profile
from typing import TypeVar, Generic, Callable
from dataclasses import dataclass
from argparse import Namespace
T = TypeVar('T')
S = TypeVar('S')
@dataclass
class ListMap(Generic[S, T]):
f: Callable[[T], S]
@joelgrus
joelgrus / typ3.py
Last active December 29, 2022 09:35
"""
"algebraic data types" in python
"""
from __future__ import annotations
from dataclasses import dataclass
@dataclass
class Value:
value: int
@joelgrus
joelgrus / lstm.py
Created March 14, 2020 15:45
this is the LSTM implementation that the 2ed of data science from scratch promised to share
class Lstm(Layer):
def __init__(self, input_dim: int, hidden_dim: int) -> None:
self.input_dim = input_dim
self.hidden_dim = hidden_dim
# Forget-gate weights
self.w_f = random_tensor(hidden_dim, input_dim, variance=2/(hidden_dim + input_dim))
self.u_f = random_tensor(hidden_dim, hidden_dim, variance=1/hidden_dim)
self.b_f = random_tensor(hidden_dim)
"""
I am trying to figure out asyncio.
I have no idea if this code is good or not, but it works.
I also have no idea how to type annotate asyncio code.
"""
from typing import List, NamedTuple
import random
import asyncio
@joelgrus
joelgrus / ner-openai.jsonnet
Created October 22, 2018 13:59
config file for using the openai token embedder in allennlp
{
"dataset_reader": {
"type": "conll2003",
"token_indexers": {
"tokens": {
"type": "single_id",
"lowercase_tokens": true
},
"token_characters": {
"type": "characters"
@joelgrus
joelgrus / mpg.csv
Created October 14, 2018 17:12 — forked from omarish/mpg.csv
mpg cylinders displacement horsepower weight acceleration model_year origin name
18 8 307 130 3504 12 70 1 chevrolet chevelle malibu
15 8 350 165 3693 11.5 70 1 buick skylark 320
18 8 318 150 3436 11 70 1 plymouth satellite
16 8 304 150 3433 12 70 1 amc rebel sst
17 8 302 140 3449 10.5 70 1 ford torino
15 8 429 198 4341 10 70 1 ford galaxie 500
14 8 454 220 4354 9 70 1 chevrolet impala
14 8 440 215 4312 8.5 70 1 plymouth fury iii
14 8 455 225 4425 10 70 1 pontiac catalina

Installing AllenNLP on Windows

This is not guaranteed to work, but it might be worth trying. (If it doesn't work, it's doubtful I can help you any further than this.)

  1. Make sure you have a lot of hard drive space
  2. Install Anaconda and create a python 3.7 environment
  3. Follow the instructions at pytorch.org to install the appropriate pytorch
  4. Install the Visual C++ Build Tools https://visualstudio.microsoft.com/downloads/#build-tools-for-visual-studio-2017
  5. install cython: conda install cython
  6. pip install allennlp==0.6.0
"""
a response to https://gist.github.com/TomAugspurger/f96d41ae7a40d41fe9053d0adbceb4f1
"""
from collections import deque
import datetime
import itertools
import random
import string
from typing import NamedTuple, Tuple, Iterable, List
@joelgrus
joelgrus / run_length_encoding.py
Created February 1, 2018 05:30
I get daily interview coding problems in the mail, this was the jerkiest way I could think of to do run length encoding
"""
Run-length encoding is a fast and simple method of encoding strings.
The basic idea is to represent repeated successive characters
as a single count and character. For example, the string
"AAAABBBCCDAA" would be encoded as "4A3B2C1D2A".
Implement run-length encoding and decoding. You can assume
the string to be encoded has no digits and consists solely of alphabetic
characters. You can assume the string to be decoded is valid.
"""
@joelgrus
joelgrus / xkcd_knapsack.py
Last active August 21, 2017 03:30
xkcd_knapsack.py
# c.f. https://xkcd.com/287/
from typing import Sequence
target = 1505
weights = [215, 275, 335, 355, 420, 580]
def knapsack(goal: int, # what we're trying to add up to
weights: Sequence[int], # the weights we can use
start: int=0, # only add weights starting here (prevents duplicate work / answers)