Skip to content

Instantly share code, notes, and snippets.

View thomwolf's full-sized avatar
🚂
training

Thomas Wolf thomwolf

🚂
training
View GitHub Profile
@thomwolf
thomwolf / MetaLearner.py
Last active June 29, 2023 10:06
A simple bare MetaLearner class in PyTorch
class MetaLearner(nn.Module):
""" Bare Meta-learner class
Should be added: intialization, hidden states, more control over everything
"""
def __init__(self, model):
super(MetaLearner, self).__init__()
self.weights = Parameter(torch.Tensor(1, 2))
def forward(self, forward_model, backward_model):
""" Forward optimizer with a simple linear neural net
@thomwolf
thomwolf / meta_train.py
Last active July 5, 2019 15:43
Simple gist on how to train a meta-learner in PyTorch
def train(forward_model, backward_model, optimizer, meta_optimizer, train_data, meta_epochs):
""" Train a meta-learner
Inputs:
forward_model, backward_model: Two identical PyTorch modules (can have shared Tensors)
optimizer: a neural net to be used as optimizer (an instance of the MetaLearner class)
meta_optimizer: an optimizer for the optimizer neural net, e.g. ADAM
train_data: an iterator over an epoch of training data
meta_epochs: meta-training steps
To be added: intialization, early stopping, checkpointing, more control over everything
"""
@thomwolf
thomwolf / profile.py
Created June 4, 2018 08:31
Profiling a Python module
import cProfile
import pstats
import my_slow_module
cProfile.run('my_slow_module.run()', 'restats')
p = pstats.Stats('restats')
p.sort_stats('cumulative').print_stats(30)
@thomwolf
thomwolf / rectangle_class.py
Last active June 6, 2018 08:29
A simple Python Rectangle class
class Rectangle:
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
@thomwolf
thomwolf / slow_loop.py
Last active June 11, 2018 10:06
A Python loop on a list of Python objects
from random import random
class Rectangle:
def __init__(self, w, h):
self.w = w
self.h = h
def area(self):
return self.w * self.h
def check_rectangles(rectangles, threshold):
@thomwolf
thomwolf / fast_loop.pyx
Last active January 10, 2021 15:59
A Cython loop on an array of C structs
from cymem.cymem cimport Pool
from random import random
cdef struct Rectangle:
float w
float h
cdef int check_rectangles(Rectangle* rectangles, int n_rectangles, float threshold):
cdef int n_out = 0
# C arrays contain no size information => we need to give it explicitly
@thomwolf
thomwolf / rectangle_struct.pyx
Created June 6, 2018 09:19
A C structure for a rectangle
cdef struct Rectangle:
float w
float h
@thomwolf
thomwolf / slow_python_loop.py
Last active June 6, 2018 22:46
Example of Python loop to count the occurrences of the word "run" tagged as noun by spaCy in a portion of Wikitext2
def slow_loop(doc_list, word, tag):
n_out = 0
for doc in doc_list:
for tok in doc:
if tok.lower_ == word and tok.tag_ == tag:
n_out += 1
return n_out
def main_nlp_slow(doc_list):
n_out = slow_loop(doc_list, 'run', 'NN')
@thomwolf
thomwolf / fast_cython_loop.pyx
Last active June 11, 2018 13:50
Example of Cython loop to count the occurrences of the word "run" tagged as noun by spaCy in a portion of Wikitext2
%%cython -+
import numpy # Sometime we have a fail to import numpy compilation error if we don't import numpy
from cymem.cymem cimport Pool
from spacy.tokens.doc cimport Doc
from spacy.typedefs cimport hash_t
from spacy.structs cimport TokenC
cdef struct DocElement:
TokenC* c
int length
@thomwolf
thomwolf / download_big_doc.py
Last active June 7, 2018 08:28
Download and parse an extract of wikitext-2 with ~170k words
import urllib.request
import spacy
with urllib.request.urlopen('https://raw.githubusercontent.com/pytorch/examples/master/word_language_model/data/wikitext-2/valid.txt') as response:
text = response.read()
nlp = spacy.load('en')
doc_list = list(nlp(text[:800000].decode('utf8')) for i in range(10))