forked from cjiang2/VDCNN
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvdcnn.py
More file actions
220 lines (196 loc) · 6.18 KB
/
vdcnn.py
File metadata and controls
220 lines (196 loc) · 6.18 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
"""Very Deep CNN model. https://arxiv.org/abs/1606.01781"""
import tensorflow as tf
from k_maxpooling import KMaxPooling
def identity_block(inputs, filters, kernel_size=3, use_bias=False, shortcut=False):
x = tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, strides=1, padding="same"
)(inputs)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, strides=1, padding="same"
)(x)
x = tf.keras.layers.BatchNormalization()(x)
if shortcut:
x = tf.keras.layers.Add()([x, inputs])
return tf.keras.activations.relu(x)
def conv_block(
inputs,
filters,
kernel_size=3,
use_bias=False,
shortcut=False,
pool_type="max",
sort=True,
stage=1,
):
x = tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, strides=1, padding="same"
)(inputs)
x = tf.keras.layers.BatchNormalization()(x)
x = tf.keras.activations.relu(x)
x = tf.keras.layers.Conv1D(
filters=filters, kernel_size=kernel_size, strides=1, padding="same"
)(x)
x = tf.keras.layers.BatchNormalization()(x)
if shortcut:
residual = tf.keras.layers.Conv1D(
filters=filters, kernel_size=1, strides=2, name="shortcut_conv1d_%d" % stage
)(inputs)
residual = tf.keras.layers.BatchNormalization(
name="shortcut_batch_normalization_%d" % stage
)(residual)
x = downsample(x, pool_type=pool_type, sort=sort, stage=stage)
x = tf.keras.layers.Add()([x, residual])
x = tf.keras.activations.relu(x)
else:
x = tf.keras.activations.relu(x)
x = downsample(x, pool_type=pool_type, sort=sort, stage=stage)
if pool_type is not None:
x = tf.keras.layers.Conv1D(
filters=2 * filters,
kernel_size=1,
strides=1,
padding="same",
name="1_1_conv_%d" % stage,
)(x)
x = tf.keras.layers.BatchNormalization(
name="1_1_batch_normalization_%d" % stage
)(x)
return x
def downsample(inputs, pool_type="max", sort=True, stage=1):
if pool_type == "max":
x = tf.keras.layers.MaxPooling1D(
pool_size=3, strides=2, padding="same", name="pool_%d" % stage
)(inputs)
elif pool_type == "k_max":
k = int(inputs._keras_shape[1] / 2)
x = KMaxPooling(k=k, sort=sort, name="pool_%d" % stage)(inputs)
elif pool_type == "conv":
x = tf.keras.layers.Conv1D(
filters=inputs._keras_shape[-1],
kernel_size=3,
strides=2,
padding="same",
name="pool_%d" % stage,
)(inputs)
x = tf.keras.layers.BatchNormalization()(x)
elif pool_type is None:
x = inputs
else:
raise ValueError("unsupported pooling type!")
return x
def VDCNN(
num_classes,
depth=9,
sequence_length=1024,
embedding_dim=16,
shortcut=False,
pool_type="max",
sort=True,
use_bias=False,
embedding_input=False,
input_tensor=None,
):
if depth == 9:
num_conv_blocks = (1, 1, 1, 1)
elif depth == 17:
num_conv_blocks = (2, 2, 2, 2)
elif depth == 29:
num_conv_blocks = (5, 5, 2, 2)
elif depth == 49:
num_conv_blocks = (8, 8, 5, 3)
else:
raise ValueError("unsupported depth for VDCNN.")
if embedding_input:
# Input is a n x m matrix of n ordered m-dimenstional vector embeddings
inputs = tf.keras.Input(shape=(sequence_length, embedding_dim), name="inputs")
embedded_chars = inputs
else:
# Input is raw text
inputs = tf.keras.Input(shape=(sequence_length,), name="inputs")
embedded_chars = tf.keras.layers.Embedding(
input_dim=sequence_length, output_dim=embedding_dim
)(inputs)
x = tf.keras.layers.Conv1D(
filters=64, kernel_size=3, strides=1, padding="same", name="temp_conv"
)(embedded_chars)
# Convolutional Block 64
for _ in range(num_conv_blocks[0] - 1):
x = identity_block(
x, filters=64, kernel_size=3, use_bias=use_bias, shortcut=shortcut
)
x = conv_block(
x,
filters=64,
kernel_size=3,
use_bias=use_bias,
shortcut=shortcut,
pool_type=pool_type,
sort=sort,
stage=1,
)
# Convolutional Block 128
for _ in range(num_conv_blocks[1] - 1):
x = identity_block(
x, filters=128, kernel_size=3, use_bias=use_bias, shortcut=shortcut
)
x = conv_block(
x,
filters=128,
kernel_size=3,
use_bias=use_bias,
shortcut=shortcut,
pool_type=pool_type,
sort=sort,
stage=2,
)
# Convolutional Block 256
for _ in range(num_conv_blocks[2] - 1):
x = identity_block(
x, filters=256, kernel_size=3, use_bias=use_bias, shortcut=shortcut
)
x = conv_block(
x,
filters=256,
kernel_size=3,
use_bias=use_bias,
shortcut=shortcut,
pool_type=pool_type,
sort=sort,
stage=3,
)
# Convolutional Block 512
for _ in range(num_conv_blocks[3] - 1):
x = identity_block(
x, filters=512, kernel_size=3, use_bias=use_bias, shortcut=shortcut
)
x = conv_block(
x,
filters=512,
kernel_size=3,
use_bias=use_bias,
shortcut=False,
pool_type=None,
stage=4,
)
# k-max pooling with k = 8
k = min(x.shape[1], 8)
x = KMaxPooling(k=k, sort=True)(x)
x = tf.keras.layers.Flatten()(x)
# Dense Layers
x = tf.keras.layers.Dense(2048, activation="relu")(x)
x = tf.keras.layers.Dense(2048, activation="relu")(x)
x = tf.keras.layers.Dense(num_classes, activation="softmax")(x)
if input_tensor is not None:
inputs = tf.keras.get_source_inputs(input_tensor)
else:
inputs = inputs
# Create model.
model = tf.keras.Model(inputs=inputs, outputs=x, name="VDCNN")
return model
def main():
model = VDCNN(10, depth=9, shortcut=False, pool_type="max")
model.summary()
if __name__ == "__main__":
main()