From 491f0b32d59ef944cf6f5ea37b27c0d4886a2fde Mon Sep 17 00:00:00 2001 From: rosequ Date: Thu, 12 Oct 2017 22:54:45 -0400 Subject: [PATCH] Updated sm (#72) + removed redundant loss regularization + added script to create torch word embedding file from word2vec model + updated README --- sm_modified_cnn/README.md | 19 +++++++++++++++++-- sm_modified_cnn/requirements.txt | 1 + sm_modified_cnn/train.py | 15 --------------- sm_modified_cnn/utils.py | 27 ++++++++++----------------- 4 files changed, 28 insertions(+), 34 deletions(-) diff --git a/sm_modified_cnn/README.md b/sm_modified_cnn/README.md index 173e5ec..f9bfc3d 100644 --- a/sm_modified_cnn/README.md +++ b/sm_modified_cnn/README.md @@ -11,12 +11,16 @@ Retrieval (SIGIR '15). ACM, New York, NY, USA, 373-382. DOI: http://dx.doi.org/1 nltk==3.2.2 numpy==1.11.3 pytorch==0.1.12 +gensim==1.0.1 ``` The code uses torchtext for text processing. Set torchtext: ```bash git clone https://github.com/pytorch/text.git cd text + +#use this commit number +git reset --hard 2980f1bc39ba6af332c5c2783da8bee109796d4c python setup.py install ``` @@ -25,8 +29,9 @@ We use `trec_eval` for evaluation: ```bash cd eval tar -xvf trec_eval.9.0.tar.gz +cd trec_eval.9.0 make -cd .. +cd ../.. ``` @@ -131,4 +136,14 @@ Metric |rand |static|non-static|multichannel MAP |0.6313 |0.6378|0.6455 |0.6476 MRR |0.6522 |0.6542|0.6689 |0.6646 -NB: The results on WikiQA are based on the SM model hyperparameters. \ No newline at end of file +NB: The results on WikiQA are based on the SM model hyperparameters. + + +### To create your own word2vec.pt file + ++ Download word2vec from [here](https://drive.google.com/drive/u/0/folders/0B-yipfgecoSBfkZlY2FFWEpDR3M4Qkw5U055MWJrenE5MTBFVXlpRnd0QjZaMDQxejh1cWs) +to the `data/` folder + +```bash +python utils.py --input data/aquaint+wiki.txt.gz.ndim=50.bin +``` \ No newline at end of file diff --git a/sm_modified_cnn/requirements.txt b/sm_modified_cnn/requirements.txt index 00d0293..a5fbe01 100644 --- a/sm_modified_cnn/requirements.txt +++ b/sm_modified_cnn/requirements.txt @@ -1,3 +1,4 @@ nltk==3.2.1 numpy==1.11.3 +gensim==1.0.1 pytorch==0.1.12 diff --git a/sm_modified_cnn/train.py b/sm_modified_cnn/train.py index 1f6a601..5f41de2 100644 --- a/sm_modified_cnn/train.py +++ b/sm_modified_cnn/train.py @@ -31,24 +31,10 @@ def set_vectors(field, vector_path): # initialize with U(-0.25, 0.25) vectors field.vocab.vectors[i] = torch.FloatTensor(dim).uniform_(-0.25, 0.25) else: - print("Error: Need word embedding pt file") print("Error: Need word embedding pt file") exit(1) return field - -def regularize_loss(model, loss): - flattened_params = [] - reg = args.weight_decay - - for p in model.parameters(): - f = p.data.clone() - flattened_params.append(f.view(-1)) - - fp = torch.cat(flattened_params) - loss = loss + 0.5 * reg * fp.norm() * fp.norm() - return loss - # Set default configuration in : args.py args = get_args() config = args @@ -165,7 +151,6 @@ while True: train_acc = 100. * n_correct / n_total loss = criterion(scores, batch.label) - loss = regularize_loss(model, loss) loss.backward() optimizer.step() diff --git a/sm_modified_cnn/utils.py b/sm_modified_cnn/utils.py index d53753d..f83dd93 100644 --- a/sm_modified_cnn/utils.py +++ b/sm_modified_cnn/utils.py @@ -1,30 +1,23 @@ from tqdm import tqdm -import array import torch -import numpy as np +from gensim.models.keyedvectors import KeyedVectors from argparse import ArgumentParser -def convert(fname, vocab): - save_file = '{}.pt'.format(fname) - stoi, vectors, dim = [], array.array('d'), None - - # TODO: fix by reading the .dimensions file - vocab_size, dim = 2470719, 50 - W = np.memmap(fname, dtype=np.double, shape=(vocab_size, dim)) +def convert(fname, save_file): + with open(fname, 'rb') as dim_file: + vocab_size, dim = (int(x) for x in dim_file.readline().split()) + word_vectors = KeyedVectors.load_word2vec_format(fname, binary=True) print("Loading vectors from {}".format(fname)) vectors = [] - for line in tqdm(W, total=len(W)): - entry = line - vectors.extend(entry) - + for line in tqdm(word_vectors.syn0, total=len(word_vectors.syn0)): + vectors.extend(line.tolist()) vectors = torch.Tensor(vectors).view(-1, dim) - with open(vocab) as f: - stoi = {word.strip():i for i, word in enumerate(f)} + stoi = {word.strip():voc.index for word, voc in word_vectors.vocab.items()} print('saving vectors to', save_file) torch.save((stoi, vectors, dim), save_file) @@ -32,7 +25,7 @@ def convert(fname, vocab): if __name__ == '__main__': parser = ArgumentParser(description='create word embedding') parser.add_argument('--input', type=str, required=True) - parser.add_argument('--vocab', type=str, required=True) + parser.add_argument('--output', type=str, default='data/word2vec.trecqa.pt') args = parser.parse_args() - convert(args.input, args.vocab) + convert(args.input, args.output) \ No newline at end of file