mirror of
https://github.com/wassname/Castor.git
synced 2026-09-09 11:13:20 +08:00
Updated sm (#72)
+ removed redundant loss regularization + added script to create torch word embedding file from word2vec model + updated README
This commit is contained in:
@@ -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.
|
||||
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
|
||||
```
|
||||
@@ -1,3 +1,4 @@
|
||||
nltk==3.2.1
|
||||
numpy==1.11.3
|
||||
gensim==1.0.1
|
||||
pytorch==0.1.12
|
||||
|
||||
@@ -31,24 +31,10 @@ def set_vectors(field, vector_path):
|
||||
# initialize <unk> 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()
|
||||
|
||||
|
||||
+10
-17
@@ -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)
|
||||
Reference in New Issue
Block a user