-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathvdcnn.py
More file actions
48 lines (35 loc) · 1.95 KB
/
vdcnn.py
File metadata and controls
48 lines (35 loc) · 1.95 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
import tensorflow as tf
from Layers import ConvBlockLayer
from keras.models import Model
from keras.layers.convolutional import Conv1D
from keras.layers.embeddings import Embedding
from keras.layers import Input, Dense, Dropout, Lambda
from keras.layers.pooling import MaxPooling1D
from keras.optimizers import SGD
from utils import get_conv_shape
def build_model(num_filters, num_classes, sequence_max_length=512, num_quantized_chars=71, embedding_size=16, learning_rate=0.001, top_k=3, model_path=None):
inputs = Input(shape=(sequence_max_length, ), dtype='int32', name='inputs')
embedded_sent = Embedding(num_quantized_chars, embedding_size, input_length=sequence_max_length)(inputs)
# First conv layer
conv = Conv1D(filters=64, kernel_size=3, strides=2, padding="same")(embedded_sent)
# Each ConvBlock with one MaxPooling Layer
for i in range(len(num_filters)):
conv = ConvBlockLayer(get_conv_shape(conv), num_filters[i])(conv)
conv = MaxPooling1D(pool_size=3, strides=2, padding="same")(conv)
# k-max pooling (Finds values and indices of the k largest entries for the last dimension)
def _top_k(x):
x = tf.transpose(x, [0, 2, 1])
k_max = tf.nn.top_k(x, k=top_k)
return tf.reshape(k_max[0], (-1, num_filters[-1] * top_k))
k_max = Lambda(_top_k, output_shape=(num_filters[-1] * top_k,))(conv)
# 3 fully-connected layer with dropout regularization
fc1 = Dropout(0.2)(Dense(512, activation='relu', kernel_initializer='he_normal')(k_max))
fc2 = Dropout(0.2)(Dense(512, activation='relu', kernel_initializer='he_normal')(fc1))
fc3 = Dense(num_classes, activation='softmax')(fc2)
# define optimizer
sgd = SGD(lr=learning_rate, decay=1e-6, momentum=0.9, nesterov=False)
model = Model(inputs=inputs, outputs=fc3)
model.compile(optimizer=sgd, loss='mean_squared_error', metrics=['accuracy'])
if model_path is not None:
model.load_weights(model_path)
return model