-
Notifications
You must be signed in to change notification settings - Fork 1
/
predict.py
48 lines (41 loc) · 1.63 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# coding: UTF-8
import time
import torch
import numpy as np
from train_eval import train, init_network, predict
from importlib import import_module
import argparse
from utils import build_dataset, build_iterator, get_time_dif
import pandas as pd
parser = argparse.ArgumentParser(description='Chinese Text Classification')
parser.add_argument('--model', type=str, required=True, help='choose a model: Bert, ERNIE')
args = parser.parse_args()
if __name__ == '__main__':
dataset = 'THUCNews' # 数据集
model_name = args.model # bert
x = import_module('models.' + model_name)
config = x.Config(dataset)
np.random.seed(1)
torch.manual_seed(1)
torch.cuda.manual_seed_all(1)
torch.backends.cudnn.deterministic = True # 保证每次结果一样
start_time = time.time()
print("Loading data...")
train_data, dev_data, test_data, pred_data = build_dataset(config)
train_iter = build_iterator(train_data, config)
dev_iter = build_iterator(dev_data, config)
test_iter = build_iterator(test_data, config)
pred_iter = build_iterator(pred_data, config)
time_dif = get_time_dif(start_time)
print("Time usage:", time_dif)
# train
model = x.Model(config).to(config.device)
model = torch.load('model.pth')
# train(config, model, train_iter, dev_iter, test_iter)
prediction = predict(config, model, pred_iter)
predict_df = pd.read_csv("./THUCNews/data/predict.txt", sep="\t", names=['text', 'label'])
predict_df['label'] = prediction.tolist()
predict_df.to_csv("output.csv", index=False, sep="\t")
# save the model
# FILE = "model.pth"
# torch.save(model, FILE)