-
Notifications
You must be signed in to change notification settings - Fork 1
/
model_load.py
176 lines (149 loc) · 4.87 KB
/
model_load.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import torch
import torch.nn as nn
from pytorch_pretrained import BertModel, BertTokenizer
from torch.utils.data import Dataset, DataLoader
import pandas as pd
import numpy as np
import models.bert as model_bert
import transformers
from transformers import BertTokenizerFast
# def create_data_loader(df, tokenizer, max_len, batch_size):
pred_data = pd.read_csv("./predict_data/comments.csv")
pred_data = pred_data.dropna()
class Model(nn.Module):
def __init__(self):
super(Model, self).__init__()
self.bert = BertModel.from_pretrained('./bert_pretrain')
for param in self.bert.parameters():
param.requires_grad = True
self.fc = nn.Linear(768, 2)
self.num_epochs = 3
self.batch_size = 128
self.pad_size = 32
self.learning_rate = 5e-5
self.tokenizer = BertTokenizer.from_pretrained('./bert_pretrain')
def forward(self, x):
context = x[0] # 输入的句子
mask = x[2] # 对padding部分进行mask,和句子一个size,padding部分用0表示,如:[1, 1, 1, 1, 0, 0]
_, pooled = self.bert(context, attention_mask=mask, output_all_encoded_layers=False)
# _, pooled = self.bert(input_ids, attention_mask=mask, output_all_encoded_layers=False)
out = self.fc(pooled)
return self.out(out)
bertmodel = Model()
bertmodel = torch.load('model.pth')
bertmodel.eval()
# for param in model.parameters():
# print(param)
# one single sentence test
tokenizer = BertTokenizerFast.from_pretrained('./bert_pretrain')
sample_txt = "你是个傻逼吗?"
# tokens = tokenizer.tokenize(sample_txt)
# token_ids = tokenizer.convert_tokens_to_ids(tokens)
# print(f' Sentence: {sample_txt}')
# print(f' Tokens: {tokens}')
# print(f'Token IDs: {token_ids}')
encoding = tokenizer.encode_plus(
sample_txt,
max_length = 32,
add_special_tokens = True,
return_token_type_ids = False,
pad_to_max_length = True,
return_attention_mask = True,
return_tensors = 'pt',
)
encoding.keys()
# dict_keys(['input_ids', 'attention_mask'])
# print(encoding['input_ids'][0])
# print(tokenizer.convert_ids_to_tokens(encoding['input_ids'][0]))
# token_lens = []
# for txt in pred_data.text:
# tokens = tokenizer.encode(txt, max_length=512)
# token_lens.append(len(tokens))
# sns.distplot(token_lens)
# plt.xlim([0, 256]);
# plt.xlabel('Token count');
class Collect_Dataset(Dataset):
def __init__(self, texts, tokenizer, max_len=32):
self.texts = texts
# self.targets = targets
self.tokenizer = tokenizer
self.max_len = max_len
def __len__(self):
return len(self.texts)
def __getitem__(self, item):
text = str(self.texts[item])
# target = self.targets[item]
encoding = self.tokenizer.encode_plus(
text,
add_special_tokens=True,
max_length=self.max_len,
return_token_type_ids=False,
pad_to_max_length=True,
return_attention_mask=True,
return_tensors='pt',
)
return {
'text': text,
'input_ids': encoding['input_ids'].flatten(),
'attention_mask': encoding['attention_mask'].flatten()
# 'targets': torch.tensor(target, dtype=torch.long)
}
def data_loader(df, tokenizer, max_len, batch_size):
dataset = Collect_Dataset(
texts=df.to_numpy(),
tokenizer=tokenizer,
max_len=max_len
)
return DataLoader(
dataset,
batch_size=batch_size
)
BATCH_SIZE = 128
pred_data_loader = data_loader(pred_data, tokenizer, 32, BATCH_SIZE)
# data = next(iter(pred_data_loader))
# print(data['input_ids'].shape)
# print(data['attention_mask'].shape)
# max_seq_len = 32
# tokenize and encode sequences in the predict set
# tokens_pred = tokenizer.batch_encode_plus(
# data.tolist(),
# max_length = max_seq_len,
# padding='max_length',
# truncation=True,
# return_token_type_ids=False
# )
# print(tokens_pred)
# convert integer sequences to tensors
# pred_seq = torch.tensor(tokens_pred['input_ids'])
# pred_mask = torch.tensor(tokens_pred['attention_mask'])
def get_predictions(model, data_loader):
model = model.eval()
texts = []
predictions = []
prediction_probs = []
real_values = []
with torch.no_grad():
for d in data_loader:
texts = d["text"]
input_ids = d["input_ids"].to("cpu")
attention_mask = d["attention_mask"].to("cpu")
# targets = d["targets"].to("cpu")
outputs = model(
d
# attention_mask=attention_mask
)
_, preds = torch.max(outputs, dim=1)
texts.extend(texts)
predictions.extend(preds)
prediction_probs.extend(outputs)
# real_values.extend(targets)
predictions = torch.stack(predictions).cpu()
prediction_probs = torch.stack(prediction_probs).cpu()
real_values = torch.stack(real_values).cpu()
return texts, predictions, prediction_probs, real_values
y_review_texts, y_pred, y_pred_probs, y_test = get_predictions(
bertmodel,
pred_data_loader
)
print(y_pred)
# print(classification_report(y_test, y_pred, target_names=class_names))