Repository: dmlc/gluon-nlp

Issue:
TextCNN rand model downloads pretrained vectors even if not needed
1. TextCNN rand model downloads pretrained word vectors even if not needed. rand model initializes word vectors randomly and updates in the training process. So, I think there is not need to download word vectors for this model. 

2. There should be a flag passed to ```_build_vocab```  function representing the type of model. If the model is other than rand, then pretrained vectors should be downloaded.





Discussion:
@leezu @eric-haibin-lin your thoughts on this?
@leezu I have code ready for this and iam able to reproduce the results as well. What do you think?
Yes, your proposal is sensible. Would you like to open a PR? Thank you.
Sure, will submit PR to this

Resolving patch:
diff --git a/scripts/sentiment_analysis/process_data.py b/scripts/sentiment_analysis/process_data.py
--- a/scripts/sentiment_analysis/process_data.py
+++ b/scripts/sentiment_analysis/process_data.py
@@ -80,7 +80,7 @@ def _clean_str(string, data_name):
         return string.strip() if data_name == 'TREC' else string.strip().lower()
 
 
-def _build_vocab(data_name, train_dataset, test_dataset, dev_dataset):
+def _build_vocab(data_name, train_dataset, test_dataset, dev_dataset, model_name):
     all_token = []
     max_len = 0
     for dataset in (train_dataset, dev_dataset, test_dataset):
@@ -89,7 +89,13 @@ def _build_vocab(data_name, train_dataset, test_dataset, dev_dataset):
             max_len = max_len if max_len > len(line) else len(line)
             all_token.extend(line)
     vocab = nlp.Vocab(nlp.data.count_tokens(all_token))
-    vocab.set_embedding(nlp.embedding.create('Word2Vec', source='GoogleNews-vectors-negative300'))
+    if(model_name == 'rand'):
+        emb = nlp.embedding.TokenEmbedding()
+        emb[emb.unknown_token] = nd.zeros(300)
+        vocab.set_embedding(emb)
+    else:
+        vocab.set_embedding(nlp.embedding.create('Word2Vec',
+                                                 source='GoogleNews-vectors-negative300'))
     for word in vocab.embedding._idx_to_token:
         if (vocab.embedding[word] == nd.zeros(300)).sum() == 300:
             vocab.embedding[word] = nd.random.uniform(0, 0.05, 300)
@@ -118,23 +124,24 @@ def _preprocess_dataset(dataset, vocab, max_len):
     return dataset, lengths
 
 
-def load_dataset(data_name):
+def load_dataset(data_name, model_name):
     """Load sentiment dataset."""
     if data_name in ('MR', 'Subj', 'CR', 'MPQA'):
         train_dataset, output_size = _load_file(data_name)
-        vocab, max_len = _build_vocab(data_name, train_dataset, [], [])
+        vocab, max_len = _build_vocab(data_name, train_dataset, [], [], model_name)
         train_dataset, train_data_lengths = _preprocess_dataset(train_dataset, vocab, max_len)
         return vocab, max_len, output_size, train_dataset, train_data_lengths
     elif data_name == 'TREC':
         train_dataset, test_dataset, output_size = _load_file(data_name)
-        vocab, max_len = _build_vocab(data_name, train_dataset, test_dataset, [])
+        vocab, max_len = _build_vocab(data_name, train_dataset, test_dataset, [], model_name)
         train_dataset, train_data_lengths = _preprocess_dataset(train_dataset, vocab, max_len)
         test_dataset, test_data_lengths = _preprocess_dataset(test_dataset, vocab, max_len)
         return vocab, max_len, output_size, train_dataset, train_data_lengths, test_dataset, \
                test_data_lengths
     else:
         train_dataset, test_dataset, dev_dataset, output_size = _load_file(data_name)
-        vocab, max_len = _build_vocab(data_name, train_dataset, test_dataset, dev_dataset)
+        vocab, max_len = _build_vocab(data_name, train_dataset, test_dataset, dev_dataset,
+                                      model_name)
         train_dataset, train_data_lengths = _preprocess_dataset(train_dataset, vocab, max_len)
         test_dataset, test_data_lengths = _preprocess_dataset(test_dataset, vocab, max_len)
         dev_dataset, dev_data_lengths = _preprocess_dataset(dev_dataset, vocab, max_len)
diff --git a/scripts/sentiment_analysis/sentiment_analysis_cnn.py b/scripts/sentiment_analysis/sentiment_analysis_cnn.py
--- a/scripts/sentiment_analysis/sentiment_analysis_cnn.py
+++ b/scripts/sentiment_analysis/sentiment_analysis_cnn.py
@@ -28,7 +28,6 @@
 import argparse
 import time
 import random
-import numpy as np
 
 import mxnet as mx
 from mxnet import nd, gluon, autograd
@@ -39,9 +38,8 @@
 
 gluonnlp.utils.check_version('0.7.0')
 
-np.random.seed(3435)
-random.seed(3435)
-mx.random.seed(3435)
+seed = 3435
+gluonnlp.utils.set_seed(seed)
 
 parser = argparse.ArgumentParser(description='Sentiment analysis with the textCNN model on\
                                  various datasets.')
@@ -72,13 +70,14 @@
 
 if args.data_name in ('MR', 'Subj', 'CR', 'MPQA'):
     vocab, max_len, output_size, train_dataset, train_data_lengths \
-    = process_data.load_dataset(args.data_name)
+    = process_data.load_dataset(args.data_name, args.model_mode)
 elif args.data_name == 'TREC':
     vocab, max_len, output_size, train_dataset, train_data_lengths, \
-    test_dataset, test_data_lengths = process_data.load_dataset(args.data_name)
+    test_dataset, test_data_lengths = process_data.load_dataset(args.data_name, args.model_mode)
 else:
     vocab, max_len, output_size, train_dataset, train_data_lengths, test_dataset, \
-    test_data_lengths, dev_dataset, dev_data_lengths = process_data.load_dataset(args.data_name)
+    test_data_lengths, dev_dataset, dev_data_lengths = process_data.load_dataset(args.data_name,
+                                                                                 args.model_mode)
 
 model = text_cnn.model(args.dropout, vocab, args.model_mode, output_size)
 print(model)


Test patch: