/
GRU_pretrained_GloVe.py
217 lines (177 loc) · 7.96 KB
/
GRU_pretrained_GloVe.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
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
import zipfile
import numpy as np
import tensorflow as tf
####################
# Hyper Parameters #
####################
path_to_glove = '../data/glove.840B.300d.zip'
PRE_TRAINED = True
GLOVE_SIZE = 300
batch_size = 128
embedding_dimension = 64
num_classes = 2
hidden_layer_size = 32
time_steps = 6
digit_to_word_map = {1: "One", 2: "Two", 3: "Three", 4: "Four", 5: "Five",
6: "Six", 7: "Seven", 8: "Eight", 9: "Nine"}
digit_to_word_map[0] = "PAD_TOKEN"
odd_sentences = []
even_sentences = []
seqlens = []
for i in range(10000):
rand_seq_len = np.random.choice(range(3, 7))
seqlens.append(rand_seq_len)
rand_odd_ints = np.random.choice(range(1, 10, 2), rand_seq_len)
rand_even_ints = np.random.choice(range(2, 10, 2), rand_seq_len)
# Padding
if rand_seq_len < 6:
rand_odd_ints = np.append(rand_odd_ints, [0]*(6-rand_seq_len))
rand_even_ints = np.append(rand_even_ints, [0]*(6-rand_seq_len))
odd_sentences.append(" ".join([digit_to_word_map[r] for r in rand_odd_ints]))
even_sentences.append(" ".join([digit_to_word_map[r] for r in rand_even_ints]))
data = odd_sentences + even_sentences
# 홀수, 짝수 시퀀스의 seq 길이 저장
seqlens*=2
# 원-핫 인코딩 작업
labels = [1]*10000 + [0]*10000
for i in range(len(labels)):
label = labels[i]
one_hot_encoding = [0]*2
one_hot_encoding[label] = 1
labels[i] = one_hot_encoding
# 단어를 인덱스에 매핑
word2index_map = {}
index = 0
for sent in data:
for word in sent.split():
if word not in word2index_map:
word2index_map[word] = index
index += 1
# 역방향 매핑
index2word_map = {index: word for word, index in word2index_map.items()}
vocabulary_size = len(index2word_map)
print('word2index_map :', word2index_map)
print('index2word_map :', index2word_map)
def get_glove(path_to_glove, word2index_map):
embedding_weights = {}
count_all_words = 0
with zipfile.ZipFile(path_to_glove) as z:
with z.open("glove.840B.300d.txt") as f:
for line in f:
vals = line.split()
word = str(vals[0].decode('utf-8'))
if word in word2index_map:
print(word)
count_all_words += 1
coefs = np.asarray(vals[1:], dtype='float32')
coefs /= np.linalg.norm(coefs)
embedding_weights[word] = coefs
if count_all_words == vocabulary_size-1:
break
return embedding_weights
word2embedding_dict = get_glove(path_to_glove, word2index_map)
print(word2embedding_dict['One'].shape)
embedding_matrix = np.zeros((vocabulary_size, GLOVE_SIZE))
for word, index in word2index_map.items():
if not word == "PAD_TOKEN":
word_embedding = word2embedding_dict[word]
embedding_matrix[index, :] = word_embedding
print('embedding_matrix.shape :', embedding_matrix.shape)
data_indices = list(range(len(data)))
np.random.shuffle(data_indices)
data = np.array(data)[data_indices]
labels = np.array(labels)[data_indices]
seqlens = np.array(seqlens)[data_indices]
train_x = data[:10000]
train_y = labels[:10000]
train_seqlens = seqlens[:10000]
test_x = data[10000:]
test_y = labels[10000:]
test_seqlens = seqlens[10000:]
def get_sentence_batch(batch_size, data_x, data_y, data_seqlens):
instance_indices = list(range(len(data_x)))
np.random.shuffle(instance_indices)
batch = instance_indices[:batch_size]
x = [[word2index_map[word] for word in data_x[i].split()]
for i in batch]
y = [data_y[i] for i in batch]
seqlens = [data_seqlens[i] for i in batch]
return x, y, seqlens
_inputs = tf.placeholder(tf.int32, shape=[batch_size, time_steps])
embedding_placeholder = tf.placeholder(tf.float32, [vocabulary_size, GLOVE_SIZE])
_labels = tf.placeholder(tf.float32, shape=[batch_size, num_classes])
# 동적 계산을 위한 seqlens
_seqlens = tf.placeholder(tf.int32, shape=[batch_size])
if PRE_TRAINED:
embeddings = tf.Variable(
tf.constant(0.0, shape=[vocabulary_size, GLOVE_SIZE]), trainable=True)
# 사전 학습된 임베딩을 사용한다면 임베딩 변수에 할당
embedding_init = embeddings.assign(embedding_placeholder)
embed = tf.nn.embedding_lookup(embeddings, _inputs)
else:
embeddings = tf.Variable(
tf.random_uniform([vocabulary_size, embedding_dimension], -1.0, 1.0))
embed = tf.nn.embedding_lookup(embeddings, _inputs)
with tf.name_scope('biGRU'):
with tf.variable_scope('forward'):
gru_fw_cell = tf.nn.rnn_cell.GRUCell(hidden_layer_size)
gru_fw_cell = tf.nn.rnn_cell.DropoutWrapper(gru_fw_cell)
with tf.variable_scope('backward'):
gru_bw_cell = tf.nn.rnn_cell.GRUCell(hidden_layer_size)
gru_bw_cell = tf.nn.rnn_cell.DropoutWrapper(gru_bw_cell)
outputs, states = tf.nn.bidirectional_dynamic_rnn(cell_fw=gru_fw_cell,
cell_bw=gru_bw_cell,
inputs=embed,
sequence_length=_seqlens,
dtype=tf.float32,
scope='biGRU')
states = tf.concat(values=states, axis=1)
weights = {
'linear_layer': tf.Variable(tf.truncated_normal([2*hidden_layer_size, num_classes],
mean=0, stddev=.01))
}
biases = {
'linear_layer': tf.Variable(tf.truncated_normal([num_classes],
mean=0, stddev=.01))
}
# 최종 상태(states)를 뽑아 선형 계층에 적용
final_output = tf.matmul(states, weights['linear_layer']) + biases['linear_layer']
# loss function
cross_entropy = tf.reduce_mean(
tf.nn.softmax_cross_entropy_with_logits_v2(logits=final_output, labels=_labels))
# optimizer
train_step = tf.train.RMSPropOptimizer(0.001, 0.9).minimize(cross_entropy)
# accuracy
correct_prediction = tf.equal(tf.argmax(_labels, 1), tf.argmax(final_output, 1))
accuracy = (tf.reduce_mean(tf.cast(correct_prediction, tf.float32)))*100
with tf.Session() as sess:
sess.run(tf.global_variables_initializer())
sess.run(embedding_init, feed_dict={embedding_placeholder: embedding_matrix})
for step in range(1000):
x_batch, y_batch, seqlen_batch = get_sentence_batch(batch_size, train_x,
train_y, train_seqlens)
sess.run(train_step, feed_dict={_inputs: x_batch, _labels: y_batch,
_seqlens: seqlen_batch})
if step % 100 == 0:
acc = sess.run(accuracy, feed_dict={_inputs: x_batch,
_labels: y_batch,
_seqlens: seqlen_batch})
print("Accuracy at %d: %.5f" % (step, acc))
norm = tf.sqrt(tf.reduce_sum(tf.square(embeddings), 1, keepdims=True))
normalized_embeddings = embeddings / norm
normalized_embeddings_matrix = sess.run(normalized_embeddings)
for test_batch in range(5):
x_test, y_test, seqlen_test = get_sentence_batch(batch_size,
test_x, test_y,
test_seqlens)
batch_pred, batch_acc = sess.run([tf.argmax(final_output, 1), accuracy],
feed_dict={_inputs: x_test,
_labels: y_test,
_seqlens: seqlen_test})
print("Test batch accuracy %d: %.5f" % (test_batch, batch_acc))
ref_word = normalized_embeddings_matrix[word2index_map["Three"]]
cosine_dists = np.dot(normalized_embeddings_matrix, ref_word)
ff = np.argsort(cosine_dists)[::-1][1:10]
for f in ff:
print(index2word_map[f])
print(cosine_dists[f])