MCPcopy Create free account
hub / github.com/brightmart/text_classification / TextCNN

Class TextCNN

a02_TextCNN/p7_TextCNN_model.py:7–201  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

5import numpy as np
6
7class TextCNN:
8 def __init__(self, filter_sizes,num_filters,num_classes, learning_rate, batch_size, decay_steps, decay_rate,sequence_length,vocab_size,embed_size
9 ,initializer=tf.random_normal_initializer(stddev=0.1),multi_label_flag=False,clip_gradients=5.0,decay_rate_big=0.50):
10 """init all hyperparameter here"""
11 # set hyperparamter
12 self.num_classes = num_classes
13 self.batch_size = batch_size
14 self.sequence_length=sequence_length
15 self.vocab_size=vocab_size
16 self.embed_size=embed_size
17 self.learning_rate = tf.Variable(learning_rate, trainable=False, name="learning_rate")#ADD learning_rate
18 self.learning_rate_decay_half_op = tf.assign(self.learning_rate, self.learning_rate * decay_rate_big)
19 self.filter_sizes=filter_sizes # it is a list of int. e.g. [3,4,5]
20 self.num_filters=num_filters
21 self.initializer=initializer
22 self.num_filters_total=self.num_filters * len(filter_sizes) #how many filters totally.
23 self.multi_label_flag=multi_label_flag
24 self.clip_gradients = clip_gradients
25 self.is_training_flag = tf.placeholder(tf.bool, name="is_training_flag")
26
27 # add placeholder (X,label)
28 self.input_x = tf.placeholder(tf.int32, [None, self.sequence_length], name="input_x") # X
29 #self.input_y = tf.placeholder(tf.int32, [None,],name="input_y") # y:[None,num_classes]
30 self.input_y_multilabel = tf.placeholder(tf.float32,[None,self.num_classes], name="input_y_multilabel") # y:[None,num_classes]. this is for multi-label classification only.
31 self.dropout_keep_prob=tf.placeholder(tf.float32,name="dropout_keep_prob")
32 self.iter = tf.placeholder(tf.int32) #training iteration
33 self.tst=tf.placeholder(tf.bool)
34 self.use_mulitple_layer_cnn=False
35
36 self.global_step = tf.Variable(0, trainable=False, name="Global_Step")
37 self.epoch_step=tf.Variable(0,trainable=False,name="Epoch_Step")
38 self.epoch_increment=tf.assign(self.epoch_step,tf.add(self.epoch_step,tf.constant(1)))
39 self.b1 = tf.Variable(tf.ones([self.num_filters]) / 10)
40 self.b2 = tf.Variable(tf.ones([self.num_filters]) / 10)
41 self.decay_steps, self.decay_rate = decay_steps, decay_rate
42
43 self.instantiate_weights()
44 self.logits = self.inference() #[None, self.label_size]. main computation graph is here.
45 self.possibility=tf.nn.sigmoid(self.logits)
46 if multi_label_flag:
47 print("going to use multi label loss.");
48 self.loss_val = self.loss_multilabel()
49 else:print("going to use single label loss.");self.loss_val = self.loss()
50 self.train_op = self.train()
51 if not self.multi_label_flag:
52 self.predictions = tf.argmax(self.logits, 1, name="predictions") # shape:[None,]
53 print("self.predictions:", self.predictions)
54 correct_prediction = tf.equal(tf.cast(self.predictions,tf.int32), self.input_y) #tf.argmax(self.logits, 1)-->[batch_size]
55 self.accuracy =tf.reduce_mean(tf.cast(correct_prediction, tf.float32), name="Accuracy") # shape=()
56
57 def instantiate_weights(self):
58 """define all weights here"""
59 with tf.name_scope("embedding"): # embedding matrix
60 self.Embedding = tf.get_variable("Embedding",shape=[self.vocab_size, self.embed_size],initializer=self.initializer) #[vocab_size,embed_size] tf.random_uniform([self.vocab_size, self.embed_size],-1.0,1.0)
61 self.W_projection = tf.get_variable("W_projection",shape=[self.num_filters_total, self.num_classes],initializer=self.initializer) #[embed_size,label_size]
62 self.b_projection = tf.get_variable("b_projection",shape=[self.num_classes]) #[label_size] #ADD 2017.06.09
63
64 def inference(self):

Callers 15

mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90
mainFunction · 0.90

Calls

no outgoing calls

Tested by

no test coverage detected