{
"cells": [
{
"cell_type": "markdown",
"id": "b9dcaba2",
"metadata": {
"slideshow": {
"slide_type": "-"
}
},
"source": [
"# 多层感知机\n",
"\n",
"简要介绍一些常见的激活函数"
]
},
{
"cell_type": "code",
"execution_count": 1,
"id": "5fe77ef0",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:49.792151Z",
"iopub.status.busy": "2023-08-18T06:56:49.791335Z",
"iopub.status.idle": "2023-08-18T06:56:51.817055Z",
"shell.execute_reply": "2023-08-18T06:56:51.816151Z"
},
"origin_pos": 2,
"tab": [
"pytorch"
]
},
"outputs": [],
"source": [
"%matplotlib inline\n",
"import torch\n",
"from d2l import torch as d2l"
]
},
{
"cell_type": "markdown",
"id": "785aeacf",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"ReLU提供了一种非常简单的非线性变换\n",
"$$\\operatorname{ReLU}(x) = \\max(x, 0)$$"
]
},
{
"cell_type": "code",
"execution_count": 2,
"id": "e5c9d313",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:51.821221Z",
"iopub.status.busy": "2023-08-18T06:56:51.820525Z",
"iopub.status.idle": "2023-08-18T06:56:52.021660Z",
"shell.execute_reply": "2023-08-18T06:56:52.020787Z"
},
"origin_pos": 7,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)\n",
"y = torch.relu(x)\n",
"d2l.plot(x.detach(), y.detach(), 'x', 'relu(x)', figsize=(5, 2.5))"
]
},
{
"cell_type": "code",
"execution_count": 3,
"id": "98dfa40b",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:52.025683Z",
"iopub.status.busy": "2023-08-18T06:56:52.025087Z",
"iopub.status.idle": "2023-08-18T06:56:52.225589Z",
"shell.execute_reply": "2023-08-18T06:56:52.224709Z"
},
"origin_pos": 12,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"y.backward(torch.ones_like(x), retain_graph=True)\n",
"d2l.plot(x.detach(), x.grad, 'x', 'grad of relu', figsize=(5, 2.5))"
]
},
{
"cell_type": "markdown",
"id": "d7338227",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"对于一个定义域在$\\mathbb{R}$中的输入,\n",
"*sigmoid函数*将输入变换为区间(0, 1)上的输出\n",
"$$\\operatorname{sigmoid}(x) = \\frac{1}{1 + \\exp(-x)}$$"
]
},
{
"cell_type": "code",
"execution_count": 4,
"id": "2dff6175",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:52.229275Z",
"iopub.status.busy": "2023-08-18T06:56:52.228651Z",
"iopub.status.idle": "2023-08-18T06:56:52.431085Z",
"shell.execute_reply": "2023-08-18T06:56:52.430246Z"
},
"origin_pos": 17,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"y = torch.sigmoid(x)\n",
"d2l.plot(x.detach(), y.detach(), 'x', 'sigmoid(x)', figsize=(5, 2.5))"
]
},
{
"cell_type": "code",
"execution_count": 5,
"id": "21eafca3",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:52.434872Z",
"iopub.status.busy": "2023-08-18T06:56:52.434265Z",
"iopub.status.idle": "2023-08-18T06:56:52.637287Z",
"shell.execute_reply": "2023-08-18T06:56:52.636457Z"
},
"origin_pos": 22,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x.grad.data.zero_()\n",
"y.backward(torch.ones_like(x),retain_graph=True)\n",
"d2l.plot(x.detach(), x.grad, 'x', 'grad of sigmoid', figsize=(5, 2.5))"
]
},
{
"cell_type": "markdown",
"id": "65d3a5dd",
"metadata": {
"slideshow": {
"slide_type": "slide"
}
},
"source": [
"Tanh(双曲正切)函数也能将其输入压缩转换到区间(-1, 1)上\n",
"$$\\operatorname{tanh}(x) = \\frac{1 - \\exp(-2x)}{1 + \\exp(-2x)}$$"
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3d4603cd",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:52.641041Z",
"iopub.status.busy": "2023-08-18T06:56:52.640414Z",
"iopub.status.idle": "2023-08-18T06:56:52.836897Z",
"shell.execute_reply": "2023-08-18T06:56:52.836054Z"
},
"origin_pos": 27,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"y = torch.tanh(x)\n",
"d2l.plot(x.detach(), y.detach(), 'x', 'tanh(x)', figsize=(5, 2.5))"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "175057b9",
"metadata": {
"execution": {
"iopub.execute_input": "2023-08-18T06:56:52.840722Z",
"iopub.status.busy": "2023-08-18T06:56:52.840116Z",
"iopub.status.idle": "2023-08-18T06:56:53.041511Z",
"shell.execute_reply": "2023-08-18T06:56:53.040572Z"
},
"origin_pos": 32,
"tab": [
"pytorch"
]
},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {
"needs_background": "light"
},
"output_type": "display_data"
}
],
"source": [
"x.grad.data.zero_()\n",
"y.backward(torch.ones_like(x),retain_graph=True)\n",
"d2l.plot(x.detach(), x.grad, 'x', 'grad of tanh', figsize=(5, 2.5))"
]
}
],
"metadata": {
"celltoolbar": "Slideshow",
"language_info": {
"name": "python"
},
"required_libs": [],
"rise": {
"autolaunch": true,
"enable_chalkboard": true,
"overlay": "",
"scroll": true
}
},
"nbformat": 4,
"nbformat_minor": 5
}