{
"cells": [
{
"cell_type": "code",
"execution_count": 39,
"id": "a78a2429-dc77-4dd5-af16-010af469b348",
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[NbConvertApp] WARNING | Config option `kernel_spec_manager_class` not recognized by `NbConvertApp`.\n",
"[NbConvertApp] Converting notebook 6_6_4_Exercises.ipynb to markdown\n",
"[NbConvertApp] Writing 7171 bytes to 6_6_4_Exercises.md\n"
]
}
],
"source": [
"!jupyter nbconvert --to markdown 6_6_4_Exercises.ipynb"
]
},
{
"cell_type": "markdown",
"id": "cfdb4382-bf00-4a2d-a09a-f61783997893",
"metadata": {},
"source": [
"# 1. What kinds of problems will occur if you change MySequential to store modules in a Python list?"
]
},
{
"cell_type": "markdown",
"id": "9d49dbf5-b970-4b32-8269-15e64aa016f5",
"metadata": {},
"source": [
"If you change `MySequential` to store modules in a Python list instead of using the `nn.Sequential` container, you might get `ValueError: optimizer got an empty parameter list` error, because the `nn.Sequential` container automatically tracks and registers the parameters of each module added to it. If you use a Python list, you'll need to manually manage parameter registration, which can lead to errors if not done correctly."
]
},
{
"cell_type": "code",
"execution_count": 23,
"id": "6db4fd67-a4af-4067-a4de-cb1caa49e6bc",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import torch.nn as nn\n",
"import torch\n",
"import sys\n",
"sys.path.append('/home/jovyan/work/d2l_solutions/notebooks/exercises/d2l_utils/')\n",
"import d2l\n",
"import warnings\n",
"warnings.filterwarnings(\"ignore\")\n",
"\n",
"class MySequential(d2l.Module):\n",
" def __init__(self, *args):\n",
" super().__init__()\n",
" self.modules = []\n",
" for idx, module in enumerate(args):\n",
" self.modules.append(module)\n",
" \n",
" def forward(self, X):\n",
" for module in self.modules:\n",
" X = module(X)\n",
" return X\n",
" \n",
"class MySequentialMLP(d2l.Classifier):\n",
" def __init__(self, num_outputs, num_hiddens, lr):\n",
" super().__init__()\n",
" self.save_hyperparameters()\n",
" layers = [nn.Flatten()]\n",
" for num in num_hiddens:\n",
" layers.append(nn.LazyLinear(num))\n",
" layers.append(nn.ReLU())\n",
" layers.append(nn.LazyLinear(num_outputs))\n",
" self.net = MySequential(*layers)"
]
},
{
"cell_type": "code",
"execution_count": 21,
"id": "314ef490-2457-4c15-a471-42eb34c9586c",
"metadata": {
"tags": []
},
"outputs": [
{
"data": {
"text/plain": [
"(86.71588633954525, 16.023116797208786)"
]
},
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"