-
Notifications
You must be signed in to change notification settings - Fork 1
/
Auto.py
282 lines (260 loc) · 13.4 KB
/
Auto.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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
from __future__ import absolute_import
from __future__ import print_function
from keras.models import Sequential, Model
from keras.optimizers import RMSprop
from keras.layers import Dense, Dropout
import warnings
from keras import backend as K
import numpy
# from multiprocessing import Pool
# from Metrics import *
class Autoencoder(Sequential):
def __init__(self,hid, layers=None, name=None):
super(Autoencoder, self).__init__()
self.layers = [] # Stack of layers.
self.model = None # Internal Model instance.
self.inputs = [] # List of input tensors
self.outputs = [] # List of length 1: the output tensor (unique).
self._trainable = True
self._initial_weights = None
self.hid = hid
# Model attributes.
self.inbound_nodes = []
self.outbound_nodes = []
self.built = False
self.pretrain = False
# Set model name.
if not name:
prefix = 'sequential_'
name = prefix + str(K.get_uid(prefix))
self.name = name
# Add to the model any layers passed to the constructor.
if layers:
for layer in layers:
self.add(layer)
def fit(self, x, y, batch_size=32, epochs=10, verbose=1, callbacks=None,
validation_split=0., validation_data=None, shuffle=True,
class_weight=None, sample_weight=None, initial_epoch=0,
activation='sigmoid', loss='mean_squared_error', classloss='categorical_crossentropy',
metrics=['mse', 'acc'], pre_epoch=10, droupout=0, lr=0.1, decay=1e-6, momentum=0.2, nesterov=True,
lastLoss = 'softmax', **kwargs):
"""Trains the model for a fixed number of epochs.
# Arguments
x: input data, as a Numpy array or list of Numpy arrays
(if the model has multiple inputs).
y: labels, as a Numpy array.
batch_size: integer. Number of samples per gradient update.
epochs: integer, the number of epochs to train the model.
verbose: 0 for no logging to stdout,
1 for progress bar logging, 2 for one log line per epoch.
callbacks: list of `keras.callbacks.Callback` instances.
List of callbacks to apply during training.
See [callbacks](/callbacks).
validation_split: float (0. < x < 1).
Fraction of the data to use as held-out validation data.
validation_data: tuple (x_val, y_val) or tuple
(x_val, y_val, val_sample_weights) to be used as held-out
validation data. Will override validation_split.
shuffle: boolean or str (for 'batch').
Whether to shuffle the samples at each epoch.
'batch' is a special option for dealing with the
limitations of HDF5 data; it shuffles in batch-sized chunks.
class_weight: dictionary mapping classes to a weight value,
used for scaling the loss function (during training only).
sample_weight: Numpy array of weights for
the training samples, used for scaling the loss function
(during training only). You can either pass a flat (1D)
Numpy array with the same length as the input samples
(1:1 mapping between weights and samples),
or in the case of temporal data,
you can pass a 2D array with shape (samples, sequence_length),
to apply a different weight to every timestep of every sample.
In this case you should make sure to specify
sample_weight_mode="temporal" in compile().
initial_epoch: epoch at which to start training
(useful for resuming a previous training run)
# Returns
A `History` object. Its `History.history` attribute is
a record of training loss values and metrics values
at successive epochs, as well as validation loss values
and validation metrics values (if applicable).
# Raises
RuntimeError: if the model was never compiled.
"""
# Legacy support
if 'nb_epoch' in kwargs:
warnings.warn('The `nb_epoch` argument in `fit` '
'has been renamed `epochs`.')
epochs = kwargs.pop('nb_epoch')
if kwargs:
raise TypeError('Unrecognized keyword arguments: ' + str(kwargs))
if self.model is None:
raise RuntimeError('The model needs to be compiled '
'before being used.')
hid=self.hid
sgd = RMSprop(lr=lr)
if not self.pretrain:
x_input=x
decoder_layers = []
autoencoder=Sequential()
for i in range(1, len(self.layers)):
temp = Sequential()
temp.add(Dense(hid[i], activation=activation, input_shape=(hid[i - 1],)))
# temp.add(normalization.BatchNormalization())
temp.add(Dropout(droupout))
temp.add(Dense(hid[i - 1], activation=activation, input_shape=(hid[i],)))
temp.compile(loss=loss, optimizer=sgd, metrics=metrics)
temp.fit(x_input, x_input, batch_size=batch_size, epochs=epochs, shuffle=True, verbose=0)
decoder_layers.append(temp.layers[-1])
autoencoder.add(temp.layers[0])
#func = K.function([autoencoder.model.input], [autoencoder.model.layers[-1].get_output_at(0)])
temp2=Sequential()
# print(x_input.shape)
temp2.add(Dense(hid[i], activation=activation, input_shape=(hid[i - 1],)))
temp2.set_weights(temp.layers[0].get_weights())
x_input=temp2.predict(x_input)
del temp
del temp2
decoder_layers.reverse()
print('after layer by layer pretrain')
for i in range(0, len(self.layers)-1):
autoencoder.add(decoder_layers[i])
# autoencoder.layers[i].set_weights(decoder_layers[i].get_weights())
autoencoder.compile(loss=loss, optimizer=sgd, metrics=metrics)
autoencoder.fit(x, x, batch_size=batch_size, epochs=pre_epoch, shuffle=True,verbose=0)
temp = Sequential()
for i in range(0, len(self.layers)-1):
temp.add(autoencoder.layers[i])
temp.layers[i].set_weights(autoencoder.layers[i].get_weights())
if len(y):
temp.add(Dense(y.shape[1], activation=lastLoss, input_shape=(hid[-1],)))
temp.compile(loss=classloss, optimizer=sgd, metrics=metrics)
temp.fit(x, y, batch_size=batch_size, epochs=pre_epoch, shuffle=True,verbose=0)
score = temp.evaluate(x, y, batch_size=20, verbose=1)
print(score)
print('After supervised Training')
for i in range(0,len(self.layers)):
self.layers[i].set_weights(temp.layers[i].get_weights())
self.layers=temp.layers
self.pretrain=True
del temp
del autoencoder
del decoder_layers
# return self.model.fit(x, y,
# batch_size=batch_size,
# epochs=epochs,
# verbose=verbose,
# callbacks=callbacks,
# validation_split=validation_split,
# validation_data=validation_data,
# shuffle=shuffle,
# class_weight=class_weight,
# sample_weight=sample_weight,
# initial_epoch=initial_epoch)
class MDL_CW(Sequential):
def __init__(self, Auto1, Auto2, layers=None, name=None):
super(MDL_CW, self).__init__()
self.layers = [] # Stack of layers.
self.model = None # Internal Model instance.
self.inputs = [] # List of input tensors
self.outputs = [] # List of length 1: the output tensor (unique).
self._trainable = True
self._initial_weights = None
# Model attributes.
self.inbound_nodes = []
self.outbound_nodes = []
self.built = False
self.pretrain = False
self.Auto1 = Auto1
self.Auto2 = Auto2
self.Unsupervised_train = False
# Set model name.
if not name:
prefix = 'sequential_'
name = prefix + str(K.get_uid(prefix))
self.name = name
def cross(self, x1, x2, y, batch_size=32, epochs=10, verbose=1, callbacks=None,
validation_split=0., validation_data=None, shuffle=True, class_weight=None, sample_weight=None,
initial_epoch=0, activation='sigmoid', loss='mean_squared_error', classloss='categorical_crossentropy',
metrics=['mse', 'acc'], pre_epoch=10, droupout=0, lr=0.1, decay=1e-6, momentum=0.2, nesterov=True,
unsupervised_train=False, supervised_train=False, lastLoss='softmax', **kwargs):
Auto1 = self.Auto1
Auto2 = self.Auto2
hid1 = Auto1.hid
hid2 = Auto2.hid
inp1 = Auto1.model.input
outputs1 = [layer.get_output_at(0) for layer in Auto1.model.layers] # all layer outputs
functors1 = [K.function([inp1], [out]) for out in outputs1]
layer_outs1 = [func([x1]) for func in functors1]
inp2 = Auto2.model.get_input_at(0)
outputs2 = [layer.get_output_at(0) for layer in Auto2.model.layers] # all layer outputs
functors2 = [K.function([inp2], [out]) for out in outputs2]
layer_outs2 = [func([x2]) for func in functors2]
sgd = RMSprop(lr=lr)
if len(Auto1.layers) != len(Auto2.layers):
raise TypeError('cross currently is for same-length Autoencoders')
x1_in = x1
x2_in = x2
for i in range(1, len(Auto1.layers)):
temp1 = Sequential()
temp2 = Sequential()
self.add(Dense(hid1[i]+hid2[i], activation=activation, input_shape=(hid1[i - 1]+hid2[i-1],)))
temp1.add(Dense(hid1[i], activation=activation, input_shape=(hid2[i - 1],)))
temp2.add(Dense(hid2[i], activation=activation, input_shape=(hid1[i - 1],)))
temp1.compile(loss=classloss, optimizer=sgd, metrics=metrics)
temp1.fit(x2_in, layer_outs1[i], batch_size=batch_size, epochs=pre_epoch, shuffle=True, verbose=0)
score = temp1.evaluate(x2_in,layer_outs1[i], batch_size=20, verbose=1)
print(score)
print('cross 2->1')
temp2.compile(loss=classloss, optimizer=sgd, metrics=metrics)
temp2.fit(x1_in, layer_outs2[i], batch_size=batch_size, epochs=pre_epoch, shuffle=True, verbose=0)
score = temp2.evaluate(x1_in,layer_outs2[i], batch_size=20, verbose=1)
print(score)
print('cross 1->2')
# print(Auto1.layers[0].get_weights())
w1 = numpy.concatenate((Auto1.model.layers[i].get_weights()[0], temp1.model.layers[1].get_weights()[0]), axis=0)
b1 = Auto1.model.layers[i].get_weights()[1] + temp1.model.layers[1].get_weights()[1]
w2 = numpy.concatenate((temp2.model.layers[1].get_weights()[0], Auto2.model.layers[i].get_weights()[0]), axis=0)
b2 = temp2.model.layers[1].get_weights()[1] + Auto2.model.layers[i].get_weights()[1]
w0 = numpy.hstack((w1, w2))/2
b0 = numpy.hstack((b1, b2))/2
self.layers[i-1].set_weights([w0, b0])
x1_in = layer_outs1[i]
x2_in = layer_outs2[i]
del temp1
del temp2
print('after cross')
set1 = numpy.concatenate((x1, x2), axis=1)
set2 = numpy.concatenate((2*x1, numpy.zeros(x2.shape)), axis=1)
set3 = numpy.concatenate((numpy.zeros(x1.shape), 2*x2), axis=1)
dataset = numpy.concatenate((set1, set2, set3), axis=0)
supervised_set = numpy.concatenate((y, y, y), axis=0)
if unsupervised_train:
un_set = numpy.concatenate((layer_outs1[-2][0], layer_outs2[-2][0]), axis=1)
unsupervised_set = numpy.concatenate((un_set, un_set, un_set), axis=0)
self.compile(loss=loss, optimizer=sgd, metrics=metrics)
self.fit(dataset, unsupervised_set,
batch_size=batch_size,
epochs=epochs,
verbose=verbose,
callbacks=callbacks,
validation_split=validation_split,
validation_data=validation_data,
shuffle=shuffle,
class_weight=class_weight,
sample_weight=sample_weight,
initial_epoch=initial_epoch)
if supervised_train:
self.add(Dense(y.shape[1], activation=lastLoss, input_shape=(hid1[-1]+hid2[-1],)))
self.compile(loss=classloss, optimizer=sgd, metrics=metrics)
self.fit(dataset, supervised_set,
batch_size=batch_size,
epochs=epochs,
verbose=verbose,
callbacks=callbacks,
validation_split=validation_split,
validation_data=validation_data,
shuffle=shuffle,
class_weight=class_weight,
sample_weight=sample_weight,
initial_epoch=initial_epoch)