forked from textclf/fancy-cnn
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprepare_imdb_new.py
67 lines (52 loc) · 2.67 KB
/
prepare_imdb_new.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
"""
prepare-imdb.py
description: prepare the imdb data for training in DNNs
"""
from datahandlers import ImdbDataHandler
import cPickle as pickle
import logging
import numpy as np
from wordvectors.glove import GloVeBox
LOGGER_PREFIX = ' %s'
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
def log(msg, logger=logger):
logger.info(LOGGER_PREFIX % msg)
IMDB_DATA = './datasets/aclImdb/aclImdb'
IMDB_WV_FILE = './data/wv/IMDB-GloVe-300dim.txt'
GLOBAL_WV_FILE = './data/wv/glove.42B.300d.120000.txt'
WORDS_PER_SENTENCE = 50
SENTENCES_PER_PARAGRAPH = 50
PREPEND = False
if __name__ == '__main__':
log('Building word vectors from {}'.format(IMDB_WV_FILE))
gb = GloVeBox(IMDB_WV_FILE)
gb.build(zero_token=True, normalize_variance=False, normalize_norm=True)
log('Building global word vectors from {}'.format(GLOBAL_WV_FILE))
global_gb = GloVeBox(GLOBAL_WV_FILE)
global_gb.build(zero_token=True, normalize_variance=False, normalize_norm=True)
log('writing GloVeBox pickle...')
pickle.dump(gb, open(IMDB_WV_FILE.replace('.txt', '-glovebox.pkl'), 'wb'), pickle.HIGHEST_PROTOCOL)
pickle.dump(global_gb, open(GLOBAL_WV_FILE.replace('.txt', '-glovebox.pkl'), 'wb'), pickle.HIGHEST_PROTOCOL)
log('Load data from original source')
imdb = ImdbDataHandler(source=IMDB_DATA)
(train_reviews, train_labels) = imdb.get_data(type=ImdbDataHandler.DATA_TRAIN)
(test_reviews, test_labels) = imdb.get_data(type=ImdbDataHandler.DATA_TEST)
log('Converting to sentences: global word vectors')
train_global_wvs_reviews = imdb.to_sentence_vectors(train_reviews, SENTENCES_PER_PARAGRAPH,
WORDS_PER_SENTENCE, global_gb)
test_global_wvs_reviews = imdb.to_sentence_vectors(test_reviews, SENTENCES_PER_PARAGRAPH,
WORDS_PER_SENTENCE, global_gb)
log('Converting to sentences: only imdb word vectors')
train_imdb_wvs_reviews = imdb.to_sentence_vectors(train_reviews, SENTENCES_PER_PARAGRAPH,
WORDS_PER_SENTENCE, gb)
test_imdb_wvs_reviews = imdb.to_sentence_vectors(test_reviews, SENTENCES_PER_PARAGRAPH,
WORDS_PER_SENTENCE, gb)
# -- training data save
np.save('IMDB_train_glove_X.npy', train_imdb_wvs_reviews)
np.save('IMDB_train_global_glove_X.npy', train_global_wvs_reviews)
np.save('IMDB_train_glove_y.npy', train_labels)
# -- testing data save
np.save('IMDB_test_glove_X.npy', test_imdb_wvs_reviews)
np.save('IMDB_test_global_glove_X.npy', test_global_wvs_reviews)
np.save('IMDB_test_glove_y.npy', test_labels)