Skip to content

Commit fc3762a

Browse files
committed
fix AlexNet
1 parent c206444 commit fc3762a

File tree

1 file changed

+27
-41
lines changed

1 file changed

+27
-41
lines changed

Ch09_Modern_Convolutional_Networks/AlexNet.ipynb

Lines changed: 27 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -641,7 +641,7 @@
641641
],
642642
"source": [
643643
"from IPython.display import SVG\n",
644-
"SVG(filename=\"./img/alexnet-all.svg\")"
644+
"SVG(filename=\"../img/alexnet-all.svg\")"
645645
]
646646
},
647647
{
@@ -698,7 +698,6 @@
698698
"import d2l\n",
699699
"import torch\n",
700700
"import torch.nn as nn\n",
701-
"import os\n",
702701
"import torch.optim as optim"
703702
]
704703
},
@@ -712,7 +711,7 @@
712711
" def forward(self, input):\n",
713712
" return input.view(input.size(0), -1)\n",
714713
"\n",
715-
"net=nn.Sequential(\n",
714+
"net = nn.Sequential(\n",
716715
" nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1),\n",
717716
" nn.ReLU(inplace=True),\n",
718717
" nn.MaxPool2d(kernel_size=3, stride=2),\n",
@@ -779,7 +778,7 @@
779778
}
780779
],
781780
"source": [
782-
"X=torch.randn(size=(1,1,224,224))\n",
781+
"X = torch.randn(size=(1,1,224,224))\n",
783782
"\n",
784783
"for layer in net:\n",
785784
" X=layer(X)\n",
@@ -816,31 +815,34 @@
816815
"metadata": {},
817816
"outputs": [],
818817
"source": [
819-
"batch_size=128\n",
820-
"train_iter,test_iter=d2l.load_data_fashion_mnist(batch_size,resize=224)"
818+
"batch_size = 128\n",
819+
"train_iter, test_iter = d2l.load_data_fashion_mnist(batch_size, resize=224)"
821820
]
822821
},
823822
{
824823
"cell_type": "code",
825-
"execution_count": null,
824+
"execution_count": 7,
826825
"metadata": {},
827826
"outputs": [
828827
{
829828
"name": "stdout",
830829
"output_type": "stream",
831830
"text": [
832-
"training on cpu\n",
833-
"epoch 1, loss 0.0125, train acc 0.390, test acc 0.667, time 7254.1 sec\n",
834-
"epoch 2, loss 0.0057, train acc 0.726, test acc 0.777, time 7257.3 sec\n",
835-
"epoch 3, loss 0.0046, train acc 0.782, test acc 0.805, time 34438.2 sec\n"
831+
"training on cuda:0\n",
832+
"epoch 1, loss 0.0127, train acc 0.393, test acc 0.712, time 31.3 sec\n",
833+
"epoch 2, loss 0.0056, train acc 0.733, test acc 0.776, time 30.8 sec\n",
834+
"epoch 3, loss 0.0045, train acc 0.782, test acc 0.806, time 32.1 sec\n",
835+
"epoch 4, loss 0.0040, train acc 0.809, test acc 0.831, time 32.0 sec\n",
836+
"epoch 5, loss 0.0037, train acc 0.826, test acc 0.844, time 32.2 sec\n"
836837
]
837838
}
838839
],
839840
"source": [
840-
"lr,num_epochs,device=0.01,5,d2l.try_gpu()\n",
841+
"lr, num_epochs, device = 0.01, 5, d2l.try_gpu()\n",
841842
"def init_weights(m):\n",
842843
" if type(m) == nn.Linear or type(m) == nn.Conv2d:\n",
843844
" torch.nn.init.xavier_uniform_(m.weight)\n",
845+
"\n",
844846
"net.apply(init_weights)\n",
845847
"optimizer = optim.Adam(net.parameters(), lr=0.001, weight_decay=0.0005)\n",
846848
"criterion = nn.CrossEntropyLoss()\n",
@@ -851,45 +853,29 @@
851853
"cell_type": "markdown",
852854
"metadata": {},
853855
"source": [
854-
"### Summary\n",
855-
"• AlexNet has a similar structure to that of LeNet, but uses more convolutional layers and a larger\n",
856-
"parameter space to fit the large-scale data set ImageNet.\n",
857-
"\n",
858-
"• Today AlexNet has been surpassed by much more effective architectures but it is a key step from\n",
859-
"shallow to deep networks that are used nowadays.\n",
856+
"## Summary\n",
860857
"\n",
861-
"• Although it seems that there are only a few more lines in AlexNet’s implementation than in LeNet, it\n",
862-
"took the academic community many years to embrace this conceptual change and take advantage of\n",
863-
"its excellent experimental results. This was also due to the lack of efficient computational tools.\n",
864-
"\n",
865-
"• Dropout, ReLU and preprocessing were the other key steps in achieving excellent performance in\n",
866-
"computer vision tasks"
858+
"* AlexNet has a similar structure to that of LeNet, but uses more convolutional layers and a larger parameter space to fit the large-scale data set ImageNet.\n",
859+
"* Today AlexNet has been surpassed by much more effective architectures but it is a key step from shallow to deep networks that are used nowadays.\n",
860+
"* Although it seems that there are only a few more lines in AlexNet's implementation than in LeNet, it took the academic community many years to embrace this conceptual change and take advantage of its excellent experimental results. This was also due to the lack of efficient computational tools.\n",
861+
"* Dropout, ReLU and preprocessing were the other key steps in achieving excellent performance in computer vision tasks."
867862
]
868863
},
869864
{
870865
"cell_type": "markdown",
871866
"metadata": {},
872867
"source": [
873-
"### Exercises\n",
874-
"1. Try increasing the number of epochs. Compared with LeNet, how are the results different? Why?\n",
868+
"## Exercises\n",
875869
"\n",
870+
"1. Try increasing the number of epochs. Compared with LeNet, how are the results different? Why?\n",
876871
"2. AlexNet may be too complex for the Fashion-MNIST data set.\n",
877-
"\n",
878-
"• Try to simplify the model to make the training faster, while ensuring that the accuracy does not\n",
879-
"drop significantly.\n",
880-
"\n",
881-
"• Can you design a better model that works directly on 28 x 28 images.\n",
882-
"\n",
872+
" * Try to simplify the model to make the training faster, while ensuring that the accuracy does not drop significantly.\n",
873+
" * Can you design a better model that works directly on $28 \\times 28$ images.\n",
883874
"3. Modify the batch size, and observe the changes in accuracy and GPU memory.\n",
884-
"\n",
885875
"4. Rooflines\n",
886-
"\n",
887-
"• What is the dominant part for the memory footprint of AlexNet?\n",
888-
"\n",
889-
"• What is the dominant part for computation in AlexNet?\n",
890-
"\n",
891-
"• How about memory bandwidth when computing the results?\n",
892-
"\n",
876+
" * What is the dominant part for the memory footprint of AlexNet?\n",
877+
" * What is the dominant part for computation in AlexNet?\n",
878+
" * How about memory bandwidth when computing the results?\n",
893879
"5. Apply dropout and ReLU to LeNet5. Does it improve? How about preprocessing?"
894880
]
895881
}
@@ -910,7 +896,7 @@
910896
"name": "python",
911897
"nbconvert_exporter": "python",
912898
"pygments_lexer": "ipython3",
913-
"version": "3.6.5"
899+
"version": "3.6.8"
914900
}
915901
},
916902
"nbformat": 4,

0 commit comments

Comments
 (0)