This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Rectangle: | |
def __init__(self, w, h): | |
self.w = w | |
self.h = h | |
def area(self): | |
return self.w * self.h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
cdef struct Rectangle: | |
float w | |
float h |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%%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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) |