1#![warn(missing_docs)]
97#![warn(clippy::all)]
98#![warn(clippy::pedantic)]
99#![allow(clippy::cast_possible_truncation)]
101#![allow(clippy::cast_sign_loss)]
102#![allow(clippy::cast_precision_loss)]
103#![allow(clippy::cast_possible_wrap)]
104#![allow(clippy::missing_errors_doc)]
105#![allow(clippy::missing_panics_doc)]
106#![allow(clippy::must_use_candidate)]
107#![allow(clippy::module_name_repetitions)]
108#![allow(clippy::similar_names)]
109#![allow(clippy::many_single_char_names)]
110#![allow(clippy::too_many_arguments)]
111#![allow(clippy::doc_markdown)]
112#![allow(clippy::cast_lossless)]
113#![allow(clippy::needless_pass_by_value)]
114#![allow(clippy::redundant_closure_for_method_calls)]
115#![allow(clippy::uninlined_format_args)]
116#![allow(clippy::ptr_arg)]
117#![allow(clippy::return_self_not_must_use)]
118#![allow(clippy::not_unsafe_ptr_arg_deref)]
119#![allow(clippy::items_after_statements)]
120#![allow(clippy::unreadable_literal)]
121#![allow(clippy::if_same_then_else)]
122#![allow(clippy::needless_range_loop)]
123#![allow(clippy::trivially_copy_pass_by_ref)]
124#![allow(clippy::unnecessary_wraps)]
125#![allow(clippy::match_same_arms)]
126#![allow(clippy::unused_self)]
127#![allow(clippy::too_many_lines)]
128#![allow(clippy::single_match_else)]
129#![allow(clippy::fn_params_excessive_bools)]
130#![allow(clippy::struct_excessive_bools)]
131#![allow(clippy::format_push_string)]
132#![allow(clippy::erasing_op)]
133#![allow(clippy::type_repetition_in_bounds)]
134#![allow(clippy::iter_without_into_iter)]
135#![allow(clippy::should_implement_trait)]
136#![allow(clippy::use_debug)]
137#![allow(clippy::case_sensitive_file_extension_comparisons)]
138#![allow(clippy::large_enum_variant)]
139#![allow(clippy::panic)]
140#![allow(clippy::struct_field_names)]
141#![allow(clippy::missing_fields_in_debug)]
142#![allow(clippy::upper_case_acronyms)]
143#![allow(clippy::assigning_clones)]
144#![allow(clippy::option_if_let_else)]
145#![allow(clippy::manual_let_else)]
146#![allow(clippy::explicit_iter_loop)]
147#![allow(clippy::default_trait_access)]
148#![allow(clippy::only_used_in_recursion)]
149#![allow(clippy::manual_clamp)]
150#![allow(clippy::ref_option)]
151#![allow(clippy::multiple_bound_locations)]
152#![allow(clippy::comparison_chain)]
153#![allow(clippy::manual_assert)]
154#![allow(clippy::unnecessary_debug_formatting)]
155
156pub mod adam;
161pub mod grad_scaler;
162pub mod health;
163pub mod lamb;
164pub mod lr_scheduler;
165pub mod optimizer;
166pub mod rmsprop;
167pub mod sgd;
168
169pub use adam::{Adam, AdamW};
174pub use grad_scaler::{GradScaler, GradScalerState};
175pub use health::{
176 AlertKind, AlertSeverity, HealthReport, LossTrend, MonitorConfig, TrainingAlert,
177 TrainingMonitor,
178};
179pub use lamb::LAMB;
180pub use lr_scheduler::{
181 CosineAnnealingLR, ExponentialLR, LRScheduler, MultiStepLR, OneCycleLR, ReduceLROnPlateau,
182 StepLR, WarmupLR,
183};
184pub use optimizer::Optimizer;
185pub use rmsprop::RMSprop;
186pub use sgd::SGD;
187
188pub mod prelude {
194 pub use crate::{
195 Adam, AdamW, CosineAnnealingLR, ExponentialLR, GradScaler, LRScheduler, MultiStepLR,
196 OneCycleLR, Optimizer, RMSprop, ReduceLROnPlateau, StepLR, WarmupLR, LAMB, SGD,
197 };
198}
199
200#[cfg(test)]
205mod tests {
206 use super::*;
207 use axonml_autograd::Variable;
208 use axonml_nn::{Linear, MSELoss, Module, ReLU, Sequential};
209 use axonml_tensor::Tensor;
210
211 #[test]
212 fn test_sgd_optimization() {
213 let model = Sequential::new()
214 .add(Linear::new(2, 4))
215 .add(ReLU)
216 .add(Linear::new(4, 1));
217
218 let mut optimizer = SGD::new(model.parameters(), 0.01);
219 let loss_fn = MSELoss::new();
220
221 let input = Variable::new(
222 Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).unwrap(),
223 false,
224 );
225 let target = Variable::new(Tensor::from_vec(vec![1.0, 2.0], &[2, 1]).unwrap(), false);
226
227 let initial_loss = loss_fn.compute(&model.forward(&input), &target);
228 let initial_loss_val = initial_loss.data().to_vec()[0];
229
230 for _ in 0..10 {
232 optimizer.zero_grad();
233 let output = model.forward(&input);
234 let loss = loss_fn.compute(&output, &target);
235 loss.backward();
236 optimizer.step();
237 }
238
239 let final_loss = loss_fn.compute(&model.forward(&input), &target);
240 let final_loss_val = final_loss.data().to_vec()[0];
241
242 assert!(final_loss_val <= initial_loss_val);
244 }
245
246 #[test]
247 fn test_adam_optimization() {
248 let model = Sequential::new()
249 .add(Linear::new(2, 4))
250 .add(ReLU)
251 .add(Linear::new(4, 1));
252
253 let mut optimizer = Adam::new(model.parameters(), 0.01);
254 let loss_fn = MSELoss::new();
255
256 let input = Variable::new(
257 Tensor::from_vec(vec![1.0, 2.0, 3.0, 4.0], &[2, 2]).unwrap(),
258 false,
259 );
260 let target = Variable::new(Tensor::from_vec(vec![1.0, 2.0], &[2, 1]).unwrap(), false);
261
262 for _ in 0..20 {
264 optimizer.zero_grad();
265 let output = model.forward(&input);
266 let loss = loss_fn.compute(&output, &target);
267 loss.backward();
268 optimizer.step();
269 }
270
271 let final_output = model.forward(&input);
273 assert_eq!(final_output.shape(), vec![2, 1]);
274 }
275
276 #[test]
277 fn test_lr_scheduler() {
278 let model = Linear::new(10, 5);
279 let mut optimizer = SGD::new(model.parameters(), 0.1);
280 let mut scheduler = StepLR::new(&optimizer, 10, 0.1);
281
282 assert!((optimizer.get_lr() - 0.1).abs() < 1e-6);
283
284 for _ in 0..10 {
285 scheduler.step(&mut optimizer);
286 }
287
288 assert!((optimizer.get_lr() - 0.01).abs() < 1e-6);
289 }
290}