-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodel.py
252 lines (195 loc) · 7.17 KB
/
model.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import keras
from keras.models import Model
from keras.layers import SimpleRNN,Dense,Multiply,Activation,Input,Add,Subtract,Reshape,Concatenate,Lambda,GRU,Flatten,GaussianNoise,Dropout,LSTM,TimeDistributed,Bidirectional,ActivityRegularization,RepeatVector
from sklearn.neighbors import KDTree
from keras.optimizers import Adam
import numpy as np
import tensorflow as tf
import keras.regularizers
def lamb1(x):
return x[:,:,0:-no]
def lamb1_os(x):
ll=list(x)
ll[-1]=ll[-1]-no
return tuple(ll)
def lamb2(x):
sh=tf.shape(x)
#sh[-1]=1
return x[:,:,-no::]
def lamb2_os(x):
ll=list(x)
ll[-1]=no
return tuple(ll)
def lamb3(x):
return tf.reduce_sum(x,1)
def lamb3_os(x):
ll=list(x)
ll=[ll[0],ll[-1]]
return tuple(ll)
def get_model(n_neighbors,n_dim=1,n_out=1,alpha=1e-10,lossfn='mse'):
l1 = Lambda(lamb1,output_shape=lamb1_os)
l2 = Lambda(lamb2,output_shape=lamb2_os)
l3 = Lambda(lamb3,output_shape=lamb3_os)
inp1 = Input((n_dim,))
inp2 = Input((n_neighbors,n_dim+n_out))
inp_mask = Input((n_neighbors,1))
sc10 = Lambda(lambda x: 10.0*x)
h=l1(inp2)
hi=RepeatVector(n_neighbors)(inp1)
noise=Lambda(lambda x: tf.random_normal(tf.shape(x),0.0,0.02))(l1(inp2))
#noise=RepeatVector(n_neighbors)(noise)
h=Add()([h,noise])
hi=Add()([hi,noise])
d_0=TimeDistributed(Dense(32,activation='relu',kernel_regularizer=keras.regularizers.l2(alpha)))
d_02=TimeDistributed(Dense(n_dim,kernel_regularizer=keras.regularizers.l2(alpha)))
hh = d_02(d_0(h))
it = d_02(d_0(hi))
h=Subtract()([hh,it])
h2=sc10(h)
h=Concatenate()([h,h2])
h=Multiply()([h,inp_mask])
h=Concatenate()([h,inp_mask])
d_1=Dense(64,activation='relu',kernel_regularizer=keras.regularizers.l2(alpha))
d_2=Dense(64,activation='relu',kernel_regularizer=keras.regularizers.l2(alpha))
d_3=Dense(n_out,kernel_regularizer=keras.regularizers.l2(alpha))
h=TimeDistributed(d_1)(h)
h=TimeDistributed(d_2)(h)
h=TimeDistributed(d_3)(h)
h=Lambda(lambda x:tf.nn.softmax(x,1))(h)
h=Multiply()([h,l2(inp2)])
print(h)
h = Lambda(lambda x:tf.reduce_sum(x,1))(h)
out=h#Dense(n_out)(h)
model=Model([inp1,inp2,inp_mask],out)
model.compile(loss=lossfn,optimizer=Adam(1e-3))
return model
class CCNet:
def __init__(self,n_neighbours,do_rate=0.2,id_dropout=0.9,shuffle=False,model=None,loss='mse'):
self.nn=n_neighbours
self.do_rate=do_rate
self.id_dropout=id_dropout
self.shuffle=shuffle
self.inp_model=model
self.lossfn=loss
def fit(self,X,Y,epochs=10,batch_size=128,verbose=True):
self.n_dim=X.shape[1]
self.n_out=Y.shape[1]
global no
no=self.n_out
if self.inp_model is None:
self.model=get_model(self.nn,self.n_dim,self.n_out,alpha=1e-10,lossfn=self.lossfn)
else:
self.model = self.inp_model
self.kdt=KDTree(X)
m=int(X.shape[0]/batch_size)
self.X=np.copy(X)
self.Y=np.copy(Y)
X2=np.zeros((X.shape[0],self.nn,self.n_dim+self.n_out))
self.X2=np.copy(X2)
idx=self.kdt.query(X,self.nn+1,return_distance=False)
idx=idx[:,0:-1]
for i in range(0,X.shape[0]):
for j in range(0,self.n_dim):
X2[i,:,j]=X[idx[i,:],j]
for j in range(0,self.n_out):
X2[i,:,self.n_dim+j]=self.Y[idx[i,:],j]
print(X.shape,X2.shape)
outp_noise=0.0
inp_noise=0.0
for i in range(0,epochs):
for j in range(0,m):
if self.shuffle:
idx2=np.transpose(np.copy(idx))
np.random.shuffle(idx2)
idx=np.copy(idx2).T
for k in range(0,X.shape[0]):
for l in range(0,self.n_dim):
X2[k,:,l]=X[idx[k,:],l]
for l in range(0,self.n_out):
X2[k,:,self.n_dim+l]=self.Y[idx[k,:],l]
batch_x1 = X[j*batch_size:(j+1)*batch_size]
batch_x2 = X2[j*batch_size:(j+1)*batch_size]
mask = np.random.random(size=(batch_x2.shape[0],batch_x2.shape[1]))>=self.do_rate
mask[:,0] = mask[:,0]*(np.random.random(size=(batch_x2.shape[0],))>=self.id_dropout)
#print(np.all(mask))
batch_mask=mask.reshape(mask.shape[0],mask.shape[1],1)#/(1.0-self.do_rate)
#batch_mask
batch_y=Y[j*batch_size:(j+1)*batch_size]+np.random.normal(0.0,outp_noise,size=(batch_size,self.n_out))
#print(batch_y.shape,batch_x1.shape,batch_x2.shape,batch_mask.shape)
#idx = self.kdt.query(batch_x1,self.nn,return_distance=False)
ll=self.model.train_on_batch([batch_x1,batch_x2,batch_mask],batch_y)
if verbose:
print("Epoch: "+ str(i)+". Loss: "+str(ll))
if X.shape[0] > m*batch_size:
if self.shuffle:
idx2=np.transpose(np.copy(idx))
np.random.shuffle(idx2)
idx=np.copy(idx2).T
for k in range(0,X.shape[0]):
for l in range(0,self.n_dim):
X2[k,:,l]=X[idx[k,:],l]
for l in range(0,self.n_out):
X2[k,:,self.n_dim+l]=self.Y[idx[k,:],l]
bs=X.shape[0]-m*batch_size
batch_x1 = X[m*batch_size::]
batch_x2 = X2[m*batch_size::]
mask = np.random.random(size=(bs,batch_x2.shape[1]))>=self.do_rate
mask[:,0] = mask[:,0]*(np.random.random(size=(bs,))>=self.id_dropout)
#print(np.all(mask))
batch_mask=mask.reshape(mask.shape[0],mask.shape[1],1)#/(1.0-self.do_rate)
#batch_mask
batch_y=Y[m*batch_size::]+np.random.normal(0.0,outp_noise,size=(bs,self.n_out))
#print(batch_y.shape,batch_x1.shape,batch_x2.shape,batch_mask.shape)
#idx = self.kdt.query(batch_x1,self.nn,return_distance=False)
ll=self.model.train_on_batch([batch_x1,batch_x2,batch_mask],batch_y)
if verbose:
print("Epoch: "+ str(i)+". Loss: "+str(ll))
def predict(self,X):
assert(X.shape[1]==self.n_dim)
idx = self.kdt.query(X,self.nn,return_distance=False)
X2=np.zeros((X.shape[0],self.nn,self.n_dim+self.n_out))
mask=np.ones((X2.shape[0],self.nn,1))
for i in range(0,X2.shape[0]):
for j in range(0,self.n_dim):
X2[i,:,j]=self.X[idx[i,:],j]
for j in range(0,self.n_out):
X2[i,:,self.n_dim+j]=self.Y[idx[i,:],j]
#print(X2)
return self.model.predict([X,X2,mask])
def stochastic_predict(self,X,n_iter=100,verbose=True):
assert(X.shape[1]==self.n_dim)
idx = self.kdt.query(X,self.nn,return_distance=False)
#if np.all(X==self.X):
#idx=idx[:,1::]
#else:
# idx=idx[:,0:-1]
#idx = self.kdt.query(X,self.nn,return_distance=False)
X2=np.zeros((X.shape[0],self.nn,self.n_dim+self.n_out))
for i in range(0,X2.shape[0]):
for j in range(0,self.n_dim):
X2[i,:,j]=self.X[idx[i,:],j]
for j in range(0,self.n_out):
X2[i,:,self.n_dim+j]=self.Y[idx[i,:],j]
preds = np.zeros((n_iter,X.shape[0],self.n_out))
for n in range(0,n_iter):
if self.shuffle:
idx2=np.transpose(np.copy(idx))
np.random.shuffle(idx2)
idx=np.copy(idx2).T
X2=np.zeros((X.shape[0],self.nn,self.n_dim+self.n_out))
for i in range(0,X2.shape[0]):
for j in range(0,self.n_dim):
X2[i,:,j]=self.X[idx[i,:],j]
for j in range(0,self.n_out):
X2[i,:,self.n_dim+j]=self.Y[idx[i,:],j]
mask = np.random.random(size=(X.shape[0],self.nn))>=self.do_rate
#mask[:,0] = mask[:,0]*(np.random.random(size=(X.shape[0],))>=self.id_dropout)
mask=mask.reshape(mask.shape[0],mask.shape[1],1)#/(1.0-self.do_rate)
#X2p=np.zeros_like(X2)
#for k in range(0,X.shape[0]):
# kk = int(np.sum(mask[k]))
# X2p[k,0:kk]=X2[k,mask[k]]
preds[n]=self.model.predict([X,X2,mask])
if verbose:
print(n,n_iter)
return np.mean(preds,0),np.std(preds,0)