-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathneo.py
206 lines (166 loc) · 7.26 KB
/
neo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
import numpy as np
def sigmoid(z):
branch4
"""Compute the sigmoid function."""
return 1 / (1 + np.exp(-z))
=======
try:
return 1 / (1 + np.exp(-z))
except OverflowError as e:
issue_2_branch
=======
print(f"OverflowError in sigmoid: {e}")
main
return 1.0 if z > 0 else 0.0
main
class LogisticRegression:
issue_3_branch
def compute_loss(self, X, y):
linear_model = np.dot(X, self.weights) + self.bias
y_predicted = sigmoid(linear_model)
log_loss = -np.mean(y * np.log(y_predicted) + (1 - y) * np.log(1 - y_predicted))
if self.use_regularization:
log_loss += (self.regularization_strength / 2) * np.sum(np.square(self.weights)) # L2 regularization
return log_loss
def __init__(self, learning_rate=0.01, epochs=50, batch_size=4, regularization_strength=0.01, use_regularization=True):
=======
def __init__(self, learning_rate=0.01, epochs=50, batch_size=4, regularization_strength=0.01, use_regularization=True, learning_rate_deacy = 0.99):
main
self.learning_rate = learning_rate
self.epochs = epochs
self.batch_size = batch_size
self.regularization_strength = regularization_strength
self.use_regularization = use_regularization
self.learning_rate_decay = learning_rate_deacy
def fit(self, X, y):
issue_2_branch
try:
n_samples, n_features = X.shape
self.weights = np.zeros(n_features) # Corrected weight initialization
self.bias = 0 # Corrected bias initialization
prev_weights = np.zeros(n_features)
for epoch in range(self.epochs):
indices = np.random.permutation(n_samples)
X_shuffled = X[indices]
y_shuffled = y[indices]
for i in range(0, n_samples, self.batch_size):
X_batch = X_shuffled[i:i + self.batch_size]
y_batch = y_shuffled[i:i + self.batch_size]
=======
n_samples, n_features = X.shape
main
self.weights = np.zeros(n_features) # Error 1: Improper weight initialization
self.bias = 0.0 # Error 2: Bias should be a scalar, not an array
=======
main
self.weights = np.random.rand(n_features) * 0.01 # Error 1: Improper weight initialization
self.bias = 0 # Error 2: Bias should be a scalar, not an array
=======
branch4
self.weights = np.zeros(n_features) # Initialize weights to zeros for better convergence
self.bias = 0.0 # Bias should be a scalar
=======
self.weights = np.random.randn(n_features) # Corrected weight initialization
self.bias = 0 # Corrected bias initialization
prev_weights = np.zeros(n_features)
prev_bias = 0
main
main
main
main
linear_model = np.dot(X_batch, self.weights) + self.bias
y_predicted = sigmoid(linear_model)
dw = (1 / len(X_batch)) * np.dot(X_batch.T, (y_predicted - y_batch))
db = (1 / len(X_batch)) * np.sum(y_predicted - y_batch)
branch4
# Calculate gradients
dw = (1 / len(X_batch)) * np.dot(X_batch.T, (y_predicted - y_batch))
db = (1 / len(X_batch)) * np.sum(y_predicted - y_batch)
# Apply regularization if required
if self.use_regularization:
main
dw += (self.regularization_strength / n_samples) * self.weights # Error 3: Regularization applied incorrectly
=======
dw += (self.regularization_strength / len(X_batch)) * self.weights
main
# Update weights and bias
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db
main
if np.linalg.norm(dw) and np.linalg.norm(db)< 0.001:
break # Error 5: Inadequate stopping condition
=======
# Improved stopping condition
weight_update_norm = np.linalg.norm(dw)
if weight_update_norm < 0.001:
print(f"Stopping early at epoch {epoch} with weight update norm: {weight_update_norm:.6f}")
break
main
def predict(self, X):
linear_model = np.dot(X, self.weights) + self.bias
y_predicted = sigmoid(linear_model)
main
y_class_pred = (y_predicted > 0.5).astype(int)
ambiguous_indices = np.where(y_predicted == 0.5)[0]
if ambiguous_indices.size > 0:
random_choices = np.random.choice([0, 1], size=ambiguous_indices.size)
y_class_pred[ambiguous_indices] = random_choices
=======
y_class_pred = (y_predicted >= 0.5).astype(int) # More concise and clear prediction
main
return y_class_pred
# Sample training data
X_train = np.array([[1, 2], [2, 3], [3, 4], [4, 5], [5, 6], [6, 7], [7, 8], [8, 9]])
y_train = np.array([0, 0, 0, 1, 1, 1, 1, 1])
# Model instantiation and training
model = LogisticRegression(learning_rate=0.0001, epochs=5000, batch_size=2, regularization_strength=0.5)
model.fit(X_train, y_train)
# Predictions
predictions = model.predict(X_train)
print("Predicted classes:", predictions)
=======
if self.use_regularization:
dw += (self.regularization_strength * self.weights) # Corrected regularization term
issue_2_branch
self.weights -= self.learning_rate * dw
self.bias -= self.learning_rate * db # Corrected bias update logic
=======
if self.use_regularization:
dw += (self.regularization_strength * self.weights) # Corrected regularization term
dw += (self.regularization_strength * self.bias)
main
if np.allclose(prev_weights, self.weights, rtol=1e-05): # Corrected stopping condition
break
issue_2_branch
prev_weights = self.weights
except ValueError as e:
print(f"ValueError in fit method: {e}")
except TypeError as e:
print(f"TypeError in fit method: {e}")
except IndexError as e:
print(f"IndexError in fit method: {e}")
except Exception as e:
print(f"Unexpected error in fit method: {e}")
=======
self.learning_rate *= self.learning_rate_decay
loss = self.compute_loss(X, y)
print(f'Epoch {epoch+1}/{self.epochs}, Loss: {loss:.4f}')
if np.allclose(prev_weights, self.weights, rtol=1e-05): # Corrected stopping condition
break
prev_weights = np.copy(self.weights)
prev_bias = self.bias
print(f"Epoch {epoch}: Weights change: {np.linalg.norm(dw)}, Bias change: {abs(db)}")
main
def predict(self, X):
try:
linear_model = np.dot(X, self.weights) + self.bias
y_predicted = sigmoid(linear_model)
y_class_pred = [1 if i > 0.5 else 0 for i in y_predicted] # Corrected equality condition
return np.array(y_class_pred)
except ValueError as e:
print(f"ValueError in fit method: {e}")
except TypeError as e:
print(f"TypeError in fit method: {e}")
except Exception as e:
print(f"Unexpected error in fit method: {e}")
main