Commit 91aeb914f7a4a592c9645fb28e6f39f9a73116df

Authored by Killian
1 parent d1012a7a16
Exists in master

add Botttleneck MLp

Showing 1 changed file with 120 additions and 6 deletions Side-by-side Diff

... ... @@ -6,13 +6,15 @@
6 6 from keras.models import Sequential
7 7 from keras.layers import Input, Dense, Dropout
8 8 from keras.models import Model
  9 +from keras.callbacks import ModelCheckpoint, EarlyStopping
9 10 from keras.utils.layer_utils import layer_from_config
10 11 from itertools import izip_longest
11   -
  12 +import tempfile
  13 +import shutil
12 14 import pandas
13 15 from collections import namedtuple
14 16 from sklearn.metrics import accuracy_score as perf
15   -save_tuple= namedtuple("save_tuple",["pred_train","pred_dev","pred_test"])
  17 +save_tuple = namedtuple("save_tuple",["pred_train","pred_dev","pred_test"])
16 18  
17 19  
18 20 def ft_dsae(train,dev,test,
19 21  
... ... @@ -74,12 +76,114 @@
74 76 layers.append(Dense(y_train.shape[1],activation=output_activation)(layers[-1]))
75 77 models = [Model(input=layers[0] , output=x) for x in layers[1:]]
76 78 models[-1].compile(optimizer=sgd,loss=loss)
77   - models[-1].fit(train,y_train,nb_epoch=epochs,batch_size=batch_size,callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=patience, verbose=0)],validation_data=(dev,dev),verbose=verbose)
  79 + models[-1].fit(train,y_train,nb_epoch=epochs,batch_size=batch_size,callbacks=[EarlyStopping(monitor='val_loss', patience=patience, verbose=0)],validation_data=(dev,dev),verbose=verbose)
78 80 predictions = [ [x.predict(y) for y in param_predict ] for x in models ]
79 81 pred_by_level.append(predictions)
80 82  
81 83 return pred_by_level
82 84  
  85 +def train_mlp_proj(x_train,y_train,x_dev,y_dev,x_test,y_test,hidden_size,input_activation="relu",hidden_activation="relu",output_activation="softmax",loss="mse",init="glorot_uniform",dropouts=None,sgd=None,epochs=1200,batch_size=16,fit_verbose=1,patience=20,test_verbose=0):
  86 +
  87 + #model_tempfile=tempfile.mkstemp()
  88 + tempfold = tempfile.mkdtemp()
  89 + model_tempfile= tempfold+"/model.hdf"
  90 +
  91 + layers = [Input(shape=(x_train.shape[1],))]
  92 +
  93 + for h in hidden_size:
  94 + print h
  95 + if dropouts:
  96 + d = dropouts.pop(0)
  97 + if d > 0 :
  98 + ldo = Dropout(d)(layers[-1])
  99 + print 'append'
  100 + layers.append(Dense(h,init=init,activation=input_activation)(ldo))
  101 + else :
  102 + print " append"
  103 + layers.append(Dense(h,init=init,activation=input_activation)(layers[-1]))
  104 +
  105 +
  106 + if dropouts:
  107 + d = dropouts.pop(0)
  108 + if d > 0 :
  109 + ldo =Dropout(d)(layers[-1])
  110 + print "end"
  111 + layers.append(Dense( y_train.shape[1],activation=output_activation,init=init)(ldo))
  112 + else:
  113 + print "end"
  114 + layers.append(Dense( y_train.shape[1],activation=output_activation,init=init)(layers[-1]))
  115 +
  116 + models = []
  117 + for l in layers[1:] :
  118 + models.append(Model(layers[0] , l))
  119 + print "nb models : ", len(models), "h :",hidden_size , "layer", len(layers)
  120 + if not sgd:
  121 + sgd = SGD(lr=0.01, decay=0, momentum=0.9)
  122 +
  123 + models[-1].compile(loss=loss, optimizer=sgd,metrics=['accuracy'])
  124 + callbacks = [ModelCheckpoint(model_tempfile, monitor='val_acc', verbose=test_verbose, save_best_only=True, save_weights_only=True, mode='auto'),
  125 + EarlyStopping(monitor='val_acc', patience=patience, verbose=test_verbose) ] # On pourrai essayer avec la loss aussi
  126 + print models[-1].summary()
  127 + hist=models[-1].fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size,verbose=fit_verbose,validation_data=(x_dev,y_dev),callbacks=callbacks)
  128 + models[-1].load_weights(model_tempfile, by_name=False)
  129 + proj = []
  130 + for layer,model in enumerate(models):
  131 + proj.append((model.predict(x_train),model.predict(x_dev),model.predict(x_test)))
  132 +
  133 + shutil.rmtree(tempfold)
  134 + return models[-1].summary(),proj
  135 +
  136 +
  137 +
  138 +
  139 +
  140 +def train_mlp_pred(x_train,y_train,x_dev,y_dev,x_test,y_test,hidden_size,input_activation="relu",hidden_activation="relu",output_activation="softmax",loss="mse",init="glorot_uniform",dropouts=None,sgd=None,epochs=1200,batch_size=16,fit_verbose=1,patience=20,test_verbose=0):
  141 +
  142 + #model_tempfile=tempfile.mkstemp()
  143 + tempfold = tempfile.mkdtemp()
  144 + model_tempfile= tempfold+"/model.hdf"
  145 +
  146 + layers = [Input(shape=(x_train.shape[1],))]
  147 +
  148 + for h in hidden_size:
  149 + if dropouts:
  150 + d = dropouts.pop(0)
  151 + if d > 0 :
  152 + ldo = Dropout(d)(layers[-1])
  153 + layers.append(Dense(h,init=init,activation=input_activation)(ldo))
  154 + else :
  155 + layers.append(Dense(h,init=init,activation=input_activation)(layers[-1]))
  156 +
  157 +
  158 + if dropouts:
  159 + d = dropouts.pop(0)
  160 + if d > 0 :
  161 + ldo =Dropout(d)(layers[-1])
  162 + layers.append(Dense( y_train.shape[1],activation=output_activation,init=init)(ldo))
  163 + else:
  164 + layers.append(Dense( y_train.shape[1],activation=output_activation,init=init)(layers[-1]))
  165 +
  166 + model=Model(layers[0] , layers[-1])
  167 + if not sgd:
  168 + sgd = SGD(lr=0.01, decay=0, momentum=0.9)
  169 +
  170 + model.compile(loss=loss, optimizer=sgd,metrics=['accuracy'])
  171 + callbacks = [ModelCheckpoint(model_tempfile, monitor='val_acc', verbose=test_verbose, save_best_only=True, save_weights_only=True, mode='auto'),
  172 + EarlyStopping(monitor='val_acc', patience=patience, verbose=test_verbose) ] # On pourrai essayer avec la loss aussi
  173 + print model.summary()
  174 + hist=model.fit(x_train, y_train, nb_epoch=epochs, batch_size=batch_size,verbose=fit_verbose,validation_data=(x_dev,y_dev),callbacks=callbacks)
  175 + model.load_weights(model_tempfile, by_name=False)
  176 + pred=(model.predict(x_train),model.predict(x_dev),model.predict(x_test))
  177 +
  178 + shutil.rmtree(tempfold)
  179 + return pred,hist
  180 +
  181 +
  182 +
  183 +
  184 +
  185 +
  186 +
83 187 def train_mlp(x_train,y_train,x_dev,y_dev,x_test,y_test,hidden_size,input_activation="relu",hidden_activation="relu",output_activation="softmax",loss="mse",init="glorot_uniform",dropouts=None,sgd=None,epochs=1200,batch_size=16,fit_verbose=1,test_verbose=0,save_pred=False,keep_histo=False):
84 188  
85 189 layers = [Input(shape=(x_train.shape[1],))]
... ... @@ -107,7 +211,7 @@
107 211 d = dropouts.pop(0)
108 212 if d > 0 :
109 213 layers.append(Dropout(d)(layers[-1]))
110   -
  214 + print y_train[2:10]
111 215 layers.append(Dense( y_train.shape[1],activation=output_activation,init=init)(layers[-1]))
112 216  
113 217 model = Model(layers[0] , layers[-1])
... ... @@ -147,7 +251,7 @@
147 251 res.append(hist)
148 252 return res
149 253  
150   -def train_ae(train,dev,test,hidden_sizes,y_train=None,y_dev=None,y_test=None,dropouts=None,input_activation="tanh",output_activation="tanh",loss="mse",sgd=None,epochs=500,batch_size=8,verbose=1,patience=20,get_weights=False,set_weights=[]):
  254 +def train_ae(train,dev,test,hidden_sizes,y_train=None,y_dev=None,y_test=None,dropouts=None,input_activation="tanh",output_activation="tanh",loss="mse",sgd=None,epochs=500,batch_size=8,test_verbose=0,verbose=1,patience=20,get_weights=False,set_weights=[],best_mod=False):
151 255  
152 256 input_vect = Input(shape=(train.shape[1],))
153 257  
... ... @@ -193,7 +297,17 @@
193 297 models = [Model(input=previous[0] , output=x) for x in previous[1:]]
194 298 print "MLP", sgd, loss
195 299 models[-1].compile(optimizer=sgd,loss=loss)
196   - models[-1].fit(train,y_train,nb_epoch=epochs,batch_size=batch_size,callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss', patience=patience, verbose=0)],validation_data=(dev,dev),verbose=verbose)
  300 + cb = [EarlyStopping(monitor='val_loss', patience=patience, verbose=0)]
  301 + if best_mod:
  302 + tempfold = tempfile.mkdtemp()
  303 + model_tempfile= tempfold+"/model.hdf"
  304 + cb.append( ModelCheckpoint(model_tempfile, monitor='val_loss', verbose=test_verbose, save_best_only=True, save_weights_only=True, mode='auto') )
  305 +
  306 + models[-1].summary()
  307 + models[-1].fit(train,y_train,nb_epoch=epochs,batch_size=batch_size,callbacks=cb,validation_data=(dev,dev),verbose=verbose)
  308 + if best_mod:
  309 + models[-1].load_weights(model_tempfile)
  310 + shutil.rmtree(tempfold)
197 311 param_predict = [ train, dev, test ]
198 312 if predict_y :
199 313 param_predict += [ y_train, y_dev ,y_test ]