GithubHelp home page GithubHelp logo

hitluobin / sent-conv-torch Goto Github PK

View Code? Open in Web Editor NEW

This project forked from harvardnlp/sent-conv-torch

0.0 2.0 0.0 58.84 MB

Text classification using a convolutional neural network.

License: MIT License

Jupyter Notebook 28.43% Shell 1.60% Lua 53.04% Python 16.93%

sent-conv-torch's Introduction

Sentence Convolution Code in Torch

This code implements Kim (2014) sentence convolution code in Torch with GPUs. It replicates the results on existing datasets, and allows training of models on arbitrary other text datasets.

Quickstart

To make data in hdf5 format, run the following (with word2vec .bin path and choice of dataset):

python preprocess.py MR /path/to/word2vec.bin

To run training with GPUs:

th main.lua -data MR.hdf5 -cudnn 1

Results are timestamped and saved to the results/ directory.

Dependencies

The training pipeline requires Python hdf5 (the h5py module) and the following lua packages:

  • hdf5
  • cudnn

Training on word2vec architecture models requires downloading word2vec and unzipping. Simply run the script

./get_word2vec.sh

Creating datasets

We provide the following datasets: MR, SST1, SST2, SUBJ, TREC, CR, MPQA. All raw training data is located in the data/ directory. The SST1, SST2 data have both test and dev sets, and TREC has a test set.

The data takes word2vec embeddings, processes the vocabulary, and outputs a data matrix of vocabulary indices for each sentence.

To create the hdf5 file, run the following with DATASET as one of the described datasets:

python preprocess.py DATASET /path/to/word2vec.bin

The script outputs:

  • the DATASET.hdf5 file with the data matrix and word2vec embeddings
  • a DATASET.txt file with a word-index dictionary for the word embeddings

Training on custom datasets

We allow training on arbitrary text datasets. They should be formatted in the same way as the sample data, with one sentence per line, and the first word the class label (0-indexed). Our code handles most parsing of punctuation, possessives, capitalization, etc.

Example line:

1 no movement , no yuks , not much of anything .

Then run:

python preprocess.py custom /path/to/word2vec.bin --train /path/to/train/data --test /path/to/test/data --dev /path/to/dev/data

The output file's name can be set with the flag --custom_name (default is named custom).

Running torch

Training is typically done with 10-fold cross-validation and 25 epochs. If the data set comes with a test set, we don't do cross validation (but split training data 90/10 for the dev set). If the data comes with the dev set, we don't do the split for train/dev.

There are four main model architectures we implemented, as described in Kim (2014): rand, static, nonstatic, multichannel.

  • rand initializes the word embeddings randomly and learns them.
  • static initializes the word embeddings to word2vec and keeps the weight static.
  • nonstatic also initializes to word2vec, but allows them to be learned.
  • multichannel has two word2vec embedding layers, one static and one nonstatic. The two layers outputs are summed.

It is highly recommended that GPUs are used during training if possible (see Results section for timing benchmarks).

Separating out training and testing is easy; use the parameters -train_only and -test_only. Also, pretrained models at any stage can be loaded from a .t7 file with -warm_start_model (see more parameters below).

Output

When training is complete, the code outputs a file with name -savefile, with default TIMESTAMP_results.t7.

The following are saved as a table:

  • dev_scores with dev scores,
  • test scores with test scores,
  • opt with model parameters,
  • model with best model (as determined by cross-validation)
  • embeddings with the updated word embeddings

Model augmentations

A few modifications were made to the model architecture as experiments.

  • we include an option to include highway layers at the final MLP step (which increases depth of the model),
  • we also include highway layers at the convolutional step (which performs multiple convolutions on the resulting feature maps) as an option,
  • we experimented with skip kernels of size 5 (added in parallel with the other kernel sizes)

Results from these experiments are described below in the Results section.

Parameters

The following is a list of complete parameters allowed by the torch code.

  • model_type: Model architecture, as described above. Options: rand, static, nonstatic, multichannel
  • data: Training dataset to use, including word2vec data. This should be a .hdf5 file made with preprocess.py.
  • cudnn: Use GPUs if set to 1, otherwise set to 0
  • seed: Random seed, set to -1 for actual randomness
  • folds: Number of folds for cross-validation.
  • debug: Print debugging info including timing and confusions
  • savefile: Name of output .t7 file, which will hold the trained model. Default is TIMESTAMP_results
  • zero_indexing: Set to 1 if data is zero indexed
  • warm_start_model: Load a .t7 file with pretrained model. Should contain a table with key 'model'
  • train_only: Set to 1 to only train (no testing)
  • test_only: Given a .t7 file with model, test on testing data

Training hyperparameters:

  • num_epochs: Number of training epochs.
  • optim_method: Gradient descent method. Options: adadelta, adam
  • L2s: Set L2 norm of final linear layer weights to this.
  • batch_size: Batch size for training.

Model hyperparameters:

  • num_feat_maps: Number of convolution feature maps.
  • kernels: Kernel sizes of different convolutions.
  • dropout_p: Dropout probability.
  • highway_mlp: Number of highway MLP layers (0 for none)
  • highway_conv_layers: Number of highway convolutional layers (0 for none)
  • skip_kernel: Set 1 to use skip kernels

Results

The following results were collected with the same training setup as in Kim (2014) (same parameters, 10-fold cross validation if data has no test set, 25 epochs).

Scores

Dataset rand static nonstatic multichannel
MR 75.9 80.5 81.3 80.8
SST1 42.2 44.8 46.7 44.6
SST2 83.5 85.6 87.0 87.1
Subj 89.2 93.0 93.4 93.2
TREC 88.2 91.8 92.8 91.8
CR 78.3 83.3 84.4 83.7
MPQA 84.6 89.6 89.7 89.6

With 5 trials on SST1, we have a mean nonstatic score of 46.7 with standard deviation 1.69.

With 1 highway layer, SST1 achieves a mean score of mean 47.8, stddev 0.857, over 5 trials, and with 2 highway layers, mean 47.1, stddev 1.47, over 10 trials.

Timing

We ran timing benchmarks on SST1, which has train/dev/test data sizes of 156817/1101/2210. We used a batch size of 50.

| non-GPU | GPU --- | --- | --- per epoch | 2475 s | 54.0 s per batch | 787 ms | 15.6 ms

From these results, we see that using GPUs achieves almost a 50x speedup on training. This allows much faster tuning of parameters and model experimentation.

Relevant publications

This code is based on Kim (2014) and the corresponding Theano code.

Kim, Y. (2014). Convolutional Neural Networks for Sentence Classification. In Proceedings of the 2014 Conference on Empirical Methods in Natural Language Processing (EMNLP), pp. 1746โ€“1751, Doha, Qatar. Association for Computational Linguistics.

Srivastava, R. K., Greff, K., & Schmidhuber, J. (2015). Training very deep networks. In Advances in Neural Information Processing Systems (pp. 2368-2376).

sent-conv-torch's People

Contributors

jeffreyling avatar srush avatar

Watchers

James Cloos avatar LuoBin avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.