Esempio n. 1
0
def build_model(word_vocab):
    if FLAGS.model_choice == 'bilstm':
        valid_model = model.cnn_sen_enc(
            word_vocab_size=word_vocab.size,
            word_embed_size=FLAGS.word_embed_size,
            batch_size=FLAGS.batch_size,
            num_highway_layers=FLAGS.highway_layers,
            max_sen_length=FLAGS.max_sen_length,
            kernels=eval(FLAGS.kernels),
            kernel_features=eval(FLAGS.kernel_features),
            max_doc_length=FLAGS.max_doc_length)

        valid_model.update(
            model.bilstm_doc_enc(valid_model.input_cnn,
                                 batch_size=FLAGS.batch_size,
                                 num_rnn_layers=FLAGS.rnn_layers,
                                 rnn_size=FLAGS.rnn_size,
                                 max_doc_length=FLAGS.max_doc_length,
                                 dropout=0.0))

        valid_model.update(model.label_prediction(valid_model.enc_outputs))
        valid_model.update(
            model.loss_extraction(valid_model.logits, FLAGS.batch_size,
                                  FLAGS.max_doc_length))
    elif FLAGS.model_choice == 'lstm':
        valid_model = model.cnn_sen_enc(
            word_vocab_size=word_vocab.size,
            word_embed_size=FLAGS.word_embed_size,
            batch_size=FLAGS.batch_size,
            num_highway_layers=FLAGS.highway_layers,
            max_sen_length=FLAGS.max_sen_length,
            kernels=eval(FLAGS.kernels),
            kernel_features=eval(FLAGS.kernel_features),
            max_doc_length=FLAGS.max_doc_length)

        valid_model.update(
            model.lstm_doc_enc(valid_model.input_cnn,
                               batch_size=FLAGS.batch_size,
                               num_rnn_layers=FLAGS.rnn_layers,
                               rnn_size=FLAGS.rnn_size,
                               max_doc_length=FLAGS.max_doc_length,
                               dropout=0.0))

        valid_model.update(
            model.lstm_doc_dec(valid_model.input_cnn,
                               valid_model.final_enc_state,
                               batch_size=FLAGS.batch_size,
                               num_rnn_layers=FLAGS.rnn_layers,
                               rnn_size=FLAGS.rnn_size,
                               max_doc_length=FLAGS.max_doc_length,
                               dropout=0.0))

        valid_model.update(
            model.label_prediction_att(valid_model.enc_outputs,
                                       valid_model.dec_outputs))
        valid_model.update(
            model.loss_extraction(valid_model.logits, FLAGS.batch_size,
                                  FLAGS.max_doc_length))

    return valid_model
Esempio n. 2
0
def build_model(word_vocab, max_doc_length, train):
    '''build a training or inference graph, based on the model choice'''

    my_model = None
    if train:
        pretrained_emb = None
        embedding_path = FLAGS.embedding_path
        if FLAGS.load_model is None and embedding_path != None and os.path.exists(
                embedding_path):
            pretrained_emb = load_wordvec(embedding_path, word_vocab)
        if FLAGS.model_choice == 'bilstm':
            my_model = model.cnn_sen_enc(
                word_vocab_size=word_vocab.size,
                word_embed_size=FLAGS.word_embed_size,
                batch_size=FLAGS.batch_size,
                num_highway_layers=FLAGS.highway_layers,
                max_sen_length=FLAGS.max_sen_length,
                kernels=eval(FLAGS.kernels),
                kernel_features=eval(FLAGS.kernel_features),
                max_doc_length=max_doc_length,
                pretrained=pretrained_emb)

            my_model.update(
                model.bilstm_doc_enc(my_model.input_cnn,
                                     batch_size=FLAGS.batch_size,
                                     num_rnn_layers=FLAGS.rnn_layers,
                                     rnn_size=FLAGS.rnn_size,
                                     max_doc_length=max_doc_length,
                                     dropout=FLAGS.dropout))

            my_model.update(model.label_prediction(my_model.enc_outputs))
            my_model.update(
                model.loss_extraction(my_model.logits, FLAGS.batch_size,
                                      max_doc_length))
            my_model.update(
                model.training_graph(my_model.loss * max_doc_length,
                                     FLAGS.learning_rate, FLAGS.max_grad_norm))

        elif FLAGS.model_choice == 'lstm':
            my_model = model.cnn_sen_enc(
                word_vocab_size=word_vocab.size,
                word_embed_size=FLAGS.word_embed_size,
                batch_size=FLAGS.batch_size,
                num_highway_layers=FLAGS.highway_layers,
                max_sen_length=FLAGS.max_sen_length,
                kernels=eval(FLAGS.kernels),
                kernel_features=eval(FLAGS.kernel_features),
                max_doc_length=max_doc_length,
                pretrained=pretrained_emb)

            my_model.update(
                model.lstm_doc_enc(my_model.input_cnn,
                                   batch_size=FLAGS.batch_size,
                                   num_rnn_layers=FLAGS.rnn_layers,
                                   rnn_size=FLAGS.rnn_size,
                                   max_doc_length=max_doc_length,
                                   dropout=FLAGS.dropout))

            my_model.update(
                model.lstm_doc_dec(my_model.input_cnn,
                                   my_model.final_enc_state,
                                   batch_size=FLAGS.batch_size,
                                   num_rnn_layers=FLAGS.rnn_layers,
                                   rnn_size=FLAGS.rnn_size,
                                   max_doc_length=max_doc_length,
                                   dropout=FLAGS.dropout))

            my_model.update(
                model.label_prediction_att(my_model.enc_outputs,
                                           my_model.dec_outputs))
            my_model.update(
                model.loss_extraction(my_model.logits, FLAGS.batch_size,
                                      max_doc_length))

            my_model.update(
                model.training_graph(my_model.loss * max_doc_length,
                                     FLAGS.learning_rate, FLAGS.max_grad_norm))

    else:
        if FLAGS.model_choice == 'bilstm':
            my_model = model.cnn_sen_enc(
                word_vocab_size=word_vocab.size,
                word_embed_size=FLAGS.word_embed_size,
                batch_size=FLAGS.batch_size,
                num_highway_layers=FLAGS.highway_layers,
                max_sen_length=FLAGS.max_sen_length,
                kernels=eval(FLAGS.kernels),
                kernel_features=eval(FLAGS.kernel_features),
                max_doc_length=max_doc_length)

            my_model.update(
                model.bilstm_doc_enc(my_model.input_cnn,
                                     batch_size=FLAGS.batch_size,
                                     num_rnn_layers=FLAGS.rnn_layers,
                                     rnn_size=FLAGS.rnn_size,
                                     max_doc_length=max_doc_length,
                                     dropout=FLAGS.dropout))

            my_model.update(model.label_prediction(my_model.enc_outputs))
            my_model.update(
                model.loss_extraction(my_model.logits, FLAGS.batch_size,
                                      max_doc_length))

        elif FLAGS.model_choice == 'lstm':
            my_model = model.cnn_sen_enc(
                word_vocab_size=word_vocab.size,
                word_embed_size=FLAGS.word_embed_size,
                batch_size=FLAGS.batch_size,
                num_highway_layers=FLAGS.highway_layers,
                max_sen_length=FLAGS.max_sen_length,
                kernels=eval(FLAGS.kernels),
                kernel_features=eval(FLAGS.kernel_features),
                max_doc_length=max_doc_length)

            my_model.update(
                model.lstm_doc_enc(my_model.input_cnn,
                                   batch_size=FLAGS.batch_size,
                                   num_rnn_layers=FLAGS.rnn_layers,
                                   rnn_size=FLAGS.rnn_size,
                                   max_doc_length=max_doc_length,
                                   dropout=FLAGS.dropout))

            my_model.update(
                model.lstm_doc_dec(my_model.input_cnn,
                                   my_model.final_enc_state,
                                   batch_size=FLAGS.batch_size,
                                   num_rnn_layers=FLAGS.rnn_layers,
                                   rnn_size=FLAGS.rnn_size,
                                   max_doc_length=max_doc_length,
                                   dropout=FLAGS.dropout))

            my_model.update(
                model.label_prediction_att(my_model.enc_outputs,
                                           my_model.dec_outputs))
            my_model.update(
                model.loss_extraction(my_model.logits, FLAGS.batch_size,
                                      max_doc_length))
    return my_model