Skip to content

Instantly share code, notes, and snippets.

@dcrystalj
Created May 15, 2015 13:49
Show Gist options
  • Save dcrystalj/7bebae9c6e15baa43d89 to your computer and use it in GitHub Desktop.
Save dcrystalj/7bebae9c6e15baa43d89 to your computer and use it in GitHub Desktop.
nn
def backprop(self, thetas):
theta = self.shape_thetas(thetas)
a1 = add_ones(X)
z2 = a1.dot(theta[0])
a2 = add_ones(g(z2))
z3 = a2.dot(theta[1])
a3 = add_ones(g(z3))
z4 = a3.dot(theta[2])
a4 = g(z4)
d4 = -(Y-a4) * a4 * (1-a4)
d3 = (theta[2].dot(d4.T).T * (a3 * (1-a3)))[:, 1:]
d2 = (theta[1].dot(d3.T).T * (a2 * (1-a2)))[:, 1:]
T3 = a3.T.dot(d4) / self.m
T2 = a2.T.dot(d3) / self.m
T1 = a1.T.dot(d2) / self.m
grad = np.hstack((T1.flat, T2.flat, T3.flat))
return grad
def J(self, thetas):
Y = self.Y
theta = self.shape_thetas(thetas)
h0 = self.h(self.X, thetas)
h0 = np.maximum(np.minimum(h0, 1 - 1e-15), 1e-15)
return 1/self.m * .5 * np.sum(np.power(h0 - Y, 2))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment