Blame view

LDA/04b-mmf_mini_ae.py 6.74 KB
7db73861f   Killian   add vae et mmf
1
2
3
4
5
6
7
8
9
10
11
12
  
  # 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
2af8e57f4   Killian   change all
13
  from keras.layers.advanced_activations import ELU,PReLU
7db73861f   Killian   add vae et mmf
14
15
16
17
18
19
20
21
22
23
24
25
26
27
  from mlp import *
  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]:
2af8e57f4   Killian   change all
28
29
30
31
32
  if len(sys.argv) > 4 :
      features_key = sys.argv[4]
  else :
      features_key = "LDA"
  save_projection = True
e5108393c   Killian   replace du mlp.p...
33
34
35
36
  json_conf =json.load(open(sys.argv[3]))
  ae_conf = json_conf["ae"]
  
  hidden_size= ae_conf["hidden_size"]
2af8e57f4   Killian   change all
37
38
39
40
41
42
43
44
45
  input_activation = None
  print ae_conf["input_activation"]
  if ae_conf["input_activation"] == "elu":
      print " ELU"
      input_activation = PReLU()
  else:
      print " ELSE"
      input_activation = ae_conf["input_activation"]
  #input_activation=ae_conf["input_activation"]
e5108393c   Killian   replace du mlp.p...
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
  output_activation=ae_conf["output_activation"]
  loss=ae_conf["loss"]
  epochs=ae_conf["epochs"]
  batch=ae_conf["batch"]
  patience=ae_conf["patience"]
  do_do=ae_conf["do"]
  try:
      k = ae_conf["sgd"]
      if ae_conf["sgd"]["name"] == "adam":
          sgd = Adam(lr=ae_conf["sgd"]["lr"])#SGD(lr=0.00001,nesterov=False) #'rmsprop'# Adam(lr=0.00001)#SGD(lr=0.001, momentum=0.9, nesterov=True)
      elif ae_conf["sgd"]["name"] == "sgd":
          sgd = SGD(lr=ae_conf["sgd"]["lr"])
  except: 
      sgd = ae_conf["sgd"]
  
  mlp_conf = json_conf["mlp"]
  mlp_h = mlp_conf["hidden_size"]
  mlp_loss = mlp_conf["loss"]
  mlp_dropouts = mlp_conf["do"]
  mlp_epochs = mlp_conf["epochs"]
  mlp_batch_size = mlp_conf["batch"]
  mlp_input_activation=mlp_conf["input_activation"]
  mlp_output_activation=mlp_conf["output_activation"]
7db73861f   Killian   add vae et mmf
69

e5108393c   Killian   replace du mlp.p...
70
71
72
73
74
75
76
77
78
79
80
  try:
      k = mlp_conf["sgd"]
      if mlp_conf["sgd"]["name"] == "adam":
          mlp_sgd = Adam(lr=mlp_conf["sgd"]["lr"])#SGD(lr=0.00001,nesterov=False) #'rmsprop'# Adam(lr=0.00001)#SGD(lr=0.001, momentum=0.9, nesterov=True)
      elif mlp_conf["sgd"]["name"] == "sgd":
          mlp_sgd = SGD(lr=mlp_conf["sgd"]["lr"])
  except: 
      mlp_sgd = mlp_conf["sgd"]
  
  
  name = json_conf["name"]
7db73861f   Killian   add vae et mmf
81
82
83
84
85
  try:
      os.mkdir("{}/{}".format(in_dir,name))
  except:
      pass
  db = shelve.open("{}/{}/ae_model.shelve".format(in_dir,name),writeback=True)
7db73861f   Killian   add vae et mmf
86
87
  db["LABEL"]=infer_model["LABEL"]
  #
2af8e57f4   Killian   change all
88
  keys = infer_model[features_key].keys()
7db73861f   Killian   add vae et mmf
89
90
  
  db["AE"] = {}
2af8e57f4   Killian   change all
91
  db[features_key] = {}
7db73861f   Killian   add vae et mmf
92
  for mod in keys : 
2af8e57f4   Killian   change all
93
94
95
96
97
98
99
      print infer_model[features_key][mod]["TRAIN"].shape
      print infer_model[features_key][mod]["DEV"].shape
      print infer_model[features_key][mod]["TEST"].shape
  
      db[features_key][mod] = train_mlp(infer_model[features_key][mod]["TRAIN"],infer_model["LABEL"][mod]["TRAIN"],
                              infer_model[features_key][mod]["DEV"],infer_model["LABEL"][mod]["DEV"],
                              infer_model[features_key][mod]["TEST"],infer_model["LABEL"][mod]["TEST"],
7db73861f   Killian   add vae et mmf
100
101
102
                              mlp_h ,sgd=mlp_sgd,
                              epochs=mlp_epochs,
                              batch_size=mlp_batch_size,
e5108393c   Killian   replace du mlp.p...
103
                              input_activation=mlp_input_activation,
7db73861f   Killian   add vae et mmf
104
105
106
                              output_activation=mlp_output_activation,
                              dropouts=mlp_dropouts,
                              fit_verbose=0)
2af8e57f4   Killian   change all
107
108
      print input_activation
      res=train_ae(infer_model[features_key][mod]["TRAIN"],infer_model[features_key][mod]["DEV"],infer_model[features_key][mod]["TEST"],
e5108393c   Killian   replace du mlp.p...
109
                   hidden_size,patience = patience,sgd=sgd,
7db73861f   Killian   add vae et mmf
110
111
112
                   dropouts=do_do,input_activation=input_activation,output_activation=output_activation,
                   loss=loss,epochs=epochs,batch_size=batch,verbose=0)
      mlp_res_list=[]
2af8e57f4   Killian   change all
113
114
115
116
117
118
119
120
121
122
123
124
125
      for nb,layer in enumerate(res) :
          if save_projection:
              pd = pandas.DataFrame(layer[0])
              col_count = (pd.sum(axis=0) != 0)
              pd = pd.loc[:,col_count]
              pd.to_hdf("{}/{}/AE_{}_{}_df.hdf".format(in_dir,name,nb,mod),"TRAIN")
              pd = pandas.DataFrame(layer[1])
              pd = pd.loc[:,col_count]
              pd.to_hdf("{}/{}/AE_{}_{}_df.hdf".format(in_dir,name,nb,mod),"DEV")
              pd = pandas.DataFrame(layer[2])
              pd = pd.loc[:,col_count]
              pd.to_hdf("{}/{}/AE_{}_{}_df.hdf".format(in_dir,name,nb,mod),"TEST")
              del pd
7db73861f   Killian   add vae et mmf
126
127
128
129
130
          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,
e5108393c   Killian   replace du mlp.p...
131
                                        input_activation=mlp_input_activation,
7db73861f   Killian   add vae et mmf
132
133
                                        batch_size=mlp_batch_size,fit_verbose=0))
      db["AE"][mod]=mlp_res_list
2af8e57f4   Killian   change all
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
  if "ASR" in keys and "TRS" in keys:
      mod = "ASR"
      mod2= "TRS"
      mlp_res_list=[]
  
      res = train_ae(infer_model[features_key][mod]["TRAIN"],
                      infer_model[features_key][mod]["DEV"],
                      infer_model[features_key][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,
                      y_train=infer_model[features_key][mod]["TRAIN"],
                      y_dev=infer_model[features_key][mod2]["DEV"],
                      y_test=infer_model[features_key][mod2]["TEST"])
  
      for nb,layer in enumerate(res) :
          if save_projection:
              pd = pandas.DataFrame(layer[0])
              col_count= (pd.sum(axis=0) != 0)
              pd = pd.loc[:,col_count]
              pd.to_hdf("{}/{}/AE_{}_{}_df.hdf".format(in_dir,name,nb,"SPE"),"TRAIN")
              pd = pandas.DataFrame(layer[1])
              pd = pd.loc[:,col_count]
              pd.to_hdf("{}/{}/AE_{}_{}_df.hdf".format(in_dir,name,nb,"SPE"),"DEV")
              pd = pandas.DataFrame(layer[2])
              pd = pd.loc[:,col_count]
              pd.to_hdf("{}/{}/AE_{}_{}_df.hdf".format(in_dir,name,nb,"SPE"),"TEST")
              del pd
  
          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=input_activation,
                                        batch_size=mlp_batch_size,fit_verbose=0))
  
      db["AE"]["SPE"] = mlp_res_list
7db73861f   Killian   add vae et mmf
172
173
174
  
  db.sync()
  db.close()