Blame view

DECODA_binary_BOW_SPE.py 2.94 KB
b6d0165d1   Killian   Initial commit
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
  # 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("DECODA_sparse.shelve")
  out_db=shelve.open("{}.shelve".format(sys.argv[1]),writeback=True)
  # In[6]:
  
  ASR_sparse=db["ASR_SPARSE"]
  TRS_sparse=db["TRS_SPARSE"]
  LABEL=db["LABEL"]
  
  def select(elm):
      return int(elm.split("_")[-1])
  
  print "gogo autoencoder ASR"
  hidden_size=3076
  hidden_size2=2048
  input_activation="relu"
  out_activation="relu"
  loss="mse"
  epochs=500
  patience=50
  
  
  sgd = Adam(lr=0.001) #SGD(lr=0.05,momentum=0.9)
  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)
  
  
  # In[ ]:
  
  autoencode.fit(ASR_sparse["TRAIN"].todense(),TRS_sparse["TRAIN"].todense(),nb_epoch=epochs,batch_size=16,
                 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(ASR_sparse["DEV"].todense().shape[1],hidden_size,init='glorot_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)
  
  
  # In[77]:
  
  #autoencode.predict(ASR_sparse["DEV"].todense())
  
  
  # In[ ]:
  
  print "auto encoder et auto decoder asr okay"
  
  ASR_sparse_AE={}
  TRS_sparse_AE={}
  for i in ASR_sparse.keys():
      ASR_sparse_AE[i]=auto_decoder.predict(ASR_sparse[i].todense())
      TRS_sparse_AE[i]=auto_decoder.predict(TRS_sparse[i].todense())
      #TRS_sparse[i]=dico.transform(TRS[i][2])
  
  out_db["ASR_SPELIKE_H2_RELU"]=ASR_sparse_AE
  out_db["TRS_SPELIKE_H2_RELU"]=TRS_sparse_AE
  
  
  ASR_AE={}
  for i in TRS_sparse.keys():
      ASR_AE[i]=autoencode.predict(ASR_sparse[i].todense())
  
  
  out_db["ASR_SPELIKE_OUT_RELU"]=ASR_AE
  
  
  db.close()
  out_db.close()