{
  "cells": [
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "# Bab 10 — Deep Learning Playground\n",
        "\n",
        "Notebook ringan untuk menjalankan contoh CNN, RNN/LSTM, Transformer attention, dan efisiensi convolution dari `deep_learning_playground.py`. Cocok untuk Jupyter, VS Code, Google Colab, dan Kaggle."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "from pathlib import Path\n",
        "import sys\n",
        "# Jika notebook dijalankan dari folder lain, arahkan ke folder code Bab 10.\n",
        "chapter_code = Path.cwd()\n",
        "if not (chapter_code / 'deep_learning_playground.py').exists():\n",
        "    candidates = list(Path.cwd().rglob('deep_learning_playground.py'))\n",
        "    if candidates:\n",
        "        chapter_code = candidates[0].parent\n",
        "sys.path.insert(0, str(chapter_code))\n",
        "import deep_learning_playground as dl\n",
        "chapter_code"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 1. Convolution manual\n",
        "Kita ulangi contoh patch dan kernel dari draft."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "import numpy as np\n",
        "x = np.array([[1,2,0,1],[3,1,2,2],[0,1,3,1],[2,2,1,0]], dtype=float)\n",
        "kernel = np.array([[1,0],[0,-1]], dtype=float)\n",
        "dl.conv2d_valid(x, kernel)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 2. Shape dan biaya convolution\n",
        "Bandingkan convolution standar dengan depthwise separable convolution."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "standard = dl.conv_multadds(kernel=3, in_channels=32, out_channels=64, feature_size=64)\n",
        "separable = dl.depthwise_separable_multadds(kernel=3, in_channels=32, out_channels=64, feature_size=64)\n",
        "standard, separable, round(standard / separable, 3)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 3. RNN, LSTM, dan attention\n",
        "Contoh ini kecil agar mudah dihitung ulang dengan kalkulator."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "q = np.array([[1.0, 0.0], [0.2, 0.8]])\n",
        "k = np.array([[1.0, 0.0], [0.0, 1.0]])\n",
        "v = np.array([[10.0, 0.0], [0.0, 5.0]])\n",
        "context, weights = dl.self_attention(q, k, v)\n",
        "weights, context"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "## 4. Jalankan demo lengkap dan simpan output\n",
        "Script akan membuat `outputs/bab10_demo_results.json` dan, jika matplotlib tersedia, `outputs/bab10_loss_curves.png`."
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {},
      "outputs": [],
      "source": [
        "result = dl.run_demo()\n",
        "result['plot_status']"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "name": "python",
      "version": "3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 5
}