style: apply rustfmt across all crates and demos
Consistent formatting pass: line wrapping, import sorting, trailing whitespace removal, let-chain indentation, merged derive attributes, and unsafe block reformatting. Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>
This commit is contained in:
@@ -101,7 +101,7 @@ impl LogCoshLoss {
|
||||
/// Create a new `LogCoshLoss` with default parameters
|
||||
///
|
||||
/// Default: Mean reduction
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
reduction: Reduction::Mean,
|
||||
@@ -109,7 +109,7 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Set the reduction mode
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn with_reduction(mut self, reduction: Reduction) -> Self {
|
||||
self.reduction = reduction;
|
||||
self
|
||||
@@ -134,52 +134,52 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Check if error is in the approximately quadratic region
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn is_in_quadratic_region(&self, error: f32) -> bool {
|
||||
error.abs() < 1.0
|
||||
}
|
||||
|
||||
/// Check if error is in the approximately linear region
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn is_in_linear_region(&self, error: f32) -> bool {
|
||||
error.abs() > 3.0
|
||||
}
|
||||
|
||||
/// Get the quadratic approximation for small errors
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn quadratic_approximation(&self, error: f32) -> f32 {
|
||||
0.5 * error * error
|
||||
}
|
||||
|
||||
/// Get the linear approximation for large errors
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn linear_approximation(&self, error: f32) -> f32 {
|
||||
error.abs() - 2.0f32.ln()
|
||||
}
|
||||
|
||||
/// Compute the derivative of `LogCoshLoss`
|
||||
/// d/dx log(cosh(x)) = tanh(x)
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn derivative(&self, error: f32) -> f32 {
|
||||
error.tanh()
|
||||
}
|
||||
|
||||
/// Compute the second derivative of `LogCoshLoss`
|
||||
/// d²/dx² log(cosh(x)) = sech²(x) = 1 - tanh²(x)
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn second_derivative(&self, error: f32) -> f32 {
|
||||
let tanh_x = error.tanh();
|
||||
1.0 - tanh_x * tanh_x
|
||||
}
|
||||
|
||||
/// Compute loss value for a single error (useful for testing)
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn compute_single_loss(&self, error: f32) -> f32 {
|
||||
self.stable_logcosh(error)
|
||||
}
|
||||
|
||||
/// Get approximation error for quadratic region
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn quadratic_approximation_error(&self, error: f32) -> f32 {
|
||||
let true_loss = self.compute_single_loss(error);
|
||||
let approx_loss = self.quadratic_approximation(error);
|
||||
@@ -192,7 +192,7 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Get approximation error for linear region
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn linear_approximation_error(&self, error: f32) -> f32 {
|
||||
let true_loss = self.compute_single_loss(error);
|
||||
let approx_loss = self.linear_approximation(error);
|
||||
@@ -205,7 +205,7 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Check if `LogCoshLoss` is more robust than MSE for given error
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn robustness_vs_mse(&self, error: f32) -> f32 {
|
||||
let logcosh_loss = self.compute_single_loss(error);
|
||||
let mse_loss = 0.5 * error * error;
|
||||
@@ -219,7 +219,7 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Compute numerical gradient using finite differences (for testing)
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn numerical_gradient(&self, error: f32, h: f32) -> f32 {
|
||||
let loss_plus = self.compute_single_loss(error + h);
|
||||
let loss_minus = self.compute_single_loss(error - h);
|
||||
@@ -227,7 +227,7 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Verify gradient correctness by comparing analytical vs numerical
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn verify_gradient(&self, error: f32, tolerance: f32) -> bool {
|
||||
let analytical_grad = self.derivative(error);
|
||||
let numerical_grad = self.numerical_gradient(error, 1e-5);
|
||||
@@ -235,7 +235,7 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Get loss function characteristics at a given point
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn get_characteristics(&self, error: f32) -> LossCharacteristics {
|
||||
LossCharacteristics {
|
||||
error,
|
||||
@@ -252,13 +252,13 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
/// Get the transition point where quadratic and linear approximations have equal accuracy
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn transition_point() -> f32 {
|
||||
1.2
|
||||
}
|
||||
|
||||
/// Get the stability threshold above which we use the stable approximation
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn stability_threshold() -> f32 {
|
||||
12.0
|
||||
}
|
||||
@@ -314,13 +314,14 @@ impl LogCoshLoss {
|
||||
}
|
||||
|
||||
if let Some(w) = weights
|
||||
&& w.shape() != predictions.shape() {
|
||||
return Err(TransformerError::InvalidInput(format!(
|
||||
"Weight shape {:?} doesn't match predictions shape {:?}",
|
||||
w.shape(),
|
||||
predictions.shape()
|
||||
)));
|
||||
}
|
||||
&& w.shape() != predictions.shape()
|
||||
{
|
||||
return Err(TransformerError::InvalidInput(format!(
|
||||
"Weight shape {:?} doesn't match predictions shape {:?}",
|
||||
w.shape(),
|
||||
predictions.shape()
|
||||
)));
|
||||
}
|
||||
|
||||
// Compute element-wise errors
|
||||
let errors = (predictions - targets)?;
|
||||
@@ -522,19 +523,19 @@ impl Loss for LogCoshLoss {
|
||||
/// Additional methods for autograd integration and advanced features
|
||||
impl LogCoshLoss {
|
||||
/// Create a `LogCoshLoss` builder for advanced configuration
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn builder() -> LogCoshLossBuilder {
|
||||
LogCoshLossBuilder::new()
|
||||
}
|
||||
|
||||
/// Check if this loss is equivalent to another `LogCoshLoss`
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn is_equivalent(&self, other: &Self) -> bool {
|
||||
self.reduction == other.reduction
|
||||
}
|
||||
|
||||
/// Clone with different reduction mode
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn with_different_reduction(&self, reduction: Reduction) -> Self {
|
||||
Self { reduction }
|
||||
}
|
||||
@@ -567,7 +568,7 @@ pub struct LogCoshLossBuilder {
|
||||
|
||||
impl LogCoshLossBuilder {
|
||||
/// Create a new builder
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
reduction: Reduction::Mean,
|
||||
@@ -575,14 +576,14 @@ impl LogCoshLossBuilder {
|
||||
}
|
||||
|
||||
/// Set the reduction mode
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn reduction(mut self, reduction: Reduction) -> Self {
|
||||
self.reduction = reduction;
|
||||
self
|
||||
}
|
||||
|
||||
/// Build the `LogCoshLoss`
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn build(self) -> LogCoshLoss {
|
||||
LogCoshLoss {
|
||||
reduction: self.reduction,
|
||||
@@ -601,7 +602,7 @@ pub struct LossComparison;
|
||||
|
||||
impl LossComparison {
|
||||
/// Compare `LogCosh` vs MSE loss for given errors
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn logcosh_vs_mse(errors: &[f32]) -> ComparisonResult {
|
||||
let logcosh = LogCoshLoss::new();
|
||||
|
||||
@@ -630,7 +631,7 @@ impl LossComparison {
|
||||
}
|
||||
|
||||
/// Compare `LogCosh` vs Huber loss (simulated)
|
||||
#[must_use]
|
||||
#[must_use]
|
||||
pub fn logcosh_vs_huber(errors: &[f32], delta: f32) -> ComparisonResult {
|
||||
let logcosh = LogCoshLoss::new();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user