RNN TIMIT demo
This notebook is an extension of the previous one on MLP. To find more details, read that one first.
This is still work in progress!
Using Theano backend.
Using gpu device 0: Tesla K80 (CNMeM is enabled with initial size: 90.0% of memory, cuDNN not available)
def pad_feat(data,maxlen):
s=data.shape[0]
f=data[0].shape[1]
ret=np.zeros((s,maxlen,f))
for u in range(s):
w=data[u].shape[0]
ret[u,:w,:]=data[u]
return ret
def get_masking(data,maxlen):
s=data.shape[0]
ret=np.zeros((s,maxlen))
for u in range(s):
w=data[u].shape[0]
ret[u,:w]=1
return ret
tr_in=pad_feat(tr_in,maxlen)
dev_in=pad_feat(dev_in,maxlen)
tst_in=pad_feat(tst_in,maxlen)
tr_out=pad_feat(tr_out,maxlen)
dev_out=pad_feat(dev_out,maxlen)
tst_out=pad_feat(tst_out,maxlen)
#tr_sw=get_masking(tr_out_dec,maxlen)
#tst_sw=get_masking(tst_out_dec,maxlen)
#dev_sw=get_masking(dev_out_dec,maxlen)
class LSTM_w_peepholes(LSTM):
def __init__(self,**kwargs):
super(LSTM_w_peepholes, self).__init__(**kwargs)
def build(self):
super(LSTM_w_peepholes,self).build()
self.P_i = self.inner_init((self.output_dim, self.output_dim),
name='{}_P_i'.format(self.name))
self.P_f = self.inner_init((self.output_dim, self.output_dim),
name='{}_P_f'.format(self.name))
self.P_o = self.inner_init((self.output_dim, self.output_dim),
name='{}_P_o'.format(self.name))
self.trainable_weights.append(self.P_i)
self.trainable_weights.append(self.P_f)
self.trainable_weights.append(self.P_o)
def step(self, x, states):
h_tm1 = states[0]
c_tm1 = states[1]
if len(states) == 3:
B_U = states[2]
else:
B_U = [1. for _ in range(4)]
x_i = x[:, :self.output_dim]
x_f = x[:, self.output_dim: 2 * self.output_dim]
x_c = x[:, 2 * self.output_dim: 3 * self.output_dim]
x_o = x[:, 3 * self.output_dim:]
i = self.inner_activation(x_i + K.dot(h_tm1 * B_U[0], self.U_i) + K.dot(c_tm1,self.P_i))
f = self.inner_activation(x_f + K.dot(h_tm1 * B_U[1], self.U_f) + K.dot(c_tm1,self.P_f))
c = f * c_tm1 + i * self.activation(x_c + K.dot(h_tm1 * B_U[2], self.U_c))
o = self.inner_activation(x_o + K.dot(h_tm1 * B_U[3], self.U_o) + K.dot(c_tm1,self.P_o))
h = o * self.activation(c)
return h, [h, c]
Train loss: 1.03277289867
Train PER: 32.014815%
Dev loss: 1.24962925911
Dev PER: 36.585923%
Test loss: 1.27113211155
Test PER: 37.309000%
Done!
Out[9]:
<matplotlib.legend.Legend at 0x53b0a810>
Min test PER: 36.911894%
Min dev PER epoch: #24
Test PER on min dev: 36.911894%