-
Notifications
You must be signed in to change notification settings - Fork 0
/
word_cnn.py
50 lines (40 loc) · 2.59 KB
/
word_cnn.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
import tensorflow as tf
from tensorflow.contrib import rnn
class WordCNN(object):
def __init__(self, vocabulary_size, document_max_len, num_class):
self.embedding_size = 256
self.num_hidden = 256
self.num_layers = 2
self.learning_rate = 1e-3
self.x = tf.placeholder(tf.int32, [None, document_max_len], name="x")
self.x_len = tf.reduce_sum(tf.sign(self.x), 1)
self.y = tf.placeholder(tf.int32, [None], name="y")
self.is_training = tf.placeholder(tf.bool, [], name="is_training")
self.global_step = tf.Variable(0, trainable=False)
self.keep_prob = tf.where(self.is_training, 0.5, 1.0)
with tf.name_scope("embedding"):
init_embeddings = tf.random_uniform([vocabulary_size, self.embedding_size])
self.embeddings = tf.get_variable("embeddings", initializer=init_embeddings)
self.x_emb = tf.nn.embedding_lookup(self.embeddings, self.x)
with tf.name_scope("birnn"):
fw_cells = [rnn.BasicLSTMCell(self.num_hidden) for _ in range(self.num_layers)]
bw_cells = [rnn.BasicLSTMCell(self.num_hidden) for _ in range(self.num_layers)]
fw_cells = [rnn.DropoutWrapper(cell, output_keep_prob=self.keep_prob) for cell in fw_cells]
bw_cells = [rnn.DropoutWrapper(cell, output_keep_prob=self.keep_prob) for cell in bw_cells]
self.rnn_outputs, _, _ = rnn.stack_bidirectional_dynamic_rnn(
fw_cells, bw_cells, self.x_emb, sequence_length=self.x_len, dtype=tf.float32)
with tf.name_scope("attention"):
self.attention_score = tf.nn.softmax(tf.layers.dense(self.rnn_outputs, 1, activation=tf.nn.tanh), axis=1)
self.attention_out = tf.squeeze(
tf.matmul(tf.transpose(self.rnn_outputs, perm=[0, 2, 1]), self.attention_score),
axis=-1)
with tf.name_scope("output"):
self.logits = tf.layers.dense(self.attention_out, num_class, activation=None)
self.predictions = tf.argmax(self.logits, -1, output_type=tf.int32)
with tf.name_scope("loss"):
self.loss = tf.reduce_mean(
tf.nn.sparse_softmax_cross_entropy_with_logits(logits=self.logits, labels=self.y))
self.optimizer = tf.train.AdamOptimizer(self.learning_rate).minimize(self.loss, global_step=self.global_step)
with tf.name_scope("accuracy"):
correct_predictions = tf.equal(self.predictions, self.y)
self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, "float"), name="accuracy")