Following this instruction :
1. Strictly adhere to PEP-8 standards, ensuring code is clean, well-structured, and follows Python's style guide for readability.
2. Use appropriate modules like `typing` for type hints, ensuring code clarity, maintainability, and leveraging Python's standard libraries effectively.
3. Focus on time, space, robustness, scalability; write optimized, efficient code that can handle large datasets and scale seamlessly.
4. Implement comprehensive error handling, catching exceptions gracefully, providing meaningful error messages, and ensuring code robustness under unexpected conditions.
5. Ensure the developer agent writes advanced, maintainable Python code, balancing performance and readability while adhering to best practices.


TASK : we have develop very algorithm from starch using torch module below develop all defences logic from scratch,  

Note : below user data given
#==========================================
from torchvision import transforms
import torch

from rldata import datapipeline,DatasetConfig
from rlmodel import ImageClassificationPipeline,ModelSource
from rlmetrics import MetricsCalculator
#dataset
config=DatasetConfig(
    dataset_path="zh-plus/tiny-imagenet",
    split_config={"train": 0.8, "test": 0.2},
    columns=["image","label"],
    num_samples=1000,
    seed=42,
    kwargs={"cache_dir": "./data_test",} 
)


trans = transforms.Compose([
    transforms.Lambda(lambda img: img.convert("RGB")),  # Convert to RGB first
    transforms.Resize((224, 224)),
    transforms.ToTensor(), 
    transforms.Normalize(mean=[0.485, 0.456, 0.406],  # Use three values for three channels
                       std=[0.229, 0.224, 0.225])
])

train_loader, test_loader=datapipeline(config=config,
             transforms_fn=trans,
             target_column="image",
             label_column="label")


model_pipeline=ImageClassificationPipeline(
    model_name_or_path="google/vit-base-patch16-224",
    model_source=ModelSource.TRANSFORMERS.value,

)
model=model_pipeline.model # loading model
def print_metrics(metrics: Dict[str, Any]):
    for metric, value in metrics.items():
        if isinstance(value, torch.Tensor):
            if value.numel() == 1:
                print(f"{metric}: {value.item()}")
            else:
                print(f"{metric}: {value.cpu().numpy()}")
        elif isinstance(value, float):
            print(f"{metric}: {value}")
        else:
            # Handle other possible types (e.g., None should have been removed)
            print(f"{metric}: {value}")
for batch in train_dataloader:
    images, labels = batch["images"], batch["labels"]
    model_output = model(images)  
    metrics_calc_multi = MetricsCalculator(preds=model_output.logits , labels=labels, num_classes=1000)
    metrics_multi = metrics_calc_multi.get_all_metrics()
    print("\nMulti-Class Classification Metrics:")
    print_metrics(metrics_multi)
#===========================================================================

now we have devlop below api  from starch (devlop below api in a single .py file)

str: 

Here are the key mathematical equations for the defensive techniques in tabular form:

| Defense Technique | Mathematical Equation | Parameters & Description |
|------------------|----------------------|-------------------------|
| Gaussian Noise | x' = x + ε, where ε ~ N(0, σ²) | σ: standard deviation of noise |
| Label Smoothing | y' = (1 - α)y + α/K | α: smoothing parameter <br> K: number of classes <br> y: original label |
| Mixup | x̃ = λxi + (1-λ)xj <br> ỹ = λyi + (1-λ)yj | λ ~ Beta(α,α) <br> xi,xj: input pairs <br> yi,yj: label pairs |
| Feature Squeezing | x' = round(x * d)/d | d: bit depth parameter |
| Spatial Smoothing | x'[i,j] = 1/N ∑(x[i±k,j±k]) | N: window size <br> k: kernel radius |
| TRADES Training | min L(f(x),y) + β * max KL(f(x),f(x')) | β: trade-off parameter <br> KL: Kullback-Leibler divergence |
| CutMix | x̃ = M⊙xa + (1-M)⊙xb <br> ỹ = λya + (1-λ)yb | M: binary mask <br> ⊙: element-wise multiplication |
| Cutout | x' = x⊙(1-M) | M: binary mask for regions to remove |
| Defensive GAN | min_G max_D E[log(D(x)) + log(1-D(G(z)))] | G: generator <br> D: discriminator |
| Jpeg Compression | DCT(x) → Quantize → IDCT | DCT: Discrete Cosine Transform |
| Gaussian Augmentation | x' = x + N(μ, σ²) | μ: mean <br> σ: standard deviation |
| Inverse GAN | z* = argmin_z ||G(z) - x||₂ | G: trained generator <br> x: input sample |
| Adversarial Training | min_θ E[max_{||δ||≤ε} L(θ,x+δ,y)] | θ: model parameters <br> ε: perturbation bound |
| Binary Input Detector | P(y=1|x) = σ(w^T x + b) | σ: sigmoid function |
| PixelDefend | P(x'|x) = ∏ P(x'_i|x'_{<i}) | Autoregressive probability model |
| Resample | x' = Interpolate(Downsample(x)) | Various interpolation methods |

Additional Training Variations:

| Training Method | Key Equation | Description |
|----------------|--------------|-------------|
| AWP Training | min_θ max_w L(θ + w) | Adversarial Weight Perturbation |
| OAAT Training | min_θ ∑ L(fᵢ(x + δᵢ), y) | One-at-a-time adversarial training |
| IBP Training | min_θ (L_clean + λL_IBP) | Interval Bound Propagation |
| FBF Training | min_θ E[L(x + δ_t, y)] | Forward-Backward-Forward |
| DP InstaHide | x' = ∑αᵢxᵢ + N(0,σ²) | Differential Privacy with mixing |

Notes:
- L(): Loss function
- f(): Model function
- x: Input data
- y: True labels
- δ: Adversarial perturbation
- θ: Model parameters

These equations represent the core mathematical operations of each defense, though implementations may include additional complexities and variations.