@@ -0,0 +1,2684 @@
+from __future__ import print_function%0Aimport plac%0Aimport dill as pickle%0Afrom tqdm import tqdm%0Afrom thinc.v2v import Model, ReLu, Softmax%0Afrom thinc.api import clone, chain%0Afrom thinc.neural.util import to_categorical, prefer_gpu%0A%0Afrom thinc.extra import datasets%0Afrom thinc.neural.ops import CupyOps%0Afrom thinc.extra.wrappers import PyTorchWrapper%0A%0Aimport torch%0Aimport torch.nn as nn%0Aimport torch.nn.functional as F%0A%0A%0Aclass PyTorchFeedForward(nn.Module):%0A    def __init__(self, depth, width, input_size, output_size):%0A        super(PyTorchFeedForward, self).__init__()%0A        self.linears = %5Bnn.Linear(input_size, width)%5D%0A        for i in range(depth-1):%0A            self.linears.append(nn.Linear(width, width))%0A        self.linears.append(nn.Linear(width, output_size))%0A        for i, child in enumerate(self.linears):%0A            self.add_module('child%25d' %25 i, child)%0A%0A    def forward(self, x):%0A        y = F.dropout(F.relu(self.linears%5B0%5D(x)), self.training)%0A        for layer in self.linears%5B1:-1%5D:%0A            y = F.relu(layer(y))%0A            y = F.dropout(y, self.training)%0A        y = F.log_softmax(self.linears%5B-1%5D(y))%0A        return y%0A%0A%0Adef main(depth=2, width=512, nb_epoch=30):%0A    prefer_gpu()%0A    torch.set_num_threads(1)%0A%0A    train_data, dev_data, _ = datasets.mnist()%0A    train_X, train_y = Model.ops.unzip(train_data)%0A    dev_X, dev_y = Model.ops.unzip(dev_data)%0A%0A    dev_y = to_categorical(dev_y)%0A    model = PyTorchWrapper(%0A        PyTorchFeedForward(depth=depth, width=width, input_size=train_X.shape%5B1%5D,%0A            output_size=dev_y.shape%5B1%5D))%0A    with model.begin_training(train_X, train_y, L2=1e-6) as (trainer, optimizer):%0A        epoch_loss = %5B0.%5D%0A        def report_progress():%0A            #with model.use_params(optimizer.averages):%0A            print(epoch_loss%5B-1%5D, model.evaluate(dev_X, dev_y), trainer.dropout)%0A            epoch_loss.append(0.)%0A%0A        trainer.each_epoch.append(report_progress)%0A        trainer.nb_epoch = nb_epoch%0A        trainer.dropout = 0.3%0A        trainer.batch_size = 128%0A        trainer.dropout_decay = 0.0%0A        train_X = model.ops.asarray(train_X, dtype='float32')%0A        y_onehot = to_categorical(train_y)%0A        for X, y in trainer.iterate(train_X, y_onehot):%0A            yh, backprop = model.begin_update(X, drop=trainer.dropout)%0A            loss = ((yh-y)**2.).sum() / y.shape%5B0%5D%0A            backprop(yh-y, optimizer)%0A            epoch_loss%5B-1%5D += loss%0A        with model.use_params(optimizer.averages):%0A            print('Avg dev.: %25.3f' %25 model.evaluate(dev_X, dev_y))%0A            with open('out.pickle', 'wb') as file_:%0A                pickle.dump(model, file_, -1)%0A%0A%0Aif __name__ == '__main__':%0A    plac.call(main)%0A

Add example using PyTorch for mnist