This file contains hidden or 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 DeepMojiDataset(Dataset): | |
| """ A simple Dataset class. | |
| # Arguments: | |
| X_in: Inputs of the given dataset. | |
| y_in: Outputs of the given dataset. | |
| # __getitem__ output: | |
| (torch.LongTensor, torch.LongTensor) | |
| """ | |
| def __init__(self, X_in, y_in): |
This file contains hidden or 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 DeepMojiBatchSampler(object): | |
| """A Batch sampler that enables larger epochs on small datasets and | |
| has upsampling functionality. | |
| # Arguments: | |
| y_in: Labels of the dataset. | |
| batch_size: Batch size. | |
| epoch_size: Number of samples in an epoch. | |
| upsample: Whether upsampling should be done. This flag should only be | |
| set on binary class problems. | |
| seed: Random number generator seed. |
This file contains hidden or 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 Model(nn.Module): | |
| def __init__(self, vocab_size, embed_dim, H1, H2, H3, pairs_in, single_in, drop=0.5): | |
| super(Model, self).__init__() | |
| self.embed = nn.Embedding(vocab_size, embedding_dim) | |
| self.drop = nn.Dropout(drop) | |
| self.pairs = nn.Sequential(nn.Linear(pairs_in, H1), nn.ReLU(), nn.Dropout(drop), | |
| nn.Linear(H1, H2), nn.ReLU(), nn.Dropout(drop), | |
| nn.Linear(H2, H3), nn.ReLU(), nn.Dropout(drop), | |
| nn.Linear(H3, 1), | |
| nn.Linear(1, 1)) |
This file contains hidden or 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 get_params(module, memo=None, pointers=None): | |
| """ Returns an iterator over PyTorch module parameters that allows to update parameters | |
| (and not only the data). | |
| ! Side effect: update shared parameters to point to the first yield instance | |
| (i.e. you can update shared parameters and keep them shared) | |
| Yields: | |
| (Module, string, Parameter): Tuple containing the parameter's module, name and pointer | |
| """ | |
| if memo is None: | |
| memo = set() |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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)) |
This file contains hidden or 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 hidden or 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 |
OlderNewer