-
Notifications
You must be signed in to change notification settings - Fork 9
/
modules.py
executable file
·230 lines (165 loc) · 7.26 KB
/
modules.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
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
221
222
223
224
225
226
227
228
229
230
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Nov 15 00:02:51 2020
@author: hsjomaa
"""
import tensorflow as tf
def importance(config):
if config['importance'] == 'linear':
fn = lambda x:x
elif config['importance'] == 'None':
fn = None
else:
raise('please define n importance function')
return fn
ARCHITECTURES = ['SQU','ASC','DES','SYM','ENC']
def get_units(idx,neurons,architecture,layers=None):
assert architecture in ARCHITECTURES
if architecture == 'SQU':
return neurons
elif architecture == 'ASC':
return (2**idx)*neurons
elif architecture == 'DES':
return (2**(layers-1-idx))*neurons
elif architecture=='SYM':
assert (layers is not None and layers > 2)
if layers%2==1:
return neurons*2**(int(layers/2) - abs(int(layers/2)-idx))
else:
x = int(layers/2)
idx = idx if idx < x else 2*x-idx -1
return neurons*2**(int(layers/2) - abs(int(layers/2)-idx))
elif architecture=='ENC':
assert (layers is not None and layers > 2)
if layers%2==0:
x = int(layers/2)
idx = idx if idx < x else 2*x-idx -1
return neurons*2**(int(layers/2)-1 -idx)
else:
x = int(layers/2)
idx = idx if idx < x else 2*x-idx
return neurons*2**(int(layers/2) -idx)
class Function(tf.keras.layers.Layer):
def __init__(self, units, nhidden, nonlinearity,architecture,trainable):
super(Function, self).__init__()
self.n = nhidden
self.units = units
self.nonlinearity = tf.keras.layers.Activation(nonlinearity)
self.block = [tf.keras.layers.Dense(units=get_units(_,self.units,architecture,self.n),trainable=trainable) \
for _ in range(self.n)]
def call(self):
raise Exception("Call not implemented")
class ResidualBlock(Function):
def __init__(self, units, nhidden, nonlinearity,architecture,trainable):
super().__init__(units, nhidden, nonlinearity,architecture,trainable)
def call(self, x):
e = x + 0
for i, layer in enumerate(self.block):
e = layer(e)
if i < (self.n - 1):
e = self.nonlinearity(e)
return self.nonlinearity(e + x)
class FunctionF(Function):
def __init__(self, units, nhidden, nonlinearity,architecture,trainable, resblocks=0):
# m number of residual blocks
super().__init__(units, nhidden, nonlinearity,architecture,trainable)
# override function with residual blocks
self.resblocks=resblocks
if resblocks>0:
self.block = [tf.keras.layers.Dense(units=self.units,trainable=trainable)]
assert(architecture=="SQU")
self.block += [ResidualBlock(units=self.units,architecture=architecture,nhidden=self.n,trainable=trainable,nonlinearity=self.nonlinearity) \
for _ in range(resblocks)]
self.block += [tf.keras.layers.Dense(units=self.units,trainable=trainable)]
def call(self, x):
e = x
for i,fc in enumerate(self.block):
e = fc(e)
# make sure activation only applied once!
if self.resblocks == 0:
e = self.nonlinearity(e)
else:
# only first one
if i==0 or i == (len(self.block)-1):
e = self.nonlinearity(e)
return e
class PoolF(tf.keras.layers.Layer):
def __init__(self,units):
super(PoolF, self).__init__()
self.units = units
def call(self,x,nclasses,nfeature,ninstanc):
s = tf.multiply(nclasses,tf.multiply(nfeature,ninstanc))
x = tf.split(x,num_or_size_splits=s,axis=0)
e = []
for i,bx in enumerate(x):
te = tf.reshape(bx,shape=(1,nclasses[i],nfeature[i],ninstanc[i],self.units))
te = tf.reduce_mean(te,axis=3)
e.append(tf.reshape(te,shape=(nclasses[i]*nfeature[i],self.units)))
e = tf.concat(e,axis=0)
return e
class FunctionG(Function):
def __init__(self, units, nhidden, nonlinearity,architecture,trainable):
super().__init__(units, nhidden, nonlinearity,architecture,trainable)
def call(self, x):
e = x
for fc in self.block:
e = fc(e)
e = self.nonlinearity(e)
return e
class PoolG(tf.keras.layers.Layer):
def __init__(self,units):
super(PoolG, self).__init__()
self.units = units
def call(self, x,nclasses,nfeature):
s = tf.multiply(nclasses, nfeature)
x = tf.split(x,num_or_size_splits=s,axis=0)
e = []
for i,bx in enumerate(x):
te = tf.reshape(bx,shape=(1,nclasses[i]*nfeature[i],self.units))
te = tf.reduce_mean(te,axis=1)
e.append(te)
e = tf.concat(e,axis=0)
return e
class FunctionH(Function):
def __init__(self, units, nhidden, nonlinearity,architecture,trainable, resblocks=0):
# m number of residual blocks
super().__init__(units, nhidden, nonlinearity,architecture,trainable)
# override function with residual blocks
self.resblocks = resblocks
if resblocks>0:
self.block = [tf.keras.layers.Dense(units=self.units,trainable=trainable)]
assert(architecture=="SQU")
self.block += [ResidualBlock(units=self.units,architecture=architecture,nhidden=self.n,trainable=trainable,nonlinearity=self.nonlinearity) \
for _ in range(resblocks)]
self.block += [tf.keras.layers.Dense(units=self.units,trainable=trainable)]
def call(self,x):
e = x
for i,fc in enumerate(self.block):
e = fc(e)
# make sure activation only applied once!
if self.resblocks == 0:
if i<(len(self.blocks)-1):
e = self.nonlinearity(e)
else:
# only first one
if i==0:
e = self.nonlinearity(e)
return e
class PoolH(tf.keras.layers.Layer):
def __init__(self, batch_size,units):
"""
"""
super(PoolH, self).__init__()
self.batch_size = batch_size
self.units = units
def call(self, x,ignore_negative):
e = tf.reshape(x,shape=(self.batch_size,3,self.units))
# average positive meta-features
e1 = tf.reduce_mean(e[:,:2],axis=1)
if not ignore_negative:
# select negative meta-feautures
e1 = e[:,-1][:,None]
# reshape, i.e. output is [batch_size,nhidden]
e = tf.reshape(e1,shape=(self.batch_size,self.units))
return e