mirror of
https://github.com/wassname/Castor.git
synced 2026-10-05 12:20:28 +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 * Add ignore_lengths option to ReutersTrainer and ReutersEvaluator * Add AAPDCharQuantized and ReutersCharQuantized * Rename Reuters_hierarchical to ReutersHierarchical * Add CharacterCNN for document classification * Update README.md for CharacterCNN * Fix table in README.md for CharacterCNN * Add AAPDHierarchical for HAN * Update HAN for changes in Reuters dataset endpoints * Fix bug in CharCNN when running on CPU * Add AAPD dataset support for KimCNN * Fix dataset paths for SST-1 * Fix dimensions of FC1 in CharCNN * Add model checkpointing for Reuters based on F1 * Refactor LSTM baseline __main__ * Add precision, recall and F1 to Reuters evaluator
46 lines
1.9 KiB
Python
46 lines
1.9 KiB
Python
import torch
|
|
import torch.nn as nn
|
|
|
|
import torch.nn.functional as F
|
|
|
|
|
|
class CharCNN(nn.Module):
|
|
def __init__(self, config):
|
|
super(CharCNN, self).__init__()
|
|
self.is_cuda_enabled = config.cuda
|
|
dataset = config.dataset
|
|
num_conv_filters = config.num_conv_filters
|
|
output_channel = config.output_channel
|
|
num_affine_neurons = config.num_affine_neurons
|
|
target_class = config.target_class
|
|
input_channel = 68
|
|
|
|
self.conv1 = nn.Conv1d(input_channel, num_conv_filters, kernel_size=7) # Default padding=0
|
|
self.conv2 = nn.Conv1d(num_conv_filters, num_conv_filters, kernel_size=7)
|
|
self.conv3 = nn.Conv1d(num_conv_filters, num_conv_filters, kernel_size=3)
|
|
self.conv4 = nn.Conv1d(num_conv_filters, num_conv_filters, kernel_size=3)
|
|
self.conv5 = nn.Conv1d(num_conv_filters, num_conv_filters, kernel_size=3)
|
|
self.conv6 = nn.Conv1d(num_conv_filters, output_channel, kernel_size=3)
|
|
self.dropout = nn.Dropout(config.dropout)
|
|
self.fc1 = nn.Linear(output_channel, num_affine_neurons)
|
|
self.fc2 = nn.Linear(num_affine_neurons, num_affine_neurons)
|
|
self.fc3 = nn.Linear(num_affine_neurons, target_class)
|
|
|
|
def forward(self, x, **kwargs):
|
|
if torch.cuda.is_available() and self.is_cuda_enabled:
|
|
x = x.transpose(1, 2).type(torch.cuda.FloatTensor)
|
|
else:
|
|
x = x.transpose(1, 2).type(torch.FloatTensor)
|
|
x = F.max_pool1d(F.relu(self.conv1(x)), 3)
|
|
x = F.max_pool1d(F.relu(self.conv2(x)), 3)
|
|
x = F.relu(self.conv3(x))
|
|
x = F.relu(self.conv4(x))
|
|
x = F.relu(self.conv5(x))
|
|
x = F.relu(self.conv6(x))
|
|
x = F.max_pool1d(x, x.size(2)).squeeze(2)
|
|
x = F.relu(self.fc1(x.view(x.size(0), -1)))
|
|
x = self.dropout(x)
|
|
x = F.relu(self.fc2(x))
|
|
x = self.dropout(x)
|
|
return self.fc3(x)
|