-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlstm_wordvec.py
More file actions
134 lines (104 loc) · 3.97 KB
/
Copy pathlstm_wordvec.py
File metadata and controls
134 lines (104 loc) · 3.97 KB
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import numpy as np
from keras.datasets import imdb
from keras.models import Sequential
from keras.layers import Dense
from keras.layers import LSTM
from keras.layers.embeddings import Embedding
from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer
from keras.preprocessing.sequence import pad_sequences
import time, os
from keras.utils import to_categorical
import gensim
dimension = 300
model_wordvec = gensim.models.KeyedVectors.load_word2vec_format('google_word_vec/GoogleNews-vectors-negative300.bin', binary=True)
print("Model loaded!")
word_vectors = model_wordvec.wv
Y_train = np.concatenate((np.full(12500, 0), np.full(12500, 1)))
Y_test = Y_train
print("\nCreating average vectors..")
start_time = time.time()
roots = ["aclImdb/train/neg", "aclImdb/train/pos"]
# Y_train_neg = np.full(len(os.listdir(root)), 1)
texts_train = []
labels_index_train = {}
labels_train = []
for root in roots:
i = 0
for filename in os.listdir(root):
with open(root+"/"+filename) as f:
if i%5000 ==0:
print(i)
i += 1
label_id = len(labels_index_train)
labels_index_train[filename] = label_id
raw = f.read()
texts_train.append(raw)
labels_train.append(label_id)
roots = ["aclImdb/test/neg", "aclImdb/test/pos"]
texts_test = []
labels_index_test = {}
labels_test = []
for root in roots:
i = 0
for filename in os.listdir(root):
with open(root+"/"+filename) as f:
if i%5000 ==0:
print(i)
i += 1
label_id = len(labels_index_test)
labels_index_test[filename] = label_id
raw = f.read()
texts_test.append(raw)
labels_test.append(label_id)
print("Average vectors calculated in %d Seconds" % int(time.time()-start_time))
MAX_NB_WORDS = 5000
MAX_SEQUENCE_LENGTH = 300
tokenizer = Tokenizer(num_words=MAX_NB_WORDS) #nb_words=MAX_NB_WORDS
tokenizer.fit_on_texts(texts_train)
sequences = tokenizer.texts_to_sequences(texts_train)
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
X_train = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH, padding="post")
Y_train = to_categorical(Y_train, num_classes=2)
print('Shape of data tensor:', X_train.shape)
print('Shape of label tensor:', Y_train.shape)
embedding_dimension = 300
embedding_matrix = np.zeros((len(word_index) + 1, embedding_dimension))
for word, i in word_index.items():
if word in word_vectors.vocab:
embedding_matrix[i] = model_wordvec[word.lower()]
embedding_layer = Embedding(len(word_index) + 1,
embedding_dimension,
weights=[embedding_matrix],
input_length=MAX_SEQUENCE_LENGTH,
trainable=False)
# sequence_input = Input(shape=(MAX_SEQUENCE_LENGTH,), dtype='int32')
# embedded_sequences = embedding_layer(sequence_input)
lstm_out = 5
model = Sequential()
model.add(embedding_layer)
#Add Embedding Layer
model.add(LSTM(lstm_out,dropout_U=0.2,dropout_W=0.2))
model.add(Dense(2,activation='softmax'))
#Add hidden layer
model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])
#Create LSTM model
print(model.summary())
batch_size = 50
model.fit(X_train, Y_train, epochs = 10, batch_size=batch_size, verbose = 1)
#Train the model
#Predict accuracy
#Test
tokenizer = Tokenizer(nb_words=MAX_NB_WORDS) #nb_words=MAX_NB_WORDS
tokenizer.fit_on_texts(texts_test)
sequences = tokenizer.texts_to_sequences(texts_test)
word_index = tokenizer.word_index
print('Found %s unique tokens.' % len(word_index))
X_test = pad_sequences(sequences, maxlen=MAX_SEQUENCE_LENGTH)
Y_test = to_categorical(Y_test, num_classes=2)
# labels_test = to_categorical(np.asarray(labels_test))
print('Shape of data tensor:', X_test.shape)
print('Shape of label tensor:', Y_test.shape)
score,acc = model.evaluate(X_test, Y_test, verbose = 1, batch_size = batch_size)
print(acc)