Skip to content

Commit 05325d8

Browse files
honnibalines
andauthored
Rethinc (explosion#151)
* Add relu layer * Add residual * Add dropout * Reformat dropout * Add ExtractWindow * Add concatenate * Fix layers * Add with_flatten * Tidy up and move util * Reorder functions * Add ForEach layer * Tidy up * Add embed layer * Update embed.py * WIP on draft 'define a model' example * Add uniqued * Draft up how to use a model * Add get_layer function * Update utils * Tidy up * Add Mish * Add Add * Add draft mnist script * Tidy up, remove old files, Model.layers etc. * Tidy up * Move create_init function into base * Update maxout * Make ReLu a weights layer * Fix mish layer * Add dropout and normalize args to Mish * Update laters * Move things, remove thinc.neural module * Remove more stuff * Remove typedefs * Typedef weight_t inline * Remove more things * Update search * Fix typedef * Fix dropout arg * Add softmax layer * Update chain layer * Update softmax * Remove add_layer method * Fix import * Fix import * Fix compile errors * Move optimizers * Add __init__ * Tidy up * Fix base * Update mem tests * Update affine test * Update strategies * Update util * Fix hash embed * Tidy up and add type annotations * Fix test_model * Fix base Model * Update test_hash_embed * Xfail test * Fix import * Xfail test * Update tests for schedules * Remove bad tests * Update rnn test * Update test sparse linear * Update type annotations * Fix type error * Update test * Update test * Update test * Update optimizers * Update affine test * Update loss.py * Fix softmax * Fix test * Fix test * Fix test_feed_forward * Fix relu * Fix maxout * Fix initializer * Update test * Update test * Remove pickle hint in Model * Update type annotations * Fix maxout * Fix test * Fix test * Xfail * Make chain variadic * Fix print * Update schedules.py * Tidy up * Update compiler directives * Move device and ops selection out of Model * Improve cythonize * Move layers.base.Model to model.Model * Update setup.cfg * Update optimizers.pyx * Tidy up * Tidy up * Tidy up * Update script * Add helper for shuffled array batches * Add comparison * Tidy up and fix type annotations * Don't allow None return values in getters * Update add.py * Fix typo * Fix type errors * Fix type errors * Add more input/output types * Fix add layer * Update concatenate backprop * Update concatenate.py * Update input/output types * Add featureextractor layer * Add static vectors * Add with_getitem layer * Remove dims from with_getitem * Remove todos from wire * Add pooling layers * Add unflatten layer * Add flattenaddlengths * Update wire * Add with square sequences * Add with pad and mask * Remove wires * Finish wires * Add copy method * Fix clone * Update clone test * return self from initialize * Fix naming * Add mnist test * Move evaluation method into thinc.util * Add mnist integration test * Update type annotations and tidy up layers * Fix type errors * Update test for basic tagger * Fix layer inits * Fix type errors and tidy up * Fix type errors * Update clone.py * Make arguments consistent * Add sparse_linear layer * Remove sparse_linear from todo * Compile sparse linear * Make initializer arg names consistent * Add parametricattention * Cross attention off todo * Add difference layer * Add cauchy layer * Cross off last todo * Update sparse_linear.pyx * Fix parametric attention * Add type annotations and tidy up * Auto-format * Rename sparse_linear * Update util.py * Fix import * Add sdist to thinc tests * Update init * Update model.py * Document and improve Model API * Fix chain init * Fix with_flatten init * Xfail pytorch tests * Fix model * Fix Model.use_params * Update model.py * base -> ops * Replace call to ops.affine * Move normal_init initializer to initializers * Remove some redundant ops * Clean up unused ops * Fix syntax * Update ops.py * Unbreak MANIFEST.in * Update ops.py * Improve dropout layer * Remove dodgy dropout stuff * Remove dropout test * Fix import * Fix sparse linear layer * Fix sparse linear test * Move test * Remove redundant test file * Remove commented code * Fix rountrip bytes * Update ops.py * Try using find_packages() * Mark slower tests as slow * Unxfail tests * Update dropout.py * Update mish.py * Update initializers.py * Update dropout.py * Add missing ops * Update util.py * Update residual.py * Fix dimension inference in residual * Fix affine tests * Remove wrapper from extra * Make Ops more consistent * Update test * Move mem to backend * Move mem * Port over docstrings [ci skip] * Update _registry.py * Use srsly pickle utils * Update and document Config Co-authored-by: Ines Montani <[email protected]>
1 parent e1058ff commit 05325d8

File tree

130 files changed

+3543
-4596
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

130 files changed

+3543
-4596
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,4 @@ tmp/
3838
.eggs/
3939
.pytest_cache/
4040
.vscode/
41+
.mypy_cache

MANIFEST.in

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
global-include *.h *.S *.c *.cu
2-
recursive-include include *.cpp
1+
recursive-include thinc *.cu *.pyx *.pxd
32
include LICENSE
43
include README.md
54
prune tmp/

azure-pipelines.yml

+15-3
Original file line numberDiff line numberDiff line change
@@ -42,8 +42,20 @@ jobs:
4242
pip install -r requirements.txt
4343
displayName: 'Install dependencies'
4444
45-
- script: pip install -e .
46-
displayName: 'Install'
45+
- script: |
46+
python setup.py build_ext --inplace
47+
python setup.py sdist --formats=gztar
48+
displayName: 'Build sdist'
49+
50+
- task: DeleteFiles@1
51+
inputs:
52+
contents: 'thinc'
53+
displayName: 'Delete source directory'
54+
55+
- bash: |
56+
SDIST=$(python -c "import os;print(os.listdir('./dist')[-1])" 2>&1)
57+
pip install dist/$SDIST
58+
displayName: 'Install from sdist'
4759
48-
- script: python -m pytest thinc
60+
- script: python -m pytest --pyargs thinc
4961
displayName: 'Run tests'

bin/cythonize.py

-221
This file was deleted.
+86
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
from __future__ import print_function
2+
import ml_datasets
3+
import torch
4+
import torch.nn as nn
5+
import torch.nn.functional as F
6+
import torch.optim as optim
7+
from thinc.util import xp2torch, get_shuffled_batches
8+
import tqdm
9+
10+
11+
class Net(nn.Module):
12+
def __init__(self, n_class, n_in, n_hidden, dropout=0.2):
13+
super(Net, self).__init__()
14+
self.fc1 = nn.Linear(n_in, n_hidden)
15+
self.dropout1 = nn.Dropout2d(0.2)
16+
self.fc2 = nn.Linear(n_hidden, n_hidden)
17+
self.dropout2 = nn.Dropout2d(0.2)
18+
self.fc3 = nn.Linear(n_hidden, n_class)
19+
20+
def forward(self, x):
21+
x = x.view(-1, 28*28)
22+
x = self.fc1(x)
23+
x = F.relu(x)
24+
x = self.dropout1(x)
25+
x = self.fc2(x)
26+
x = F.relu(x)
27+
x = self.dropout2(x)
28+
x = self.fc3(x)
29+
output = F.log_softmax(x, dim=-1)
30+
return output
31+
32+
33+
def load_mnist():
34+
from thinc.backends import NumpyOps
35+
from thinc.util import to_categorical
36+
ops = NumpyOps()
37+
mnist_train, mnist_dev, _ = ml_datasets.mnist()
38+
train_X, train_Y = ops.unzip(mnist_train)
39+
dev_X, dev_Y = ops.unzip(mnist_dev)
40+
train_Y = train_Y.astype("int64")
41+
dev_Y = dev_Y.astype("int64")
42+
return (train_X, train_Y), (dev_X, dev_Y)
43+
44+
45+
def test(args, model, device, test_loader):
46+
model.eval()
47+
test_loss = 0
48+
correct = 0
49+
with torch.no_grad():
50+
for data, target in test_loader:
51+
data, target = data.to(device), target.to(device)
52+
output = model(data)
53+
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
54+
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
55+
correct += pred.eq(target.view_as(pred)).sum().item()
56+
57+
test_loss /= len(test_loader.dataset)
58+
59+
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
60+
test_loss, correct, len(test_loader.dataset),
61+
100. * correct / len(test_loader.dataset)))
62+
63+
64+
def main(n_hidden=32, dropout=0.2, n_iter=10, batch_size=128, n_epoch=10):
65+
torch.set_num_threads(1)
66+
(train_X, train_Y), (dev_X, dev_Y) = load_mnist()
67+
model = Net(10, 28*28, n_hidden)
68+
optimizer = optim.Adam(model.parameters())
69+
70+
for epoch in range(n_epoch):
71+
model.train()
72+
train_batches = list(get_shuffled_batches(train_X, train_Y, batch_size))
73+
for images, true_labels in tqdm.tqdm(train_batches):
74+
images = xp2torch(images)
75+
true_labels = xp2torch(true_labels)
76+
77+
optimizer.zero_grad()
78+
guess_labels = model(images)
79+
loss = F.nll_loss(guess_labels, true_labels)
80+
loss.backward()
81+
optimizer.step()
82+
83+
84+
if __name__ == '__main__':
85+
import plac
86+
plac.call(main)

0 commit comments

Comments
 (0)