Last active
October 17, 2021 17:48
-
-
Save Saransh-cpp/75c4e38748b75636d0707816f3e5ab3d to your computer and use it in GitHub Desktop.
Fit function for [logistic regression as a neural network](https://whiteviolin.medium.com/implementing-logistic-regression-as-a-neural-network-from-scratch-eff9d9cc98bc) article
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 fit(self): | |
""" | |
Maths involved - | |
z = w.T * x + b | |
y_predicted = a = sigmoid(z) | |
dw += (1 / m) * x * dz | |
db += dz | |
Gradient descent - | |
w = w - α * dw | |
b = b - α * db | |
""" | |
self.J = 0 | |
self.J_last = 1 | |
dW = np.zeros(shape=(self.m, 1)) | |
self.b = 0 | |
self.W = np.zeros(shape=(self.m, 1)) | |
while True: | |
Z = np.dot(self.W.T, self.X) + self.b | |
A = np.array([self.sigmoid(x) for x in Z]) | |
dZ = A - self.Y | |
dW = (1 / self.m) * (np.dot(self.X, dZ.T)) | |
db = (1 / self.m) * np.sum(dZ) | |
self.J = -np.sum( | |
np.multiply(self.Y.T, np.array([np.log(x) for x in A.T])) | |
+ np.multiply(1 - self.Y.T, np.array([np.log(1 - x) for x in A.T])) | |
) | |
self.W = self.W - self.alpha * dW | |
self.b = self.b - self.alpha * db | |
if self.debug: | |
print(self.J) | |
if abs(self.J - self.J_last) < 1e-5: | |
break | |
else: | |
self.J_last = self.J |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment