Blame view
LDA/04d-mmf_dsae.py
9.16 KB
7db73861f add vae et mmf |
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 |
# coding: utf-8 # In[2]: # Import import gensim from scipy import sparse import itertools from sklearn import preprocessing from keras.models import Sequential from keras.optimizers import SGD,Adam from mlp import * import mlp import sklearn.metrics import shelve import pickle from utils import * import sys import os import json # In[4]: infer_model=shelve.open("{}".format(sys.argv[2])) in_dir = sys.argv[1] #['ASR', 'TRS', 'LABEL'] # In[6]: # AE params hidden_size=[ 100, 100 ] input_activation="relu" output_activation="relu" loss="mse" epochs= 1000 batch_size=1 patience=20 do_do=[ 0.25 ] * len(hidden_size) sgd = Adam(lr=0.00001)#SGD(lr=0.00001,nesterov=False) #'rmsprop'# Adam(lr=0.00001)#SGD(lr=0.001, momentum=0.9, nesterov=True) try : sgd_repr=sgd.get_config()["name"] except AttributeError : sgd_repr=sgd # Transforme : trans_hidden_size=[ 300 , 300 ] trans_input_activation="relu" trans_output_activation="relu" trans_loss="mse" trans_epochs=1000 trans_batch_size=8 trans_patience=20 trans_do=[ 0.25 ] * len(trans_hidden_size) trans_sgd = Adam(lr=0.0001)#SGD(lr=0.00001,nesterov=False) #'rmsprop'# Adam(lr=0.00001)#SGD(lr=0.001, momentum=0.9, nesterov=True) try : trans_sgd_repr=trans_sgd.get_config()["name"] except AttributeError : trans_sgd_repr=trans_sgd ae={ "h1" : "_".join([str(x) for x in hidden_size]), "inside_activation" : input_activation, "out_activation" : output_activation, "do_dropout": "_".join([str(x) for x in do_do]), "loss" : loss, "epochs" : epochs , "batch_size" : batch_size, "patience" : patience, "sgd" : sgd_repr} name = "_".join([ str(x) for x in ae.values()]) trans={ "h1" : "_".join([str(x) for x in trans_hidden_size]), "inside_activation" : trans_input_activation, "out_activation" : trans_output_activation, "do_dropout": "_".join([str(x) for x in trans_do]), "loss" : trans_loss, "epochs" : trans_epochs , "batch_size" : trans_batch_size, "patience" : trans_patience, "sgd" : trans_sgd_repr} mlp_h = [ 300 , 300 ] mlp_loss ="categorical_crossentropy" mlp_dropouts = [0,0,0,0] mlp_sgd = Adam(0.0001) mlp_epochs = 1000 mlp_batch_size = 8 mlp_input_activation = "relu" mlp_output_activation = "softmax" try : mlp_sgd_repr=mlp_sgd.get_config()["name"] except AttributeError : mlp_sgd_repr=mlp_sgd mlp={ "h1" : "_".join([str(x) for x in mlp_h ]), "inside_activation" : mlp_input_activation, "out_activation" : mlp_output_activation, "do_dropout": "_".join([str(x) for x in mlp_dropouts]), "loss" : mlp_loss, "epochs" : mlp_epochs , "batch_size" : mlp_batch_size, "sgd" : mlp_sgd_repr} params = { "ae":ae, "trans":trans, "mlp":mlp} try: os.mkdir("{}/DSAE_{}".format(in_dir,name)) except: pass db = shelve.open("{}/DSAE_{}/ae_model.shelve".format(in_dir,name),writeback=True) # json.dump(params, open("{}/DSAE_{}/ae_model.json".format(in_dir,name),"w"), indent=4) keys = ["ASR","TRS"] db["DSAE"] = {} db["DSAEFT"] = {} mod = "ASR" res_tuple_ASR = train_ae(infer_model["LDA"][mod]["TRAIN"], infer_model["LDA"][mod]["DEV"], infer_model["LDA"][mod]["TEST"], hidden_size,dropouts=do_do, patience = patience,sgd=sgd, input_activation=input_activation, output_activation=output_activation,loss=loss,epochs=epochs, batch_size=batch_size,verbose=0,get_weights=True) mlp_res_list = [] for layer in res_tuple_ASR[0]: mlp_res_list.append(train_mlp(layer[0],infer_model['LABEL'][mod]["TRAIN"], layer[1],infer_model["LABEL"][mod]["DEV"], layer[2],infer_model["LABEL"][mod]["TEST"], mlp_h,loss=mlp_loss,dropouts=mlp_dropouts, sgd=mlp_sgd,epochs=mlp_epochs, output_activation=mlp_output_activation, input_activation=mlp_input_activation, batch_size=mlp_batch_size,fit_verbose=0)) db["DSAE"][mod] = mlp_res_list mod = "TRS" print hidden_size res_tuple_TRS = train_ae(infer_model["LDA"][mod]["TRAIN"], infer_model["LDA"][mod]["DEV"], infer_model["LDA"][mod]["TEST"], hidden_size,dropouts=do_do, sgd=sgd,input_activation=input_activation, output_activation=output_activation,loss=loss,epochs=epochs, batch_size=batch_size,patience=patience, verbose=0,get_weights=True) mlp_res_list = [] for layer in res_tuple_TRS[0]: mlp_res_list.append(train_mlp(layer[0],infer_model['LABEL'][mod]["TRAIN"], layer[1],infer_model["LABEL"][mod]["DEV"], layer[2],infer_model["LABEL"][mod]["TEST"], mlp_h,loss=mlp_loss,dropouts=mlp_dropouts, sgd=mlp_sgd,epochs=mlp_epochs, output_activation=mlp_output_activation, input_activation=mlp_input_activation, batch_size=mlp_batch_size,fit_verbose=0)) db["DSAE"][mod] = mlp_res_list transfert = [] print " get weight trans" for asr_pred, trs_pred in zip(res_tuple_ASR[0], res_tuple_TRS[0]): print "ASR", [ x.shape for x in asr_pred] print "TRS", [ x.shape for x in trs_pred] print for asr_pred, trs_pred in zip(res_tuple_ASR[0], res_tuple_TRS[0]): print "ASR", [ x.shape for x in asr_pred] print "TRS", [ x.shape for x in trs_pred] transfert.append( train_ae(asr_pred[0], asr_pred[1], asr_pred[2], trans_hidden_size, dropouts=trans_do, y_train = trs_pred[0], y_dev=trs_pred[1], y_test = trs_pred[2], patience = trans_patience,sgd=trans_sgd, input_activation=trans_input_activation, output_activation=trans_output_activation, loss=trans_loss, epochs=trans_epochs, batch_size=trans_batch_size,verbose=0,get_weights=True) ) mod = "ASR" mlp_res_bylvl = [] print " MLP on transfert " for level, w in transfert : mlp_res_list = [] for layer in level : mlp_res_list.append(train_mlp(layer[0],infer_model['LABEL'][mod]["TRAIN"], layer[1],infer_model["LABEL"][mod]["DEV"], layer[2],infer_model["LABEL"][mod]["TEST"], mlp_h,loss=mlp_loss,dropouts=mlp_dropouts, sgd=mlp_sgd,epochs=mlp_epochs, output_activation=mlp_output_activation, input_activation=mlp_input_activation, batch_size=mlp_batch_size,fit_verbose=0)) mlp_res_bylvl.append(mlp_res_list) db["DSAE"]["transfert"] = mlp_res_bylvl print " FT " WA = res_tuple_ASR[1] print "WA", len(WA), [ len(x) for x in WA] WT = res_tuple_TRS[1] print "WT", len(WT), [ len(x) for x in WT] Wtr = [ x[1] for x in transfert] print "Wtr", len(Wtr), [ len(x) for x in Wtr],[ len(x[1]) for x in Wtr] ft_res = ft_dsae(infer_model["LDA"]["ASR"]["TRAIN"], infer_model["LDA"]["ASR"]["DEV"], infer_model["LDA"]["ASR"]["TEST"], y_train=infer_model["LDA"]["TRS"]["TRAIN"], y_dev=infer_model["LDA"]["TRS"]["DEV"], y_test=infer_model["LDA"]["TRS"]["TEST"], ae_hidden = hidden_size, transfer_hidden = trans_hidden_size, start_weights = WA, transfer_weights = Wtr, end_weights = WT, input_activation = input_activation, output_activation = output_activation, ae_dropouts= do_do, transfer_do = trans_do, sgd = sgd, loss = loss , patience = patience, batch_size = batch_size, epochs= epochs) mlps_by_lvls= [] for level in ft_res : mlp_res_list = [] for layer in level : mlp_res_list.append(train_mlp(layer[0],infer_model['LABEL'][mod]["TRAIN"], layer[1],infer_model["LABEL"][mod]["DEV"], layer[2],infer_model["LABEL"][mod]["TEST"], mlp_h,loss=mlp_loss,dropouts=mlp_dropouts, sgd=mlp_sgd,epochs=mlp_epochs, output_activation=mlp_output_activation, input_activation=mlp_input_activation, batch_size=mlp_batch_size,fit_verbose=0)) mlps_by_lvls.append(mlp_res_list) db["DSAEFT"]["transfert"] = mlps_by_lvls db.close() |