Exemplo n.º 1
0
 def train_batch(model, xy):
     x, y = xy
     loss = 0.0
     for x, y in zip(X, Y):
         eg = Example(nr_class=model.nr_class, widths=model.widths)
         eg.features = x
         eg.costs = y
         eg = list(model.update(eg, force_update=True))[-1]
         loss += eg.loss
     return loss
Exemplo n.º 2
0
def get_scores(nr_class, model, feats):
    eg = Example(nr_class)
    eg.set_features(feats)
    model(eg)
    return list(eg.scores)
Exemplo n.º 3
0
def get_score(nr_class, model, feats, clas):
    eg = Example(nr_class)
    eg.set_features(feats)
    eg.set_label(clas)
    model(eg)
    return eg.scores[clas]
Exemplo n.º 4
0
def test_basic():
    nr_class = 3
    model = AveragedPerceptron(((1,), (2,), (3,), (4,), (5,)))
    instances = [
        (1, {1: 1, 3: -5}),
        (2, {2: 4, 3: 5})
    ]
    for clas, feats in instances:
        eg = Example(nr_class)
        eg.set_features(feats)
        model(eg)
        eg.set_label(clas)
        model.update(eg)
    eg = Example(nr_class)
    eg.set_features({1: 2, 2: 1})
    model(eg)
    assert eg.guess == 2
    eg = Example(nr_class)
    eg.set_features({0: 2, 2: 1})
    model(eg)
    assert eg.scores[1] == 0
    eg = Example(nr_class)
    eg.set_features({1: 2, 2: 1})
    model(eg)
    assert eg.scores[2] > 0
    eg = Example(nr_class)
    eg.set_features({1: 2, 1: 1})
    model(eg)
    assert eg.scores[1] > 0
    eg = Example(nr_class)
    eg.set_features({0: 3, 3: 1})
    model(eg)
    assert eg.scores[1] < 0 
    eg = Example(nr_class)
    eg.set_features({0: 3, 3: 1})
    model(eg)
    assert eg.scores[2] > 0 
Exemplo n.º 5
0
 def __init__(self, nlp, nr_class):
     self.nlp = nlp
     self.nr_class = nr_class
     self._eg = Example(nr_class=nr_class)
     self._model = AveragedPerceptron([])
Exemplo n.º 6
0
 def Eg(self, text, label=None):
     bow = self.get_feats(text, dropout=bool(label is not None))
     eg = Example(nr_class=self.nr_class, nr_feat=len(bow))
     eg.costs = [i != label for i in range(self.nr_class)]
     eg.features = bow
     return eg
Exemplo n.º 7
0
def test_basic():
    nr_class = 3
    model = AveragedPerceptron(((1, ), (2, ), (3, ), (4, ), (5, )))
    instances = [(1, {1: 1, 3: -5}), (2, {2: 4, 3: 5})]
    for clas, feats in instances:
        eg = Example(nr_class)
        eg.features = feats
        model(eg)
        eg.costs = [i != clas for i in range(nr_class)]
        model.update(eg)
    eg = Example(nr_class)
    eg.features = {1: 2, 2: 1}
    model(eg)
    assert eg.guess == 2
    eg = Example(nr_class)
    eg.features = {0: 2, 2: 1}
    model(eg)
    assert eg.scores[1] == 0
    eg = Example(nr_class)
    eg.features = {1: 2, 2: 1}
    model(eg)
    assert eg.scores[2] > 0
    eg = Example(nr_class)
    eg.features = {1: 2, 1: 1}
    model(eg)
    assert eg.scores[1] > 0
    eg = Example(nr_class)
    eg.features = {0: 3, 3: 1}
    model(eg)
    assert eg.scores[1] < 0
    eg = Example(nr_class)
    eg.features = {0: 3, 3: 1}
    model(eg)
    assert eg.scores[2] > 0
Exemplo n.º 8
0
def get_scores(nr_class, model, feats):
    eg = Example(nr_class)
    eg.features = feats
    model(eg)
    return list(eg.scores)
Exemplo n.º 9
0
def get_score(nr_class, model, feats, clas):
    eg = Example(nr_class)
    eg.features = feats
    eg.costs = [i != clas for i in range(nr_class)]
    model(eg)
    return eg.scores[clas]
Exemplo n.º 10
0
def get_score(nr_class, model, feats, clas):
    eg = Example(nr_class)
    eg.features = feats
    eg.costs = [i != clas for i in range(nr_class)]
    model(eg)
    return eg.scores[clas]
Exemplo n.º 11
0
def test_basic():
    nr_class = 3
    model = AveragedPerceptron(((1,), (2,), (3,), (4,), (5,)))
    instances = [
        (1, {1: 1, 3: -5}),
        (2, {2: 4, 3: 5})
    ]
    for clas, feats in instances:
        eg = Example(nr_class)
        eg.features = feats
        model(eg)
        eg.costs = [i != clas for i in range(nr_class)]
        model.update(eg)
    eg = Example(nr_class)
    eg.features = {1: 2, 2: 1}
    model(eg)
    assert eg.guess == 2
    eg = Example(nr_class)
    eg.features = {0: 2, 2: 1}
    model(eg)
    assert eg.scores[1] == 0
    eg = Example(nr_class)
    eg.features = {1: 2, 2: 1}
    model(eg)
    assert eg.scores[2] > 0
    eg = Example(nr_class)
    eg.features = {1: 2, 1: 1}
    model(eg)
    assert eg.scores[1] > 0
    eg = Example(nr_class)
    eg.features = {0: 3, 3: 1}
    model(eg)
    assert eg.scores[1] < 0 
    eg = Example(nr_class)
    eg.features = {0: 3, 3: 1}
    model(eg)
    assert eg.scores[2] > 0 
Exemplo n.º 12
0
def test_embedding():
    model = NeuralNet((10, 4, 2), embed=((5, ), (0, 0)), rho=0.0, eta=0.005)
    assert model.nr_in == 10
    eg = Example(nr_class=model.nr_class, widths=model.widths)
    eg.features = {(0, 1): 2.5}
    model(eg)
    assert eg.activation(0, 0) != 0
    assert eg.activation(0, 1) != 0
    assert eg.activation(0, 2) != 0
    assert eg.activation(0, 3) != 0
    assert eg.activation(0, 4) != 0

    eg = Example(nr_class=model.nr_class, widths=model.widths)
    eg.features = {(1, 1867): 0.5}
    model(eg)
    assert eg.activation(0, 0) == 0.0
    assert eg.activation(0, 1) == 0.0
    assert eg.activation(0, 2) == 0.0
    assert eg.activation(0, 3) == 0.0
    assert eg.activation(0, 4) == 0.0
    assert eg.activation(0, 5) != 0.0
    assert eg.activation(0, 6) != 0.0
    assert eg.activation(0, 7) != 0.0
    assert eg.activation(0, 8) != 0.0
    assert eg.activation(0, 9) != 0.0