예제 #1
0
            reduce(lambda x, y: list(x) + list(y),
                   train_lex + valid_lex + test_lex)))
    num_classes = len(
        set(reduce(lambda x, y: list(x) + list(y),
                   train_y + valid_y + test_y)))
    num_sentences = len(train_lex)

    # instantiate the model
    numpy.random.seed(s['seed'])
    random.seed(s['seed'])

    if rnn_type == "elman":
        rnn = elman.model(nh=s['nhidden'],
                          nc=num_classes,
                          ne=voc_size,
                          de=s['emb_dimension'],
                          cs=s['win'],
                          em=dataset["embeddings"],
                          init=initialize,
                          featdim=14)

    elif rnn_type == "jordan":
        rnn = jordan.model(nh=s['nhidden'],
                           nc=num_classes,
                           ne=voc_size,
                           de=s['emb_dimension'],
                           cs=s['win'],
                           em=dataset["embeddings"],
                           init=initialize)

    elif rnn_type == "elman_M1":
        rnn = elman_M1.model(nh=s['nhidden'],
예제 #2
0
    assert len(test_feat)  == len(test_lex)  == len(test_y)
    assert len(valid_feat) == len(valid_lex) == len(valid_y)

    voc_size = len(set(reduce(lambda x, y: list(x) + list(y), train_lex + valid_lex + test_lex)))
    num_classes = len(set(reduce(lambda x, y: list(x) + list(y), train_y + valid_y + test_y)))
    num_sentences = len(train_lex)

    # instantiate the model
    numpy.random.seed(s['seed'])
    random.seed(s['seed'])

    if rnn_type == "elman":
        rnn = elman.model(nh=s['nhidden'],
                          nc=num_classes,
                          ne=voc_size,
                          de=s['emb_dimension'],
                          cs=s['win'],
                          em=dataset["embeddings"],
                          init=initialize,
                          featdim=14)

    elif rnn_type == "jordan":
        rnn = jordan.model(nh=s['nhidden'],
                           nc=num_classes,
                           ne=voc_size,
                           de=s['emb_dimension'],
                           cs=s['win'],
                           em=dataset["embeddings"],
                           init=initialize)
    else:
        print "Invalid RNN type: ", rnn_type
        sys.exit(-1)
예제 #3
0
    idx2word  = dict((k,v) for v,k in dic['words2idx'].items())

    train_lex, train_ne, train_y = train_set
    valid_lex, valid_ne, valid_y = valid_set
    test_lex,  test_ne,  test_y  = test_set

    vocsize = len(dic['words2idx'])
    nclasses = len(dic['labels2idx'])
    nsentences = len(train_lex)

    # instanciate the model
    numpy.random.seed(s['seed'])
    random.seed(s['seed'])
    rnn = model(    nh = s['nhidden'],
                    nc = nclasses,
                    ne = vocsize,
                    de = s['emb_dimension'],
                    cs = s['win'] )

    # train with early stopping on validation set
    best_f1 = -numpy.inf
    s['clr'] = s['lr']
    for e in range(s['nepochs']):
        # shuffle
        shuffle([train_lex, train_ne, train_y], s['seed'])
        s['ce'] = e
        tic = time.time()
        for i in range(nsentences):
            cwords = contextwin(train_lex[i], s['win'])
            words  = map(lambda x: numpy.asarray(x).astype('int32'),\
                         minibatch(cwords, s['bs']))
예제 #4
0
    vocsize = len(set(reduce(\
                       lambda x, y: list(x)+list(y),\
                       train_lex+valid_lex+test_lex)))

    nclasses = len(set(reduce(\
                       lambda x, y: list(x)+list(y),\
                       train_y+test_y+valid_y)))
    
    nsentences = len(train_lex)

    # instanciate the model
    numpy.random.seed(s['seed'])
    random.seed(s['seed'])
    rnn = model(    nh = s['nhidden'],
                    nc = nclasses,
                    ne = vocsize,
                    de = s['emb_dimension'],
                    cs = s['win'] )

    # train with early stopping on validation set
    best_f1 = -numpy.inf
    s['clr'] = s['lr']
    for e in xrange(s['nepochs']):
        # shuffle
        shuffle([train_lex, train_ne, train_y], s['seed'])
        s['ce'] = e
        tic = time.time()
        for i in xrange(nsentences):
            cwords = contextwin(train_lex[i], s['win'])
            words  = map(lambda x: numpy.asarray(x).astype('int32'),\
                         minibatch(cwords, s['bs']))