First version of the your-model-name model and tokenizer.
Browse files- __pycache__/preprocess.cpython-37.pyc +0 -0
- main.py +37 -36
- preprocess.py +71 -1
- pytorch_model.bin +1 -1
__pycache__/preprocess.cpython-37.pyc
CHANGED
Binary files a/__pycache__/preprocess.cpython-37.pyc and b/__pycache__/preprocess.cpython-37.pyc differ
|
|
main.py
CHANGED
@@ -6,55 +6,56 @@ import torch
|
|
6 |
import subprocess
|
7 |
|
8 |
data = Model()
|
9 |
-
|
10 |
-
|
11 |
-
|
|
|
12 |
|
13 |
-
train_answers, train_contexts = data.add_end_idx(train_answers, train_contexts)
|
14 |
-
val_answers, val_contexts = data.add_end_idx(val_answers, val_contexts)
|
15 |
|
16 |
-
train_encodings, val_encodings = data.Tokenizer(train_contexts, train_questions, val_contexts, val_questions)
|
17 |
|
18 |
-
train_encodings = data.add_token_positions(train_encodings, train_answers)
|
19 |
-
val_encodings = data.add_token_positions(val_encodings, val_answers)
|
20 |
|
21 |
-
train_dataset = SquadDataset(train_encodings)
|
22 |
-
val_dataset = SquadDataset(val_encodings)
|
23 |
|
24 |
|
25 |
|
26 |
-
model = DistilBertForQuestionAnswering.from_pretrained("distilbert-base-uncased")
|
27 |
|
28 |
|
29 |
-
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
30 |
|
31 |
-
model.to(device)
|
32 |
-
model.train()
|
33 |
|
34 |
-
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
|
35 |
|
36 |
-
optim = AdamW(model.parameters(), lr=5e-5)
|
37 |
|
38 |
-
for epoch in range(2):
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
print("Done")
|
51 |
-
model.eval()
|
52 |
-
model.save_pretrained("./")
|
53 |
-
data.tokenizer.save_pretrained("./")
|
54 |
|
55 |
|
56 |
-
subprocess.call(["git", "add","--all"])
|
57 |
-
subprocess.call(["git", "status"])
|
58 |
-
subprocess.call(["git", "commit", "-m", "First version of the your-model-name model and tokenizer."])
|
59 |
-
subprocess.call(["git", "push"])
|
60 |
|
|
|
6 |
import subprocess
|
7 |
|
8 |
data = Model()
|
9 |
+
data.ModelExecution()
|
10 |
+
# train_contexts, train_questions, train_answers = data.ArrangeData("livecheckcontainer")
|
11 |
+
# val_contexts, val_questions, val_answers = data.ArrangeData("livecheckcontainer")
|
12 |
+
# print(train_answers)
|
13 |
|
14 |
+
# train_answers, train_contexts = data.add_end_idx(train_answers, train_contexts)
|
15 |
+
# val_answers, val_contexts = data.add_end_idx(val_answers, val_contexts)
|
16 |
|
17 |
+
# train_encodings, val_encodings = data.Tokenizer(train_contexts, train_questions, val_contexts, val_questions)
|
18 |
|
19 |
+
# train_encodings = data.add_token_positions(train_encodings, train_answers)
|
20 |
+
# val_encodings = data.add_token_positions(val_encodings, val_answers)
|
21 |
|
22 |
+
# train_dataset = SquadDataset(train_encodings)
|
23 |
+
# val_dataset = SquadDataset(val_encodings)
|
24 |
|
25 |
|
26 |
|
27 |
+
# model = DistilBertForQuestionAnswering.from_pretrained("distilbert-base-uncased")
|
28 |
|
29 |
|
30 |
+
# device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
31 |
|
32 |
+
# model.to(device)
|
33 |
+
# model.train()
|
34 |
|
35 |
+
# train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
|
36 |
|
37 |
+
# optim = AdamW(model.parameters(), lr=5e-5)
|
38 |
|
39 |
+
# for epoch in range(2):
|
40 |
+
# print(epoch)
|
41 |
+
# for batch in train_loader:
|
42 |
+
# optim.zero_grad()
|
43 |
+
# input_ids = batch['input_ids'].to(device)
|
44 |
+
# attention_mask = batch['attention_mask'].to(device)
|
45 |
+
# start_positions = batch['start_positions'].to(device)
|
46 |
+
# end_positions = batch['end_positions'].to(device)
|
47 |
+
# outputs = model(input_ids, attention_mask=attention_mask, start_positions=start_positions, end_positions=end_positions)
|
48 |
+
# loss = outputs[0]
|
49 |
+
# loss.backward()
|
50 |
+
# optim.step()
|
51 |
+
# print("Done")
|
52 |
+
# model.eval()
|
53 |
+
# model.save_pretrained("./")
|
54 |
+
# data.tokenizer.save_pretrained("./")
|
55 |
|
56 |
|
57 |
+
# subprocess.call(["git", "add","--all"])
|
58 |
+
# subprocess.call(["git", "status"])
|
59 |
+
# subprocess.call(["git", "commit", "-m", "First version of the your-model-name model and tokenizer."])
|
60 |
+
# subprocess.call(["git", "push"])
|
61 |
|
preprocess.py
CHANGED
@@ -4,7 +4,9 @@ from pathlib import Path
|
|
4 |
from azure.cosmos import CosmosClient, PartitionKey, exceptions
|
5 |
from transformers import DistilBertTokenizerFast
|
6 |
import torch
|
7 |
-
|
|
|
|
|
8 |
|
9 |
class Model:
|
10 |
|
@@ -80,6 +82,55 @@ class Model:
|
|
80 |
# train_contexts, train_questions, train_answers = read_squad('squad/train-v2.0.json')
|
81 |
# val_contexts, val_questions, val_answers = read_squad('squad/dev-v2.0.json')
|
82 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
|
84 |
|
85 |
|
@@ -94,3 +145,22 @@ class SquadDataset(torch.utils.data.Dataset):
|
|
94 |
def __len__(self):
|
95 |
return len(self.encodings.input_ids)
|
96 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
4 |
from azure.cosmos import CosmosClient, PartitionKey, exceptions
|
5 |
from transformers import DistilBertTokenizerFast
|
6 |
import torch
|
7 |
+
from transformers import DistilBertForQuestionAnswering, AdamW
|
8 |
+
from torch.utils.data import DataLoader
|
9 |
+
import subprocess
|
10 |
|
11 |
class Model:
|
12 |
|
|
|
82 |
# train_contexts, train_questions, train_answers = read_squad('squad/train-v2.0.json')
|
83 |
# val_contexts, val_questions, val_answers = read_squad('squad/dev-v2.0.json')
|
84 |
|
85 |
+
def ModelExecution(self):
|
86 |
+
train_contexts, train_questions, train_answers = self.ArrangeData("livecheckcontainer")
|
87 |
+
val_contexts, val_questions, val_answers = self.ArrangeData("livecheckcontainer")
|
88 |
+
print(train_answers)
|
89 |
+
|
90 |
+
train_answers, train_contexts = self.add_end_idx(train_answers, train_contexts)
|
91 |
+
val_answers, val_contexts = self.add_end_idx(val_answers, val_contexts)
|
92 |
+
|
93 |
+
train_encodings, val_encodings = self.Tokenizer(train_contexts, train_questions, val_contexts, val_questions)
|
94 |
+
|
95 |
+
train_encodings = self.add_token_positions(train_encodings, train_answers)
|
96 |
+
val_encodings = self.add_token_positions(val_encodings, val_answers)
|
97 |
+
|
98 |
+
train_dataset = SquadDataset(train_encodings)
|
99 |
+
val_dataset = SquadDataset(val_encodings)
|
100 |
+
|
101 |
+
model = DistilBertForQuestionAnswering.from_pretrained("distilbert-base-uncased")
|
102 |
+
|
103 |
+
|
104 |
+
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
|
105 |
+
|
106 |
+
model.to(device)
|
107 |
+
model.train()
|
108 |
+
|
109 |
+
train_loader = DataLoader(train_dataset, batch_size=16, shuffle=True)
|
110 |
+
|
111 |
+
optim = AdamW(model.parameters(), lr=5e-5)
|
112 |
+
|
113 |
+
for epoch in range(2):
|
114 |
+
print(epoch)
|
115 |
+
for batch in train_loader:
|
116 |
+
optim.zero_grad()
|
117 |
+
input_ids = batch['input_ids'].to(device)
|
118 |
+
attention_mask = batch['attention_mask'].to(device)
|
119 |
+
start_positions = batch['start_positions'].to(device)
|
120 |
+
end_positions = batch['end_positions'].to(device)
|
121 |
+
outputs = model(input_ids, attention_mask=attention_mask, start_positions=start_positions, end_positions=end_positions)
|
122 |
+
loss = outputs[0]
|
123 |
+
loss.backward()
|
124 |
+
optim.step()
|
125 |
+
print("Done")
|
126 |
+
model.eval()
|
127 |
+
model.save_pretrained("./")
|
128 |
+
self.tokenizer.save_pretrained("./")
|
129 |
+
|
130 |
+
subprocess.call(["git", "add","--all"])
|
131 |
+
subprocess.call(["git", "status"])
|
132 |
+
subprocess.call(["git", "commit", "-m", "First version of the your-model-name model and tokenizer."])
|
133 |
+
subprocess.call(["git", "push"])
|
134 |
|
135 |
|
136 |
|
|
|
145 |
def __len__(self):
|
146 |
return len(self.encodings.input_ids)
|
147 |
|
148 |
+
# import requests
|
149 |
+
# API_URL = "https://api-inference.huggingface.co/models/Ateeb/QA"
|
150 |
+
# headers = {"Authorization": "Bearer api_DHnvjPKdjmjkmEYQubgvmIKJqWaNNYljaF"}
|
151 |
+
|
152 |
+
# def query(payload):
|
153 |
+
# data = json.dumps(payload)
|
154 |
+
# response = requests.request("POST", API_URL, headers=headers, data=data)
|
155 |
+
# return json.loads(response.content.decode("utf-8"))
|
156 |
+
|
157 |
+
|
158 |
+
# data = query(
|
159 |
+
# {
|
160 |
+
# "inputs": {
|
161 |
+
# "question": "What is my name?",
|
162 |
+
# "context": "My name is Clara and I live in Berkeley.",
|
163 |
+
# }
|
164 |
+
# }
|
165 |
+
# )
|
166 |
+
# print(data)
|
pytorch_model.bin
CHANGED
@@ -1,3 +1,3 @@
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
-
oid sha256:
|
3 |
size 265498527
|
|
|
1 |
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8e93e749fc2915653de7b297c5bae0125876890474e01ad3fd9c196680bd2fa3
|
3 |
size 265498527
|