-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtextCNN.py
More file actions
107 lines (87 loc) · 5.19 KB
/
textCNN.py
File metadata and controls
107 lines (87 loc) · 5.19 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
# -*- coding: utf-8 -*-
import tensorflow as tf
class TextCNN(object):
"""A CNN for text classification."""
def __init__(
self, sequence_length, num_classes, vocab_size, fc_hidden_size, embedding_size,
embedding_type, filter_sizes, num_filters, l2_reg_lambda=0.0, pretrained_embedding=None):
# Placeholders for input, output, dropout_prob and training_tag
self.input_x = tf.placeholder(tf.int32, [None, None], name="input_x")
self.input_y = tf.placeholder(tf.float32, [None, num_classes], name="input_y")
self.dropout_keep_prob = tf.placeholder(tf.float32, name="dropout_keep_prob")
self.global_step = tf.Variable(0, trainable=False, name="Global_Step")
# Embedding Layer
with tf.device('/cpu:0'), tf.name_scope("embedding"):
# Use random generated the word vector by default
# Can also be obtained through our own word vectors trained by our corpus
if pretrained_embedding is None:
self.embedding = tf.Variable(tf.random_uniform([vocab_size, embedding_size], -1.0, 1.0,
dtype=tf.float32), trainable=True, name="embedding")
else:
if embedding_type == 0:
self.embedding = tf.constant(pretrained_embedding, dtype=tf.float32, name="embedding")
if embedding_type == 1:
self.embedding = tf.Variable(pretrained_embedding, trainable=True,
dtype=tf.float32, name="embedding")
self.embedded_sentence = tf.nn.embedding_lookup(self.embedding, self.input_x)
self.embedded_sentence_expanded = tf.expand_dims(self.embedded_sentence, -1)
# Create a convolution + maxpool layer for each filter size
pooled_outputs = []
for filter_size in filter_sizes:
with tf.name_scope("conv-filter{0}".format(filter_size)):
# Convolution Layer
filter_shape = [filter_size, embedding_size, 1, num_filters]
W = tf.Variable(tf.truncated_normal(shape=filter_shape, stddev=0.1, dtype=tf.float32), name="W")
b = tf.Variable(tf.constant(0.1, shape=[num_filters], dtype=tf.float32), name="b")
conv = tf.nn.conv2d(
self.embedded_sentence_expanded,
W,
strides=[1, 1, 1, 1],
padding="VALID",
name="conv")
conv = tf.nn.bias_add(conv, b)
# Apply nonlinearity
conv_out = tf.nn.relu(conv, name="relu")
with tf.name_scope("pool-filter{0}".format(filter_size)):
# Maxpooling over the outputs
pooled = tf.nn.max_pool(
conv_out,
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.pool = tf.concat(pooled_outputs, 3)
self.pool_flat = tf.reshape(self.pool, [-1, num_filters_total])
# Fully Connected Layer
with tf.name_scope("fc"):
W = tf.Variable(tf.truncated_normal(shape=[num_filters_total, fc_hidden_size],
stddev=0.1, dtype=tf.float32), name="W")
b = tf.Variable(tf.constant(0.1, shape=[fc_hidden_size], dtype=tf.float32), name="b")
self.fc = tf.nn.xw_plus_b(self.pool_flat, W, b)
# Apply nonlinearity
self.fc_out = tf.nn.relu(self.fc, name="relu")
# Add dropout
with tf.name_scope("dropout"):
self.h_drop = tf.nn.dropout(self.fc_out, self.dropout_keep_prob)
# Final scores
with tf.name_scope("output"):
W = tf.Variable(tf.truncated_normal(shape=[fc_hidden_size, num_classes],
stddev=0.1, dtype=tf.float32), name="W")
b = tf.Variable(tf.constant(0.1, shape=[num_classes], dtype=tf.float32), name="b")
self.logits = tf.nn.xw_plus_b(self.h_drop, W, b, name="logits")
self.scores = tf.sigmoid(self.logits, name="scores")
self.predictions = tf.round(self.scores, name="predictions")
# Calculate mean cross-entropy loss, L2 loss
with tf.name_scope("loss"):
losses = tf.nn.sigmoid_cross_entropy_with_logits(labels=self.input_y, logits=self.logits)
losses = tf.reduce_mean(tf.reduce_sum(losses, axis=1), name="sigmoid_losses")
l2_losses = tf.add_n([tf.nn.l2_loss(tf.cast(v, tf.float32)) for v in tf.trainable_variables()],
name="l2_losses") * l2_reg_lambda
self.loss = tf.add(losses, l2_losses, name="loss")
# Calculate performance
with tf.name_scope('performance'):
self.precision = tf.metrics.precision(self.input_y, self.predictions, name="precision-micro")[1]
self.recall = tf.metrics.recall(self.input_y, self.predictions, name="recall-micro")[1]