{ "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/01_relu.ipynb)\n", "\n", "# ๐ŸŸข Easy: Implement ReLU\n", "\n", "Implement the **ReLU** (Rectified Linear Unit) activation function from scratch.\n", "\n", "$$\\text{ReLU}(x) = \\max(0, x)$$\n", "\n", "### Signature\n", "```python\n", "def relu(x: torch.Tensor) -> torch.Tensor:\n", " ...\n", "```\n", "\n", "### Rules\n", "- Do **NOT** use `torch.relu`, `F.relu`, `torch.clamp`, or any built-in activation\n", "- Must support autograd (gradients should flow back)\n", "\n", "### Example\n", "```\n", "Input: tensor([-2., -1., 0., 1., 2.])\n", "Output: tensor([ 0., 0., 0., 1., 2.])\n", "```" ], "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": {}, "source": [ "import torch" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "metadata": {}, "source": [ "# โœ๏ธ YOUR IMPLEMENTATION HERE\n", "\n", "def relu(x: torch.Tensor) -> torch.Tensor:\n", " pass # Replace this\n", "" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "metadata": {}, "source": [ "# ๐Ÿงช Test your implementation (feel free to add more debug prints)\n", "x = torch.tensor([-2., -1., 0., 1., 2.])\n", "print(\"Input: \", x)\n", "print(\"Output:\", relu(x))\n", "print(\"Shape: \", relu(x).shape)" ], "outputs": [], "execution_count": null }, { "cell_type": "code", "metadata": {}, "source": [ "# โœ… SUBMIT โ€” Run this cell to check your solution\n", "from torch_judge import check\n", "check(\"relu\")" ], "outputs": [], "execution_count": null } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "language": "python", "name": "python3" }, "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.11.14" } }, "nbformat": 4, "nbformat_minor": 4 }