Exemplo n.º 1
0
def main():
    print "Starting evaluation..."
    sents = load_sentences("c:\\corpora\\corrected.txt")
    for i in xrange(len(sents)):
        mid_index = get_check_index(sents[i])
        print get_original_word(sents[i], mid_index), "->", get_corrected_word(sents[i], mid_index)

    train = list(read("C:\\corpora\\long30k.txt"))
    words = []
    wc = Counter()
    for sent in train:
        for w in sent:
            words.append(w)
            wc[w] += 1

    vw = Vocab.from_corpus([words])

    nwords = vw.size()
    LAYERS = 2
    INPUT_DIM = 200  # 50  #256
    HIDDEN_DIM = 300  # 50  #1024
    print "words", nwords
    # DyNet Starts
    dy.init()
    model = dy.Model()
    #W_sm = model.add_parameters((nwords, HIDDEN_DIM))
    #b_sm = model.add_parameters(nwords)
    #trainer = dy.SimpleSGDTrainer(model)
    #WORDS_LOOKUP = model.add_lookup_parameters((nwords, INPUT_DIM))
    #RNN = dy.LSTMBuilder(LAYERS, INPUT_DIM, HIDDEN_DIM, model)
    (RNN, WORDS_LOOKUP, W_sm, b_sm) = model.load("C:\\corpora\\batch_bigmodel.txt")

    for sentence in sents:
        evaluate_sentence(sentence, vw, [RNN, WORDS_LOOKUP, W_sm, b_sm])
Exemplo n.º 2
0
    def __init__(self, c2i, num_lstm_layers=-1,\
                char_dim=-1, hidden_dim=-1, word_embedding_dim=-1, file=None):
        self.c2i = c2i
        self._model = dy.Model()
        #self._model = dy.ParameterCollection()
        if file == None:
            # Char LSTM Parameters
            self.char_lookup = self._model.add_lookup_parameters((len(c2i), char_dim))
            self.char_fwd_lstm = dy.LSTMBuilder(num_lstm_layers, char_dim, hidden_dim, self._model)
            self.char_bwd_lstm = dy.LSTMBuilder(num_lstm_layers, char_dim, hidden_dim, self._model)

            # Post-LSTM Parameters
            self.lstm_to_rep_params = self._model.add_parameters((word_embedding_dim, hidden_dim * 2))
            self.lstm_to_rep_bias = self._model.add_parameters(word_embedding_dim)
            self.mlp_out = self._model.add_parameters((word_embedding_dim, word_embedding_dim))
            self.mlp_out_bias = self._model.add_parameters(word_embedding_dim)
        else:
            model_members = iter(self._model.load(file))
            #pc2 = dy.ParameterCollection()
            #model_members = iter(dy.load(file, pc2))
            self.char_lookup = model_members.next()
            self.char_fwd_lstm = model_members.next()
            self.char_bwd_lstm = model_members.next()
            self.lstm_to_rep_params = model_members.next()
            self.lstm_to_rep_bias = model_members.next()
            self.mlp_out = model_members.next()
            self.mlp_out_bias = model_members.next()
Exemplo n.º 3
0
def main():
    model = dy.Model()
    trainer = dy.SimpleSGDTrainer(model)
    if sys.argv[1] == '-train':
        training_src = read_file(sys.argv[2])
        training_tgt = read_file(sys.argv[3])
        dev_src = read_file(sys.argv[4])
        dev_tgt = read_file(sys.argv[5])
        test_src = read_file(sys.argv[6])
        blind_src = read_file(sys.argv[7])
        attention = Attention(model, training_src, training_tgt, dev_src,
                              dev_tgt, test_src, blind_src)
        attention.train()

    elif sys.argv[1] == '-trans':
        test_src = read_file(sys.argv[2])
        attention = Attention(model, None, None, None, None, test_src, None,
                              'test')
Exemplo n.º 4
0
    for w in sent:
        words.append(w)
        wc[w] += 1

vw = Vocab.from_corpus([words])
STOP = vw.w2i["<stop>"]
START = vw.w2i["<start>"]
nwords = vw.size()

LAYERS = 1
INPUT_DIM = 200  #50  #256
HIDDEN_DIM = 200  # 50  #1024
print "words", nwords

dy.init()
model = dy.Model()
trainer = dy.AdamTrainer(model)

WORDS_LOOKUP = model.add_lookup_parameters((nwords, INPUT_DIM))

W_sm = model.add_parameters((nwords, HIDDEN_DIM * 2))
b_sm = model.add_parameters(nwords)

builders = [
    LSTMBuilder(LAYERS, INPUT_DIM, HIDDEN_DIM, model),
    LSTMBuilder(LAYERS, INPUT_DIM, HIDDEN_DIM, model),
]


def predict_middle_word(iprefix, ipostfix, builders):
    renew_cg()
Exemplo n.º 5
0
import _gdynet as G
print()
import _dynet as C

cm = C.Model()
gm = G.Model()

cpW = cm.add_parameters((1000,1000))
gpW = gm.add_parameters((1000,1000))

def do_cpu():
	C.renew_cg()
	W = C.parameter(cpW)
	W = W*W*W*W*W*W*W
	z = C.squared_distance(W,W)
	z.value()
	z.backward()

def do_gpu():
	G.renew_cg()
	W = G.parameter(gpW)
	W = W*W*W*W*W*W*W
	z = G.squared_distance(W,W)
	z.value()
	z.backward()

import time
s = time.time()
do_cpu()
print("CPU time:",time.time() - s)