-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_loading.py
170 lines (125 loc) · 4.95 KB
/
data_loading.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
import torch
from torch.utils.data import Dataset, DataLoader, TensorDataset
import numpy as np
import pandas as pd
from tqdm.auto import tqdm
def load_from_csv(path):
# load data from csv
return pd.read_csv(path)
class BracketDataset(Dataset):
def __init__(self, data=None, list=None, batch_size=64):
if list is None:
self.batch_size = batch_size
self.encode_dict = {
"(": [1, 0, 0, 0],
")": [0, 1, 0, 0],
"&": [0, 0, 1, 0],
"-": [0, 0, 0, 1],
}
ctoi = {c: i for i, c in enumerate("()P")}
max_len = 0
self.X = []
self.eos = []
for i, row in tqdm(data.iterrows(), total=len(data), desc='Generating sequences'):
x = row['sequence']
self.X.append([ctoi[c] for c in x])
self.eos.append(len(x)-1)
max_len = max(max_len, len(x))
for i in tqdm(range(len(self.X)), desc='Padding sequences'):
self.X[i] += [ctoi['P']] * (max_len - len(self.X[i]))
self.X = torch.tensor(self.X, dtype=torch.long)
self.stack_depth = data["stack_depth"].values
self.Y = np.array(
[1 if int(y) > 0 else 0 for y in self.stack_depth]
)
# self.stack_depth = np.abs(self.stack_depth)
# self.counts = np.abs(data["count"].values)
# self.max_len = max([len(seq) for seq in self.X])
# self.eos_index = np.array([len(seq) for seq in self.X])
# self.X_encoded = np.array(
# [self.encode_sequence(self.pad_sequence(seq)) for seq in self.X]
# )
self.list = None
else:
self.list = list
def encode_sequence(self, seq):
return np.array([self.encode_dict[c] for c in seq])
def pad_sequence(self, seq):
return seq + "&" + "-" * (self.max_len - len(seq))
def __len__(self):
return len(self.X)
# def __getitem__(self, idx):
# if self.list:
# return self.list[idx]
# return {
# "x": self.X_encoded[idx],
# "y": self.Y[idx],
# "eos": self.eos_index[idx],
# "sd": self.stack_depth[idx],
# "count": self.counts[idx],
# }
def __getitem__(self, index):
return self.X[index], self.Y[index], self.eos[index]
def load_data(path):
data = load_from_csv(path)
# take 70% of the data for training
# data = data.sample(frac=0.7).reset_index(drop=True)
return BracketDataset(data)
def get_loaders(data, batch_size=32, return_data=False, train_frac=0.6):
torch.manual_seed(0)
bracket_size = len(data)
train_size = int(train_frac * bracket_size)
val_size = int(0.1 * bracket_size)
test_size = bracket_size - train_size - val_size
train_bracket, val_bracket, test_bracket = torch.utils.data.random_split(
data, [train_size, val_size, test_size]
)
train_loader = DataLoader(
train_bracket,
batch_size=batch_size,
shuffle=True,
num_workers=torch.get_num_threads(),
)
val_loader = DataLoader(
val_bracket, batch_size=batch_size, num_workers=torch.get_num_threads()
)
test_loader = DataLoader(
test_bracket, batch_size=batch_size, num_workers=torch.get_num_threads()
)
if return_data:
return (
train_loader,
val_loader,
test_loader,
train_bracket,
val_bracket,
test_bracket,
)
return train_loader, val_loader, test_loader
def remove_batch_dimension(outbeddings, stack_depths=[5, 15]):
outbeds = torch.cat([x[0] for x in outbeddings], dim=0)
depths = torch.cat([x[1] for x in outbeddings])
res = torch.where(torch.isin(depths, torch.tensor(stack_depths)))
indices = res[0]
print(indices, res, len(res), indices.shape)
outbeds = outbeds[indices]
depths = depths[indices]
return outbeds, depths
def get_probe_data(outbeddings, stack_depths=[5, 15]):
outbeddings = [(x.cpu(), y.cpu()) for x, y in outbeddings]
outbeds, depths = remove_batch_dimension(outbeddings, stack_depths=stack_depths)
return outbeds, depths
def make_dataset(X, y):
return TensorDataset(X.cpu(), y.cpu())
def get_probe_loaders(train_data, val_data, test_data, batch_size=64):
train_loader = DataLoader(train_data, batch_size=batch_size, shuffle=True)
val_loader = DataLoader(val_data, batch_size=batch_size)
test_loader = DataLoader(test_data, batch_size=batch_size)
return train_loader, val_loader, test_loader
def get_stack_depths(data, stack_depths=[5, 15]):
stack_data = torch.where(torch.isin(data, torch.tensor(stack_depths)))[0]
loader = DataLoader(stack_data, batch_size=64)
return loader
if __name__ == "__main__":
data = load_data("brackets.csv")
print(data[0])