Skip to content

Instantly share code, notes, and snippets.

@EderSantana
Created February 17, 2016 15:03
Show Gist options
  • Save EderSantana/f07fa7a0371d0e1c4ef1 to your computer and use it in GitHub Desktop.
Save EderSantana/f07fa7a0371d0e1c4ef1 to your computer and use it in GitHub Desktop.
# NOTE: I'm not sure if this is right
from keras.layers.recurrent import LSTM
class LSTMpeephole(LSTM):
def __init__(self, **kwargs):
super(LSTMpeephole, self).__init__(**kwargs)
def build(self):
super(LSTMpeephole, self).build()
self.P_i = self.inner_init((self.output_dim, self.output_dim))
self.P_f = self.inner_init((self.output_dim, self.output_dim))
self.P_c = self.inner_init((self.output_dim, self.output_dim))
self.P_o = self.inner_init((self.output_dim, self.output_dim))
self.trainable_weights += [self.P_i, self.P_f, self.P_o]
def step(self, x, states):
assert len(states) == 2
h_tm1 = states[0]
c_tm1 = states[1]
x_i = K.dot(x, self.W_i) + self.b_i
x_f = K.dot(x, self.W_f) + self.b_f
x_c = K.dot(x, self.W_c) + self.b_c
x_o = K.dot(x, self.W_o) + self.b_o
i = self.inner_activation(x_i + K.dot(h_tm1, self.U_i) + K.dot(c_tm1, self.P_i))
f = self.inner_activation(x_f + K.dot(h_tm1, self.U_f) + K.dot(c_tm1, self.P_f))
c = f * c_tm1 + i * self.activation(x_c + K.dot(h_tm1, self.U_c) + K.dot(c_tm1, self.P_c))
o = self.inner_activation(x_o + K.dot(h_tm1, self.U_o) + K.dot(c_tm1, self.P_o))
h = o * self.activation(c)
return h, [h, c]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment