Skip to content

Commit 79af8c9

Browse files
updates
1 parent de359f0 commit 79af8c9

File tree

15 files changed

+98
-14
lines changed

15 files changed

+98
-14
lines changed

ML/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@
2727
from sklearn.metrics import *
2828
from torch.hub import *
2929
import torchtext.functional as F
30+
import warnings
3031

32+
warnings.filterwarnings("ignore")
3133
print(torch.__version__, torchvision.__version__, torchtext.__version__)
3234
os.environ["CUDA_LAUNCH_BLOCKING"] = "1"
3335
# os.environ["WANDB_SILENT"] = "true"

ML/dataset/main_loaders.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ def __init__(
2929
self.batch_size = batch_size
3030
self.get_batches()
3131

32-
def get_batches(self):
32+
def get_batches(self) -> None:
3333
X = self.X_train if self.train else self.X_test
3434
y = self.y_train if self.train else self.y_test
3535
X_batches = []

ML/dataset/valid_loaders.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,10 @@
55
class Valid_Loader(Loader):
66
def __init__(self, *args) -> None:
77
super().__init__(*args)
8+
self.data["id"].dropna(inplace=True)
89
self.X = self.data["text"].to_numpy()
10+
self.ids = self.data["id"].to_numpy()
11+
print(len(self.X), len(self.ids))
912

1013
def __getitem__(self, index) -> np.array:
11-
return self.transform(self.X[index])
14+
return (self.ids[index], [self.transform(self.X[index])])

ML/helper_functions/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
from ML.helper_functions.load_data import *
22
from ML.helper_functions.test import *
33
from ML.helper_functions.train import *
4-
from ML.helper_functions.transformer import *
4+
from ML.helper_functions.transformations.transformer import *

ML/helper_functions/alert.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
from ML import *
2+
3+
4+
class Alert:
5+
def __init__(self) -> None:
6+
pass

ML/helper_functions/clearcache.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22

33

44
class ClearCache:
5-
def __enter__(self):
5+
def __enter__(self) -> None:
66
torch.cuda.empty_cache()
77

8-
def __exit__(self, *args):
8+
def __exit__(self, *args) -> None:
99
torch.cuda.empty_cache()

ML/helper_functions/load_data.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def ld(self) -> Tuple[DataLoader, DataLoader, DataLoader]:
4949
num_workers=round(os.cpu_count() / 2),
5050
)
5151
self.valid_data_loader = DataLoader(
52-
self.dataset_valid(self.valid_path, None),
52+
self.dataset_valid(self.valid_path, self.main_transform),
5353
batch_size=None,
5454
shuffle=False,
5555
num_workers=round(os.cpu_count() / 2),

ML/helper_functions/test.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def __init__(
1616
self.model = model
1717
self.name = name
1818

19-
def test(self):
19+
def test(self) -> Dict:
2020
a_tot = 0
2121
p_tot = 0
2222
r_tot = 0
@@ -29,7 +29,7 @@ def test(self):
2929
X = F.to_tensor(X, padding_value=1).to("cuda")
3030
y = torch.tensor(y).to("cuda")
3131
preds = torch.argmax(torch.softmax(self.model(X), dim=1), dim=1)
32-
loss = self.criteria(preds, y.view(-1, 1).squeeze(1))
32+
loss = self.criterion(preds.float(), y.view(-1, 1).squeeze(1).float())
3333
results = classification_report(
3434
preds.cpu(), y.view(-1, 1).squeeze(1).cpu(), output_dict=True
3535
)
@@ -43,10 +43,25 @@ def test(self):
4343
f1_tot += f1score
4444
l_tot += loss.item()
4545
n += 1
46+
print(loss.item(), l_tot, l_tot / n)
4647
return {
4748
f"{self.name} precision": p_tot / n,
4849
f"{self.name} recall": r_tot / n,
4950
f"{self.name} f1-score": f1_tot / n,
5051
f"{self.name} accuracy": a_tot / n,
5152
f"{self.name} loss": l_tot / n,
5253
}
54+
55+
def make_predictions(self, run_name: str, epoch: int) -> pd.DataFrame:
56+
ids = []
57+
target = []
58+
for i, X in enumerate(self.valid_dataloader):
59+
X = F.to_tensor(X, padding_value=1).to("cuda")
60+
pred = torch.argmax(torch.softmax(self.model(X), dim=1), dim=1).squeeze().cpu().item()
61+
ids.append(i)
62+
target.append(pred)
63+
if run_name not in os.listdir("./ML/predictions/"):
64+
os.mkdir(f"./ML/predictions/{run_name}")
65+
df = pd.DataFrame({"id": ids, "target": target})
66+
df.to_csv(f"./ML/predictions/{run_name}/{epoch}.csv", index=False)
67+
return df

ML/helper_functions/train.py

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,12 @@ def __init__(
2626
self.optimizer = optimizer
2727
self.lr_schedular = lr_schedular
2828

29-
def train(self, run_name):
29+
def train(self, run_name: str) -> None:
3030
print(torchinfo.summary(self.model))
3131
wandb.init(project=PROJECT_NAME, name=run_name, config=self.config)
3232
wandb.watch(self.model, log="all")
3333
iterator = tqdm(range(self.epochs))
34-
for _ in iterator:
34+
for epoch in iterator:
3535
torch.cuda.empty_cache()
3636
for i, (X, y) in enumerate(self.train_dataloader):
3737
y = y[0]
@@ -61,6 +61,13 @@ def train(self, run_name):
6161
"Train",
6262
).test()
6363
)
64+
Test(
65+
self.train_dataloader,
66+
self.valid_dataloader,
67+
self.criterion,
68+
self.model,
69+
"Train",
70+
).make_predictions(run_name, epoch)
6471
iterator.set_description(f"Testing Done")
6572
self.model.train()
6673
wandb.save()
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
from ML.helper_functions.transformations.randomize import *
2+
from ML.helper_functions.transformations.transformer import *

0 commit comments

Comments
 (0)