{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"import os\n",
"import sys\n",
"sys.path.append(os.path.abspath(os.path.join('..')))\n",
"from ch07_autograd.utils import Scalar, draw_graph"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"# 定义计算图\n",
"w = Scalar(0.3, label='w')\n",
"wh = Scalar(0.5, label='wh')\n",
"x1 = Scalar(1., label='x1', requires_grad=False)\n",
"x2 = Scalar(1., label='x2', requires_grad=False)\n",
"x3 = Scalar(1., label='x3', requires_grad=False)\n",
"# 可以将h1,h2,h3理解为隐藏状态\n",
"# 它们分别表示第一步、第二步、第三步的隐藏状态\n",
"h1 = w * x1\n",
"h2 = w * x2 + wh * h1\n",
"h3 = w * x3 + wh * h2"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 3,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 对第一步的隐藏状态进行反向传播\n",
"h1.backward()\n",
"draw_graph(h1, 'backward')"
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 对第二步的隐藏状态进行反向传播\n",
"h2.backward()\n",
"draw_graph(h2, 'backward')"
]
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n",
"\n",
"\n"
],
"text/plain": [
""
]
},
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# 对第三步的隐藏状态进行反向传播\n",
"h3.backward()\n",
"draw_graph(h3, 'backward')"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"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.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}