File size: 10,356 Bytes
f953fd7 96a7d84 f953fd7 96a7d84 f953fd7 96a7d84 f953fd7 |
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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
from typing import Any
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
import numpy as np
from os import listdir
from os.path import isfile, join
import concurrent
import itertools
if __package__ == None or __package__ == "":
from utils import tag_training_data, get_upenn_tags_dict, parse_tags
else:
from .utils import tag_training_data, get_upenn_tags_dict, parse_tags
# Model Type 1: LSTM with 1-logit lookahead.
class SegmentorDataset(Dataset):
def __init__(self, datapoints):
self.datapoints = [(torch.from_numpy(k).float(), torch.tensor([t]).float()) for k, t in datapoints]
def __len__(self):
return len(self.datapoints)
def __getitem__(self, idx):
return self.datapoints[idx][0], self.datapoints[idx][1]
class RNN(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, device=None):
super(RNN, self).__init__()
if device == None:
if torch.cuda.is_available():
self.device = "cuda"
else:
self.device = "cpu"
else:
self.device = device
self.num_layers = num_layers
self.hidden_size = hidden_size
self.rnn = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True)
self.fc = nn.Linear(hidden_size, 1)
def forward(self, x):
h0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, device=self.device)
c0 = torch.zeros(self.num_layers, x.size(0), self.hidden_size, device=self.device)
out, _ = self.rnn(x, (h0, c0))
out = out[:, -1, :]
out = self.fc(out)
return out
# Model 2: Bidirectional LSTM with entire sequence context (hopefully)
class SegmentorDatasetDirectTag(Dataset):
def __init__(self, document_root: str):
self.tags_dict = get_upenn_tags_dict()
self.datapoints = []
self.eye = np.eye(len(self.tags_dict))
files = listdir(document_root)
for f in files:
if f.endswith(".txt"):
fname = join(document_root, f)
print(f"Loaded datafile: {fname}")
reconstructed_tags = tag_training_data(fname)
input, tag = parse_tags(reconstructed_tags)
self.datapoints.append((
np.array(input),
np.array(tag)
))
def __len__(self):
return len(self.datapoints)
def __getitem__(self, idx):
item = self.datapoints[idx]
return torch.from_numpy(self.eye[item[0]]).float(), torch.from_numpy(item[1]).float()
# The same dataset without one-hot embedding of the input.
class SegmentorDatasetNonEmbed(Dataset):
@staticmethod
def read_file(f: str, document_root: str):
if f.endswith(".txt"):
fname = join(document_root, f)
print(f"Loaded datafile: {fname}")
reconstructed_tags = tag_training_data(fname)
input, tag = parse_tags(reconstructed_tags)
return [(
np.array(input),
np.array(tag)
)]
else:
return []
def __init__(self, document_root: str):
self.datapoints = []
files = listdir(document_root)
with concurrent.futures.ProcessPoolExecutor() as pool:
out = pool.map(SegmentorDatasetNonEmbed.read_file, files, itertools.repeat(document_root))
self.datapoints = list(itertools.chain.from_iterable(out))
# for f in files:
# if f.endswith(".txt"):
# fname = join(document_root, f)
# print(f"Loaded datafile: {fname}")
# reconstructed_tags = tag_training_data(fname)
# input, tag = parse_tags(reconstructed_tags)
# self.datapoints.append((
# np.array(input),
# np.array(tag)
# ))
def __len__(self):
return len(self.datapoints)
def __getitem__(self, idx):
item = self.datapoints[idx]
return torch.from_numpy(item[0]).int(), torch.from_numpy(item[1]).float()
class BidirLSTMSegmenter(nn.Module):
def __init__(self, input_size, hidden_size, num_layers, device = None):
super(BidirLSTMSegmenter, self).__init__()
if device == None:
if torch.cuda.is_available():
self.device = "cuda"
else:
self.device = "cpu"
else:
self.device = device
self.num_layers = num_layers
self.hidden_size = hidden_size
self.rnn = nn.LSTM(input_size, hidden_size, num_layers, batch_first=True, bidirectional=True, device = self.device)
self.fc = nn.Linear(2*hidden_size, 1, device = self.device)
self.final = nn.Sigmoid()
def forward(self, x):
h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size, device=self.device)
c0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size, device=self.device)
out, _ = self.rnn(x, (h0, c0))
# out_fced = [torch.zeros((out.shape[0], out.shape[1]), device=device)]
# # Shape of out: [batch, seq_length, 256 (num_directions * hidden_size)]
# for i in range(out.shape[1]):
# out_fced[:, i] = self.fc(out[:, i, :])[0]
out_fced = self.fc(out)[:, :, 0]
# Shape of out:
return self.final(out_fced)
class BidirLSTMSegmenterWithEmbedding(nn.Module):
def __init__(self, input_size, embedding_size, hidden_size, num_layers, device = None):
super(BidirLSTMSegmenterWithEmbedding, self).__init__()
if device == None:
if torch.cuda.is_available():
self.device = "cuda"
else:
self.device = "cpu"
else:
self.device = device
self.num_layers = num_layers
self.hidden_size = hidden_size
self.embedding_size = embedding_size
self.embedding = nn.Embedding(input_size, embedding_dim=embedding_size, device = self.device)
self.rnn = nn.LSTM(embedding_size, hidden_size, num_layers, batch_first=True, bidirectional=True, device = self.device)
self.fc = nn.Linear(2*hidden_size, 1, device = self.device)
self.final = nn.Sigmoid()
def forward(self, x):
h0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size, device=self.device)
c0 = torch.zeros(self.num_layers * 2, x.size(0), self.hidden_size, device=self.device)
embedded = self.embedding(x)
out, _ = self.rnn(embedded, (h0, c0))
# out_fced = [torch.zeros((out.shape[0], out.shape[1]), device=device)]
# # Shape of out: [batch, seq_length, 256 (num_directions * hidden_size)]
# for i in range(out.shape[1]):
# out_fced[:, i] = self.fc(out[:, i, :])[0]
out_fced = self.fc(out)[:, :, 0]
# Shape of out:
return self.final(out_fced)
def collate_fn_padd(batch):
'''
Padds batch of variable length
note: it converts things ToTensor manually here since the ToTensor transform
assume it takes in images rather than arbitrary tensors.
'''
## get sequence lengths
inputs = [i[0] for i in batch]
tags = [i[1] for i in batch]
padded_input = torch.nn.utils.rnn.pad_sequence(inputs, batch_first=True)
combined_outputs = torch.nn.utils.rnn.pad_sequence(tags, batch_first=True)
## compute mask
return (padded_input, combined_outputs)
def get_dataloader(dataset: SegmentorDataset, batch_size):
return DataLoader(dataset, batch_size=batch_size, shuffle=True, collate_fn=collate_fn_padd)
def train_model(model: RNN,
dataset,
lr = 1e-3,
num_epochs = 3,
batch_size = 100,
):
train_loader = get_dataloader(dataset, batch_size=batch_size)
n_total_steps = len(train_loader)
criterion = nn.MSELoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
device = model.device
for epoch in range(num_epochs):
for i, (input, tags) in enumerate(train_loader):
input = input.to(device)
tags = tags.to(device)
outputs = model(input)
loss = criterion(outputs, tags)
optimizer.zero_grad()
loss.backward()
optimizer.step()
if i%100 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss [{loss.item():.4f}]")
def train_bidirlstm_model(model: BidirLSTMSegmenter,
dataset: SegmentorDatasetDirectTag,
lr = 1e-3,
num_epochs = 3,
batch_size = 1,
):
train_loader = get_dataloader(dataset, batch_size=batch_size)
n_total_steps = len(train_loader)
criterion = nn.BCELoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
device = model.device
for epoch in range(num_epochs):
for i, (input, tags) in enumerate(train_loader):
input = input.to(device)
tags = tags.to(device)
optimizer.zero_grad()
outputs = model(input)
loss = criterion(outputs, tags)
loss.backward()
optimizer.step()
if i%10 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss [{loss.item():.4f}]")
def train_bidirlstm_embedding_model(model: BidirLSTMSegmenterWithEmbedding,
dataset: SegmentorDatasetNonEmbed,
lr = 1e-3,
num_epochs = 3,
batch_size = 1,
):
train_loader = get_dataloader(dataset, batch_size=batch_size)
n_total_steps = len(train_loader)
criterion = nn.BCELoss()
optimizer = torch.optim.AdamW(model.parameters(), lr=lr)
device = model.device
for epoch in range(num_epochs):
for i, (input, tags) in enumerate(train_loader):
input = input.to(device)
tags = tags.to(device)
optimizer.zero_grad()
outputs = model(input)
loss = criterion(outputs, tags)
loss.backward()
optimizer.step()
if i%10 == 0:
print(f"Epoch [{epoch+1}/{num_epochs}], Step [{i+1}/{n_total_steps}], Loss [{loss.item():.4f}]")
|