-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLSTMModel.py
180 lines (143 loc) · 6.8 KB
/
LSTMModel.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
171
172
173
174
175
176
177
178
179
180
import torch
from torch.utils.data import Dataset, DataLoader
from sklearn.model_selection import train_test_split
import numpy as np
import random
import torch.nn as nn
import pandas as pd
from torch.nn.utils.rnn import pack_padded_sequence, pad_packed_sequence
class BibleDataset(Dataset):
def __init__(self, data, max_len):
self.data = data
self.max_len = max_len
self.vocab = self.build_vocab(data['verse'])
self.vocab_size = len(self.vocab)
def build_vocab(self, verses):
vocab = set()
for verse in verses:
for word in verse.split():
vocab.add(word)
vocab = {word: i + 2 for i, word in enumerate(vocab)}
vocab['<PAD>'] = 0
vocab['<UNK>'] = 1
return vocab
def encode(self, verse):
encoding = [self.vocab.get(word, self.vocab['<UNK>']) for word in verse.split()]
encoding = encoding[:self.max_len]
encoding += [self.vocab['<PAD>']] * (self.max_len - len(encoding))
return torch.tensor(encoding)
def __len__(self):
return len(self.data)
def __getitem__(self, idx):
verse = self.data.iloc[idx, 0]
label = self.data.iloc[idx, 1]
encoding = self.encode(verse)
return {
'input_ids': encoding,
'labels': torch.tensor(label, dtype=torch.long),
}
class BibleClassifier(torch.nn.Module):
def __init__(self, embedding_dim, hidden_dim, output_dim, dropout, vocab_size):
super(BibleClassifier, self).__init__()
self.embedding = nn.Embedding(vocab_size, embedding_dim)
self.lstm = nn.LSTM(embedding_dim, hidden_dim, num_layers=1, batch_first=True, bidirectional=True)
self.dropout = nn.Dropout(dropout)
self.fc = nn.Linear(hidden_dim*2, output_dim)
def forward(self, input_ids):
embedding_out = self.embedding(input_ids)
packed_out = pack_padded_sequence(embedding_out, torch.count_nonzero(input_ids, dim=1).cpu(), batch_first=True, enforce_sorted=False)
lstm_out, _ = self.lstm(packed_out)
lstm_out, _ = pad_packed_sequence(lstm_out, batch_first=True)
lstm_out = torch.cat((lstm_out[:, -1, :self.lstm.hidden_size], lstm_out[:, 0, self.lstm.hidden_size:]), dim=1)
dropout_out = self.dropout(lstm_out)
outputs = self.fc(dropout_out)
return outputs
def LSTM_EXPERIMENT():
torch.manual_seed(0)
np.random.seed(0)
random.seed(0)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
train_text, val_text, train_labels, val_labels = train_test_split(df['verse'], df['label'], test_size=0.2, stratify=df['label'], random_state=0)
train_data = pd.DataFrame({'verse': train_text, 'label': train_labels})
val_data = pd.DataFrame({'verse': val_text, 'label': val_labels})
train_dataset = BibleDataset(train_data, 512)
val_dataset = BibleDataset(val_data, 512)
train_data_loader = DataLoader(train_dataset, batch_size=8, shuffle=True)
val_data_loader = DataLoader(val_dataset, batch_size=8)
model = BibleClassifier(embedding_dim=128, hidden_dim=128, output_dim=4, dropout=0.1, vocab_size=train_dataset.vocab_size)
model.to(device)
optimizer = torch.optim.Adam(model.parameters(), lr=1e-5)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=1, gamma=0.1)
# Test cases
assert len(train_dataset) > 0, "Training dataset is empty"
assert len(val_dataset) > 0, "Validation dataset is empty"
# Test a batch from the training data loader
train_batch = next(iter(train_data_loader))
assert train_batch['input_ids'].shape[1] == 512, "Input IDs have incorrect shape"
assert train_batch['labels'].shape[0] == train_batch['input_ids'].shape[0], "Labels have incorrect shape"
# Test a batch from the validation data loader
val_batch = next(iter(val_data_loader))
assert val_batch['input_ids'].shape[1] == 512, "Input IDs have incorrect shape"
assert val_batch['labels'].shape[0] == val_batch['input_ids'].shape[0], "Labels have incorrect shape"
# Test the model's forward pass
input_ids = train_batch['input_ids'].to(device)
labels = train_batch['labels'].to(device)
outputs = model(input_ids)
assert outputs.shape[0] == input_ids.shape[0], "Model output has incorrect shape"
assert outputs.shape[1] == 4, "Model output has incorrect number of classes"
# Train the model on a small portion of the dataset
train_subset = torch.utils.data.Subset(train_dataset, range(100))
train_subset_loader = DataLoader(train_subset, batch_size=8, shuffle=True)
print('Training the model on a subset of the data...')
for epoch in range(2):
model.train()
total_loss = 0
for batch in train_subset_loader:
input_ids = batch['input_ids'].to(device)
labels = batch['labels'].to(device)
optimizer.zero_grad()
outputs = model(input_ids)
loss = torch.nn.CrossEntropyLoss()(outputs, labels)
loss.backward()
optimizer.step()
total_loss += loss.item()
scheduler.step()
print(f'Epoch {epoch+1}, Loss: {total_loss / len(train_subset_loader)}')
model.eval()
total_correct = 0
with torch.no_grad():
for batch in val_data_loader:
input_ids = batch['input_ids'].to(device)
labels = batch['labels'].to(device)
outputs = model(input_ids)
_, predicted = torch.max(outputs, dim=1)
total_correct += (predicted == labels).sum().item()
accuracy = total_correct / len(val_data)
print(f'Epoch {epoch+1}, Val Accuracy: {accuracy:.4f}')
# If all test cases pass, train the model on the full dataset
print('Training the model on the full dataset...')
for epoch in range(5):
model.train()
total_loss = 0
for batch in train_data_loader:
input_ids = batch['input_ids'].to(device)
labels = batch['labels'].to(device)
optimizer.zero_grad()
outputs = model(input_ids)
loss = torch.nn.CrossEntropyLoss()(outputs, labels)
loss.backward()
optimizer.step()
total_loss += loss.item()
scheduler.step()
print(f'Epoch {epoch+1}, Loss: {total_loss / len(train_data_loader)}')
model.eval()
total_correct = 0
with torch.no_grad():
for batch in val_data_loader:
input_ids = batch['input_ids'].to(device)
labels = batch['labels'].to(device)
outputs = model(input_ids)
_, predicted = torch.max(outputs, dim=1)
total_correct += (predicted == labels).sum().item()
accuracy = total_correct / len(val_data)
print(f'Epoch {epoch+1}, Val Accuracy: {accuracy:.4f}')