Spaces:
Sleeping
Sleeping
Simon Clematide commited on
Commit ·
4380e67
1
Parent(s): cca13df
Add dedicated Online Help tab, quick grammar accordion, and clickable examples
Browse files
app.py
CHANGED
|
@@ -175,17 +175,37 @@ with gr.Blocks(title="Autodiff Mermaid Visualizer") as demo:
|
|
| 175 |
btn = gr.Button("Generate Diagram", variant="primary")
|
| 176 |
|
| 177 |
with gr.Tabs():
|
| 178 |
-
with gr.TabItem("Visual Diagram"):
|
| 179 |
diagram_output = gr.HTML(label="Diagram")
|
| 180 |
with gr.Row():
|
| 181 |
download_svg = gr.File(label="Download SVG (Vector)", interactive=False)
|
| 182 |
download_png = gr.File(label="Download PNG (Raster)", interactive=False)
|
| 183 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 184 |
code_output = gr.Code(label="Mermaid Syntax", language="markdown")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 185 |
|
| 186 |
-
with gr.Accordion("📖 Understanding the Computation Graph", open=True):
|
| 187 |
-
gr.Markdown(
|
| 188 |
-
"""
|
| 189 |
The graph shows how an expression is evaluated in the forward pass and how its gradients are calculated in the backward pass. Read the forward computation from bottom to top and the backward computation from top to bottom.
|
| 190 |
|
| 191 |
* **Blue area — Inputs**: The input variables and their values. Each green node shows the value `v(...)` used in the computation and, after backpropagation, the resulting `grad(...)`.
|
|
@@ -200,8 +220,67 @@ During the forward pass, each operation receives values from below, computes its
|
|
| 200 |
When an input influences the output through more than one path—as $x$ does in $x^x$—the relevant derivative contributions are combined to obtain the final `grad(x)`.
|
| 201 |
|
| 202 |
*In short: green nodes and solid arrows represent values and forward computation; pink nodes and purple dotted arrows represent local derivatives and gradient propagation.*
|
| 203 |
-
|
| 204 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 205 |
|
| 206 |
btn.click(
|
| 207 |
generate_diagram,
|
|
|
|
| 175 |
btn = gr.Button("Generate Diagram", variant="primary")
|
| 176 |
|
| 177 |
with gr.Tabs():
|
| 178 |
+
with gr.TabItem("📊 Visual Diagram"):
|
| 179 |
diagram_output = gr.HTML(label="Diagram")
|
| 180 |
with gr.Row():
|
| 181 |
download_svg = gr.File(label="Download SVG (Vector)", interactive=False)
|
| 182 |
download_png = gr.File(label="Download PNG (Raster)", interactive=False)
|
| 183 |
+
with gr.Accordion("💡 Quick Guide & Visual Grammar", open=False):
|
| 184 |
+
gr.Markdown(
|
| 185 |
+
"""
|
| 186 |
+
### Visual Grammar Summary
|
| 187 |
+
* **Blue area — Inputs**: Leaf input variables and assigned values. Pill nodes show $v(\\dots)$ and final $\\text{grad}(\\dots)$.
|
| 188 |
+
* **Yellow area — Operations**: Decomposed elementary operation steps ($+, -, \\times, \\div, \\text{pow}, \\dots$).
|
| 189 |
+
* **Pink trapezoids — Local derivatives**: Operation-local partial derivatives (e.g. $\\frac{\\partial (b \\cdot y)}{\\partial b} = y$).
|
| 190 |
+
* **Green solid arrows — Forward computation ($f$)**: Numerical values flowing upward toward Output ($f = \\text{val}$).
|
| 191 |
+
* **Purple dotted arrows — Backward computation ($g$)**: Propagated adjoint gradients flowing downward toward Inputs ($g = \\text{grad}$).
|
| 192 |
+
* **Green area — Output**: Scalar target evaluation. Backward pass begins here with $g = 1$.
|
| 193 |
+
* **Branching accumulation**: Gradients across multiple paths (like $x$ in $x^x$) automatically sum up to yield $\\text{grad}(x)$.
|
| 194 |
+
"""
|
| 195 |
+
)
|
| 196 |
+
with gr.TabItem("💻 Mermaid Code"):
|
| 197 |
code_output = gr.Code(label="Mermaid Syntax", language="markdown")
|
| 198 |
+
with gr.TabItem("📖 Online Help"):
|
| 199 |
+
gr.Markdown(
|
| 200 |
+
"""
|
| 201 |
+
# 📖 Online Help & Documentation
|
| 202 |
+
|
| 203 |
+
Welcome to the **Automatic Differentiation Visualizer**. This interactive tool decomposes mathematical expressions into computational graphs and demonstrates both **forward numerical evaluation** and **reverse-mode automatic differentiation (backpropagation)**.
|
| 204 |
+
|
| 205 |
+
---
|
| 206 |
+
|
| 207 |
+
### 1. Understanding the Computation Graph
|
| 208 |
|
|
|
|
|
|
|
|
|
|
| 209 |
The graph shows how an expression is evaluated in the forward pass and how its gradients are calculated in the backward pass. Read the forward computation from bottom to top and the backward computation from top to bottom.
|
| 210 |
|
| 211 |
* **Blue area — Inputs**: The input variables and their values. Each green node shows the value `v(...)` used in the computation and, after backpropagation, the resulting `grad(...)`.
|
|
|
|
| 220 |
When an input influences the output through more than one path—as $x$ does in $x^x$—the relevant derivative contributions are combined to obtain the final `grad(x)`.
|
| 221 |
|
| 222 |
*In short: green nodes and solid arrows represent values and forward computation; pink nodes and purple dotted arrows represent local derivatives and gradient propagation.*
|
| 223 |
+
|
| 224 |
+
---
|
| 225 |
+
|
| 226 |
+
### 2. Supported Syntax & Mathematical Operations
|
| 227 |
+
|
| 228 |
+
The visualizer uses a secure, strict AST-based mathematical parser. The following operations and functions are supported:
|
| 229 |
+
|
| 230 |
+
| Category | Operators / Functions | Example Syntax |
|
| 231 |
+
| :--- | :--- | :--- |
|
| 232 |
+
| **Arithmetic** | `+`, `-`, `*`, `/` | `x * y + 2`, `(a - b) / (c + 1)` |
|
| 233 |
+
| **Powers & Roots** | `**`, `sqrt` | `x**2`, `x**y`, `sqrt(x**2 + y**2)` |
|
| 234 |
+
| **Trigonometric** | `sin`, `cos`, `tan` | `sin(x) * cos(y)` |
|
| 235 |
+
| **Exponential & Logarithm** | `exp`, `log`, `ln` | `exp(y) * sin(x)`, `log(x)` |
|
| 236 |
+
| **Hyperbolic** | `sinh`, `cosh`, `tanh` | `tanh(w * x + b)` |
|
| 237 |
+
| **Inverse Trigonometric** | `asin`, `acos`, `atan` | `atan(x / y)` |
|
| 238 |
+
| **Absolute Value** | `abs` | `abs(x - y)` |
|
| 239 |
+
|
| 240 |
+
> **Power Rule Handling**: Negative bases with constant powers (e.g. `x**2` at $x = -3$) evaluate cleanly using the power rule $\\frac{d}{dx} x^2 = 2x$ without complex logarithms.
|
| 241 |
+
|
| 242 |
+
---
|
| 243 |
+
|
| 244 |
+
### 3. Supplying Input Values
|
| 245 |
+
|
| 246 |
+
Enter comma-separated assignments for independent variables in the **Input Values** textbox:
|
| 247 |
+
* `x=3.0, y=4.0`
|
| 248 |
+
* `x_1=1.0, x_2=2.0, x_3=3.0`
|
| 249 |
+
* `w=0.5, b=-0.1, x=2.0`
|
| 250 |
+
|
| 251 |
+
Any variable left unspecified defaults to `0.0`. Numeric literals inside the formula (such as `2` in `y + 2`) are automatically classified as constants in the inputs band.
|
| 252 |
+
|
| 253 |
+
---
|
| 254 |
+
|
| 255 |
+
### 4. Graph Modes
|
| 256 |
+
|
| 257 |
+
* **Forward & Backward (Autodiff)**: Generates the full computational graph with intermediate operations, local derivative rules, adjoint flows, and final input gradients.
|
| 258 |
+
* **Forward Only**: Generates only the upward forward evaluation graph ($f = \\text{val}$), ideal for initial lectures before introducing backpropagation.
|
| 259 |
+
|
| 260 |
+
---
|
| 261 |
+
|
| 262 |
+
### 5. Exporting Diagrams for Teaching & Slides
|
| 263 |
+
|
| 264 |
+
* **Download SVG (Vector)**: Exports a clean, infinitely scalable vector graphic for LaTeX/Beamer, Google Slides, Keynote, and print.
|
| 265 |
+
* **Download PNG (Raster)**: Exports a high-resolution raster graphic for web pages and documentation.
|
| 266 |
+
* **Mermaid Syntax**: Copy the raw diagram markup directly into GitHub Markdown, Obsidian, or Notion.
|
| 267 |
+
"""
|
| 268 |
+
)
|
| 269 |
+
|
| 270 |
+
gr.Examples(
|
| 271 |
+
examples=[
|
| 272 |
+
["(x * x) * y + (y + 2)", "x=3.0, y=4.0", "Forward & Backward (Autodiff)"],
|
| 273 |
+
["x * y", "x=2.0, y=3.0", "Forward & Backward (Autodiff)"],
|
| 274 |
+
["(x + y)**2", "x=1.0, y=2.0", "Forward & Backward (Autodiff)"],
|
| 275 |
+
["sin(x) * exp(y)", "x=1.5, y=0.5", "Forward & Backward (Autodiff)"],
|
| 276 |
+
["((x_1 + x_2) * x_3)**2", "x_1=1.0, x_2=2.0, x_3=3.0", "Forward & Backward (Autodiff)"],
|
| 277 |
+
["(x * x) * y + (y + 2)", "x=3.0, y=4.0", "Forward Only"],
|
| 278 |
+
],
|
| 279 |
+
inputs=[expr_input, vals_input, mode_input],
|
| 280 |
+
outputs=[diagram_output, code_output, download_svg, download_png],
|
| 281 |
+
fn=generate_diagram,
|
| 282 |
+
cache_examples=False,
|
| 283 |
+
)
|
| 284 |
|
| 285 |
btn.click(
|
| 286 |
generate_diagram,
|