forked from arghosh/NeurIPSEducation2020
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdataset_task_4.py
81 lines (66 loc) · 3.24 KB
/
dataset_task_4.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
import numpy as np
import torch
from torch.utils import data
import time
import torch
import random
from utils import open_json, dump_json
class FFDataset(data.Dataset):
'Characterizes a dataset for PyTorch'
def __init__(self, answers, labels, question_meta, seed =None):
'Initialization'
self.answers = answers
self.labels = labels
self.seed = seed
#self.targets = targets
self.question_meta = question_meta
def __len__(self):
'Denotes the total number of samples'
return len(self.answers)
def __getitem__(self, index):
'Generates one sample of data'
ans = self.answers[index]
label = self.labels[index]
observed_index = np.where(label != -1.)[0]
if not self.seed:
np.random.shuffle(observed_index)
else:
random.Random(index+self.seed).shuffle(observed_index)
N = len(observed_index)
target_index = observed_index[-N//5:]
trainable_index = observed_index[:-N//5]
input_ans = ans[trainable_index]
input_label = label[trainable_index]
input_question = trainable_index
input_subjects = [
self.question_meta[str(d)]['child_map'] for d in trainable_index]
#output_ans = ans[target_index]
output_label = label[target_index]
output_question = target_index
output_subjects = [
self.question_meta[str(d)]['child_map'] for d in target_index]
output = {'input_label': torch.FloatTensor(input_label), 'input_question': torch.FloatTensor(input_question),
'output_question': torch.FloatTensor(output_question), 'output_label': torch.FloatTensor(output_label),
'input_subjects': input_subjects, 'output_subjects': output_subjects, 'input_ans': torch.FloatTensor(input_ans)}
return output
class ff_collate(object):
def __init__(self):
pass
def __call__(self, batch):
# output = {'input_label': torch.FloatTensor(input_label), 'input_question': torch.FloatTensor(input_question),
# 'output_question': torch.FloatTensor(output_question), 'output_label': torch.FloatTensor(output_label),
# 'input_subjects': input_subjects, 'output_subjects': output_subjects, 'input_ans': torch.FloatTensor(input_ans)}
B = len(batch)
input_labels = torch.zeros(B,948).long()
output_labels = torch.zeros(B, 948).long()
input_ans = torch.ones(B, 948).long()
input_mask = torch.zeros(B,948).long()
output_mask = torch.zeros(B, 948).long()
for b_idx in range(B):
input_labels[b_idx, batch[b_idx]['input_question'].long()] = batch[b_idx]['input_label'].long()
input_ans[b_idx, batch[b_idx]['input_question'].long()] = batch[b_idx]['input_ans'].long()
input_mask[b_idx, batch[b_idx]['input_question'].long()] = 1
output_labels[b_idx, batch[b_idx]['output_question'].long()] = batch[b_idx]['output_label'].long()
output_mask[b_idx, batch[b_idx]['output_question'].long()] = 1
output = {'input_labels':input_labels, 'input_ans':input_ans, 'input_mask':input_mask, 'output_labels':output_labels, 'output_mask':output_mask}
return output