DECODA_binary_BOW_AE_REALSPE_TANH_MODELS.py
3.84 KB
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
# coding: utf-8
# In[2]:
# Import
import pandas
# Alignement
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[2]),writeback=True)
#['vocab', 'ASR_SPARSE', 'TRS_SPARSE', 'LABEL']
# In[6]:
# In[10]:
print "making sparse data"
sparse_corp=shelve.open("{}.shelve".format(sys.argv[1]))
ASR_sparse=sparse_corp["ASR"]
TRS_sparse=sparse_corp["TRS"]
db["LABEL"] = sparse_corp["LABEL"]
db["ASR"] = ASR_sparse
db["TRS"] = TRS_sparse
# In[11]:
#z.apply(select)
hidden_size=3096
hidden_size2=2048
input_activation="relu"
out_activation="relu"
loss="mse"
epochs=1000
batch=64
patience=40
print "gogo autoencoder ASR"
sgd = 'adam'#SGD(lr=0.0001)#( momentum=0.9, nesterov=True)
autoencode=Sequential()
autoencode.add(Dense(hidden_size,input_dim=ASR_sparse["TRAIN"].shape[1],init='glorot_uniform',activation=input_activation))
autoencode.add(Dense(hidden_size2,input_dim=hidden_size,init='glorot_uniform',activation=input_activation))
autoencode.add(Dense(hidden_size,input_dim=hidden_size2,init="glorot_uniform",activation=out_activation))
autoencode.add(Dense(ASR_sparse["DEV"].todense().shape[1],input_dim=hidden_size,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(),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)
# In[ ]:
auto_decoder=Sequential()
auto_decoder.add(Dense(hidden_size,input_dim=ASR_sparse["DEV"].todense().shape[1],init='uniform',activation=input_activation,weights=autoencode.get_weights()[:2]))
auto_decoder.add(Dense(hidden_size2,input_dim=hidden_size,init='glorot_uniform',activation=input_activation,weights=autoencode.get_weights()[2:4]))
auto_decoder.add(Dense(hidden_size,input_dim=hidden_size2,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"]=ASR_sparse_AE
auto_decoder=Sequential()
auto_decoder.add(Dense(hidden_size,input_dim=ASR_sparse["DEV"].todense().shape[1],init='uniform',activation=input_activation,weights=autoencode.get_weights()[:2]))
auto_decoder.add(Dense(hidden_size2,input_dim=hidden_size,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"]=ASR_sparse_AE_H1
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.predict(TRS_sparse[i].todense())
ASR_AE[i]=autoencode.predict(ASR_sparse[i].todense())
db["TRS_AE_OUT"]=TRS_AE
db["ASR_AE_OUT"]=ASR_AE
# # Transfert de couche
# ICI
db.sync()
db.close()