DECODA_binary_BOW_AEINIT_TANH_MODELS.py 10.2 KB
# coding: utf-8

# In[2]:

# Import
import pandas
# Alignement
from alignment.sequence import Sequence
from alignment.vocabulary import Vocabulary
from alignment.sequencealigner import *
import nltk
import codecs
import gensim
from scipy import sparse
import itertools
from sklearn.feature_extraction.text import CountVectorizer
import scipy.sparse
import scipy.io
from sklearn import preprocessing
from keras.models import Sequential
from keras.layers.core import Dense, Dropout, Activation,AutoEncoder
from keras.optimizers import SGD,Adam
from keras.layers import containers
from mlp import *
import mlp
import sklearn.metrics
import shelve
import pickle
from utils import *
import sys
# In[4]:

db=shelve.open("{}.shelve".format(sys.argv[1]),writeback=True)

sparse_corp=shelve.open("DECODA_sparse.shelve")
# In[6]:
ASR_sparse=sparse_corp["ASR_SPARSE"]
TRS_sparse=sparse_corp["TRS_SPARSE"]

# In[11]:
hidden_size=3096
hidden_size2=2048
input_activation="tanh"
out_activation="tanh"
loss="mse"
epochs=500
batch=128
patience=40

print "gogo autoencoder ASR"
sgd = SGD(lr=0.0001)#, momentum=0.9, nesterov=True)
autoencode=Sequential()
autoencode.add(Dense(ASR_sparse["TRAIN"].shape[1],hidden_size,init='glorot_uniform',activation=input_activation))
autoencode.add(Dense(hidden_size,hidden_size2,init='glorot_uniform',activation=input_activation))
autoencode.add(Dense(hidden_size2,hidden_size,init="glorot_uniform",activation=out_activation))
autoencode.add(Dense(hidden_size,ASR_sparse["DEV"].todense().shape[1],init="glorot_uniform",activation=out_activation))

#autoencode.compile(optimizer=sgd,loss=loss)

autoencode.compile(optimizer=sgd,loss=loss)


# In[ ]:

autoencode.fit(ASR_sparse["TRAIN"].todense(),ASR_sparse["TRAIN"].todense(),nb_epoch=epochs,batch_size=batch,
               callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss',
                                                        patience=patience, verbose=0)],           validation_data=(ASR_sparse["DEV"].todense(),ASR_sparse["DEV"].todense()),verbose=1)


# In[ ]:

auto_decoder=Sequential()
auto_decoder.add(Dense(ASR_sparse["DEV"].todense().shape[1],hidden_size,init='uniform',activation=input_activation,weights=autoencode.get_weights()[:2]))
auto_decoder.add(Dense(hidden_size,hidden_size2,init='glorot_uniform',activation=input_activation,weights=autoencode.get_weights()[2:4]))
auto_decoder.add(Dense(hidden_size2,hidden_size,init='glorot_uniform',activation=input_activation,weights=autoencode.get_weights()[4:6]))
auto_decoder.compile(optimizer=sgd,loss=loss)


# In[77]:

#autoencode.predict(ASR_sparse["DEV"].todense())


# In[ ]:

print "auto encoder et auto decoder asr okay"

ASR_sparse_AE={}
for i in ASR_sparse.keys():
    ASR_sparse_AE[i]=auto_decoder.predict(ASR_sparse[i].todense())
    #TRS_sparse[i]=dico.transform(TRS[i][2])

db["ASR_AE_H2_RELU"]=ASR_sparse_AE


auto_decoder=Sequential()
auto_decoder.add(Dense(ASR_sparse["DEV"].todense().shape[1],hidden_size,init='uniform',activation=input_activation,weights=autoencode.get_weights()[:2]))
auto_decoder.add(Dense(hidden_size,hidden_size2,init='glorot_uniform',activation=input_activation,weights=autoencode.get_weights()[2:4]))
auto_decoder.compile(optimizer=sgd,loss=loss)

ASR_sparse_AE_H1={}
for i in ASR_sparse.keys():
    ASR_sparse_AE_H1[i]=auto_decoder.predict(ASR_sparse[i].todense())
    #TRS_sparse[i]=dico.transform(TRS[i][2])

db["ASR_AE_H1_RELU"]=ASR_sparse_AE_H1



print "auto encoder trs learning"
# In[68]:/
sgd_trs = SGD(lr=0.1,momentum=0.9)
autoencode_trs=Sequential()
autoencode_trs.add(Dense(TRS_sparse["DEV"].todense().shape[1],hidden_size,init='glorot_uniform',activation=input_activation))
autoencode_trs.add(Dense(hidden_size,hidden_size2,init='glorot_uniform',activation=input_activation))
autoencode_trs.add(Dense(hidden_size2,hidden_size,init="glorot_uniform",activation=out_activation))
autoencode_trs.add(Dense(hidden_size,TRS_sparse["DEV"].todense().shape[1],init="glorot_uniform",activation=out_activation))

#autoencode_trs.compile(optimizer=sgd_trs,loss=loss)

autoencode_trs.compile(optimizer=sgd,loss=loss)


# In[69]:

autoencode_trs.fit(TRS_sparse["TRAIN"].todense(),TRS_sparse["TRAIN"].todense(),nb_epoch=epochs,batch_size=batch,
               callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss',
                                                        patience=patience, verbose=0)],
               validation_data=(TRS_sparse["DEV"].todense(),TRS_sparse["DEV"].todense()),verbose=1)


# In[87]:

auto_decoder_trs=Sequential()
auto_decoder_trs.add(Dense(ASR_sparse["DEV"].todense().shape[1],hidden_size,activation=input_activation,weights=autoencode_trs.get_weights()[:2]))
auto_decoder_trs.add(Dense(hidden_size,hidden_size2,activation=input_activation,weights=autoencode_trs.get_weights()[2:4]))
auto_decoder_trs.add(Dense(hidden_size2,hidden_size,activation=input_activation,weights=autoencode_trs.get_weights()[4:6]))
auto_decoder_trs.compile(optimizer=sgd,loss=loss)


# In[88]:
print "auto encoder trs okay"
TRS_sparse_AE={}

for i in TRS_sparse.keys():
    TRS_sparse_AE[i]=auto_decoder_trs.predict(TRS_sparse[i].todense())
    #TRS_sparse[i]=dico.transform(TRS[i][2])

db["TRS_AE_H2_RELU"]=TRS_sparse_AE



auto_decoder_trs=Sequential()
auto_decoder_trs.add(Dense(ASR_sparse["DEV"].todense().shape[1],hidden_size,activation=input_activation,weights=autoencode_trs.get_weights()[:2]))
auto_decoder_trs.add(Dense(hidden_size,hidden_size2,activation=input_activation,weights=autoencode_trs.get_weights()[2:4]))
auto_decoder_trs.compile(optimizer=sgd,loss=loss)


# In[88]:
print "auto encoder trs okay"
TRS_sparse_AE_H1={}

for i in TRS_sparse.keys():
    TRS_sparse_AE_H1[i]=auto_decoder_trs.predict(TRS_sparse[i].todense())
    #TRS_sparse[i]=dico.transform(TRS[i][2])

db["TRS_AE_H1_RELU"]=TRS_sparse_AE


db.sync()




# In[261]:

#pred_dev= model_TRS_AE.predict(TRS_sparse_AE["DEV"],batch_size=1)

TRS_AE={}
ASR_AE={}
for i in TRS_sparse.keys():
    TRS_AE[i]=autoencode_trs.predict(TRS_sparse[i].todense())
    ASR_AE[i]=autoencode.predict(ASR_sparse[i].todense())


db["TRS_AE_OUT_RELU"]=TRS_AE
db["ASR_AE_OUT_RELU"]=ASR_AE

db.sync()
# # Transfert de couche
# ICI
spe_finetune=Sequential()
spe_finetune.add(Dense(TRS_sparse["DEV"].todense().shape[1],hidden_size,init='glorot_uniform',activation=input_activation,weights=autoencode.get_weights()[:2]))
spe_finetune.add(Dense(hidden_size,hidden_size2,init='glorot_uniform',activation=input_activation,weights=autoencode.get_weights()[2:4]))
spe_finetune.add(Dense(hidden_size2,hidden_size,init="glorot_uniform",activation=out_activation,weights=autoencode_trs.get_weights()[4:6]))
spe_finetune.add(Dense(hidden_size,TRS_sparse["DEV"].todense().shape[1],init="glorot_uniform",activation=out_activation,weights=autoencode_trs.get_weights()[6:8]))

#spe_finetune.compile(optimizer=sgd_trs,loss=loss)

spe_finetune.compile(optimizer=sgd,loss=loss)


auto_decoder_spe_finetune=Sequential()
auto_decoder_spe_finetune.add(Dense(ASR_sparse["DEV"].todense().shape[1],hidden_size,activation=input_activation,weights=autoencode.get_weights()[:2]))
auto_decoder_spe_finetune.add(Dense(hidden_size,hidden_size2,activation=input_activation,weights=autoencode.get_weights()[2:4]))
auto_decoder_spe_finetune.add(Dense(hidden_size2,hidden_size,init="glorot_uniform",activation=out_activation,weights=autoencode_trs.get_weights()[4:6]))
auto_decoder_spe_finetune.compile(optimizer=sgd,loss=loss)



spe_finetune_fixed=Sequential()
spe_finetune_fixed.add(Dense(hidden_size2,hidden_size,init="glorot_uniform",activation=out_activation,weights=autoencode_trs.get_weights()[4:6]))
spe_finetune_fixed.add(Dense(hidden_size,TRS_sparse["DEV"].todense().shape[1],init="glorot_uniform",activation=out_activation,weights=autoencode_trs.get_weights()[6:8]))
spe_finetune_fixed.compile(optimizer=sgd,loss=loss)


# In[88]:
print "auto encoder SPE RAW OKAY"
ASR_AE_SPE_FINETUNE_RAW_OUT={}
ASR_AE_SPE_FINETUNE_RAW_H2={}
ASR_AE_SPE_FINETUNE_RAW_FIXED_OUT={}
for i in TRS_sparse.keys():
    ASR_AE_SPE_FINETUNE_RAW_OUT[i]=spe_finetune.predict(ASR_sparse[i].todense())
    #TRS_sparse[i]=dico.transform(TRS[i][2])
    ASR_AE_SPE_FINETUNE_RAW_H2[i]=auto_decoder_spe_finetune.predict(ASR_sparse[i].todense())
    ASR_AE_SPE_FINETUNE_RAW_FIXED_OUT[i]=spe_finetune_fixed.predict(ASR_sparse_AE_H1[i])

db["ASR_AE_SPE_FINETUNE_RAW_OUT"]=ASR_AE_SPE_FINETUNE_RAW_OUT
db["ASR_AE_SPE_FINETUNE_RAW_H2"]=ASR_AE_SPE_FINETUNE_RAW_H2
db["ASR_AE_SPE_FINETUNE_RAW_FIXED_OUT"]=ASR_AE_SPE_FINETUNE_RAW_FIXED_OUT
db.sync()


spe_finetune.fit(ASR_sparse["TRAIN"].todense(),TRS_sparse["TRAIN"].todense(),nb_epoch=epochs,batch_size=batch,
               callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss',
                                                        patience=patience, verbose=0)],           validation_data=(ASR_sparse["DEV"].todense(),TRS_sparse["DEV"].todense()),verbose=1)



auto_decoder_spe_finetune=Sequential()
auto_decoder_spe_finetune.add(Dense(ASR_sparse["DEV"].todense().shape[1],hidden_size,activation=input_activation,weights=spe_finetune.get_weights()[:2]))
auto_decoder_spe_finetune.add(Dense(hidden_size,hidden_size2,activation=input_activation,weights=spe_finetune.get_weights()[2:4]))
auto_decoder_spe_finetune.add(Dense(hidden_size2,hidden_size,init="glorot_uniform",activation=out_activation,weights=spe_finetune.get_weights()[4:6]))
auto_decoder_spe_finetune.compile(optimizer=sgd,loss=loss)


ASR_sparse_AE_H1[i]


spe_finetune_fixed.fit(ASR_sparse_AE_H1["TRAIN"],TRS_sparse["TRAIN"].todense(),nb_epoch=epochs,batch_size=batch,callbacks=[keras.callbacks.EarlyStopping(monitor='val_loss',
    patience=patience, verbose=0)],
    validation_data=(ASR_sparse_AE_H1["DEV"],TRS_sparse["DEV"].todense()),verbose=1)


ASR_AE_SPE_FINETUNE_TUNED_OUT={}
ASR_AE_SPE_FINETUNE_TUNED_H2={}
ASR_AE_SPE_FINETUNE_TUNED_FIXED_OUT={}

for i in TRS_sparse.keys():
    ASR_AE_SPE_FINETUNE_TUNED_OUT[i]=spe_finetune.predict(ASR_sparse[i].todense())
    #TRS_sparse[i]=dico.transform(TRS[i][2])
    ASR_AE_SPE_FINETUNE_TUNED_H2[i]=auto_decoder_spe_finetune.predict(ASR_sparse[i].todense())
    ASR_AE_SPE_FINETUNE_TUNED_FIXED_OUT[i]=spe_finetune_fixed.predict(ASR_sparse_AE_H1[i])
db["ASR_AE_SPE_FINETUNE_TUNED_OUT"]=ASR_AE_SPE_FINETUNE_RAW_OUT
db["ASR_AE_SPE_FINETUNE_TUNED_H2"]=ASR_AE_SPE_FINETUNE_RAW_H2
db["ASR_AE_SPE_FINETUNE_TUNED_FIXED_OUT"]=ASR_AE_SPE_FINETUNE_RAW_FIXED_OUT

db.sync()
db.close()