{ "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/19_gelu.ipynb)\n", "\n", "# ๐ŸŸข Easy: GELU Activation\n", "\n", "Implement the **GELU** (Gaussian Error Linear Unit) activation.\n", "\n", "$$\\text{GELU}(x) = x \\cdot \\Phi(x) = x \\cdot 0.5 \\cdot (1 + \\text{erf}(x / \\sqrt{2}))$$\n", "\n", "### Signature\n", "```python\n", "def my_gelu(x: Tensor) -> Tensor: ...\n", "```\n", "\n", "### Rules\n", "- Do NOT use `F.gelu`, `nn.GELU`, or `torch.nn.functional.gelu`\n", "- Use `torch.erf` for the exact version" ], "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 math" ], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# โœ๏ธ YOUR IMPLEMENTATION HERE\n", "\n", "def my_gelu(x):\n", " pass" ], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# ๐Ÿงช Debug\n", "x = torch.tensor([-2., -1., 0., 1., 2.])\n", "print('Output:', my_gelu(x))\n", "print('Ref: ', torch.nn.functional.gelu(x))" ], "execution_count": null }, { "cell_type": "code", "metadata": {}, "outputs": [], "source": [ "# โœ… SUBMIT\n", "from torch_judge import check\n", "check('gelu')" ], "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 }