-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataset.py
More file actions
179 lines (133 loc) · 4.97 KB
/
Copy pathDataset.py
File metadata and controls
179 lines (133 loc) · 4.97 KB
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
176
177
178
179
import json
import math
import random
import tokenize
from io import BytesIO
import keyword
import torch
from torch.utils.data import DataLoader
import invalidData
VOCAB = [
"<PAD>",
"def", "return", "if", "elif", "else", "for", "while",
"break", "continue", "pass", "try", "except",
"in", "is", "and", "or", "not",
"(", ")", "[", "]", "{", "}",
",", ":", ".", ";",
"+", "-", "*", "/", "//", "%", "**",
"=", "==", "!=", "<", "<=", ">", ">=",
"+=", "-=", "*=",
"<NEWLINE>",
"<INDENT>",
"<DEDENT>",
"<VAR>", # identifiers
"<NUM>", # numbers
"<STR>", # strings
"<UNK>"
]
TOKENS_TO_ID = {v : i for i, v in enumerate(VOCAB)}
def tokenize_code(code: str):
tokens = []
try:
for tok in tokenize.tokenize(BytesIO(code.encode()).readline):
if tok.type in {
tokenize.ENCODING,
tokenize.ENDMARKER,
tokenize.NL,
}:
continue
tokens.append(normalize_token(tok.type, tok.string))
except tokenize.TokenError:
# still return what we got
pass
return tokens
def normalize_token(token_type, token_string):
match token_type:
case tokenize.NAME:
if keyword.iskeyword(token_string):
return token_string
return "<VAR>"
case tokenize.NUMBER:
return "<NUM>"
case tokenize.STRING:
return "<STR>"
case tokenize.NEWLINE:
return "<NEWLINE>"
case tokenize.INDENT:
return "<INDENT>"
case tokenize.DEDENT:
return "<DEDENT>"
return token_string
with open("C:\\Users\\gunee\\Desktop\\Projects\\Machine Learning\\data\\Python Parser\\Clean\\AllValid.json", "r") as f:
validData = json.load(f)
random.seed(21)
good_tokens = []
good_tokens_attention_mask = []
bad_tokens = []
bad_tokens_attention_mask = []
mixedData = validData
random.shuffle(mixedData)
for unit in validData:
tokenized = tokenize_code(unit["code"])
tokenized = tokenized[:200]
to_pad_length = (max(0, 200 - len(tokenized)))
tokenized += ["<PAD>"] * to_pad_length
assert len(tokenized) == 200
attention_mask = [1] * (200 - to_pad_length) + [0] * to_pad_length
good_tokens.append(tokenized)
good_tokens_attention_mask.append(attention_mask)
tokenized_errorized = invalidData.create_random_error(tokenized)[:200]
if tokenized_errorized is not None:
to_pad_length = (max(0, 200 - len(tokenized_errorized)))
tokenized_errorized += ["<PAD>"] * to_pad_length
assert len(tokenized_errorized) == 200
bad_tokens.append(tokenized_errorized)
attention_mask = [1] * (200 - to_pad_length) + [0] * to_pad_length
bad_tokens_attention_mask.append(attention_mask)
assert abs((len(good_tokens) - len(bad_tokens)) / len(good_tokens)) < 0.1 # if the size is not similar the model will just learn to always guess valid
tokens = [[TOKENS_TO_ID.get(j, TOKENS_TO_ID["<UNK>"]) for j in i] for i in good_tokens] + [[TOKENS_TO_ID.get(j, TOKENS_TO_ID["<UNK>"]) for j in i] for i in bad_tokens]
attention_mask = good_tokens_attention_mask + bad_tokens_attention_mask
labels = len(good_tokens_attention_mask) * [1] + len(bad_tokens_attention_mask) * [0]
combined = list(zip(tokens, attention_mask, labels))
random.seed(21)
random.shuffle(combined)
tokens, attention_mask, labels = zip(*combined)
# Convert back to lists / tensors
tokens = list(tokens)
attention_mask = list(attention_mask)
labels = list(labels)
validation_cutoff = math.floor(0.8 * len(tokens))
test_cutoff = math.floor(0.9 * len(tokens))
class SyntaxCodeDataSet(torch.utils.data.Dataset):
def __init__(self, data_tensor, attention_mask, labels):
self.data_tensor = data_tensor
self.attention_mask = attention_mask
self.labels = labels
def __getitem__(self, item):
return {
"input_ids" : self.data_tensor[item],
"attention_mask" : self.attention_mask[item],
"labels" : self.labels[item]
}
def __len__(self):
return len(self.labels)
training_dataset = SyntaxCodeDataSet(torch.tensor(tokens[:validation_cutoff]), torch.tensor(attention_mask[:validation_cutoff]), torch.tensor(labels[:validation_cutoff], dtype=torch.float32))
training_loader = DataLoader(
training_dataset,
batch_size=32,
shuffle=True,
drop_last=False
)
validation_loader = DataLoader(
SyntaxCodeDataSet(torch.tensor(tokens[validation_cutoff:test_cutoff]), torch.tensor(attention_mask[validation_cutoff:test_cutoff]), torch.tensor(labels[validation_cutoff:test_cutoff], dtype=torch.float32)),
batch_size=32,
shuffle=True,
drop_last=False
)
test_loader = DataLoader(
SyntaxCodeDataSet(torch.tensor(tokens[test_cutoff:]), torch.tensor(attention_mask[test_cutoff:]), torch.tensor(labels[test_cutoff:], dtype=torch.float32)),
batch_size=32,
shuffle=True,
drop_last=False
)
print(f"total data points {len(tokens)}")