{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 3.8 多层感知机"
]
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"0.4.1\n"
]
}
],
"source": [
"%matplotlib inline\n",
"import torch\n",
"import numpy as np\n",
"import matplotlib.pylab as plt\n",
"import sys\n",
"sys.path.append(\"..\") \n",
"import d2lzh_pytorch as d2l\n",
"\n",
"print(torch.__version__)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 3.8.2 激活函数"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"def xyplot(x_vals, y_vals, name):\n",
" d2l.set_figsize(figsize=(5, 2.5))\n",
" d2l.plt.plot(x_vals.detach().numpy(), y_vals.detach().numpy())\n",
" d2l.plt.xlabel('x')\n",
" d2l.plt.ylabel(name + '(x)')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.8.2.1 ReLU函数"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x = torch.arange(-8.0, 8.0, 0.1, requires_grad=True)\n",
"y = x.relu()\n",
"xyplot(x, y, 'relu')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"y.sum().backward()\n",
"xyplot(x, x.grad, 'grad of relu')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.8.2.2 sigmoid函数"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"y = x.sigmoid()\n",
"xyplot(x, y, 'sigmoid')"
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x.grad.zero_()\n",
"y.sum().backward()\n",
"xyplot(x, x.grad, 'grad of sigmoid')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3.8.2.3 tanh函数"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"y = x.tanh()\n",
"xyplot(x, y, 'tanh')"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"metadata": {},
"output_type": "display_data"
}
],
"source": [
"x.grad.zero_()\n",
"y.sum().backward()\n",
"xyplot(x, x.grad, 'grad of tanh')"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": []
}
],
"metadata": {
"kernelspec": {
"display_name": "Python [default]",
"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.6.3"
}
},
"nbformat": 4,
"nbformat_minor": 2
}