-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathutils.py
27 lines (23 loc) · 970 Bytes
/
utils.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
import torch
import random
import numpy as np
def set_seed(args):
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
if args.n_gpu > 0 and torch.cuda.is_available():
torch.cuda.manual_seed_all(args.seed)
def collate_fn(batch):
max_len = max([len(f["input_ids"]) for f in batch])
input_ids = [f["input_ids"] + [0] * (max_len - len(f["input_ids"])) for f in batch]
input_mask = [[1.0] * len(f["input_ids"]) + [0.0] * (max_len - len(f["input_ids"])) for f in batch]
labels = [f["labels"] for f in batch]
ss = [f["ss"] for f in batch]
os = [f["os"] for f in batch]
input_ids = torch.tensor(input_ids, dtype=torch.long)
input_mask = torch.tensor(input_mask, dtype=torch.float)
labels = torch.tensor(labels, dtype=torch.long)
ss = torch.tensor(ss, dtype=torch.long)
os = torch.tensor(os, dtype=torch.long)
output = (input_ids, input_mask, labels, ss, os)
return output