Created
May 21, 2018 03:58
-
-
Save khanhnamle1994/614032ab1243ea0751aa1326d7439cd3 to your computer and use it in GitHub Desktop.
Backpropagation Through Time
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def bptt(self, x, y): | |
assert len(x) == len(y) | |
output = Softmax() | |
layers = self.forward_propagation(x) | |
dU = np.zeros(self.U.shape) | |
dV = np.zeros(self.V.shape) | |
dW = np.zeros(self.W.shape) | |
T = len(layers) | |
prev_s_t = np.zeros(self.hidden_dim) | |
diff_s = np.zeros(self.hidden_dim) | |
for t in range(0, T): | |
dmulv = output.diff(layers[t].mulv, y[t]) | |
input = np.zeros(self.word_dim) | |
input[x[t]] = 1 | |
dprev_s, dU_t, dW_t, dV_t = layers[t].backward(input, prev_s_t, self.U, self.W, self.V, diff_s, dmulv) | |
prev_s_t = layers[t].s | |
dmulv = np.zeros(self.word_dim) | |
for i in range(t-1, max(-1, t-self.bptt_truncate-1), -1): | |
input = np.zeros(self.word_dim) | |
input[x[i]] = 1 | |
prev_s_i = np.zeros(self.hidden_dim) if i == 0 else layers[i-1].s | |
dprev_s, dU_i, dW_i, dV_i = layers[i].backward(input, prev_s_i, self.U, self.W, self.V, dprev_s, dmulv) | |
dU_t += dU_i | |
dW_t += dW_i | |
dV += dV_t | |
dU += dU_t | |
dW += dW_t | |
return (dU, dW, dV) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment