Skip to content

Commit ffe76b3

Browse files
Merge branch 'main' into issue_2_branch
2 parents 699ac56 + a15d82d commit ffe76b3

File tree

1 file changed

+34
-1
lines changed

1 file changed

+34
-1
lines changed

neo.py

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,23 @@ def sigmoid(z):
44
try:
55
return 1 / (1 + np.exp(-z))
66
except OverflowError as e:
7+
issue_2_branch
8+
=======
9+
print(f"OverflowError in sigmoid: {e}")
10+
main
711
return 1.0 if z > 0 else 0.0
812

913
class LogisticRegression:
10-
def __init__(self, learning_rate=0.01, epochs=50, batch_size=4, regularization_strength=0.01, use_regularization=True):
14+
def __init__(self, learning_rate=0.01, epochs=50, batch_size=4, regularization_strength=0.01, use_regularization=True, learning_rate_deacy = 0.99):
1115
self.learning_rate = learning_rate
1216
self.epochs = epochs
1317
self.batch_size = batch_size
1418
self.regularization_strength = regularization_strength
1519
self.use_regularization = use_regularization
20+
self.learning_rate_decay = learning_rate_deacy
1621

1722
def fit(self, X, y):
23+
issue_2_branch
1824
try:
1925
n_samples, n_features = X.shape
2026
self.weights = np.zeros(n_features) # Corrected weight initialization
@@ -30,6 +36,14 @@ def fit(self, X, y):
3036
for i in range(0, n_samples, self.batch_size):
3137
X_batch = X_shuffled[i:i + self.batch_size]
3238
y_batch = y_shuffled[i:i + self.batch_size]
39+
=======
40+
n_samples, n_features = X.shape
41+
self.weights = np.random.randn(n_features) # Corrected weight initialization
42+
self.bias = 0 # Corrected bias initialization
43+
44+
prev_weights = np.zeros(n_features)
45+
prev_bias = 0
46+
main
3347

3448

3549
linear_model = np.dot(X_batch, self.weights) + self.bias
@@ -41,12 +55,19 @@ def fit(self, X, y):
4155
if self.use_regularization:
4256
dw += (self.regularization_strength * self.weights) # Corrected regularization term
4357

58+
issue_2_branch
4459
self.weights -= self.learning_rate * dw
4560
self.bias -= self.learning_rate * db # Corrected bias update logic
61+
=======
62+
if self.use_regularization:
63+
dw += (self.regularization_strength * self.weights) # Corrected regularization term
64+
dw += (self.regularization_strength * self.bias)
65+
main
4666

4767
if np.allclose(prev_weights, self.weights, rtol=1e-05): # Corrected stopping condition
4868
break
4969

70+
issue_2_branch
5071
prev_weights = self.weights
5172

5273
except ValueError as e:
@@ -60,6 +81,18 @@ def fit(self, X, y):
6081

6182
except Exception as e:
6283
print(f"Unexpected error in fit method: {e}")
84+
=======
85+
self.learning_rate *= self.learning_rate_decay
86+
87+
if np.allclose(prev_weights, self.weights, rtol=1e-05): # Corrected stopping condition
88+
break
89+
90+
prev_weights = np.copy(self.weights)
91+
prev_bias = self.bias
92+
93+
print(f"Epoch {epoch}: Weights change: {np.linalg.norm(dw)}, Bias change: {abs(db)}")
94+
95+
main
6396

6497
def predict(self, X):
6598
try:

0 commit comments

Comments
 (0)