2017-05-29 02:37:57 +03:00
|
|
|
import ujson
|
2017-10-03 21:07:17 +03:00
|
|
|
from thinc.v2v import Model, Maxout, Softmax, Affine, ReLu, SELU
|
|
|
|
from thinc.i2v import HashEmbed, StaticVectors
|
|
|
|
from thinc.t2t import ExtractWindow, ParametricAttention
|
|
|
|
from thinc.t2v import Pooling, max_pool, mean_pool, sum_pool
|
|
|
|
from thinc.misc import Residual
|
|
|
|
from thinc.misc import BatchNorm as BN
|
|
|
|
from thinc.misc import LayerNorm as LN
|
|
|
|
|
2017-05-06 21:38:12 +03:00
|
|
|
from thinc.api import add, layerize, chain, clone, concatenate, with_flatten
|
2017-10-03 21:07:17 +03:00
|
|
|
from thinc.api import FeatureExtracter, with_getitem
|
|
|
|
from thinc.api import uniqued, wrap, flatten_add_lengths, noop
|
|
|
|
|
|
|
|
from thinc.linear.linear import LinearModel
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
from thinc.neural.ops import NumpyOps, CupyOps
|
2017-07-22 21:03:40 +03:00
|
|
|
from thinc.neural.util import get_array_module
|
2017-10-03 21:07:17 +03:00
|
|
|
|
2017-07-25 19:57:59 +03:00
|
|
|
import random
|
2017-08-18 22:55:23 +03:00
|
|
|
import cytoolz
|
2017-05-05 21:12:03 +03:00
|
|
|
|
2017-05-08 12:36:37 +03:00
|
|
|
from thinc import describe
|
|
|
|
from thinc.describe import Dimension, Synapses, Biases, Gradient
|
|
|
|
from thinc.neural._classes.affine import _set_dimensions_if_needed
|
2017-10-03 19:39:57 +03:00
|
|
|
import thinc.extra.load_nlp
|
2017-08-12 13:45:20 +03:00
|
|
|
|
2017-09-02 12:41:00 +03:00
|
|
|
from .attrs import ID, ORTH, LOWER, NORM, PREFIX, SUFFIX, SHAPE, TAG, DEP, CLUSTER
|
2017-05-18 12:22:20 +03:00
|
|
|
from .tokens.doc import Doc
|
2017-08-18 22:55:23 +03:00
|
|
|
from . import util
|
2017-05-04 14:31:40 +03:00
|
|
|
|
2017-05-08 12:36:37 +03:00
|
|
|
import numpy
|
2017-05-29 02:37:57 +03:00
|
|
|
import io
|
2017-05-08 12:36:37 +03:00
|
|
|
|
2017-10-03 21:29:58 +03:00
|
|
|
# TODO: Unset this once we don't want to support models previous models.
|
|
|
|
import thinc.neural._classes.layernorm
|
2017-10-05 04:06:52 +03:00
|
|
|
thinc.neural._classes.layernorm.set_compat_six_eight(False)
|
2017-10-03 21:29:58 +03:00
|
|
|
|
2017-09-22 17:38:22 +03:00
|
|
|
VECTORS_KEY = 'spacy_pretrained_vectors'
|
2017-05-08 12:36:37 +03:00
|
|
|
|
2017-07-22 21:03:40 +03:00
|
|
|
@layerize
|
|
|
|
def _flatten_add_lengths(seqs, pad=0, drop=0.):
|
|
|
|
ops = Model.ops
|
|
|
|
lengths = ops.asarray([len(seq) for seq in seqs], dtype='i')
|
|
|
|
def finish_update(d_X, sgd=None):
|
|
|
|
return ops.unflatten(d_X, lengths, pad=pad)
|
|
|
|
X = ops.flatten(seqs, pad=pad)
|
|
|
|
return (X, lengths), finish_update
|
|
|
|
|
|
|
|
|
|
|
|
@layerize
|
|
|
|
def _logistic(X, drop=0.):
|
|
|
|
xp = get_array_module(X)
|
|
|
|
if not isinstance(X, xp.ndarray):
|
|
|
|
X = xp.asarray(X)
|
|
|
|
# Clip to range (-10, 10)
|
|
|
|
X = xp.minimum(X, 10., X)
|
|
|
|
X = xp.maximum(X, -10., X)
|
|
|
|
Y = 1. / (1. + xp.exp(-X))
|
|
|
|
def logistic_bwd(dY, sgd=None):
|
|
|
|
dX = dY * (Y * (1-Y))
|
|
|
|
return dX
|
|
|
|
return Y, logistic_bwd
|
|
|
|
|
|
|
|
|
2017-08-18 22:55:23 +03:00
|
|
|
@layerize
|
|
|
|
def add_tuples(X, drop=0.):
|
|
|
|
"""Give inputs of sequence pairs, where each sequence is (vals, length),
|
|
|
|
sum the values, returning a single sequence.
|
|
|
|
|
|
|
|
If input is:
|
|
|
|
((vals1, length), (vals2, length)
|
|
|
|
Output is:
|
|
|
|
(vals1+vals2, length)
|
|
|
|
|
|
|
|
vals are a single tensor for the whole batch.
|
|
|
|
"""
|
|
|
|
(vals1, length1), (vals2, length2) = X
|
|
|
|
assert length1 == length2
|
|
|
|
|
|
|
|
def add_tuples_bwd(dY, sgd=None):
|
|
|
|
return (dY, dY)
|
|
|
|
|
|
|
|
return (vals1+vals2, length), add_tuples_bwd
|
|
|
|
|
|
|
|
|
2017-07-22 21:03:40 +03:00
|
|
|
def _zero_init(model):
|
|
|
|
def _zero_init_impl(self, X, y):
|
|
|
|
self.W.fill(0)
|
|
|
|
model.on_data_hooks.append(_zero_init_impl)
|
|
|
|
if model.W is not None:
|
|
|
|
model.W.fill(0.)
|
|
|
|
return model
|
|
|
|
|
2017-08-18 22:55:23 +03:00
|
|
|
|
2017-07-22 21:03:40 +03:00
|
|
|
@layerize
|
|
|
|
def _preprocess_doc(docs, drop=0.):
|
|
|
|
keys = [doc.to_array([LOWER]) for doc in docs]
|
|
|
|
ops = Model.ops
|
|
|
|
lengths = ops.asarray([arr.shape[0] for arr in keys])
|
|
|
|
keys = ops.xp.concatenate(keys)
|
|
|
|
vals = ops.allocate(keys.shape[0]) + 1
|
|
|
|
return (keys, vals, lengths), None
|
|
|
|
|
|
|
|
|
2017-05-20 14:40:10 +03:00
|
|
|
def _init_for_precomputed(W, ops):
|
2017-05-27 23:50:40 +03:00
|
|
|
if (W**2).sum() != 0.:
|
|
|
|
return
|
2017-05-20 14:40:10 +03:00
|
|
|
reshaped = W.reshape((W.shape[1], W.shape[0] * W.shape[2]))
|
|
|
|
ops.xavier_uniform_init(reshaped)
|
|
|
|
W[:] = reshaped.reshape(W.shape)
|
|
|
|
|
2017-08-18 22:55:23 +03:00
|
|
|
|
2017-05-08 12:36:37 +03:00
|
|
|
@describe.on_data(_set_dimensions_if_needed)
|
|
|
|
@describe.attributes(
|
|
|
|
nI=Dimension("Input size"),
|
|
|
|
nF=Dimension("Number of features"),
|
|
|
|
nO=Dimension("Output size"),
|
|
|
|
W=Synapses("Weights matrix",
|
2017-05-20 14:40:10 +03:00
|
|
|
lambda obj: (obj.nF, obj.nO, obj.nI),
|
|
|
|
lambda W, ops: _init_for_precomputed(W, ops)),
|
2017-05-08 12:36:37 +03:00
|
|
|
b=Biases("Bias vector",
|
|
|
|
lambda obj: (obj.nO,)),
|
|
|
|
d_W=Gradient("W"),
|
|
|
|
d_b=Gradient("b")
|
|
|
|
)
|
|
|
|
class PrecomputableAffine(Model):
|
|
|
|
def __init__(self, nO=None, nI=None, nF=None, **kwargs):
|
|
|
|
Model.__init__(self, **kwargs)
|
|
|
|
self.nO = nO
|
|
|
|
self.nI = nI
|
|
|
|
self.nF = nF
|
|
|
|
|
|
|
|
def begin_update(self, X, drop=0.):
|
|
|
|
# X: (b, i)
|
2017-05-20 14:40:10 +03:00
|
|
|
# Yf: (b, f, i)
|
2017-05-08 12:36:37 +03:00
|
|
|
# dY: (b, o)
|
|
|
|
# dYf: (b, f, o)
|
2017-05-20 14:40:10 +03:00
|
|
|
#Yf = numpy.einsum('bi,foi->bfo', X, self.W)
|
2017-05-08 12:36:37 +03:00
|
|
|
Yf = self.ops.xp.tensordot(
|
2017-05-20 14:40:10 +03:00
|
|
|
X, self.W, axes=[[1], [2]])
|
2017-05-08 12:36:37 +03:00
|
|
|
Yf += self.b
|
|
|
|
def backward(dY_ids, sgd=None):
|
2017-05-20 14:40:10 +03:00
|
|
|
tensordot = self.ops.xp.tensordot
|
2017-05-08 12:36:37 +03:00
|
|
|
dY, ids = dY_ids
|
|
|
|
Xf = X[ids]
|
|
|
|
|
2017-05-20 14:40:10 +03:00
|
|
|
#dXf = numpy.einsum('bo,foi->bfi', dY, self.W)
|
|
|
|
dXf = tensordot(dY, self.W, axes=[[1], [1]])
|
2017-05-08 12:36:37 +03:00
|
|
|
#dW = numpy.einsum('bo,bfi->ofi', dY, Xf)
|
2017-05-20 14:40:10 +03:00
|
|
|
dW = tensordot(dY, Xf, axes=[[0], [0]])
|
|
|
|
# ofi -> foi
|
|
|
|
self.d_W += dW.transpose((1, 0, 2))
|
|
|
|
self.d_b += dY.sum(axis=0)
|
2017-05-08 12:36:37 +03:00
|
|
|
|
|
|
|
if sgd is not None:
|
|
|
|
sgd(self._mem.weights, self._mem.gradient, key=self.id)
|
|
|
|
return dXf
|
|
|
|
return Yf, backward
|
|
|
|
|
2017-05-04 14:31:40 +03:00
|
|
|
|
2017-05-08 15:24:43 +03:00
|
|
|
@describe.on_data(_set_dimensions_if_needed)
|
|
|
|
@describe.attributes(
|
|
|
|
nI=Dimension("Input size"),
|
|
|
|
nF=Dimension("Number of features"),
|
|
|
|
nP=Dimension("Number of pieces"),
|
|
|
|
nO=Dimension("Output size"),
|
|
|
|
W=Synapses("Weights matrix",
|
|
|
|
lambda obj: (obj.nF, obj.nO, obj.nP, obj.nI),
|
|
|
|
lambda W, ops: ops.xavier_uniform_init(W)),
|
|
|
|
b=Biases("Bias vector",
|
|
|
|
lambda obj: (obj.nO, obj.nP)),
|
|
|
|
d_W=Gradient("W"),
|
|
|
|
d_b=Gradient("b")
|
|
|
|
)
|
|
|
|
class PrecomputableMaxouts(Model):
|
2017-05-25 14:46:59 +03:00
|
|
|
def __init__(self, nO=None, nI=None, nF=None, nP=3, **kwargs):
|
2017-05-08 15:24:43 +03:00
|
|
|
Model.__init__(self, **kwargs)
|
|
|
|
self.nO = nO
|
2017-05-25 14:46:59 +03:00
|
|
|
self.nP = nP
|
2017-05-08 15:24:43 +03:00
|
|
|
self.nI = nI
|
|
|
|
self.nF = nF
|
|
|
|
|
|
|
|
def begin_update(self, X, drop=0.):
|
|
|
|
# X: (b, i)
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
# Yfp: (b, f, o, p)
|
|
|
|
# Xf: (f, b, i)
|
2017-05-08 15:24:43 +03:00
|
|
|
# dYp: (b, o, p)
|
|
|
|
# W: (f, o, p, i)
|
|
|
|
# b: (o, p)
|
|
|
|
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
# bi,opfi->bfop
|
|
|
|
# bop,fopi->bfi
|
|
|
|
# bop,fbi->opfi : fopi
|
|
|
|
|
|
|
|
tensordot = self.ops.xp.tensordot
|
|
|
|
ascontiguous = self.ops.xp.ascontiguousarray
|
|
|
|
|
|
|
|
Yfp = tensordot(X, self.W, axes=[[1], [3]])
|
2017-05-08 15:24:43 +03:00
|
|
|
Yfp += self.b
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
|
|
|
|
def backward(dYp_ids, sgd=None):
|
|
|
|
dYp, ids = dYp_ids
|
2017-05-08 15:24:43 +03:00
|
|
|
Xf = X[ids]
|
|
|
|
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
dXf = tensordot(dYp, self.W, axes=[[1, 2], [1,2]])
|
|
|
|
dW = tensordot(dYp, Xf, axes=[[0], [0]])
|
2017-05-08 15:24:43 +03:00
|
|
|
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
self.d_W += dW.transpose((2, 0, 1, 3))
|
|
|
|
self.d_b += dYp.sum(axis=0)
|
2017-05-08 15:24:43 +03:00
|
|
|
|
|
|
|
if sgd is not None:
|
|
|
|
sgd(self._mem.weights, self._mem.gradient, key=self.id)
|
|
|
|
return dXf
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
return Yfp, backward
|
2017-05-08 15:24:43 +03:00
|
|
|
|
2017-10-03 14:26:55 +03:00
|
|
|
# Thinc's Embed class is a bit broken atm, so drop this here.
|
|
|
|
from thinc import describe
|
|
|
|
from thinc.neural._classes.embed import _uniform_init
|
|
|
|
|
2017-10-06 06:21:30 +03:00
|
|
|
|
2017-10-03 14:26:55 +03:00
|
|
|
@describe.attributes(
|
|
|
|
nV=describe.Dimension("Number of vectors"),
|
|
|
|
nO=describe.Dimension("Size of output"),
|
|
|
|
vectors=describe.Weights("Embedding table",
|
|
|
|
lambda obj: (obj.nV, obj.nO),
|
|
|
|
_uniform_init(-0.1, 0.1)
|
|
|
|
),
|
|
|
|
d_vectors=describe.Gradient("vectors")
|
|
|
|
)
|
|
|
|
class Embed(Model):
|
|
|
|
name = 'embed'
|
|
|
|
|
|
|
|
def __init__(self, nO, nV=None, **kwargs):
|
2017-10-06 14:10:13 +03:00
|
|
|
if nV is not None:
|
|
|
|
nV += 1
|
2017-10-03 14:26:55 +03:00
|
|
|
Model.__init__(self, **kwargs)
|
2017-10-05 03:55:34 +03:00
|
|
|
if 'name' in kwargs:
|
|
|
|
self.name = kwargs['name']
|
2017-10-03 14:26:55 +03:00
|
|
|
self.column = kwargs.get('column', 0)
|
|
|
|
self.nO = nO
|
|
|
|
self.nV = nV
|
|
|
|
|
|
|
|
def predict(self, ids):
|
|
|
|
if ids.ndim == 2:
|
|
|
|
ids = ids[:, self.column]
|
2017-10-06 06:21:30 +03:00
|
|
|
return self.ops.xp.ascontiguousarray(self.vectors[ids], dtype='f')
|
2017-10-03 14:26:55 +03:00
|
|
|
|
|
|
|
def begin_update(self, ids, drop=0.):
|
|
|
|
if ids.ndim == 2:
|
|
|
|
ids = ids[:, self.column]
|
2017-10-06 06:21:30 +03:00
|
|
|
vectors = self.ops.xp.ascontiguousarray(self.vectors[ids], dtype='f')
|
2017-10-03 14:26:55 +03:00
|
|
|
def backprop_embed(d_vectors, sgd=None):
|
|
|
|
n_vectors = d_vectors.shape[0]
|
|
|
|
self.ops.scatter_add(self.d_vectors, ids, d_vectors)
|
|
|
|
if sgd is not None:
|
|
|
|
sgd(self._mem.weights, self._mem.gradient, key=self.id)
|
|
|
|
return None
|
|
|
|
return vectors, backprop_embed
|
|
|
|
|
2017-07-20 01:17:17 +03:00
|
|
|
|
2017-10-03 13:43:09 +03:00
|
|
|
def HistoryFeatures(nr_class, hist_size=8, nr_dim=8):
|
|
|
|
'''Wrap a model, adding features representing action history.'''
|
2017-10-06 05:52:28 +03:00
|
|
|
if hist_size == 0:
|
|
|
|
return layerize(noop())
|
2017-10-05 03:55:34 +03:00
|
|
|
embed_tables = [Embed(nr_dim, nr_class, column=i, name='embed%d')
|
|
|
|
for i in range(hist_size)]
|
2017-10-08 05:00:43 +03:00
|
|
|
embed = chain(concatenate(*embed_tables),
|
|
|
|
LN(Maxout(hist_size*nr_dim, hist_size*nr_dim)))
|
2017-10-03 13:43:09 +03:00
|
|
|
ops = embed.ops
|
|
|
|
def add_history_fwd(vectors_hists, drop=0.):
|
|
|
|
vectors, hist_ids = vectors_hists
|
2017-10-06 14:09:18 +03:00
|
|
|
hist_feats, bp_hists = embed.begin_update(hist_ids, drop=drop)
|
2017-10-03 14:26:55 +03:00
|
|
|
outputs = ops.xp.hstack((vectors, hist_feats))
|
2017-10-03 13:43:09 +03:00
|
|
|
|
|
|
|
def add_history_bwd(d_outputs, sgd=None):
|
|
|
|
d_vectors = d_outputs[:, :vectors.shape[1]]
|
|
|
|
d_hists = d_outputs[:, vectors.shape[1]:]
|
2017-10-03 14:26:55 +03:00
|
|
|
bp_hists(d_hists, sgd=sgd)
|
2017-10-03 13:43:09 +03:00
|
|
|
return embed.ops.xp.ascontiguousarray(d_vectors)
|
|
|
|
return outputs, add_history_bwd
|
|
|
|
return wrap(add_history_fwd, embed)
|
|
|
|
|
|
|
|
|
2017-08-18 22:55:23 +03:00
|
|
|
def drop_layer(layer, factor=2.):
|
|
|
|
def drop_layer_fwd(X, drop=0.):
|
2017-09-04 17:26:38 +03:00
|
|
|
if drop <= 0.:
|
2017-08-18 22:55:23 +03:00
|
|
|
return layer.begin_update(X, drop=drop)
|
|
|
|
else:
|
2017-09-04 17:26:38 +03:00
|
|
|
coinflip = layer.ops.xp.random.random()
|
|
|
|
if (coinflip / factor) >= drop:
|
|
|
|
return layer.begin_update(X, drop=drop)
|
|
|
|
else:
|
|
|
|
return X, lambda dX, sgd=None: dX
|
2017-08-22 00:23:29 +03:00
|
|
|
|
|
|
|
model = wrap(drop_layer_fwd, layer)
|
|
|
|
model.predict = layer
|
|
|
|
return model
|
2017-08-18 22:55:23 +03:00
|
|
|
|
2017-09-22 17:38:36 +03:00
|
|
|
def link_vectors_to_models(vocab):
|
|
|
|
vectors = vocab.vectors
|
|
|
|
ops = Model.ops
|
|
|
|
for word in vocab:
|
|
|
|
if word.orth in vectors.key2row:
|
|
|
|
word.rank = vectors.key2row[word.orth]
|
|
|
|
else:
|
|
|
|
word.rank = 0
|
|
|
|
data = ops.asarray(vectors.data)
|
|
|
|
# Set an entry here, so that vectors are accessed by StaticVectors
|
|
|
|
# (unideal, I know)
|
|
|
|
thinc.extra.load_nlp.VECTORS[(ops.device, VECTORS_KEY)] = data
|
2017-08-18 22:55:23 +03:00
|
|
|
|
2017-09-21 15:59:48 +03:00
|
|
|
def Tok2Vec(width, embed_size, **kwargs):
|
|
|
|
pretrained_dims = kwargs.get('pretrained_dims', 0)
|
2017-10-11 10:44:17 +03:00
|
|
|
cnn_maxout_pieces = kwargs.get('cnn_maxout_pieces', 2)
|
2017-08-18 22:55:23 +03:00
|
|
|
cols = [ID, NORM, PREFIX, SUFFIX, SHAPE, ORTH]
|
2017-09-22 17:38:36 +03:00
|
|
|
with Model.define_operators({'>>': chain, '|': concatenate, '**': clone, '+': add,
|
|
|
|
'*': reapply}):
|
2017-09-06 13:50:58 +03:00
|
|
|
norm = HashEmbed(width, embed_size, column=cols.index(NORM), name='embed_norm')
|
|
|
|
prefix = HashEmbed(width, embed_size//2, column=cols.index(PREFIX), name='embed_prefix')
|
|
|
|
suffix = HashEmbed(width, embed_size//2, column=cols.index(SUFFIX), name='embed_suffix')
|
|
|
|
shape = HashEmbed(width, embed_size//2, column=cols.index(SHAPE), name='embed_shape')
|
2017-09-22 17:38:36 +03:00
|
|
|
if pretrained_dims is not None and pretrained_dims >= 1:
|
|
|
|
glove = StaticVectors(VECTORS_KEY, width, column=cols.index(ID))
|
|
|
|
|
|
|
|
embed = uniqued(
|
|
|
|
(glove | norm | prefix | suffix | shape)
|
|
|
|
>> LN(Maxout(width, width*5, pieces=3)), column=5)
|
|
|
|
else:
|
|
|
|
embed = uniqued(
|
|
|
|
(norm | prefix | suffix | shape)
|
|
|
|
>> LN(Maxout(width, width*4, pieces=3)), column=5)
|
|
|
|
|
2017-05-15 22:46:08 +03:00
|
|
|
|
2017-09-21 03:14:41 +03:00
|
|
|
convolution = Residual(
|
|
|
|
ExtractWindow(nW=1)
|
|
|
|
>> LN(Maxout(width, width*3, pieces=cnn_maxout_pieces))
|
|
|
|
)
|
2017-09-18 23:00:05 +03:00
|
|
|
|
2017-09-22 17:38:36 +03:00
|
|
|
tok2vec = (
|
|
|
|
FeatureExtracter(cols)
|
|
|
|
>> with_flatten(
|
2017-09-23 12:39:17 +03:00
|
|
|
embed >> (convolution ** 4), pad=4)
|
2017-09-22 17:38:36 +03:00
|
|
|
)
|
2017-09-16 20:45:37 +03:00
|
|
|
|
2017-05-15 22:46:08 +03:00
|
|
|
# Work around thinc API limitations :(. TODO: Revise in Thinc 7
|
|
|
|
tok2vec.nO = width
|
2017-05-30 01:53:29 +03:00
|
|
|
tok2vec.embed = embed
|
2017-05-15 22:46:08 +03:00
|
|
|
return tok2vec
|
|
|
|
|
2017-05-04 14:31:40 +03:00
|
|
|
|
2017-09-22 17:37:03 +03:00
|
|
|
def reapply(layer, n_times):
|
|
|
|
def reapply_fwd(X, drop=0.):
|
|
|
|
backprops = []
|
|
|
|
for i in range(n_times):
|
|
|
|
Y, backprop = layer.begin_update(X, drop=drop)
|
|
|
|
X = Y
|
|
|
|
backprops.append(backprop)
|
|
|
|
def reapply_bwd(dY, sgd=None):
|
|
|
|
dX = None
|
|
|
|
for backprop in reversed(backprops):
|
|
|
|
dY = backprop(dY, sgd=sgd)
|
|
|
|
if dX is None:
|
|
|
|
dX = dY
|
|
|
|
else:
|
|
|
|
dX += dY
|
|
|
|
return dX
|
|
|
|
return Y, reapply_bwd
|
|
|
|
return wrap(reapply_fwd, layer)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2017-05-22 12:47:47 +03:00
|
|
|
def asarray(ops, dtype):
|
|
|
|
def forward(X, drop=0.):
|
|
|
|
return ops.asarray(X, dtype=dtype), None
|
|
|
|
return layerize(forward)
|
|
|
|
|
|
|
|
|
2017-05-20 14:40:10 +03:00
|
|
|
def foreach(layer):
|
|
|
|
def forward(Xs, drop=0.):
|
|
|
|
results = []
|
|
|
|
backprops = []
|
|
|
|
for X in Xs:
|
|
|
|
result, bp = layer.begin_update(X, drop=drop)
|
|
|
|
results.append(result)
|
|
|
|
backprops.append(bp)
|
|
|
|
def backward(d_results, sgd=None):
|
|
|
|
dXs = []
|
|
|
|
for d_result, backprop in zip(d_results, backprops):
|
|
|
|
dXs.append(backprop(d_result, sgd))
|
|
|
|
return dXs
|
|
|
|
return results, backward
|
|
|
|
model = layerize(forward)
|
|
|
|
model._layers.append(layer)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def rebatch(size, layer):
|
|
|
|
ops = layer.ops
|
|
|
|
def forward(X, drop=0.):
|
|
|
|
if X.shape[0] < size:
|
|
|
|
return layer.begin_update(X)
|
|
|
|
parts = _divide_array(X, size)
|
|
|
|
results, bp_results = zip(*[layer.begin_update(p, drop=drop)
|
|
|
|
for p in parts])
|
|
|
|
y = ops.flatten(results)
|
|
|
|
def backward(dy, sgd=None):
|
|
|
|
d_parts = [bp(y, sgd=sgd) for bp, y in
|
|
|
|
zip(bp_results, _divide_array(dy, size))]
|
|
|
|
try:
|
|
|
|
dX = ops.flatten(d_parts)
|
|
|
|
except TypeError:
|
|
|
|
dX = None
|
|
|
|
except ValueError:
|
|
|
|
dX = None
|
|
|
|
return dX
|
|
|
|
return y, backward
|
|
|
|
model = layerize(forward)
|
|
|
|
model._layers.append(layer)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def _divide_array(X, size):
|
|
|
|
parts = []
|
|
|
|
index = 0
|
|
|
|
while index < len(X):
|
|
|
|
parts.append(X[index : index + size])
|
|
|
|
index += size
|
|
|
|
return parts
|
|
|
|
|
|
|
|
|
2017-05-04 14:31:40 +03:00
|
|
|
def get_col(idx):
|
2017-05-20 14:40:10 +03:00
|
|
|
assert idx >= 0, idx
|
2017-05-04 14:31:40 +03:00
|
|
|
def forward(X, drop=0.):
|
2017-05-20 14:40:10 +03:00
|
|
|
assert idx >= 0, idx
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
if isinstance(X, numpy.ndarray):
|
|
|
|
ops = NumpyOps()
|
|
|
|
else:
|
|
|
|
ops = CupyOps()
|
2017-05-18 14:21:32 +03:00
|
|
|
output = ops.xp.ascontiguousarray(X[:, idx], dtype=X.dtype)
|
2017-05-06 21:38:12 +03:00
|
|
|
def backward(y, sgd=None):
|
2017-05-20 14:40:10 +03:00
|
|
|
assert idx >= 0, idx
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
dX = ops.allocate(X.shape)
|
2017-05-06 21:38:12 +03:00
|
|
|
dX[:, idx] += y
|
|
|
|
return dX
|
|
|
|
return output, backward
|
2017-05-04 14:31:40 +03:00
|
|
|
return layerize(forward)
|
|
|
|
|
|
|
|
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
def zero_init(model):
|
|
|
|
def _hook(self, X, y=None):
|
|
|
|
self.W.fill(0)
|
|
|
|
model.on_data_hooks.append(_hook)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
|
|
|
def doc2feats(cols=None):
|
2017-08-18 22:55:23 +03:00
|
|
|
if cols is None:
|
|
|
|
cols = [ID, NORM, PREFIX, SUFFIX, SHAPE, ORTH]
|
2017-05-07 03:02:43 +03:00
|
|
|
def forward(docs, drop=0.):
|
2017-05-18 12:22:20 +03:00
|
|
|
feats = []
|
|
|
|
for doc in docs:
|
2017-05-22 12:47:47 +03:00
|
|
|
feats.append(doc.to_array(cols))
|
2017-05-07 03:02:43 +03:00
|
|
|
return feats, None
|
2017-05-06 17:47:15 +03:00
|
|
|
model = layerize(forward)
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
model.cols = cols
|
2017-05-06 17:47:15 +03:00
|
|
|
return model
|
|
|
|
|
2017-05-27 23:50:40 +03:00
|
|
|
|
2017-05-06 21:38:12 +03:00
|
|
|
def print_shape(prefix):
|
|
|
|
def forward(X, drop=0.):
|
|
|
|
return X, lambda dX, **kwargs: dX
|
|
|
|
return layerize(forward)
|
2017-05-07 04:57:26 +03:00
|
|
|
|
2017-05-06 21:38:12 +03:00
|
|
|
|
|
|
|
@layerize
|
|
|
|
def get_token_vectors(tokens_attrs_vectors, drop=0.):
|
|
|
|
ops = Model.ops
|
|
|
|
tokens, attrs, vectors = tokens_attrs_vectors
|
|
|
|
def backward(d_output, sgd=None):
|
|
|
|
return (tokens, d_output)
|
|
|
|
return vectors, backward
|
2017-09-04 17:26:38 +03:00
|
|
|
|
|
|
|
|
2017-05-06 15:22:20 +03:00
|
|
|
@layerize
|
|
|
|
def flatten(seqs, drop=0.):
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
if isinstance(seqs[0], numpy.ndarray):
|
|
|
|
ops = NumpyOps()
|
2017-05-15 22:46:08 +03:00
|
|
|
elif hasattr(CupyOps.xp, 'ndarray') and isinstance(seqs[0], CupyOps.xp.ndarray):
|
Update draft of parser neural network model
Model is good, but code is messy. Currently requires Chainer, which may cause the build to fail on machines without a GPU.
Outline of the model:
We first predict context-sensitive vectors for each word in the input:
(embed_lower | embed_prefix | embed_suffix | embed_shape)
>> Maxout(token_width)
>> convolution ** 4
This convolutional layer is shared between the tagger and the parser. This prevents the parser from needing tag features.
To boost the representation, we make a "super tag" with POS, morphology and dependency label. The tagger predicts this
by adding a softmax layer onto the convolutional layer --- so, we're teaching the convolutional layer to give us a
representation that's one affine transform from this informative lexical information. This is obviously good for the
parser (which backprops to the convolutions too).
The parser model makes a state vector by concatenating the vector representations for its context tokens. Current
results suggest few context tokens works well. Maybe this is a bug.
The current context tokens:
* S0, S1, S2: Top three words on the stack
* B0, B1: First two words of the buffer
* S0L1, S0L2: Leftmost and second leftmost children of S0
* S0R1, S0R2: Rightmost and second rightmost children of S0
* S1L1, S1L2, S1R2, S1R, B0L1, B0L2: Likewise for S1 and B0
This makes the state vector quite long: 13*T, where T is the token vector width (128 is working well). Fortunately,
there's a way to structure the computation to save some expense (and make it more GPU friendly).
The parser typically visits 2*N states for a sentence of length N (although it may visit more, if it back-tracks
with a non-monotonic transition). A naive implementation would require 2*N (B, 13*T) @ (13*T, H) matrix multiplications
for a batch of size B. We can instead perform one (B*N, T) @ (T, 13*H) multiplication, to pre-compute the hidden
weights for each positional feature wrt the words in the batch. (Note that our token vectors come from the CNN
-- so we can't play this trick over the vocabulary. That's how Stanford's NN parser works --- and why its model
is so big.)
This pre-computation strategy allows a nice compromise between GPU-friendliness and implementation simplicity.
The CNN and the wide lower layer are computed on the GPU, and then the precomputed hidden weights are moved
to the CPU, before we start the transition-based parsing process. This makes a lot of things much easier.
We don't have to worry about variable-length batch sizes, and we don't have to implement the dynamic oracle
in CUDA to train.
Currently the parser's loss function is multilabel log loss, as the dynamic oracle allows multiple states to
be 0 cost. This is defined as:
(exp(score) / Z) - (exp(score) / gZ)
Where gZ is the sum of the scores assigned to gold classes. I'm very interested in regressing on the cost directly,
but so far this isn't working well.
Machinery is in place for beam-search, which has been working well for the linear model. Beam search should benefit
greatly from the pre-computation trick.
2017-05-13 00:09:15 +03:00
|
|
|
ops = CupyOps()
|
2017-05-15 22:46:08 +03:00
|
|
|
else:
|
|
|
|
raise ValueError("Unable to flatten sequence of type %s" % type(seqs[0]))
|
2017-05-07 04:57:26 +03:00
|
|
|
lengths = [len(seq) for seq in seqs]
|
2017-05-06 15:22:20 +03:00
|
|
|
def finish_update(d_X, sgd=None):
|
2017-05-07 04:57:26 +03:00
|
|
|
return ops.unflatten(d_X, lengths)
|
|
|
|
X = ops.xp.vstack(seqs)
|
2017-05-06 15:22:20 +03:00
|
|
|
return X, finish_update
|
2017-07-20 01:17:17 +03:00
|
|
|
|
|
|
|
|
|
|
|
@layerize
|
|
|
|
def logistic(X, drop=0.):
|
|
|
|
xp = get_array_module(X)
|
|
|
|
if not isinstance(X, xp.ndarray):
|
|
|
|
X = xp.asarray(X)
|
|
|
|
# Clip to range (-10, 10)
|
|
|
|
X = xp.minimum(X, 10., X)
|
|
|
|
X = xp.maximum(X, -10., X)
|
|
|
|
Y = 1. / (1. + xp.exp(-X))
|
|
|
|
def logistic_bwd(dY, sgd=None):
|
|
|
|
dX = dY * (Y * (1-Y))
|
|
|
|
return dX
|
|
|
|
return Y, logistic_bwd
|
|
|
|
|
|
|
|
|
|
|
|
def zero_init(model):
|
|
|
|
def _zero_init_impl(self, X, y):
|
|
|
|
self.W.fill(0)
|
|
|
|
model.on_data_hooks.append(_zero_init_impl)
|
|
|
|
return model
|
|
|
|
|
|
|
|
@layerize
|
|
|
|
def preprocess_doc(docs, drop=0.):
|
|
|
|
keys = [doc.to_array([LOWER]) for doc in docs]
|
|
|
|
ops = Model.ops
|
|
|
|
lengths = ops.asarray([arr.shape[0] for arr in keys])
|
|
|
|
keys = ops.xp.concatenate(keys)
|
|
|
|
vals = ops.allocate(keys.shape[0]) + 1
|
|
|
|
return (keys, vals, lengths), None
|
|
|
|
|
2017-08-18 22:55:23 +03:00
|
|
|
def getitem(i):
|
|
|
|
def getitem_fwd(X, drop=0.):
|
|
|
|
return X[i], None
|
|
|
|
return layerize(getitem_fwd)
|
|
|
|
|
2017-09-21 21:07:26 +03:00
|
|
|
def build_tagger_model(nr_class, **cfg):
|
2017-09-28 16:07:41 +03:00
|
|
|
embed_size = util.env_opt('embed_size', 7000)
|
2017-09-21 21:07:26 +03:00
|
|
|
if 'token_vector_width' in cfg:
|
|
|
|
token_vector_width = cfg['token_vector_width']
|
|
|
|
else:
|
2017-09-23 03:58:54 +03:00
|
|
|
token_vector_width = util.env_opt('token_vector_width', 128)
|
2017-09-21 21:07:26 +03:00
|
|
|
pretrained_dims = cfg.get('pretrained_dims', 0)
|
2017-08-18 22:55:23 +03:00
|
|
|
with Model.define_operators({'>>': chain, '+': add}):
|
2017-09-26 13:51:52 +03:00
|
|
|
if 'tok2vec' in cfg:
|
|
|
|
tok2vec = cfg['tok2vec']
|
|
|
|
else:
|
|
|
|
tok2vec = Tok2Vec(token_vector_width, embed_size,
|
|
|
|
pretrained_dims=pretrained_dims)
|
2017-09-23 03:58:54 +03:00
|
|
|
model = (
|
2017-09-21 15:59:48 +03:00
|
|
|
tok2vec
|
2017-09-23 03:58:54 +03:00
|
|
|
>> with_flatten(Softmax(nr_class, token_vector_width))
|
2017-08-18 22:55:23 +03:00
|
|
|
)
|
|
|
|
model.nI = None
|
2017-09-21 15:59:48 +03:00
|
|
|
model.tok2vec = tok2vec
|
2017-08-18 22:55:23 +03:00
|
|
|
return model
|
|
|
|
|
2017-08-06 02:13:23 +03:00
|
|
|
|
2017-09-01 17:39:55 +03:00
|
|
|
@layerize
|
|
|
|
def SpacyVectors(docs, drop=0.):
|
|
|
|
xp = get_array_module(docs[0].vocab.vectors.data)
|
|
|
|
width = docs[0].vocab.vectors.data.shape[1]
|
|
|
|
batch = []
|
|
|
|
for doc in docs:
|
|
|
|
indices = numpy.zeros((len(doc),), dtype='i')
|
|
|
|
for i, word in enumerate(doc):
|
|
|
|
if word.orth in doc.vocab.vectors.key2row:
|
|
|
|
indices[i] = doc.vocab.vectors.key2row[word.orth]
|
|
|
|
else:
|
|
|
|
indices[i] = 0
|
|
|
|
vectors = doc.vocab.vectors.data[indices]
|
|
|
|
batch.append(vectors)
|
|
|
|
return batch, None
|
|
|
|
|
|
|
|
|
|
|
|
def foreach(layer, drop_factor=1.0):
|
|
|
|
'''Map a layer across elements in a list'''
|
|
|
|
def foreach_fwd(Xs, drop=0.):
|
|
|
|
drop *= drop_factor
|
|
|
|
ys = []
|
|
|
|
backprops = []
|
|
|
|
for X in Xs:
|
|
|
|
y, bp_y = layer.begin_update(X, drop=drop)
|
|
|
|
ys.append(y)
|
|
|
|
backprops.append(bp_y)
|
|
|
|
def foreach_bwd(d_ys, sgd=None):
|
|
|
|
d_Xs = []
|
|
|
|
for d_y, bp_y in zip(d_ys, backprops):
|
|
|
|
if bp_y is not None and bp_y is not None:
|
|
|
|
d_Xs.append(d_y, sgd=sgd)
|
|
|
|
else:
|
|
|
|
d_Xs.append(None)
|
|
|
|
return d_Xs
|
|
|
|
return ys, foreach_bwd
|
|
|
|
model = wrap(foreach_fwd, layer)
|
|
|
|
return model
|
|
|
|
|
|
|
|
|
2017-07-20 01:17:17 +03:00
|
|
|
def build_text_classifier(nr_class, width=64, **cfg):
|
2017-09-02 15:56:30 +03:00
|
|
|
nr_vector = cfg.get('nr_vector', 5000)
|
2017-10-04 15:55:15 +03:00
|
|
|
pretrained_dims = cfg.get('pretrained_dims', 0)
|
2017-09-01 17:39:55 +03:00
|
|
|
with Model.define_operators({'>>': chain, '+': add, '|': concatenate,
|
|
|
|
'**': clone}):
|
2017-09-02 15:56:30 +03:00
|
|
|
if cfg.get('low_data'):
|
|
|
|
model = (
|
|
|
|
SpacyVectors
|
|
|
|
>> flatten_add_lengths
|
2017-09-02 16:17:32 +03:00
|
|
|
>> with_getitem(0,
|
2017-10-04 15:55:15 +03:00
|
|
|
Affine(width, pretrained_dims)
|
2017-09-02 16:17:32 +03:00
|
|
|
)
|
2017-09-02 15:56:30 +03:00
|
|
|
>> ParametricAttention(width)
|
|
|
|
>> Pooling(sum_pool)
|
|
|
|
>> Residual(ReLu(width, width)) ** 2
|
|
|
|
>> zero_init(Affine(nr_class, width, drop_factor=0.0))
|
|
|
|
>> logistic
|
|
|
|
)
|
|
|
|
return model
|
2017-07-20 01:17:17 +03:00
|
|
|
|
2017-09-02 15:56:30 +03:00
|
|
|
|
2017-09-01 17:39:55 +03:00
|
|
|
lower = HashEmbed(width, nr_vector, column=1)
|
|
|
|
prefix = HashEmbed(width//2, nr_vector, column=2)
|
|
|
|
suffix = HashEmbed(width//2, nr_vector, column=3)
|
|
|
|
shape = HashEmbed(width//2, nr_vector, column=4)
|
|
|
|
|
|
|
|
trained_vectors = (
|
|
|
|
FeatureExtracter([ORTH, LOWER, PREFIX, SUFFIX, SHAPE, ID])
|
|
|
|
>> with_flatten(
|
2017-07-25 19:57:59 +03:00
|
|
|
uniqued(
|
2017-09-02 12:41:00 +03:00
|
|
|
(lower | prefix | suffix | shape)
|
2017-09-02 15:56:30 +03:00
|
|
|
>> LN(Maxout(width, width+(width//2)*3)),
|
2017-09-02 12:41:00 +03:00
|
|
|
column=0
|
|
|
|
)
|
2017-07-20 01:17:17 +03:00
|
|
|
)
|
2017-09-01 17:39:55 +03:00
|
|
|
)
|
|
|
|
|
2017-10-04 15:55:15 +03:00
|
|
|
if pretrained_dims:
|
|
|
|
static_vectors = (
|
|
|
|
SpacyVectors
|
|
|
|
>> with_flatten(Affine(width, pretrained_dims))
|
|
|
|
)
|
2017-09-01 17:39:55 +03:00
|
|
|
# TODO Make concatenate support lists
|
2017-10-04 15:55:15 +03:00
|
|
|
vectors = concatenate_lists(trained_vectors, static_vectors)
|
|
|
|
vectors_width = width*2
|
|
|
|
else:
|
|
|
|
vectors = trained_vectors
|
|
|
|
vectors_width = width
|
|
|
|
static_vectors = None
|
|
|
|
cnn_model = (
|
|
|
|
vectors
|
2017-09-02 15:56:30 +03:00
|
|
|
>> with_flatten(
|
2017-10-04 15:55:15 +03:00
|
|
|
LN(Maxout(width, vectors_width))
|
2017-09-02 15:56:30 +03:00
|
|
|
>> Residual(
|
2017-10-04 16:15:53 +03:00
|
|
|
(ExtractWindow(nW=1) >> LN(Maxout(width, width*3)))
|
2017-09-02 15:56:30 +03:00
|
|
|
) ** 2, pad=2
|
2017-09-02 12:41:00 +03:00
|
|
|
)
|
2017-09-02 15:56:30 +03:00
|
|
|
>> flatten_add_lengths
|
2017-09-01 17:39:55 +03:00
|
|
|
>> ParametricAttention(width)
|
2017-07-25 19:57:59 +03:00
|
|
|
>> Pooling(sum_pool)
|
2017-09-02 15:56:30 +03:00
|
|
|
>> Residual(zero_init(Maxout(width, width)))
|
2017-07-25 19:57:59 +03:00
|
|
|
>> zero_init(Affine(nr_class, width, drop_factor=0.0))
|
2017-07-23 15:10:51 +03:00
|
|
|
)
|
2017-09-01 17:39:55 +03:00
|
|
|
|
2017-07-23 15:10:51 +03:00
|
|
|
linear_model = (
|
|
|
|
_preprocess_doc
|
2017-07-25 19:57:59 +03:00
|
|
|
>> LinearModel(nr_class, drop_factor=0.)
|
2017-07-23 15:10:51 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
model = (
|
2017-07-25 19:57:59 +03:00
|
|
|
(linear_model | cnn_model)
|
|
|
|
>> zero_init(Affine(nr_class, nr_class*2, drop_factor=0.0))
|
2017-07-20 01:17:17 +03:00
|
|
|
>> logistic
|
|
|
|
)
|
2017-10-04 17:07:30 +03:00
|
|
|
model.nO = nr_class
|
2017-07-20 01:17:17 +03:00
|
|
|
model.lsuv = False
|
|
|
|
return model
|
|
|
|
|
2017-09-01 17:39:55 +03:00
|
|
|
@layerize
|
|
|
|
def flatten(seqs, drop=0.):
|
|
|
|
ops = Model.ops
|
|
|
|
lengths = ops.asarray([len(seq) for seq in seqs], dtype='i')
|
|
|
|
def finish_update(d_X, sgd=None):
|
|
|
|
return ops.unflatten(d_X, lengths, pad=0)
|
|
|
|
X = ops.flatten(seqs, pad=0)
|
|
|
|
return X, finish_update
|
|
|
|
|
|
|
|
|
|
|
|
def concatenate_lists(*layers, **kwargs): # pragma: no cover
|
|
|
|
'''Compose two or more models `f`, `g`, etc, such that their outputs are
|
|
|
|
concatenated, i.e. `concatenate(f, g)(x)` computes `hstack(f(x), g(x))`
|
|
|
|
'''
|
|
|
|
if not layers:
|
|
|
|
return noop()
|
|
|
|
drop_factor = kwargs.get('drop_factor', 1.0)
|
|
|
|
ops = layers[0].ops
|
|
|
|
layers = [chain(layer, flatten) for layer in layers]
|
|
|
|
concat = concatenate(*layers)
|
|
|
|
def concatenate_lists_fwd(Xs, drop=0.):
|
|
|
|
drop *= drop_factor
|
|
|
|
lengths = ops.asarray([len(X) for X in Xs], dtype='i')
|
|
|
|
flat_y, bp_flat_y = concat.begin_update(Xs, drop=drop)
|
|
|
|
ys = ops.unflatten(flat_y, lengths)
|
|
|
|
def concatenate_lists_bwd(d_ys, sgd=None):
|
|
|
|
return bp_flat_y(ops.flatten(d_ys), sgd=sgd)
|
|
|
|
return ys, concatenate_lists_bwd
|
|
|
|
model = wrap(concatenate_lists_fwd, concat)
|
|
|
|
return model
|