Upload 19 files
Browse files- .gitattributes +1 -0
- CheckGPT/dnn.py +298 -0
- CheckGPT/exp/0001/config.ini +21 -0
- CheckGPT/features.py +78 -0
- CheckGPT/model.py +76 -0
- CheckGPT/test.py +56 -0
- Pretrained/CS_Task1.pth +3 -0
- Pretrained/CS_Task2.pth +3 -0
- Pretrained/CS_Task3.pth +3 -0
- Pretrained/HSS_Task1.pth +3 -0
- Pretrained/HSS_Task2pth +3 -0
- Pretrained/HSS_Task3.pth +3 -0
- Pretrained/PHY_Task1.pth +3 -0
- Pretrained/PHY_Task2.pth +3 -0
- Pretrained/PHY_Task3.pth +3 -0
- Pretrained/Unified_Task1.pth +3 -0
- Pretrained/Unified_Task123.pth +3 -0
- Pretrained/Unified_Task2.pth +3 -0
- Pretrained/Unified_Task3.pth +3 -0
- requirements.txt +4 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
Pretrained/HSS_Task2pth filter=lfs diff=lfs merge=lfs -text
|
CheckGPT/dnn.py
ADDED
@@ -0,0 +1,298 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import copy
|
3 |
+
import h5py
|
4 |
+
import time
|
5 |
+
import argparse
|
6 |
+
|
7 |
+
import torch
|
8 |
+
import torch.nn as nn
|
9 |
+
import torch.optim as optim
|
10 |
+
import torch.utils.data as data
|
11 |
+
from torch.utils.data import DataLoader
|
12 |
+
from configparser import ConfigParser
|
13 |
+
|
14 |
+
from model import AttenLSTM
|
15 |
+
|
16 |
+
parser = argparse.ArgumentParser(description='Demo of argparse')
|
17 |
+
parser.add_argument('domain', type=str)
|
18 |
+
parser.add_argument('task', type=int)
|
19 |
+
parser.add_argument('expid', type=str)
|
20 |
+
parser.add_argument('--early', type=float, default=99.8)
|
21 |
+
parser.add_argument('--save', type=int, default=1)
|
22 |
+
parser.add_argument('--pretrain', type=int, default=0)
|
23 |
+
parser.add_argument('--dataamount', type=int, default=1000)
|
24 |
+
parser.add_argument('--trans', type=int, default=0)
|
25 |
+
parser.add_argument('--splitr', type=float, default=0.8)
|
26 |
+
parser.add_argument('--test', type=int, default=0)
|
27 |
+
parser.add_argument('--mdomain', type=str, default="CS")
|
28 |
+
parser.add_argument('--mtask', type=int, default=1)
|
29 |
+
parser.add_argument('--printall', type=int, default=1)
|
30 |
+
parser.add_argument('--cont', type=int, default=0)
|
31 |
+
parser.add_argument('--adam', type=int, default=1)
|
32 |
+
parser.add_argument('--seed', type=int, default=100)
|
33 |
+
parser.add_argument('--dropout', type=float, default=0.5)
|
34 |
+
parser.add_argument('--batchsize', type=int, default=320)
|
35 |
+
parser.add_argument('--meta', type=int, default=0)
|
36 |
+
args = parser.parse_args()
|
37 |
+
|
38 |
+
LOG_INTERVAL = 50
|
39 |
+
ID = args.expid
|
40 |
+
|
41 |
+
if not os.path.exists("./exp/{}".format(ID)):
|
42 |
+
os.mkdir("./exp/{}".format(ID))
|
43 |
+
|
44 |
+
SEED = args.seed
|
45 |
+
DEVICE_NAME = "cuda" if torch.cuda.is_available() else "cpu"
|
46 |
+
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
47 |
+
PRETRAINED = bool(args.pretrain)
|
48 |
+
TRANSFER = bool(args.trans)
|
49 |
+
SGD_OR_ADAM = "adam" if args.adam else "sgd"
|
50 |
+
LEARNING_RATE = 2e-4 if SGD_OR_ADAM == "adam" else 1e-3
|
51 |
+
N_EPOCH = 200 if (TRANSFER or SGD_OR_ADAM == "sgd") else 100
|
52 |
+
CONTINUE = args.cont
|
53 |
+
BATCH_SIZE = args.batchsize
|
54 |
+
EARLYSTOP = args.early
|
55 |
+
SAVE = bool(args.save)
|
56 |
+
TEST = bool(args.test)
|
57 |
+
PRINT_ALL = bool(args.printall)
|
58 |
+
TEST_SIZE = 512 if TEST else BATCH_SIZE
|
59 |
+
# torch.backends.cudnn.deterministic = False
|
60 |
+
# torch.backends.cudnn.benchmark = True
|
61 |
+
|
62 |
+
domain, task = args.domain, str(args.task)
|
63 |
+
m_domain, m_task = args.mdomain, str(args.mtask)
|
64 |
+
|
65 |
+
if TRANSFER:
|
66 |
+
assert m_domain is not None
|
67 |
+
|
68 |
+
|
69 |
+
class MyDataset(data.Dataset):
|
70 |
+
def __init__(self, archive):
|
71 |
+
self.archive = archive
|
72 |
+
self.length = len(h5py.File(self.archive, 'r')["data"])
|
73 |
+
self.dataset, self.labels = None, None
|
74 |
+
|
75 |
+
def __getitem__(self, index):
|
76 |
+
if self.dataset is None:
|
77 |
+
self.dataset = h5py.File(self.archive, 'r')["data"]
|
78 |
+
self.labels = h5py.File(self.archive, 'r')["label"]
|
79 |
+
x, y = torch.from_numpy(self.dataset[index]).float(), torch.from_numpy(self.labels[index]).long()
|
80 |
+
return x, y
|
81 |
+
|
82 |
+
def __len__(self):
|
83 |
+
return self.length
|
84 |
+
|
85 |
+
|
86 |
+
class TransNet(nn.Module):
|
87 |
+
def __init__(self, host, mode=0):
|
88 |
+
super(TransNet, self).__init__()
|
89 |
+
self.host = host
|
90 |
+
self.mode = mode
|
91 |
+
self.dropout = nn.Dropout(p=0.05)
|
92 |
+
self.layers = copy.deepcopy(self.host.fc)
|
93 |
+
|
94 |
+
def forward(self, x):
|
95 |
+
x = self.layers(self.dropout(x))
|
96 |
+
return x
|
97 |
+
|
98 |
+
|
99 |
+
def save_checkpoint(model, path, optimizer, scheduler, epoch, acc):
|
100 |
+
info_dict = {
|
101 |
+
"model_state_dict": model.state_dict(),
|
102 |
+
"optimizer_state_dict": optimizer.state_dict(),
|
103 |
+
"lr_scheduler_state_dict": scheduler.state_dict(),
|
104 |
+
"last_epoch": epoch,
|
105 |
+
"best_acc": acc
|
106 |
+
}
|
107 |
+
|
108 |
+
torch.save(info_dict, path)
|
109 |
+
return
|
110 |
+
|
111 |
+
|
112 |
+
def load_checkpoint(model, path, optimizer, scheduler):
|
113 |
+
cp = torch.load(path)
|
114 |
+
model.load_state_dict(cp["model_state_dict"], strict=True)
|
115 |
+
optimizer.load_state_dict(cp["optimizer_state_dict"])
|
116 |
+
scheduler.load_state_dict(cp["lr_scheduler_state_dict"])
|
117 |
+
last_epoch = cp["last_epoch"]
|
118 |
+
best_acc = cp["best_acc"]
|
119 |
+
return last_epoch, best_acc
|
120 |
+
|
121 |
+
|
122 |
+
def load_data(domain, task, size_train=96, size_test=96):
|
123 |
+
if not TRANSFER:
|
124 |
+
dir_ = './embeddings/{}_TASK{}_{}.{}'
|
125 |
+
full_dataset = MyDataset(dir_.format(domain, task, args.dataamount, "h5"))
|
126 |
+
torch.random.manual_seed(args.seed)
|
127 |
+
train_size = int(args.splitr * len(full_dataset))
|
128 |
+
test_size = len(full_dataset) - train_size
|
129 |
+
train_data, test_data = torch.utils.data.random_split(full_dataset, [train_size, test_size])
|
130 |
+
else:
|
131 |
+
torch.random.manual_seed(SEED)
|
132 |
+
full_data = MyDataset('./embeddings/s{}{}_t{}{}.h5'.format(m_domain, args.mtask,
|
133 |
+
domain, task))
|
134 |
+
train_size = args.dataamount
|
135 |
+
test_size = len(full_data) - train_size
|
136 |
+
|
137 |
+
train_data, _ = torch.utils.data.random_split(full_data, [train_size, test_size])
|
138 |
+
test_data = MyDataset('./embeddings/s{}{}_t{}{}.h5'.format(m_domain, args.mtask,
|
139 |
+
domain, task))
|
140 |
+
|
141 |
+
train_loader = DataLoader(dataset=train_data,
|
142 |
+
num_workers=4,
|
143 |
+
batch_size=size_train,
|
144 |
+
drop_last=True,
|
145 |
+
shuffle=True,
|
146 |
+
pin_memory=True,
|
147 |
+
prefetch_factor=2)
|
148 |
+
test_loader = DataLoader(dataset=test_data,
|
149 |
+
num_workers=4,
|
150 |
+
batch_size=size_test,
|
151 |
+
drop_last=False,
|
152 |
+
shuffle=False,
|
153 |
+
pin_memory=True,
|
154 |
+
prefetch_factor=2)
|
155 |
+
return train_loader, test_loader
|
156 |
+
|
157 |
+
|
158 |
+
def test(model, dataloader, epoch, print_freq=1):
|
159 |
+
model.eval()
|
160 |
+
n_correct, correct_0, correct_1, sum_0, sum_1 = 0, 0, 0, 0, 0
|
161 |
+
start = time.time()
|
162 |
+
|
163 |
+
with torch.no_grad():
|
164 |
+
for i, (t_img, t_label) in enumerate(dataloader):
|
165 |
+
t_img, t_label = t_img.to(DEVICE), t_label.to(DEVICE).squeeze(1)
|
166 |
+
class_output = model(t_img)
|
167 |
+
pred = torch.max(class_output.data, 1)
|
168 |
+
n_correct += (pred[1] == t_label).sum().item()
|
169 |
+
correct_0 += ((pred[1] == t_label) * (t_label == 0)).sum().item()
|
170 |
+
correct_1 += ((pred[1] == t_label) * (t_label == 1)).sum().item()
|
171 |
+
sum_0 += (t_label == 0).sum().item()
|
172 |
+
sum_1 += (t_label == 1).sum().item()
|
173 |
+
if i % (LOG_INTERVAL*print_freq) == 0 and PRINT_ALL:
|
174 |
+
print('Batch: [{}/{}], Time used: {:.4f}s'.format(i, len(dataloader), time.time() - start))
|
175 |
+
|
176 |
+
accu = float(n_correct) / len(dataloader.dataset) * 100
|
177 |
+
accu_0 = float(correct_0) / sum_0 * 100
|
178 |
+
accu_1 = float(correct_1) / sum_1 * 100
|
179 |
+
if PRINT_ALL:
|
180 |
+
print('{}{}, Epoch:{}, Test accuracy: {:.4f}%, Acc_GPT: {:.4f}%, Acc_Human: {:.4f}%'.format(args.task,
|
181 |
+
args.domain, epoch,
|
182 |
+
accu, accu_0, accu_1))
|
183 |
+
return accu, accu_0, accu_1
|
184 |
+
|
185 |
+
|
186 |
+
def train(model, optimizer, scheduler, dataloader, test_loader):
|
187 |
+
loss_class = torch.nn.CrossEntropyLoss()
|
188 |
+
scaler = torch.cuda.amp.GradScaler(enabled=True)
|
189 |
+
|
190 |
+
if CONTINUE:
|
191 |
+
last_epoch, best_acc = load_checkpoint(model, "./exp/{}_Task{}.pth".format(domain, task),
|
192 |
+
optimizer, scheduler)
|
193 |
+
best_acc = test(model, test_loader, -1)
|
194 |
+
print("Checkpoint Loaded.")
|
195 |
+
else:
|
196 |
+
last_epoch, best_acc = 1, 0
|
197 |
+
|
198 |
+
len_dataloader = len(dataloader)
|
199 |
+
for epoch in range(last_epoch, N_EPOCH + 1):
|
200 |
+
start = time.time()
|
201 |
+
model.train()
|
202 |
+
data_iter = iter(dataloader)
|
203 |
+
n_correct = 0
|
204 |
+
|
205 |
+
i = 1
|
206 |
+
while i < len_dataloader + 1:
|
207 |
+
data_source = next(data_iter)
|
208 |
+
optimizer.zero_grad()
|
209 |
+
|
210 |
+
img, label = data_source[0].to(DEVICE), data_source[1].to(DEVICE).squeeze(1)
|
211 |
+
|
212 |
+
class_output = model(img)
|
213 |
+
pred = torch.max(class_output.data, 1)
|
214 |
+
n_correct += (pred[1] == label).sum().item()
|
215 |
+
err = loss_class(class_output, label)
|
216 |
+
scaler.scale(err).backward()
|
217 |
+
scaler.step(optimizer)
|
218 |
+
scaler.update()
|
219 |
+
|
220 |
+
if i % LOG_INTERVAL == 0 and PRINT_ALL:
|
221 |
+
print(
|
222 |
+
'Epoch: [{}/{}], Batch: [{}/{}], Err: {:.4f}, Time used: {:.4f}s'.format(
|
223 |
+
epoch, N_EPOCH, i, len_dataloader, err.item(), time.time() - start,
|
224 |
+
))
|
225 |
+
i += 1
|
226 |
+
# torch.cuda.empty_cache()
|
227 |
+
|
228 |
+
accu = float(n_correct) / (len(dataloader.dataset)) * 100
|
229 |
+
if PRINT_ALL:
|
230 |
+
print('{}_TASK{}, Epoch:{}, Train accuracy: {:.4f}%'.format(domain, task, epoch, accu))
|
231 |
+
if not TRANSFER and SAVE:
|
232 |
+
save_checkpoint(model, "./exp/{}/Checkpoint_{}_Task{}.pth".format(ID, domain, task),
|
233 |
+
optimizer, scheduler, epoch, best_acc)
|
234 |
+
acc = test(model, test_loader, epoch)
|
235 |
+
|
236 |
+
scheduler.step()
|
237 |
+
|
238 |
+
if acc > best_acc:
|
239 |
+
old_acc, best_acc = best_acc, acc
|
240 |
+
if TRANSFER:
|
241 |
+
name = "./exp/{}/Best_s_{}{}_t_{}{}.pth".format(ID, m_domain, m_task, domain, task)
|
242 |
+
else:
|
243 |
+
name = "./exp/{}/Best_{}_Task{}.pth".format(ID, domain, task)
|
244 |
+
if SAVE:
|
245 |
+
torch.save(model.state_dict(), name)
|
246 |
+
if not TRANSFER:
|
247 |
+
print("Best model saved.")
|
248 |
+
|
249 |
+
if EARLYSTOP > 0 and best_acc > EARLYSTOP:
|
250 |
+
break
|
251 |
+
|
252 |
+
return best_acc
|
253 |
+
|
254 |
+
|
255 |
+
if __name__ == '__main__':
|
256 |
+
torch.random.manual_seed(SEED)
|
257 |
+
train_loader, test_loader = load_data(domain, task, size_train=BATCH_SIZE, size_test=TEST_SIZE)
|
258 |
+
rnn = AttenLSTM(input_size=1024, hidden_size=256, batch_first=True, dropout=args.dropout, bidirectional=True, num_layers=2, device=DEVICE_NAME).to(DEVICE)
|
259 |
+
|
260 |
+
config = ConfigParser()
|
261 |
+
|
262 |
+
config.read('./exp/{}/config.ini'.format(ID))
|
263 |
+
config.add_section('main')
|
264 |
+
for key, value in vars(args).items():
|
265 |
+
config.set('main', key, str(value))
|
266 |
+
|
267 |
+
with open('./exp/{}/config.ini'.format(ID), 'w') as f:
|
268 |
+
config.write(f)
|
269 |
+
|
270 |
+
if PRETRAINED or TEST:
|
271 |
+
rnn.load_state_dict(torch.load("../Pretrained/{}_Task{}.pth".format(domain, task)), strict=True)
|
272 |
+
if args.meta:
|
273 |
+
rnn.load_state_dict(torch.load("../Pretrained/Unified_Task123.pth"), strict=True)
|
274 |
+
|
275 |
+
if TRANSFER:
|
276 |
+
rnn.load_state_dict(torch.load("../Pretrained/{}_Task{}.pth".format(m_domain, m_task)), strict=True)
|
277 |
+
model = TransNet(rnn)
|
278 |
+
del rnn
|
279 |
+
else:
|
280 |
+
model = rnn
|
281 |
+
|
282 |
+
if not TEST:
|
283 |
+
for param in model.parameters():
|
284 |
+
param.requires_grad = True
|
285 |
+
|
286 |
+
if SGD_OR_ADAM == "adam":
|
287 |
+
optimizer = optim.AdamW(model.parameters(), lr=LEARNING_RATE)
|
288 |
+
else:
|
289 |
+
optimizer = optim.SGD(model.parameters(), lr=LEARNING_RATE, momentum=0.9, weight_decay=5e-4)
|
290 |
+
scheduler = optim.lr_scheduler.CosineAnnealingLR(optimizer, N_EPOCH)
|
291 |
+
|
292 |
+
if not TEST:
|
293 |
+
best_acc = train(model, optimizer, scheduler, train_loader, test_loader)
|
294 |
+
else:
|
295 |
+
best_acc, _, _ = test(model, test_loader, 0)
|
296 |
+
|
297 |
+
if TRANSFER:
|
298 |
+
print("Transfer Learning, S: {}, T: {}, Acc: {:.4f}%".format(m_domain+m_task, domain+task, best_acc))
|
CheckGPT/exp/0001/config.ini
ADDED
@@ -0,0 +1,21 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
[main]
|
2 |
+
domain = CS
|
3 |
+
task = 3
|
4 |
+
expid = 0001
|
5 |
+
early = 99.8
|
6 |
+
save = 1
|
7 |
+
pretrain = 0
|
8 |
+
dataamount = 1000
|
9 |
+
trans = 0
|
10 |
+
splitr = 0.8
|
11 |
+
test = 1
|
12 |
+
mdomain = CS
|
13 |
+
mtask = 1
|
14 |
+
printall = 1
|
15 |
+
cont = 0
|
16 |
+
adam = 1
|
17 |
+
seed = 100
|
18 |
+
dropout = 0.5
|
19 |
+
batchsize = 320
|
20 |
+
meta = 0
|
21 |
+
|
CheckGPT/features.py
ADDED
@@ -0,0 +1,78 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import json
|
3 |
+
import h5py
|
4 |
+
import time
|
5 |
+
import argparse
|
6 |
+
import torch.nn.functional as F
|
7 |
+
|
8 |
+
|
9 |
+
parser = argparse.ArgumentParser(description='Demo of argparse')
|
10 |
+
parser.add_argument('domain', type=str)
|
11 |
+
parser.add_argument('task', type=str)
|
12 |
+
parser.add_argument('--pairs', type=int, default=0)
|
13 |
+
args = parser.parse_args()
|
14 |
+
|
15 |
+
domain = args.domain
|
16 |
+
task = args.task
|
17 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
18 |
+
roberta = torch.hub.load('pytorch/fairseq', 'roberta.large').to(device)
|
19 |
+
roberta.eval()
|
20 |
+
|
21 |
+
with open("../GPABenchmark/{}_TASK{}/gpt.json".format(domain, task), 'r') as f1:
|
22 |
+
data1 = json.load(f1)
|
23 |
+
f1.close()
|
24 |
+
|
25 |
+
with open("../GPABenchmark/{}_TASK{}/hum.json".format(domain, task), 'r') as f2:
|
26 |
+
data2 = json.load(f2)
|
27 |
+
f2.close()
|
28 |
+
|
29 |
+
print(len(list(data1.keys())))
|
30 |
+
print(len(list(data2.keys())))
|
31 |
+
too_long = 0
|
32 |
+
total_length = 2 * args.pairs if args.pairs else len(list(data1.keys())) + len(list(data2.keys()))
|
33 |
+
|
34 |
+
start = time.time()
|
35 |
+
data = h5py.File('./embeddings/{}_TASK{}_{}.h5'.format(domain, task, total_length), 'w')
|
36 |
+
data.create_dataset('data', (total_length, 512, 1024), dtype='f4')
|
37 |
+
data.create_dataset('label', (total_length, 1), dtype='i')
|
38 |
+
|
39 |
+
|
40 |
+
def fetch_representation(text):
|
41 |
+
tokens = roberta.encode(text)
|
42 |
+
last_layer_features = None
|
43 |
+
|
44 |
+
if len(tokens) <= 512:
|
45 |
+
last_layer_features = roberta.extract_features(tokens)
|
46 |
+
return last_layer_features
|
47 |
+
|
48 |
+
|
49 |
+
for i, (gpt, hum) in enumerate(zip(data1.values(), data2.values())):
|
50 |
+
gpt_features, hum_features = fetch_representation(gpt), fetch_representation(hum)
|
51 |
+
|
52 |
+
if gpt_features is None or hum_features is None:
|
53 |
+
too_long += 1
|
54 |
+
continue
|
55 |
+
|
56 |
+
gpt_features_ = F.pad(gpt_features, (0, 0, 0, 512 - gpt_features.size(1)))
|
57 |
+
hum_features_ = F.pad(hum_features, (0, 0, 0, 512 - hum_features.size(1)))
|
58 |
+
|
59 |
+
data["data"][2 * i] = gpt_features_.clone().detach().cpu()
|
60 |
+
data["label"][2 * i] = torch.zeros(1)
|
61 |
+
|
62 |
+
data["data"][2 * i + 1] = hum_features_.clone().detach().cpu()
|
63 |
+
data["label"][2 * i + 1] = torch.ones(1)
|
64 |
+
|
65 |
+
if i % 200 == 0:
|
66 |
+
print("{}{} at {}th pair. Time used: {}s. Outliers: {}".format(domain, task, i, time.time()-start, too_long))
|
67 |
+
|
68 |
+
i += 1
|
69 |
+
if i >= args.pairs:
|
70 |
+
break
|
71 |
+
|
72 |
+
data.close()
|
73 |
+
|
74 |
+
|
75 |
+
|
76 |
+
|
77 |
+
|
78 |
+
|
CheckGPT/model.py
ADDED
@@ -0,0 +1,76 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
import torch.nn as nn
|
3 |
+
import numpy as np
|
4 |
+
import torch.nn.functional as F
|
5 |
+
# from ptflops import get_model_complexity_info
|
6 |
+
from transformers import RobertaTokenizer, RobertaModel, RobertaConfig, RobertaPreTrainedModel
|
7 |
+
|
8 |
+
|
9 |
+
class Attention(nn.Module):
|
10 |
+
def __init__(self, hidden_size, batch_first=False, device="gpu"):
|
11 |
+
super(Attention, self).__init__()
|
12 |
+
self.batch_first = batch_first
|
13 |
+
self.hidden_size = hidden_size
|
14 |
+
stdv = 1.0 / np.sqrt(self.hidden_size)
|
15 |
+
self.att_weights = nn.Parameter(torch.Tensor(1, hidden_size), requires_grad=True)
|
16 |
+
self.device = device
|
17 |
+
|
18 |
+
for weight in self.att_weights:
|
19 |
+
nn.init.uniform_(weight, -stdv, stdv)
|
20 |
+
|
21 |
+
def get_mask(self):
|
22 |
+
pass
|
23 |
+
|
24 |
+
def forward(self, inputs):
|
25 |
+
if self.batch_first:
|
26 |
+
batch_size, max_len = inputs.size()[:2]
|
27 |
+
else:
|
28 |
+
max_len, batch_size = inputs.size()[:2]
|
29 |
+
|
30 |
+
weights = torch.bmm(inputs, self.att_weights.permute(1, 0).unsqueeze(0).repeat(batch_size, 1, 1))
|
31 |
+
attentions = torch.softmax(F.relu(weights.squeeze()), dim=-1)
|
32 |
+
mask = torch.ones(attentions.size(), requires_grad=True)
|
33 |
+
if self.device == "gpu":
|
34 |
+
mask = mask.cuda()
|
35 |
+
masked = attentions * mask
|
36 |
+
_sums = masked.sum(-1).unsqueeze(-1)
|
37 |
+
attentions = masked.div(_sums)
|
38 |
+
weighted = torch.mul(inputs, attentions.unsqueeze(-1).expand_as(inputs))
|
39 |
+
return weighted.sum(1), attentions
|
40 |
+
|
41 |
+
|
42 |
+
class AttenLSTM(nn.Module):
|
43 |
+
def __init__(self, input_size=1024, hidden_size=256, batch_first=True, dropout=0.5, bidirectional=True,
|
44 |
+
num_layers=2, device="gpu"):
|
45 |
+
super(AttenLSTM, self).__init__()
|
46 |
+
self.dropout = nn.Dropout(dropout)
|
47 |
+
self.lstm1 = nn.LSTM(input_size=input_size,
|
48 |
+
hidden_size=hidden_size,
|
49 |
+
num_layers=1,
|
50 |
+
bidirectional=bidirectional,
|
51 |
+
batch_first=batch_first)
|
52 |
+
self.atten1 = Attention(hidden_size * 2, batch_first=batch_first, device=device)
|
53 |
+
self.lstm2 = nn.LSTM(input_size=hidden_size * 2,
|
54 |
+
hidden_size=hidden_size,
|
55 |
+
num_layers=1,
|
56 |
+
bidirectional=bidirectional,
|
57 |
+
batch_first=batch_first)
|
58 |
+
self.atten2 = Attention(hidden_size * 2, batch_first=batch_first, device=device)
|
59 |
+
self.fc = nn.Linear(hidden_size * num_layers * 2, 2)
|
60 |
+
|
61 |
+
def forward(self, x):
|
62 |
+
out1, (_, _) = self.lstm1(x)
|
63 |
+
x, _ = self.atten1(out1)
|
64 |
+
out2, (_, _) = self.lstm2(out1)
|
65 |
+
y, _ = self.atten2(out2)
|
66 |
+
|
67 |
+
z = torch.cat([x, y], dim=1)
|
68 |
+
z = self.fc(self.dropout(z))
|
69 |
+
return z
|
70 |
+
|
71 |
+
|
72 |
+
if __name__ == '__main__':
|
73 |
+
pass
|
74 |
+
# config = RobertaConfig.from_pretrained("roberta-large", num_labels=2)
|
75 |
+
# m = RobertaModel(config).cuda()
|
76 |
+
# print(get_model_complexity_info(m, (512,), as_strings=True, print_per_layer_stat=True))
|
CheckGPT/test.py
ADDED
@@ -0,0 +1,56 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import torch
|
2 |
+
from transformers import RobertaTokenizer, RobertaModel, RobertaConfig, RobertaPreTrainedModel
|
3 |
+
import torch.nn.functional as F
|
4 |
+
from model import AttenLSTM
|
5 |
+
|
6 |
+
|
7 |
+
class CustomRobertaForPipeline(RobertaPreTrainedModel):
|
8 |
+
def __init__(self, config, device="gpu"):
|
9 |
+
super().__init__(config)
|
10 |
+
self.roberta = RobertaModel(config)
|
11 |
+
self.classifier = AttenLSTM(input_size=1024, hidden_size=256, batch_first=True, dropout=0.5, bidirectional=True,
|
12 |
+
num_layers=2, device=device)
|
13 |
+
|
14 |
+
def forward(self, input_ids, attention_mask=None, token_type_ids=None, position_ids=None, head_mask=None,
|
15 |
+
inputs_embeds=None):
|
16 |
+
outputs = self.roberta(input_ids, attention_mask=attention_mask, token_type_ids=token_type_ids,
|
17 |
+
position_ids=position_ids, head_mask=head_mask, inputs_embeds=inputs_embeds)
|
18 |
+
features = F.pad(outputs.last_hidden_state, (0, 0, 0, 512 - outputs.last_hidden_state.size(1)))
|
19 |
+
logits = self.classifier(features)
|
20 |
+
return logits
|
21 |
+
|
22 |
+
|
23 |
+
device_name = "cuda" if torch.cuda.is_available() else "cpu"
|
24 |
+
device = torch.device(device_name)
|
25 |
+
tokenizer = RobertaTokenizer.from_pretrained("roberta-large")
|
26 |
+
config = RobertaConfig.from_pretrained("roberta-large", num_labels=2)
|
27 |
+
model = CustomRobertaForPipeline.from_pretrained("roberta-large", config=config, device=device_name).to(device)
|
28 |
+
model.eval()
|
29 |
+
|
30 |
+
|
31 |
+
def eval_one(model, input):
|
32 |
+
item = input.replace("\n", " ").replace(" ", " ").strip()
|
33 |
+
tokens = tokenizer.encode(item)
|
34 |
+
if len(tokens) > 512:
|
35 |
+
tokens = tokens[:512]
|
36 |
+
print("!!!Input too long. Truncated to first 512 tokens.")
|
37 |
+
outputs = model(torch.tensor(tokens).unsqueeze(0).to(device))
|
38 |
+
pred = torch.max(outputs.data, 1)[1]
|
39 |
+
(gpt_prob, hum_prob) = F.softmax(outputs.data, dim=1)[0]
|
40 |
+
return pred[0].data, 100 * gpt_prob, 100 * hum_prob
|
41 |
+
|
42 |
+
try:
|
43 |
+
while True:
|
44 |
+
a = input("Please input the text to be evaluated: (0 for gpt, 1 for human)\n")
|
45 |
+
if a == "exit":
|
46 |
+
raise KeyboardInterrupt
|
47 |
+
model.classifier.load_state_dict(torch.load("../Pretrained/Unified_Task1.pth"), map_location=device)
|
48 |
+
print("- Decision of GPT-Written: {}, Probability: GPT: {:.4f}%, Human: {:.4f}%.".format(*eval_one(model, a)))
|
49 |
+
model.classifier.load_state_dict(torch.load("../Pretrained/Unified_Task2.pth"), map_location=device)
|
50 |
+
print("- Decision of GPT-Completed: {}, Probability: GPT: {:.4f}%, Human: {:.4f}%.".format(*eval_one(model, a)))
|
51 |
+
model.classifier.load_state_dict(torch.load("../Pretrained/Unified_Task3.pth"), map_location=device)
|
52 |
+
print("- Decision of GPT-Polished: {}, Probability: GPT: {:.4f}%, Human: {:.4f}%.".format(*eval_one(model, a)))
|
53 |
+
model.classifier.load_state_dict(torch.load("../Pretrained/Unified_Task123.pth"), map_location=device)
|
54 |
+
print("- Decision of GPT-Generated (any kind): {}, Probability: GPT: {:.4f}%, Human: {:.4f}%.\n".format(*eval_one(model, a)))
|
55 |
+
except KeyboardInterrupt:
|
56 |
+
exit()
|
Pretrained/CS_Task1.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:6e27561c1f6f75209e7f66f5fb4913686c94fcca37bd9416dd3d7a326138171f
|
3 |
+
size 16825967
|
Pretrained/CS_Task2.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:c5b93f922296b852a691cc099558e29ea4a28412bbe0ae0267cbe9ad34cdeb10
|
3 |
+
size 16825967
|
Pretrained/CS_Task3.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:a4ea7fe39ebe99c8d5bf6860937390e012fd5935fc29df2894f2e49e07253653
|
3 |
+
size 16825967
|
Pretrained/HSS_Task1.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2d1af6d7c3833949cc2f6a68b439c4090c45007d6bbc931d4f8eb70f2287749b
|
3 |
+
size 16825967
|
Pretrained/HSS_Task2pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:1b82886813e152da743cab512673f72d4d1085d71cc7258c32bd8352c71a5aba
|
3 |
+
size 16825967
|
Pretrained/HSS_Task3.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d50d5e32c4c3a392ab2a61b2543bb6c8865656f92a459cdcf899191eaaba20db
|
3 |
+
size 16825967
|
Pretrained/PHY_Task1.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:bf47a169527af8a8e2a1cdd5f77ab3d8454028ca9dc93dc3920f72f50512d5c7
|
3 |
+
size 16825967
|
Pretrained/PHY_Task2.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:7231d981dd6d83ea1aa5d67374bc0bb0b1340a8aa37642a63c751a9dab4ee5f4
|
3 |
+
size 16825967
|
Pretrained/PHY_Task3.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:95b4c2136a17bdf51a1ae7f1215e5edd02a29de8e3fea254c744ab8027036b9f
|
3 |
+
size 16825967
|
Pretrained/Unified_Task1.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:aaec7e43d22ed6cf14b7ab55e9f41de9272d403545ec81e39bcc129f1f24bfc0
|
3 |
+
size 16825551
|
Pretrained/Unified_Task123.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:d01eeade281fe9fc0c71a8bc9d2f0b818678ec4933c3a42137e8dc2ff4e67da1
|
3 |
+
size 16825551
|
Pretrained/Unified_Task2.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:0ee76caf3d39da78d1dbb9a037799dcd328e1bcb64b8d47e6b22d0c2a9eb2997
|
3 |
+
size 16825551
|
Pretrained/Unified_Task3.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:62ef1074c616936cd7bf1120bbc132188182507f9c5cbbdcf8a529d374b87758
|
3 |
+
size 16825551
|
requirements.txt
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
numpy~=1.23.5
|
2 |
+
h5py~=3.8.0
|
3 |
+
torch==1.13.1
|
4 |
+
transformers==4.28.1
|