Beispiel #1
0
    y = np.random.choice([0, 1], size=(N,), p=[1./3, 2./3])
    test.append((x, y))

    
model = RRBM(N, train)
# check partition function
assert np.allclose(model.Z(model.W, model.b, train[0][0]), model.enum_Z(model.W, model.b, train[0][0]), atol = 0.01)

gW_fd, gb_fd = fd(model)
gW, gb = model.g(model.W, model.b)

# check gradient
assert np.allclose(gW, gW_fd, atol=0.01)
assert np.allclose(gb, gb_fd, atol=0.01)

# check conversion
W2, b2 = model.vec2theta(model.theta2vec(model.W, model.b))
assert (W2 == model.W).all()
assert (b2 == model.b).all()

# check optimization
err, ham = model.eval(test)
print err
print ham

model.learn()

err, ham = model.eval(test)
print err
print ham
                characters[i] = 1.0
            test.append((lex_col[lang1][l1], lex_col[lang2][l2], embedding, characters))

        
        model.reset()
        model.train = train

        before = model.eval(test)[0:2]
        #print size, experiment

        C_best = 0
        err, hmm, mistakes = float("inf"), float("inf"), None
        # grid search
        for C in [1.0]: #0.0, 0.1, 0.5, 1.0, 2.0, 3.0, 4.0]:
            model.C = C
            model.learn(disp=0)
            err_tmp, hmm_tmp, mistakes_tmp = model.eval(test)

            if hmm_tmp < hmm:
                err, hmm, mistakes = err_tmp, hmm_tmp, mistakes_tmp
                C_best = C
        after = (err, hmm)


        print size, experiment, before, after, C_best

        for iter in xrange(100):
            train2 = []
            for datum in train:
                train2.append(datum)