-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclassier_test_input.py
134 lines (104 loc) · 5.34 KB
/
classier_test_input.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import argparse
import os
import numpy as np
from pytorch_pretrained_bert.modeling import BertConfig, WEIGHTS_NAME, CONFIG_NAME, BertModel, BertPreTrainedModel
from pytorch_pretrained_bert.tokenization import BertTokenizer
import torch
from torch import nn
from torch.nn import CrossEntropyLoss
import torch.nn.functional as F
def get_device(gpu_id):
device = torch.device("cuda:" + str(gpu_id) if torch.cuda.is_available() else "cpu")
n_gpu = torch.cuda.device_count()
if torch.cuda.is_available():
print("device is cuda, # cuda is: ", n_gpu)
else:
print("device is cpu, not recommend")
return device, n_gpu
def get_args():
parser = argparse.ArgumentParser(description='BERT Baseline')
parser.add_argument("--model_name", default="BertLSTM", type=str, help="the name of model")
parser.add_argument("--output_dir", default=".flickrstyle_3label_output/BertLSTM/", type=str)
parser.add_argument("--bert_vocab_file", default="/home/liwc/wxp/refercode/DataTestProcess/bert-base-uncased/vocab.txt", type=str)
parser.add_argument("--bert_model_dir", default="/home/liwc/wxp/refercode/DataTestProcess/bert-base-uncased", type=str)
parser.add_argument("--do_lower_case", default=True, type=bool, help="Set this flag if you are using an uncased model.")
parser.add_argument("--max_seq_length", default=21, type=int)
parser.add_argument("--hidden_size", default=300, type=int, help="隐层特征维度")
parser.add_argument('--num_layers', default=2, type=int, help='RNN层数')
parser.add_argument("--bidirectional", default=True, type=bool)
parser.add_argument("--dropout", default=0.2, type=float)
parser.add_argument("--gpu_ids", type=str, default="0", help="gpu 的设备id")
parser.add_argument("--save_name", default="BertLSTM", type=str, help="the name file of model")
parser.add_argument("--label_list", default=['0', '1'])
config = parser.parse_args()
return config
class BertLSTM(BertPreTrainedModel):
def __init__(self, config, num_labels, rnn_hidden_size, num_layers, bidirectional, dropout):
super(BertLSTM, self).__init__(config)
self.num_labels = num_labels
self.bert = BertModel(config)
self.dropout = nn.Dropout(config.hidden_dropout_prob)
self.rnn = nn.LSTM(config.hidden_size, rnn_hidden_size, num_layers,bidirectional=bidirectional, batch_first=True, dropout=dropout)
self.classifier = nn.Linear(rnn_hidden_size * 2, num_labels)
self.apply(self.init_bert_weights)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None):
encoded_layers, _ = self.bert(
input_ids, token_type_ids, attention_mask, output_all_encoded_layers=False)
encoded_layers = self.dropout(encoded_layers)
# encoded_layers: [batch_size, seq_len, bert_dim]
_, (hidden, cell) = self.rnn(encoded_layers)
# outputs: [batch_size, seq_len, rnn_hidden_size * 2]
hidden = self.dropout(
torch.cat((hidden[-2, :, :], hidden[-1, :, :]), dim=1)) # 连接最后一层的双向输出
logits = self.classifier(hidden)
if labels is not None:
loss_fct = CrossEntropyLoss()
loss = loss_fct(logits.view(-1, self.num_labels), labels.view(-1))
return loss
else:
return logits
def sentence_style_cls(sentence, tokenizer, max_seq_length, model, device):
token = tokenizer.tokenize(sentence)
if len(token) > max_seq_length - 2:
token = token[:(max_seq_length - 2)]
token = ["[CLS]"] + token + ["[SEP]"]
segment_ids = [0] * len(token)
input_ids = tokenizer.convert_tokens_to_ids(token)
input_mask = [1] * len(input_ids)
padding = [0] * (max_seq_length - len(input_ids))
input_ids += padding
input_mask += padding
segment_ids += padding
input_ids = torch.tensor(input_ids).unsqueeze(0)
input_mask = torch.tensor(input_mask).unsqueeze(0)
segment_ids = torch.tensor(segment_ids).unsqueeze(0)
input_ids = input_ids.to(device)
input_mask = input_mask.to(device)
segment_ids = segment_ids.to(device)
with torch.no_grad():
logits = model(input_ids, segment_ids, input_mask, labels=None)
preds = logits.detach().cpu().numpy()
outputs = np.argmax(preds, axis=1)
return outputs[0]
if __name__ == "__main__":
config = get_args()
output_config_file = os.path.join(config.output_dir, config.save_name, CONFIG_NAME)
bert_config = BertConfig(output_config_file)
# label_list = ['0', '1']
num_labels = len(config.label_list)
model = BertLSTM(bert_config, num_labels, config.hidden_size, config.num_layers, config.bidirectional, config.dropout)
output_model_file = os.path.join(config.output_dir, config.save_name, WEIGHTS_NAME)
model.load_state_dict(torch.load(output_model_file))
gpu_ids = [int(device_id) for device_id in config.gpu_ids.split()]
device, n_gpu = get_device(gpu_ids[0])
if n_gpu > 1:
n_gpu = len(gpu_ids)
model.to(device)
model.eval()
tokenizer = BertTokenizer.from_pretrained(
config.bert_vocab_file, do_lower_case=config.do_lower_case)
label_map = {label: i for i, label in enumerate(config.label_list)}
while(1):
sentence = input()
outputs = sentence_style_cls(sentence, tokenizer, config.max_seq_length, model, device)
print(outputs)