{
"cells": [
{
"cell_type": "code",
"execution_count": 1,
"id": "c9dad41e-e7f7-4c2b-a04a-0ed87e1f4eb0",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"import sys\n",
"import torch.nn as nn\n",
"import torch\n",
"import warnings\n",
"sys.path.append('/home/jovyan/work/d2l_solutions/notebooks/exercises/d2l_utils/')\n",
"import d2l\n",
"warnings.filterwarnings(\"ignore\")"
]
},
{
"cell_type": "markdown",
"id": "e15dba16-1429-4cc9-8a55-bc1cbefb997b",
"metadata": {},
"source": [
"# 1. Let’s modernize LeNet. Implement and test the following changes:"
]
},
{
"cell_type": "markdown",
"id": "39f2b521-53c1-4caf-8ce7-71eb68d4e02f",
"metadata": {},
"source": [
"## 1.1 Replace average pooling with max-pooling."
]
},
{
"cell_type": "code",
"execution_count": 6,
"id": "3a8ffbb4-4588-436d-9214-434f022b4f1e",
"metadata": {
"tags": []
},
"outputs": [],
"source": [
"class MaxPoolingLeNet(d2l.Classifier):\n",
" def __init__(self, lr=0.1, num_classes=10):\n",
" super().__init__()\n",
" self.save_hyperparameters()\n",
" self.net = nn.Sequential(nn.LazyConv2d(6, kernel_size=5, padding=2),\n",
" nn.Sigmoid(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.LazyConv2d(16, kernel_size=5),\n",
" nn.Sigmoid(),\n",
" nn.MaxPool2d(kernel_size=2, stride=2),\n",
" nn.Flatten(),\n",
" nn.LazyLinear(120),\n",
" nn.Sigmoid(),\n",
" nn.LazyLinear(84),\n",
" nn.Sigmoid(),\n",
" nn.LazyLinear(num_classes))\n",
" \n",
"def init_cnn(module):\n",
" if type(module) == nn.Linear or type(module) == nn.Conv2d:\n",
" nn.init.xavier_uniform_(module.weight)"
]
},
{
"cell_type": "code",
"execution_count": 20,
"id": "6b7cf77f-d232-4da6-9a0f-39c076fd3bed",
"metadata": {
"tags": []
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"acc: 0.32\n"
]
},
{
"data": {
"image/svg+xml": [
"\n",
"\n",
"\n"
],
"text/plain": [
"