---
title: "Clean matched benchmark: DDESONN and Keras across 100 seeds"
output: rmarkdown::html_vignette
vignette: >
  %\VignetteIndexEntry{Clean matched benchmark: DDESONN and Keras across 100 seeds}
  %\VignetteEngine{knitr::rmarkdown}
  %\VignetteEncoding{UTF-8}
---

## Study design

DDESONN and Keras each ran with summed gradients and with averaged gradients.
Weights and biases used the same reduction within each condition. The same
100 seed IDs and shared initial tensors were used across all four conditions,
for 400 fits. Each fit ran for 360 epochs with batches of 16 and was evaluated
at the final checkpoint. The frameworks used their own shuffle and dropout
streams.

The fixed split contained 3,500 training, 698 validation and 802 test rows.
Identical predictor vectors were kept in the same partition, with zero exact
overlap between partitions in raw predictors or float32 inputs.
Standardization was fitted on training data only. DDESONN used an
experimental native float32 adapter for this matched protocol. The results
apply to that adapter and this split.

Shiny's **Split overlap** monitor checks prepared predictor rows across all
three split pairs. The clean study's separate audit found zero exact matches
in each pair. Keeping identical predictors together and using the same split
for both frameworks made this an apples-to-apples comparison on this split.

## Held-out accuracy

```{r study-data, include=FALSE}
library(ggplot2)
study_dir <- system.file("shiny", "benchmark", package = "DDESONN", mustWork = TRUE)
study <- new.env()
sys.source(file.path(study_dir, "studies.R"), envir = study)
results <- read.csv(file.path(study_dir, "data", "100SeedsDDESONNvsKerasCleanSplit.csv"))
reductions <- c("Sum vs. sum" = "sum", "Mean vs. mean" = "mean")
cumulative <- do.call(rbind, lapply(names(reductions), function(label) {
  pairs <- study$study_clean_pairs(results, reductions[[label]])
  transform(study$study_seed_summary(pairs, cumulative = TRUE), comparison = label)
}))
cumulative$comparison <- factor(cumulative$comparison, levels = names(reductions))
overall <- subset(cumulative, seeds == 100)
```

```{r accuracy-summary, echo=FALSE}
knitr::kable(data.frame(
  Comparison = overall$comparison,
  `DDESONN (%)` = overall$DDESONN_pct,
  `Keras (%)` = overall$Keras_pct,
  `Difference (pp)` = overall$advantage_pp,
  `95% paired interval (pp)` = sprintf("%.2f to %.2f", overall$low_pp, overall$high_pp),
  `DDESONN wins / ties / losses` = with(overall, paste(wins, ties, losses, sep = " / ")),
  check.names = FALSE
), digits = 2, row.names = FALSE)
```

DDESONN had higher mean held-out accuracy under both matched reductions.
The paired intervals describe uncertainty in the mean difference across
training seeds on this fixed split. Differences are in percentage points (pp).

```{r accuracy-distributions, echo=FALSE, fig.width=7, fig.height=3.6, fig.alt="Boxplots of held-out accuracy for DDESONN and Keras in both matched reduction settings."}
results$comparison <- factor(results$reduction, levels = reductions, labels = names(reductions))
results$framework <- factor(results$framework, levels = c("ddesonn", "keras"),
                            labels = c("DDESONN", "Keras"))
ggplot(results, aes(framework, accuracy * 100, fill = framework)) +
  geom_boxplot(width = 0.55) +
  facet_wrap(~ comparison) +
  scale_fill_manual(values = c(DDESONN = "#087f8c", Keras = "#b65c28"), guide = "none") +
  labs(x = NULL, y = "Held-out accuracy (%)") +
  theme_minimal()
```

The boxplots show the seed-to-seed accuracy distributions for all four
conditions. The plots below keep the seed pairing and show the cumulative
mean difference in numerical seed order. Shading gives pointwise 95%
paired-t intervals. The number of seeds was a retrospective precision and
compute decision.

```{r cumulative-precision, echo=FALSE, fig.width=7, fig.height=3.6, fig.alt="Cumulative DDESONN minus Keras accuracy with pointwise 95 percent intervals for both reductions."}
ggplot(cumulative, aes(seeds, advantage_pp)) +
  geom_ribbon(aes(ymin = low_pp, ymax = high_pp), fill = "#087f8c", alpha = 0.2) +
  geom_hline(yintercept = 0, linetype = 2) +
  geom_line(color = "#087f8c") +
  geom_point(color = "#087f8c") +
  facet_wrap(~ comparison) +
  labs(x = "Training seeds per condition", y = "DDESONN − Keras accuracy (pp)") +
  theme_minimal()
```

## Reproduce and extend

Open **Experiments → Current: clean matched study** in the
[Shiny workbench](packaged-data-workbench.html) to explore the results,
download paired seed records and get the reproduction bundle.

We welcome a follow-up with four new conditions. In each framework, try
both of these reductions for the dense-layer data-loss gradients.

| Weight gradients | Bias gradients | Frameworks |
|---|---|---|
| Sum | Average | DDESONN and Keras |
| Average | Sum | DDESONN and Keras |

Compare DDESONN with Keras within each mixed setting using matched seeds
and the same split. These four conditions are proposed follow-ups. The
frozen benchmark training loops apply one reduction to all parameters and
need changes to run mixed settings. Current DDESONN supports separate
weight and bias reductions, including in the Shiny workbench.

For the follow-up, document how batch-normalization parameters are reduced.
Add the dense-kernel L1 gradient once, after reducing the data-loss gradient.
Use the actual batch size for averaging, including the smaller final batch.

See the [contribution instructions](https://github.com/MatHatter/DDESONN/blob/main/dev/research/keras-clean-reference/CONTRIBUTING_SEEDS.md)
for the baseline launchers and what to include when sharing a follow-up.
