Ejemplo n.º 1
0
import json
import sys

from model import ModelWrapper


model_wrapper = ModelWrapper()
model_wrapper.load_model()

data = json.loads(sys.argv[1])
print(f"Data: {data}")

prediction = model_wrapper.predict(data=data)
print(f"Prediction: {prediction}")
Ejemplo n.º 2
0
    for i, batch in enumerate(train_batch):
        start_time = time.time()
        global_step += 1
        loss = model.update(batch)
        train_loss += loss
        if global_step % opt['log_step'] == 0:
            duration = time.time() - start_time
            print(format_str.format(datetime.now(), global_step, max_steps, epoch, \
                                    opt['num_epoch'], loss, duration, current_lr))

    print("Evaluating on dev set ...")
    predictions = []
    probabilities = []
    dev_loss = 0
    for i, batch in enumerate(dev_batch):
        preds, probs, loss = model.predict(batch, thres=0.01)
        predictions += preds
        probabilities += probs
        dev_loss += loss

    train_loss = train_loss / train_batch.num_examples * opt[
        'batch_size']  # avg loss per batch
    dev_loss = dev_loss / dev_batch.num_examples * opt['batch_size']
    model.scheduler.step(dev_loss)

    # _, _, _, dev_f1 = metrics(dev_batch.gold(), predictions)
    _, _, _, dev_f1, _ = tune_thres_new(dev_batch.gold(), probabilities)
    print("epoch {}: train_loss = {:.6f}, dev_loss = {:.6f}, dev_f1 = {:.4f}".format(epoch,\
            train_loss, dev_loss, dev_f1))

    model_file = os.path.join(opt['model_save_dir'],
Ejemplo n.º 3
0
# train_batch = DataLoader(os.path.join(opt['data_dir'], 'train.csv'),
#                    opt['batch_size'],
#                    opt,
#                    weibo2embid=weibo2embid,
#                    evaluation=False)
dev_batch = DataLoader(os.path.join(opt['data_dir'], 'dev.csv'),
                   opt['batch_size'],
                   opt,
                   weibo2embid=weibo2embid,
                   evaluation=True)

model = ModelWrapper(opt, weibo2embid, eva=True)
model.load(os.path.join(opt['model_save_dir'], 'best_model.pt'))

all_probs = []
all_preds = []
for i, b in enumerate(dev_batch):
    preds, probs, _ = model.predict(b, thres=0.5)
    all_probs += probs
    all_preds += preds

acc, prec, rec, dev_f1 = metrics(dev_batch.gold(), all_preds)
print('acc: {}, prec: {}, rec: {}, f1: {}\n'.format(acc, prec, rec, dev_f1))

auc, prec, rec, f1, best_thres = tune_thres_new(dev_batch.gold(), all_probs)
print('auc: {}, prec: {}, rec: {}, f1: {}, best_thres: {}'.format(auc, prec, rec, f1, best_thres))

with open('./log.txt', 'a+') as fout:
    fout.write('\n' + time.asctime(time.localtime(time.time())))
    fout.write(' '.join(sys.argv))
    fout.write('\nauc: {}, prec: {}, rec: {}, f1: {}, best_thres: {}\n'.format(auc, prec, rec, f1, best_thres))