mirror of
https://github.com/wassname/Castor.git
synced 2026-09-21 12:30:20 +08:00
* Add ReutersTrainer, ReutersEvaluator options in Factory classes * Add Reuters to Kim-CNN command line arguments * Fix SST dataset path according to changes in Kim-CNN args The dataset path in args.py was made to point at the dataset folder rather than dataset/SST folder. Hence SST folder was added to paths in the SST dataset class * Add Reuters dataset class, and support in __main__ * Add Reuters dataset trainers and evaluators * Remove debug print statement in reuters_evaluator * Fix rounding bug in reuters_trainer and reuters_evaluator * Add LSTM for baseline text classification measurements * Add eval metrics for lstm_baseline * Set batch_first param in lstm_baseline * Remove onnx args from lstm_baseline * Pack padded sequences in LSTM_baseline * Add TensorBoardX support for Reuters trainer * Add Arxiv Academic Paper Dataset (AAPD) * Add Hidden Bottleneck Layer to BiLSTM * Fix packing of padded tensors in Reuters * Add cmdline args for Hidden Bottleneck Layer for BiLSTM * Include pre-padding lengths in AAPD dataset * Remove duplication of preprocessing code in AAPD * Remove batch_size condition in ReutersTrainer
67 lines
2.7 KiB
Python
67 lines
2.7 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
class LSTMBaseline(nn.Module):
|
|
def __init__(self, config):
|
|
super(LSTMBaseline, self).__init__()
|
|
dataset = config.dataset
|
|
target_class = config.target_class
|
|
self.is_bidirectional = config.bidirectional
|
|
self.has_bottleneck_layer = config.bottleneck_layer
|
|
self.mode = config.mode
|
|
|
|
input_channel = 1
|
|
if config.mode == 'rand':
|
|
rand_embed_init = torch.Tensor(config.words_num, config.words_dim).uniform_(-0.25, 0.25)
|
|
self.embed = nn.Embedding.from_pretrained(rand_embed_init, freeze=False)
|
|
elif config.mode == 'static':
|
|
self.static_embed = nn.Embedding.from_pretrained(dataset.TEXT_FIELD.vocab.vectors, freeze=True)
|
|
elif config.mode == 'non-static':
|
|
self.non_static_embed = nn.Embedding.from_pretrained(dataset.TEXT_FIELD.vocab.vectors, freeze=False)
|
|
else:
|
|
print("Unsupported Mode")
|
|
exit()
|
|
|
|
self.lstm = nn.LSTM(config.words_dim, config.hidden_dim, dropout=config.dropout, num_layers=config.num_layers,
|
|
bidirectional=self.is_bidirectional, batch_first=True)
|
|
self.dropout = nn.Dropout(config.dropout)
|
|
if self.has_bottleneck_layer:
|
|
if self.is_bidirectional:
|
|
self.fc1 = nn.Linear(2 * config.hidden_dim, config.hidden_dim) # Hidden Bottleneck Layer
|
|
self.fc2 = nn.Linear(config.hidden_dim, target_class)
|
|
else:
|
|
self.fc1 = nn.Linear(config.hidden_dim, config.hidden_dim//2) # Hidden Bottleneck Layer
|
|
self.fc2 = nn.Linear(config.hidden_dim//2, target_class)
|
|
else:
|
|
if self.is_bidirectional:
|
|
self.fc1 = nn.Linear(2 * config.hidden_dim, target_class)
|
|
else:
|
|
self.fc1 = nn.Linear(config.hidden_dim, target_class)
|
|
|
|
def forward(self, x, lengths=None):
|
|
if self.mode == 'rand':
|
|
x = self.embed(x)
|
|
elif self.mode == 'static':
|
|
x = self.static_embed(x)
|
|
elif self.mode == 'non-static':
|
|
x = self.non_static_embed(x)
|
|
else:
|
|
print("Unsupported Mode")
|
|
exit()
|
|
if lengths is not None:
|
|
x = torch.nn.utils.rnn.pack_padded_sequence(x, lengths, batch_first=True)
|
|
x, _ = self.lstm(x)
|
|
if lengths is not None:
|
|
x, _ = torch.nn.utils.rnn.pad_packed_sequence(x, batch_first=True)
|
|
x = F.relu(torch.transpose(x, 1, 2))
|
|
x = F.max_pool1d(x, x.size(2)).squeeze(2)
|
|
x = self.dropout(x)
|
|
if self.has_bottleneck_layer:
|
|
x = F.relu(self.fc1(x))
|
|
return self.fc2(x)
|
|
else:
|
|
return self.fc1(x)
|