Пример #1
0
def main():
    timer = Timer()
    timer.start('Load word2vec models...')
    vocab = load_vocab(config.VOCAB_DATA)
    embeddings = get_trimmed_w2v_vectors(config.W2V_DATA)
    timer.stop()

    timer.start('Load data...')
    train = process_data(opt.train, vocab)
    if opt.val is not None:
        if opt.val != '1vs9':
            validation = process_data(opt.val, vocab)
        else:
            validation, train = train.one_vs_nine()
    else:
        validation = None

    if opt.test is not None:
        test = process_data(opt.test, vocab)
    else:
        test = None
    timer.stop()

    timer.start('Build model...')
    model = CnnModel(embeddings=embeddings)
    model.build()
    timer.stop()

    timer.start('Train model...')
    epochs = opt.e
    batch_size = opt.b
    early_stopping = True if opt.p != 0 else False
    patience = opt.p
    pre_train = opt.pre if opt.pre != '' else None
    model_name = opt.name

    model.train(
        model_name,
        train=train,
        validation=validation,
        epochs=epochs,
        batch_size=batch_size,
        early_stopping=early_stopping,
        patience=patience,
        cont=pre_train,
    )
    timer.stop()

    if test is not None:
        timer.start('Test model...')
        preds = model.predict(test, model_name)
        labels = test.labels

        p, r, f1, _ = precision_recall_fscore_support(labels,
                                                      preds,
                                                      average='binary')
        print('Testing result:P=\t{}\tR={}\tF1={}'.format(p, r, f1))
        timer.stop()
Пример #2
0
def main():
    timer = Timer()
    timer.start('Load word2vec models...')
    vocab = load_vocab(config.VOCAB_DATA)
    embeddings = get_trimmed_w2v_vectors(config.W2V_DATA)
    timer.stop()

    timer.start('Build model...')
    model = CnnModel(embeddings=embeddings)
    model.build()
    model.restore_session(opt.pre)
    timer.stop()

    # Define app
    app = Flask(__name__)
    CORS(app)

    @app.route('/process', methods=['POST'])
    def process():
        data = request.get_json()
        try:
            words = [[
                vocab[w] if w in vocab else vocab['$UNK$']
                for w in parse_raw_data(s)
            ] for s in data['input']]
            test = Dataset(words=words)
        except:
            test = None
            abort(400)

        job_id = timer.start('Process {} example'.format(len(data['input'])))
        y_pred = model.predict(test, opt.pre, pred_class=False)

        ret = {'output': [i.tolist() for i in y_pred]}
        timer.stop(job_id)

        return jsonify(ret)

    app.run()