{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "[![Open In Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/github/duoan/TorchCode/blob/master/templates/17_dropout.ipynb)\n", "\n", "# ๐ŸŸข Easy: Implement Dropout\n", "\n", "Implement **Dropout** regularization from scratch.\n", "\n", "### Signature\n", "```python\n", "class MyDropout(nn.Module):\n", " def __init__(self, p: float = 0.5): ...\n", " def forward(self, x: Tensor) -> Tensor: ...\n", "```\n", "\n", "### Rules\n", "- During **training**: zero each element with probability `p`, scale remaining by `1/(1-p)`\n", "- During **eval**: return input unchanged (identity)\n", "- Do NOT use `nn.Dropout` or `F.dropout`" ], "outputs": [] }, { "cell_type": "code", "metadata": {}, "source": [ "# Install torch-judge in Colab (no-op in JupyterLab/Docker)\n", "try:\n", " import google.colab\n", " get_ipython().run_line_magic('pip', 'install -q torch-judge')\n", "except ImportError:\n", " pass\n" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "import torch\n", "import torch.nn as nn" ], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# โœ๏ธ YOUR IMPLEMENTATION HERE\n", "\n", "class MyDropout(nn.Module):\n", " def __init__(self, p=0.5):\n", " super().__init__()\n", " pass\n", "\n", " def forward(self, x):\n", " pass" ], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# ๐Ÿงช Debug\n", "d = MyDropout(p=0.5)\n", "d.train()\n", "x = torch.ones(10)\n", "print('Train:', d(x))\n", "d.eval()\n", "print('Eval: ', d(x))" ], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# โœ… SUBMIT\n", "from torch_judge import check\n", "check('dropout')" ], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.11.0" } }, "nbformat": 4, "nbformat_minor": 4 }