-
Notifications
You must be signed in to change notification settings - Fork 62
/
text_cnn.py
71 lines (58 loc) · 4.02 KB
/
text_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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import tensorflow as tf
class TextCNN:
def __init__(self, sequence_length, num_classes,
text_vocab_size, text_embedding_size, pos_vocab_size, pos_embedding_size,
filter_sizes, num_filters, l2_reg_lambda=0.0):
# Placeholders for input, output and dropout
self.input_text = tf.placeholder(tf.int32, shape=[None, sequence_length], name='input_text')
self.input_p1 = tf.placeholder(tf.int32, shape=[None, sequence_length], name='input_p1')
self.input_p2 = tf.placeholder(tf.int32, shape=[None, sequence_length], name='input_p2')
self.input_y = tf.placeholder(tf.float32, shape=[None, num_classes], name='input_y')
self.dropout_keep_prob = tf.placeholder(tf.float32, name='dropout_keep_prob')
initializer = tf.keras.initializers.glorot_normal
# Embedding layer
with tf.device('/cpu:0'), tf.variable_scope("text-embedding"):
self.W_text = tf.Variable(tf.random_uniform([text_vocab_size, text_embedding_size], -0.25, 0.25), name="W_text")
self.text_embedded_chars = tf.nn.embedding_lookup(self.W_text, self.input_text)
self.text_embedded_chars_expanded = tf.expand_dims(self.text_embedded_chars, -1)
with tf.device('/cpu:0'), tf.variable_scope("position-embedding"):
self.W_pos = tf.get_variable("W_pos", [pos_vocab_size, pos_embedding_size], initializer=initializer())
self.p1_embedded_chars = tf.nn.embedding_lookup(self.W_pos, self.input_p1)
self.p2_embedded_chars = tf.nn.embedding_lookup(self.W_pos, self.input_p2)
self.p1_embedded_chars_expanded = tf.expand_dims(self.p1_embedded_chars, -1)
self.p2_embedded_chars_expanded = tf.expand_dims(self.p2_embedded_chars, -1)
self.embedded_chars_expanded = tf.concat([self.text_embedded_chars_expanded,
self.p1_embedded_chars_expanded,
self.p2_embedded_chars_expanded], 2)
_embedding_size = text_embedding_size + 2*pos_embedding_size
# Create a convolution + maxpool layer for each filter size
pooled_outputs = []
for i, filter_size in enumerate(filter_sizes):
with tf.variable_scope("conv-maxpool-%s" % filter_size):
# Convolution Layer
conv = tf.layers.conv2d(self.embedded_chars_expanded, num_filters, [filter_size, _embedding_size],
kernel_initializer=initializer(), activation=tf.nn.relu, name="conv")
# Maxpooling over the outputs
pooled = tf.nn.max_pool(conv, ksize=[1, sequence_length - filter_size + 1, 1, 1],
strides=[1, 1, 1, 1], padding='VALID', name="pool")
pooled_outputs.append(pooled)
# Combine all the pooled features
num_filters_total = num_filters * len(filter_sizes)
self.h_pool = tf.concat(pooled_outputs, 3)
self.h_pool_flat = tf.reshape(self.h_pool, [-1, num_filters_total])
# Add dropout
with tf.variable_scope("dropout"):
self.h_drop = tf.nn.dropout(self.h_pool_flat, self.dropout_keep_prob)
# Final scores and predictions
with tf.variable_scope("output"):
self.logits = tf.layers.dense(self.h_drop, num_classes, kernel_initializer=initializer())
self.predictions = tf.argmax(self.logits, 1, name="predictions")
# Calculate mean cross-entropy loss
with tf.variable_scope("loss"):
losses = tf.nn.softmax_cross_entropy_with_logits_v2(logits=self.logits, labels=self.input_y)
self.l2 = tf.add_n([tf.nn.l2_loss(v) for v in tf.trainable_variables()])
self.loss = tf.reduce_mean(losses) + l2_reg_lambda * self.l2
# Accuracy
with tf.name_scope("accuracy"):
correct_predictions = tf.equal(self.predictions, tf.argmax(self.input_y, 1))
self.accuracy = tf.reduce_mean(tf.cast(correct_predictions, tf.float32), name="accuracy")