return (keys, vals, lengths), None @describe.on_data(_set_dimensions_if_needed, lambda model, X, y: model.init_weights(model)) @describe.attributes(nI=Dimension("Input size"), nF=Dimension("Number of features"), nO=Dimension("Output size"), nP=Dimension("Maxout pieces"), W=Synapses("Weights matrix", lambda obj: (obj.nF, obj.nO, obj.nP, obj.nI)), b=Biases("Bias vector", lambda obj: (obj.nO, obj.nP)), pad=Synapses("Pad", lambda obj: (1, obj.nF, obj.nO, obj.nP), lambda M, ops: ops.normal_init(M, 1.)), d_W=Gradient("W"), d_pad=Gradient("pad"), d_b=Gradient("b")) class PrecomputableAffine(Model): def __init__(self, nO=None, nI=None, nF=None, nP=None, **kwargs): Model.__init__(self, **kwargs) self.nO = nO self.nP = nP self.nI = nI self.nF = nF def begin_update(self, X, drop=0.): Yf = self.ops.xp.dot( X, self.W.reshape((self.nF * self.nO * self.nP, self.nI)).T) Yf = Yf.reshape((Yf.shape[0], self.nF, self.nO, self.nP))
@describe.on_data(_set_dimensions_if_needed, lambda model, X, y: model.init_weights(model)) @describe.attributes( nI=Dimension("Input size"), nF=Dimension("Number of features"), nO=Dimension("Output size"), nP=Dimension("Maxout pieces"), W=Synapses("Weights matrix", lambda obj: (obj.nF, obj.nO, obj.nP, obj.nI)), b=Biases("Bias vector", lambda obj: (obj.nO, obj.nP)), pad=Synapses( "Pad", lambda obj: (1, obj.nF, obj.nO, obj.nP), lambda M, ops: ops.normal_init(M, 1.0), ), d_W=Gradient("W"), d_pad=Gradient("pad"), d_b=Gradient("b"), ) class PrecomputableAffine(Model): def __init__(self, nO=None, nI=None, nF=None, nP=None, **kwargs): Model.__init__(self, **kwargs) self.nO = nO self.nP = nP self.nI = nI self.nF = nF def begin_update(self, X, drop=0.0): Yf = self.ops.gemm(X, self.W.reshape( (self.nF * self.nO * self.nP, self.nI)),
return seqs, None from thinc import describe from thinc.describe import Dimension, Synapses, Gradient from thinc.neural._lsuv import LSUVinit @describe.on_data(LSUVinit) @describe.attributes( nM=Dimension("Vector dimensions"), nO=Dimension("Size of output"), W=Synapses("A projection matrix, to change vector dimensionality", lambda obj: (obj.nO, obj.nM), lambda W, ops: ops.xavier_uniform_init(W)), d_W=Gradient("W"), ) class SpacyVectors(Model): ops = NumpyOps() name = 'spacy-vectors' def __init__(self, nlp, nO): Model.__init__(self) self._id_map = {0: 0} self.nO = nO self.nM = nlp.vocab.vectors_length self.nlp = nlp @property def nV(self): return len(self.nlp.vocab)