-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathModels.py
88 lines (62 loc) · 2.63 KB
/
Models.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
"""
@author: Dat Tran ([email protected])
"""
import Layers
import keras
def BL(template, dropout=0.1, regularizer=None, constraint=None):
"""
Bilinear Layer network, refer to the paper https://arxiv.org/abs/1712.00975
inputs
----
template: a list of network dimensions including input and output, e.g., [[40,10], [120,5], [3,1]]
dropout: dropout percentage
regularizer: keras regularizer object
constraint: keras constraint object
outputs
------
keras model object
"""
print(template)
inputs = keras.layers.Input(template[0])
x = inputs
for k in range(1, len(template)-1):
x = Layers.BL(template[k], regularizer, constraint)(x)
x = keras.layers.Activation('relu')(x)
x = keras.layers.Dropout(dropout)(x)
x = Layers.BL(template[-1], regularizer, constraint)(x)
outputs = keras.layers.Activation('softmax')(x)
model = keras.Model(inputs=inputs, outputs = outputs)
optimizer = keras.optimizers.Adam(0.01)
model.compile(optimizer, 'categorical_crossentropy', ['acc',])
return model
def TABL(template, dropout=0.1, projection_regularizer=None, projection_constraint=None,
attention_regularizer=None, attention_constraint=None):
"""
Temporal Attention augmented Bilinear Layer network, refer to the paper https://arxiv.org/abs/1712.00975
inputs
----
template: a list of network dimensions including input and output, e.g., [[40,10], [120,5], [3,1]]
dropout: dropout percentage
projection_regularizer: keras regularizer object for projection matrices
projection_constraint: keras constraint object for projection matrices
attention_regularizer: keras regularizer object for attention matrices
attention_constraint: keras constraint object for attention matrices
outputs
------
keras model object
"""
inputs = keras.layers.Input(template[0])
x = inputs
for k in range(1, len(template)-1):
x = Layers.BL(template[k], projection_regularizer, projection_constraint)(x)
x = keras.layers.Activation('relu')(x)
x = keras.layers.Dropout(dropout)(x)
x = Layers.TABL(template[-1], projection_regularizer, projection_constraint,
attention_regularizer, attention_constraint)(x)
outputs = keras.layers.Activation('softmax')(x)
model = keras.Model(inputs=inputs, outputs = outputs)
optimizer = keras.optimizers.Adam(0.01)
model.compile(optimizer, 'categorical_crossentropy', ['acc',])
return model