04c-mmf_sae.py 4.21 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 *
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]:


hidden_size=[ 100,  80, 50 , 20 ]
input_activation="relu"
output_activation="relu"
loss="mse"
epochs=3000
batch=1
patience=20
do_do=[ 0 ] * len(hidden_size)
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 :
    sgd_repr=sgd.get_config()["name"]
except AttributeError :
    sgd_repr=sgd

params={ "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,
	"patience" : patience,
        "sgd" : sgd_repr}
name = "_".join([ str(x) for x in params.values()])
try:
    os.mkdir("{}/SAE_{}".format(in_dir,name))
except:
    pass
db = shelve.open("{}/SAE_{}/ae_model.shelve".format(in_dir,name),writeback=True)
#
json.dump(params,
	open("{}/SAE_{}/ae_model.json".format(in_dir,name),"w"),
	indent=4)

keys = ["ASR","TRS"]

mlp_h = [ 150 , 300 ]
mlp_loss ="categorical_crossentropy"
mlp_dropouts = [0,0,0,0]
mlp_sgd = Adam(0.001)
mlp_epochs = 2000
mlp_batch_size = 8

db["SAE"] = {}

db["SAEFT"] = {}
for mod in keys : 
    print "MODE ", mod
    res_tuple=train_sae(infer_model["LDA"][mod]["TRAIN"],infer_model["LDA"][mod]["DEV"],
                 infer_model["LDA"][mod]["TEST"],
                 hidden_size,dropouts=do_do,
                 patience = params["patience"],sgd=sgd,input_activation="tanh",
                 output_activation="tanh",loss=loss,epochs=epochs,
                 batch_size=batch,verbose=0)
    #print len(res), [len(x) for x in res[0]], [ len(x) for x in res[1]]
    for name , levels in zip(["SAE","SAEFT"],res_tuple):
        print "NAME", name
        mlp_res_by_level = []
        for res in levels:
            mlp_res_list=[]
            for nb,layer in enumerate(res) :
                print "layer NB",nb
                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,batch_size=mlp_batch_size,
                                              fit_verbose=0))
            mlp_res_by_level.append(mlp_res_list)
        db[name][mod]=mlp_res_by_level

mod = "ASR"
mod2= "TRS"
print "mode SPE "
res_tuple = train_sae(infer_model["LDA"][mod]["TRAIN"],
                      infer_model["LDA"][mod]["DEV"],
                      infer_model["LDA"][mod]["TEST"],
                      hidden_size,dropouts=[0],patience=params["patience"],
                      sgd=sgd,input_activation=input_activation,output_activation=input_activation,
                      loss=loss,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 name , levels in zip(["SAE","SAEFT"],res_tuple):
    mlp_res_by_level = []
    for res in levels : 
        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,batch_size=mlp_batch_size,
                                fit_verbose=0))
        mlp_res_by_level.append(mlp_res_list)
    db[name]["SPE"] = mlp_res_by_level

db.close()