04e-mm_vae.py 4.85 KB
# 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 *
from vae 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]:


hidden_size= [60]
input_activation="tanh"
output_activation="sigmoid"
epochs=300
batch=1
patience=60
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)
latent_dim = 30



mlp_h = [ 256 ]
mlp_loss = "categorical_crossentropy"
mlp_dropouts = []
mlp_sgd = Adam(lr=0.001)
mlp_epochs = 1000
mlp_batch_size = 16
mlp_output_activation="softmax"

try :
    sgd_repr=sgd.get_config()["name"]
except AttributeError :
    sgd_repr=sgd

try :
    mlp_sgd_repr=mlp_sgd.get_config()["name"]
except AttributeError :
    mlp_sgd_repr=mlp_sgd


params={ "h1" : "_".join([ str(x) for x in hidden_size ]),
	"inside_activation" : input_activation,
	"output_activation" : output_activation,
	"epochs" : epochs ,
	"batch_size" : batch,
	"patience" : patience,
        "sgd" : sgd_repr,
        "mlp_h ": "_".join([str(x) for x in mlp_h]),
        "mlp_loss ": mlp_loss,
        "mlp_dropouts ": "_".join([str(x) for x in mlp_dropouts]),
        "mlp_sgd ": mlp_sgd_repr,
        "mlp_epochs ": mlp_epochs,
        "mlp_batch_size ": mlp_batch_size,
        "mlp_output" : mlp_output_activation
        }
name = "_".join([ str(x) for x in params.values()])
try:
    os.mkdir("{}/VAE_{}".format(in_dir,name))
except:
    pass
db = shelve.open("{}/VAE_{}/ae_model.shelve".format(in_dir,name),writeback=True)
db["params"] = params
db["LABEL"]=infer_model["LABEL"]
#
json.dump(params,
	open("{}/VAE_{}/ae_model.json".format(in_dir,name),"w"),
	indent=4)

keys = ["ASR","TRS"]

db["VAE"] = {}
db["LDA"] = {}
for mod in keys : 
    print mod
    db["LDA"][mod] = train_mlp(infer_model["LDA"][mod]["TRAIN"],infer_model["LABEL"][mod]["TRAIN"],
                            infer_model["LDA"][mod]["DEV"],infer_model["LABEL"][mod]["DEV"],
                            infer_model["LDA"][mod]["TEST"],infer_model["LABEL"][mod]["TEST"],
                            mlp_h ,sgd=mlp_sgd,
                            epochs=mlp_epochs,
                            batch_size=mlp_batch_size,
                            input_activation=input_activation,
                            output_activation=mlp_output_activation,
                            dropouts=mlp_dropouts,
                            fit_verbose=0)

    res=train_vae(infer_model["LDA"][mod]["TRAIN"],infer_model["LDA"][mod]["DEV"],infer_model["LDA"][mod]["TEST"],
                 hidden_size=hidden_size[0],
                 latent_dim=latent_dim,sgd=sgd,
                 input_activation=input_activation,output_activation=output_activation,
                 nb_epochs=epochs,batch_size=batch)
    mlp_res_list=[]
    for layer in res :
        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["VAE"][mod]=mlp_res_list

mod = "ASR"
mod2= "TRS"
mlp_res_list=[]

res = train_vae(infer_model["LDA"][mod]["TRAIN"],
                infer_model["LDA"][mod]["DEV"],
                infer_model["LDA"][mod]["TEST"],
                hidden_size=hidden_size[0],
                sgd=sgd,input_activation=input_activation,output_activation=output_activation,
                latent_dim=latent_dim,
                nb_epochs=epochs,
                batch_size=batch,
                y_train=infer_model["LDA"][mod2]["TRAIN"],
                y_dev=infer_model["LDA"][mod2]["DEV"],
                y_test=infer_model["LDA"][mod2]["TEST"])

for layer in res :
    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["VAE"]["SPE"] = mlp_res_list

db.sync()
db.close()