-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtraining.py
51 lines (40 loc) · 1.29 KB
/
training.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
import pandas as pd
import numpy as np
import torch
from torch import nn
from Utils import split_data, TextDataset, Training
from models import ANN, CNN1D, BILSTM
from torch.utils.data import Dataset, DataLoader
loaded_data = np.load(r"cleaned-dataset\data_npy.npy")
X_train, X_test, y_train, y_test = split_data(loaded_data)
training_dataset = TextDataset(X_train, y_train)
testing_dataset = TextDataset(X_test, y_test)
train_loader = DataLoader(dataset=training_dataset, batch_size=128, shuffle=True)
test_loader = DataLoader(dataset=training_dataset, batch_size=128, shuffle=True)
# * GLOABL VARIABLES
EPOCHS = 150
LEARNING_RATE = 0.001
DEVICE = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# DEVICE = torch.device("cpu")
# model = LSTMModel().to(DEVICE)
# * LSTM
INPUT_SIZE = train_loader.dataset[0][0].shape[0]
HIDDEN_STATE = 64
NUM_LAYERS = 4
NUM_CLASSES = 1 # * binary classification
model = BILSTM(INPUT_SIZE, HIDDEN_STATE, NUM_LAYERS, NUM_CLASSES, bidirection=True).to(
DEVICE
)
loss = torch.nn.BCELoss()
optimizer = torch.optim.Adam(model.parameters(), lr=LEARNING_RATE)
if __name__ == "__main__":
history = Training(
model,
train_loader,
test_loader,
EPOCHS,
DEVICE,
loss,
optimizer,
print_every=5,
)