Skip to content

Commit aa5fbae

Browse files
committed
add alias for sgd
1 parent 44b28a7 commit aa5fbae

2 files changed

Lines changed: 58 additions & 1 deletion

File tree

python/paddle/optim/sgd.py

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,31 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
from paddle.optimizer.sgd import SGD # noqa: F401
15+
from __future__ import annotations
16+
17+
from typing import TYPE_CHECKING
18+
19+
from paddle.optimizer.sgd import SGD as PaddleSGD
20+
21+
if TYPE_CHECKING:
22+
from collections.abc import Sequence
23+
24+
from paddle import Tensor
25+
from paddle.optimizer.optimizer import _ParameterConfig
26+
27+
28+
class SGD(PaddleSGD):
29+
def __init__(
30+
self,
31+
params: Sequence[Tensor] | Sequence[_ParameterConfig] | None = None,
32+
lr: float | Tensor = 1e-3,
33+
momentum: float = 0,
34+
dampening: float = 0,
35+
weight_decay: float | Tensor = 0,
36+
nesterov: bool = False,
37+
) -> None:
38+
super().__init__(
39+
learning_rate=lr,
40+
parameters=params,
41+
weight_decay=weight_decay,
42+
)

test/legacy_test/test_api_compatibility_part1.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2396,5 +2396,35 @@ def test_Compatibility(self):
23962396
paddle.set_rng_state(new_state=states)
23972397

23982398

2399+
# Test SGD API compatibility
2400+
class TestSGDAPI(unittest.TestCase):
2401+
def setUp(self):
2402+
np.random.seed(2025)
2403+
self.params = [
2404+
paddle.to_tensor(
2405+
np.random.randn(3, 4).astype('float32'), stop_gradient=False
2406+
)
2407+
]
2408+
2409+
def test_dygraph_Compatibility(self):
2410+
paddle.disable_static()
2411+
# 1. positional arguments
2412+
sgd1 = paddle.optim.SGD(self.params, 0.01, 0, 0, 1e-4)
2413+
# 2. keyword arguments
2414+
sgd2 = paddle.optim.SGD(
2415+
params=self.params, lr=0.01, momentum=0.9, weight_decay=1e-4
2416+
)
2417+
# 3. Mixed arguments
2418+
sgd3 = paddle.optim.SGD(
2419+
self.params, 0.01, momentum=0.9, weight_decay=1e-4
2420+
)
2421+
# Verify all optimizers created successfully
2422+
self.assertIsNotNone(sgd1)
2423+
self.assertIsNotNone(sgd2)
2424+
self.assertIsNotNone(sgd3)
2425+
2426+
paddle.enable_static()
2427+
2428+
23992429
if __name__ == '__main__':
24002430
unittest.main()

0 commit comments

Comments
 (0)