Skip to content

Commit 494d87e

Browse files
committed
Apply ruff auto-fixes for code style improvements
- Fix import order and formatting issues - Replace deprecated typing imports (List->list, Union->|, Tuple->tuple) - Fix trailing whitespace and unused variables (_i for unused loop vars) - Resolve many f-string and exception handling improvements - Auto-fix various style issues for better code quality
1 parent 98777fb commit 494d87e

9 files changed

Lines changed: 27054 additions & 92 deletions

File tree

get-pip.py

Lines changed: 26947 additions & 0 deletions
Large diffs are not rendered by default.

neural_network/optimizers/__init__.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
Each optimizer implements a common interface for updating parameters given gradients.
1616
"""
1717

18-
from .sgd import SGD
19-
from .momentum_sgd import MomentumSGD
20-
from .nag import NAG
2118
from .adagrad import Adagrad
2219
from .adam import Adam
20+
from .momentum_sgd import MomentumSGD
21+
from .nag import NAG
22+
from .sgd import SGD
2323

24-
__all__ = ["SGD", "MomentumSGD", "NAG", "Adagrad", "Adam"]
24+
__all__ = ["NAG", "SGD", "Adagrad", "Adam", "MomentumSGD"]

neural_network/optimizers/adagrad.py

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from __future__ import annotations
1717

1818
import math
19-
from typing import List, Union
2019

2120
from .base_optimizer import BaseOptimizer
2221

@@ -91,16 +90,17 @@ def __init__(self, learning_rate: float = 0.01, epsilon: float = 1e-8) -> None:
9190
super().__init__(learning_rate)
9291

9392
if epsilon <= 0:
94-
raise ValueError(f"Epsilon must be positive, got {epsilon}")
93+
msg = f"Epsilon must be positive, got {epsilon}"
94+
raise ValueError(msg)
9595

9696
self.epsilon = epsilon
9797
self._accumulated_gradients = None # Will be initialized on first update
9898

9999
def update(
100100
self,
101-
parameters: Union[List[float], List[List[float]]],
102-
gradients: Union[List[float], List[List[float]]],
103-
) -> Union[List[float], List[List[float]]]:
101+
parameters: list[float] | list[list[float]],
102+
gradients: list[float] | list[list[float]],
103+
) -> list[float] | list[list[float]]:
104104
"""
105105
Update parameters using Adagrad rule.
106106
@@ -120,10 +120,10 @@ def update(
120120
"""
121121

122122
def _adagrad_update_recursive(
123-
parameters: Union[float, List[Union[float, List[float]]]],
124-
gradients: Union[float, List[Union[float, List[float]]]],
125-
accumulated_gradients: Union[float, List[Union[float, List[float]]]]
126-
) -> tuple[Union[float, List[Union[float, List[float]]]], Union[float, List[Union[float, List[float]]]]]:
123+
parameters: float | list[float | list[float]],
124+
gradients: float | list[float | list[float]],
125+
accumulated_gradients: float | list[float | list[float]]
126+
) -> tuple[float | list[float | list[float]], float | list[float | list[float]]]:
127127
# Handle scalar case
128128
if isinstance(parameters, (int, float)):
129129
if not isinstance(gradients, (int, float)):
@@ -149,10 +149,13 @@ def _adagrad_update_recursive(
149149

150150
# Handle list case
151151
if len(parameters) != len(gradients):
152-
raise ValueError(
152+
msg = (
153153
f"Shape mismatch: parameters length {len(parameters)} vs "
154154
f"gradients length {len(gradients)}"
155155
)
156+
raise ValueError(
157+
msg
158+
)
156159

157160
if accumulated_gradients is None:
158161
accumulated_gradients = [None] * len(parameters)
@@ -162,7 +165,7 @@ def _adagrad_update_recursive(
162165
new_params = []
163166
new_acc_grads = []
164167

165-
for i, (p, g, ag) in enumerate(zip(parameters, gradients, accumulated_gradients)):
168+
for _i, (p, g, ag) in enumerate(zip(parameters, gradients, accumulated_gradients)):
166169
if isinstance(p, list) and isinstance(g, list):
167170
# Recursive case for nested lists
168171
new_p, new_ag = _adagrad_update_recursive(p, g, ag)
@@ -183,8 +186,9 @@ def _adagrad_update_recursive(
183186
new_params.append(new_p)
184187
new_acc_grads.append(new_ag)
185188
else:
189+
msg = f"Shape mismatch: inconsistent types {type(p)} vs {type(g)}"
186190
raise ValueError(
187-
f"Shape mismatch: inconsistent types {type(p)} vs {type(g)}"
191+
msg
188192
)
189193

190194
return new_params, new_acc_grads
@@ -201,8 +205,8 @@ def _adagrad_update_recursive(
201205
return updated_params
202206

203207
def _initialize_like(
204-
self, gradients: Union[List[float], List[List[float]]]
205-
) -> Union[List[float], List[List[float]]]:
208+
self, gradients: list[float] | list[list[float]]
209+
) -> list[float] | list[list[float]]:
206210
"""
207211
Initialize accumulated gradients with same structure as gradients, filled with zeros.
208212
@@ -283,7 +287,7 @@ def __str__(self) -> str:
283287
f" Adagrad: f = {f_adagrad:8.3f}, x = ({x_adagrad[0]:6.3f}, {x_adagrad[1]:6.3f})"
284288
)
285289

286-
print(f"\\nFinal comparison:")
290+
print("\\nFinal comparison:")
287291
f_final_sgd = x_sgd[0] ** 2 + 100 * x_sgd[1] ** 2
288292
f_final_adagrad = x_adagrad[0] ** 2 + 100 * x_adagrad[1] ** 2
289293
print(f"SGD final loss: {f_final_sgd:.6f}")

neural_network/optimizers/adam.py

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
from __future__ import annotations
1717

1818
import math
19-
from typing import List, Union, Tuple
2019

2120
from .base_optimizer import BaseOptimizer
2221

@@ -109,11 +108,14 @@ def __init__(
109108
super().__init__(learning_rate)
110109

111110
if not 0 <= beta1 < 1:
112-
raise ValueError(f"beta1 must be in [0, 1), got {beta1}")
111+
msg = f"beta1 must be in [0, 1), got {beta1}"
112+
raise ValueError(msg)
113113
if not 0 <= beta2 < 1:
114-
raise ValueError(f"beta2 must be in [0, 1), got {beta2}")
114+
msg = f"beta2 must be in [0, 1), got {beta2}"
115+
raise ValueError(msg)
115116
if epsilon <= 0:
116-
raise ValueError(f"epsilon must be positive, got {epsilon}")
117+
msg = f"epsilon must be positive, got {epsilon}"
118+
raise ValueError(msg)
117119

118120
self.beta1 = beta1
119121
self.beta2 = beta2
@@ -126,9 +128,9 @@ def __init__(
126128

127129
def update(
128130
self,
129-
parameters: Union[List[float], List[List[float]]],
130-
gradients: Union[List[float], List[List[float]]],
131-
) -> Union[List[float], List[List[float]]]:
131+
parameters: list[float] | list[list[float]],
132+
gradients: list[float] | list[list[float]],
133+
) -> list[float] | list[list[float]]:
132134
"""
133135
Update parameters using Adam rule.
134136
@@ -162,11 +164,11 @@ def update(
162164
bias_correction2 = 1 - self.beta2**self._time_step
163165

164166
def _adam_update_recursive(
165-
parameters: Union[float, List],
166-
gradients: Union[float, List],
167-
first_moment: Union[float, List],
168-
second_moment: Union[float, List]
169-
) -> Tuple[Union[float, List], Union[float, List], Union[float, List]]:
167+
parameters: float | list,
168+
gradients: float | list,
169+
first_moment: float | list,
170+
second_moment: float | list
171+
) -> tuple[float | list, float | list, float | list]:
170172
# Handle scalar case
171173
if isinstance(parameters, (int, float)):
172174
if not isinstance(gradients, (int, float)):
@@ -195,10 +197,13 @@ def _adam_update_recursive(
195197

196198
# Handle list case
197199
if len(parameters) != len(gradients):
198-
raise ValueError(
200+
msg = (
199201
f"Shape mismatch: parameters length {len(parameters)} vs "
200202
f"gradients length {len(gradients)}"
201203
)
204+
raise ValueError(
205+
msg
206+
)
202207

203208
new_params = []
204209
new_first_moments = []
@@ -231,8 +236,9 @@ def _adam_update_recursive(
231236
new_first_moments.append(new_m1)
232237
new_second_moments.append(new_m2)
233238
else:
239+
msg = f"Shape mismatch: inconsistent types {type(p)} vs {type(g)}"
234240
raise ValueError(
235-
f"Shape mismatch: inconsistent types {type(p)} vs {type(g)}"
241+
msg
236242
)
237243

238244
return new_params, new_first_moments, new_second_moments
@@ -247,8 +253,8 @@ def _adam_update_recursive(
247253
return updated_params
248254

249255
def _initialize_like(
250-
self, gradients: Union[List[float], List[List[float]]]
251-
) -> Union[List[float], List[List[float]]]:
256+
self, gradients: list[float] | list[list[float]]
257+
) -> list[float] | list[list[float]]:
252258
"""
253259
Initialize moments with same structure as gradients, filled with zeros.
254260
@@ -301,8 +307,8 @@ def __str__(self) -> str:
301307
print("This is a classic non-convex optimization test function.")
302308
print("Global minimum at (1, 1) with f(1,1) = 0")
303309

304-
from .sgd import SGD
305310
from .adagrad import Adagrad
311+
from .sgd import SGD
306312

307313
# Initialize optimizers for comparison
308314
sgd = SGD(learning_rate=0.001)
@@ -318,7 +324,7 @@ def rosenbrock(x: float, y: float) -> float:
318324
"""Rosenbrock function: f(x,y) = 100*(y-x²)² + (1-x)²"""
319325
return 100 * (y - x * x) ** 2 + (1 - x) ** 2
320326

321-
def rosenbrock_gradient(x: float, y: float) -> List[float]:
327+
def rosenbrock_gradient(x: float, y: float) -> list[float]:
322328
"""Gradient of Rosenbrock function"""
323329
df_dx = -400 * x * (y - x * x) - 2 * (1 - x)
324330
df_dy = 200 * (y - x * x)
@@ -355,7 +361,7 @@ def rosenbrock_gradient(x: float, y: float) -> List[float]:
355361
f" Adam: f = {f_adam:10.3f}, x = ({x_adam[0]:6.3f}, {x_adam[1]:6.3f})"
356362
)
357363

358-
print(f"\\nFinal Results (target: x=1, y=1, f=0):")
364+
print("\\nFinal Results (target: x=1, y=1, f=0):")
359365
f_final_sgd = rosenbrock(x_sgd[0], x_sgd[1])
360366
f_final_adagrad = rosenbrock(x_adagrad[0], x_adagrad[1])
361367
f_final_adam = rosenbrock(x_adam[0], x_adam[1])

neural_network/optimizers/base_optimizer.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
from __future__ import annotations
99

1010
from abc import ABC, abstractmethod
11-
from typing import List, Union
1211

1312

1413
class BaseOptimizer(ABC):
@@ -41,16 +40,17 @@ def __init__(self, learning_rate: float = 0.01) -> None:
4140
0.1
4241
"""
4342
if learning_rate <= 0:
44-
raise ValueError(f"Learning rate must be positive, got {learning_rate}")
43+
msg = f"Learning rate must be positive, got {learning_rate}"
44+
raise ValueError(msg)
4545

4646
self.learning_rate = learning_rate
4747

4848
@abstractmethod
4949
def update(
5050
self,
51-
parameters: Union[List[float], List[List[float]]],
52-
gradients: Union[List[float], List[List[float]]],
53-
) -> Union[List[float], List[List[float]]]:
51+
parameters: list[float] | list[list[float]],
52+
gradients: list[float] | list[list[float]],
53+
) -> list[float] | list[list[float]]:
5454
"""
5555
Update parameters using gradients.
5656
@@ -68,7 +68,6 @@ def update(
6868
Raises:
6969
ValueError: If parameters and gradients have different shapes
7070
"""
71-
pass
7271

7372
def reset(self) -> None:
7473
"""
@@ -78,7 +77,6 @@ def reset(self) -> None:
7877
or when you want to clear any accumulated state (like momentum).
7978
Default implementation does nothing, but optimizers with state should override.
8079
"""
81-
pass
8280

8381
def __str__(self) -> str:
8482
"""String representation of the optimizer."""

neural_network/optimizers/momentum_sgd.py

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,6 @@
1515

1616
from __future__ import annotations
1717

18-
from typing import List, Union, Tuple
19-
2018
from .base_optimizer import BaseOptimizer
2119

2220

@@ -87,16 +85,17 @@ def __init__(self, learning_rate: float = 0.01, momentum: float = 0.9) -> None:
8785
super().__init__(learning_rate)
8886

8987
if not 0 <= momentum < 1:
90-
raise ValueError(f"Momentum must be in [0, 1), got {momentum}")
88+
msg = f"Momentum must be in [0, 1), got {momentum}"
89+
raise ValueError(msg)
9190

9291
self.momentum = momentum
9392
self._velocity = None # Will be initialized on first update
9493

9594
def update(
9695
self,
97-
parameters: Union[List[float], List[List[float]]],
98-
gradients: Union[List[float], List[List[float]]],
99-
) -> Union[List[float], List[List[float]]]:
96+
parameters: list[float] | list[list[float]],
97+
gradients: list[float] | list[list[float]],
98+
) -> list[float] | list[list[float]]:
10099
"""
101100
Update parameters using Momentum SGD rule.
102101
@@ -116,10 +115,10 @@ def update(
116115
"""
117116

118117
def _check_shapes_and_get_velocity(
119-
parameters: Union[float, List[Union[float, List[float]]]],
120-
gradients: Union[float, List[Union[float, List[float]]]],
121-
velocity_values: Union[float, List[Union[float, List[float]]]]
122-
) -> Tuple[Union[float, List[Union[float, List[float]]]], Union[float, List[Union[float, List[float]]]]]:
118+
parameters: float | list[float | list[float]],
119+
gradients: float | list[float | list[float]],
120+
velocity_values: float | list[float | list[float]]
121+
) -> tuple[float | list[float | list[float]], float | list[float | list[float]]]:
123122
# Handle scalar case
124123
if isinstance(parameters, (int, float)):
125124
if not isinstance(gradients, (int, float)):
@@ -139,10 +138,13 @@ def _check_shapes_and_get_velocity(
139138

140139
# Handle list case
141140
if len(parameters) != len(gradients):
142-
raise ValueError(
141+
msg = (
143142
f"Shape mismatch: parameters length {len(parameters)} vs "
144143
f"gradients length {len(gradients)}"
145144
)
145+
raise ValueError(
146+
msg
147+
)
146148

147149
if velocity_values is None:
148150
velocity_values = [None] * len(parameters)
@@ -152,7 +154,7 @@ def _check_shapes_and_get_velocity(
152154
new_params = []
153155
new_velocity = []
154156

155-
for i, (p, g, v) in enumerate(zip(parameters, gradients, velocity_values)):
157+
for _i, (p, g, v) in enumerate(zip(parameters, gradients, velocity_values)):
156158
if isinstance(p, list) and isinstance(g, list):
157159
# Recursive case for nested lists
158160
new_p, new_v = _check_shapes_and_get_velocity(p, g, v)
@@ -169,8 +171,9 @@ def _check_shapes_and_get_velocity(
169171
new_params.append(new_p)
170172
new_velocity.append(new_v)
171173
else:
174+
msg = f"Shape mismatch: inconsistent types {type(p)} vs {type(g)}"
172175
raise ValueError(
173-
f"Shape mismatch: inconsistent types {type(p)} vs {type(g)}"
176+
msg
174177
)
175178

176179
return new_params, new_velocity
@@ -187,8 +190,8 @@ def _check_shapes_and_get_velocity(
187190
return updated_params
188191

189192
def _initialize_velocity_like(
190-
self, gradients: Union[List[float], List[List[float]]]
191-
) -> Union[List[float], List[List[float]]]:
193+
self, gradients: list[float] | list[list[float]]
194+
) -> list[float] | list[list[float]]:
192195
"""
193196
Initialize velocity with the same structure as gradients, filled with zeros.
194197
@@ -271,7 +274,7 @@ def __str__(self) -> str:
271274
f" Momentum: f = {f_momentum:.6f}, x = ({x_momentum[0]:6.3f}, {x_momentum[1]:6.3f})"
272275
)
273276

274-
print(f"\\nFinal comparison:")
277+
print("\\nFinal comparison:")
275278
f_final_sgd = x_sgd[0] ** 2 + 10 * x_sgd[1] ** 2
276279
f_final_momentum = x_momentum[0] ** 2 + 10 * x_momentum[1] ** 2
277280
print(f"SGD final loss: {f_final_sgd:.6f}")

0 commit comments

Comments
 (0)