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 : develop only defence api below user data given
#==========================================
    """
    Main function to execute adversarial attacks on the dataset.
    """
    from rldata import datapipeline, DatasetConfig
    from rlmetrics import MetricsCalculator
    from rlattack import   AdversarialAttack
    # Dataset configuration
    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"},
    )

    # Data transformations
    trans = transforms.Compose([
        transforms.Lambda(lambda img: img.convert("RGB")),
        transforms.Resize((224, 224)),
        transforms.ToTensor(),
        transforms.Normalize(mean=[0.485, 0.456, 0.406],
                             std=[0.229, 0.224, 0.225])
    ])

    # Data loaders
    try:
        train_loader, test_loader = datapipeline(
            config=config,
            transforms_fn=trans,
            target_column="image",
            label_column="label"
        )
        logger.info("Data loaders created successfully.")
    except Exception as e:
        logger.error(f"Failed to create data loaders: {e}")
        return

    # Load model
    try:
        model = load_model()
    except Exception as e:
        logger.error(f"Failed to load model: {e}")
        return

    # Initialize AdversarialAttack
    adversary = AdversarialAttack(model=model)

    # Define list of attacks
    list_attack: List[str] = [
        'FGSM',
        'FastGradientMethod',
        'PGD',
        'AutoProjectedGradientDescent',
        'DeepFool',
        'CW_L2',
        'CW_Linf',
        'ElasticNet',
        'BasicIterativeMethod',
        'MomentumIterativeMethod',
        'AutoConjugateGradient',
        'PixelAttack',
        'SquareAttack',
        'SquareAttackBatch',
        'SnowAttack',  # Assuming 'SpatialTransformation' was intended as 'SnowAttack'
        'AdversarialPatch',
        'UniversalPerturbation',
        'ZooAttack',
        'CompositeAdversarialAttackPyTorch',
        'SquareAttackBatch',  # Duplicate removed or kept based on use-case
    ]

    # Iterate over the training data and apply attacks
    try:
        for batch in train_loader:
            images, labels = batch["images"], batch["labels"]
            images = images.to(adversary.device)
            labels = labels.to(adversary.device)

            # Iterate over each attack
            for attack_name in list_attack:
                try:
                    config_attack: Optional[AttackConfig] = None
                    # Define specific configurations for each attack if needed
                    if attack_name == 'FGSM':
                        config_attack = {'epsilon': 0.007}
                    elif attack_name == 'FastGradientMethod':
                        config_attack = {'epsilon': 0.007}
                    elif attack_name == 'PGD':
                        config_attack = {'epsilon': 0.03, 'alpha': 0.007, 'iters': 40}
                    elif attack_name == 'AutoProjectedGradientDescent':
                        config_attack = {'epsilon': 0.03, 'alpha': 0.007, 'iters': 40}
                    elif attack_name == 'DeepFool':
                        config_attack = {'max_iter': 50, 'overshoot': 0.02}
                    elif attack_name == 'CW_L2':
                        config_attack = {'c': 1e-4, 'max_iter': 1000}
                    elif attack_name == 'CW_Linf':
                        config_attack = {'c': 1e-4, 'max_iter': 1000}
                    elif attack_name == 'ElasticNet':
                        config_attack = {'alpha': 1.0, 'beta': 0.01, 'iters': 1000}
                    elif attack_name == 'BasicIterativeMethod':
                        config_attack = {'epsilon': 0.03, 'alpha': 0.005, 'iters': 10}
                    elif attack_name == 'MomentumIterativeMethod':
                        config_attack = {'alpha': 0.01, 'momentum': 1.0, 'epsilon': 0.03, 'iters': 40}
                    elif attack_name == 'AutoConjugateGradient':
                        config_attack = {'epsilon': 0.03, 'alpha': 0.007, 'iters': 40}
                    elif attack_name == 'PixelAttack':
                        config_attack = {'num_pixels': 100, 'epsilon': 0.02, 'iters': 1000}
                    elif attack_name == 'SquareAttack':
                        config_attack = {'n_queries': 1000, 'p_init': 0.05, 'p_max': 0.4,
                                         'p_min': 0.001, 's_init': 0.5, 's_min': 0.05, 's_max': 0.9}
                    elif attack_name == 'SquareAttackBatch':
                        config_attack = {'n_queries': 1000, 'p_init': 0.05, 'p_max': 0.4,
                                         'p_min': 0.001, 's_init': 0.5, 's_min': 0.05, 's_max': 0.9}
                    elif attack_name == 'AdversarialPatch':
                        config_attack = {'patch_size': 50, 'epsilon': 0.03, 'iters': 100}
                    elif attack_name == 'UniversalPerturbation':
                        config_attack = {'xi': 0.03, 'delta': 0.2, 'max_iter_uni': 10, 'max_iter_df': 50}
                    elif attack_name == 'ZooAttack':
                        config_attack = {'n_queries': 1000, 'epsilon': 0.007}
                    elif attack_name == 'CompositeAdversarialAttackPyTorch':
                        # Example composite: FGSM followed by PGD
                        config_attack = {
                            'attacks': ['FGSM', 'PGD'],
                            'configs': [
                                {'epsilon': 0.007},
                                {'epsilon': 0.03, 'alpha': 0.007, 'iters': 40}
                            ]
                        }
                    elif attack_name == 'SpatialTransformation':
                        config_attack = {'alpha': 0.1, 'beta': 0.1}
                    else:
                        logger.warning(f"No configuration defined for attack '{attack_name}'. Using defaults.")

                    adv_images = adversary.attack(attack_name, images, labels, config_attack)

                    # Forward pass with adversarial examples
                    model_output = model(adv_images).logits  # Extract logits
                    metrics_calc = MetricsCalculator(preds=model_output, labels=labels, num_classes=1000)
                    metrics = metrics_calc.get_all_metrics()
                    print(f"\nAttack: {attack_name}")
                    print_metrics(metrics)

                except ValueError as ve:
                    logger.warning(f"Skipping unsupported attack '{attack_name}': {ve}")
                except NotImplementedError as nie:
                    logger.warning(f"Attack '{attack_name}' is not implemented: {nie}")
                except RuntimeError as re:
                    logger.error(f"Attack '{attack_name}' failed: {re}")
    except Exception as e:
            logger.error(f"Unexpected error during attack '{attack_name}': {e}")


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.