Browse Source

Fix run problems

pull/5/MERGE
bushuhui 3 years ago
parent
commit
81d2cdebdf
29 changed files with 1084 additions and 835 deletions
  1. +1
    -0
      .gitignore
  2. +8
    -8
      0_python/0-ipython_notebook.ipynb
  3. +69
    -77
      0_python/1_Basics.ipynb
  4. +24
    -24
      0_python/2_Print_Statement.ipynb
  5. +347
    -130
      0_python/3_Data_Structure_1.ipynb
  6. +59
    -65
      0_python/4_Data_Structure_2.ipynb
  7. +31
    -19
      0_python/5_Control_Flow.ipynb
  8. +65
    -63
      0_python/6_Function.ipynb
  9. +51
    -51
      0_python/7_Class.ipynb
  10. +19
    -19
      6_pytorch/1_NN/1-linear-regression-gradient-descend.ipynb
  11. +64
    -64
      6_pytorch/1_NN/3-nn-sequential-module.ipynb
  12. +38
    -59
      6_pytorch/1_NN/4-deep-nn.ipynb
  13. +52
    -64
      6_pytorch/1_NN/5-param_initialize.ipynb
  14. +2
    -2
      6_pytorch/1_NN/backup/6-nn_summary.ipynb
  15. +129
    -33
      6_pytorch/1_NN/optimizer/6_1-sgd.ipynb
  16. +4
    -4
      6_pytorch/1_NN/optimizer/6_2-momentum.ipynb
  17. +5
    -5
      6_pytorch/1_NN/optimizer/6_3-adagrad.ipynb
  18. +9
    -9
      6_pytorch/1_NN/optimizer/6_4-rmsprop.ipynb
  19. +7
    -7
      6_pytorch/1_NN/optimizer/6_5-adadelta.ipynb
  20. +5
    -5
      6_pytorch/1_NN/optimizer/6_6-adam.ipynb
  21. +3
    -3
      6_pytorch/2_CNN/2-batch-normalization.ipynb
  22. +4
    -4
      6_pytorch/2_CNN/3-lr-decay.ipynb
  23. +55
    -71
      6_pytorch/2_CNN/4-data-augumentation.ipynb
  24. +16
    -32
      6_pytorch/2_CNN/4-regularization.ipynb
  25. +4
    -4
      6_pytorch/2_CNN/6-vgg.ipynb
  26. +4
    -4
      6_pytorch/2_CNN/7-googlenet.ipynb
  27. +4
    -4
      6_pytorch/2_CNN/8-resnet.ipynb
  28. +2
    -2
      6_pytorch/2_CNN/9-densenet.ipynb
  29. +3
    -3
      6_pytorch/2_CNN/utils.py

+ 1
- 0
.gitignore View File

@@ -2,3 +2,4 @@
.idea
*.tar.gz
*.pth
__pycache__

+ 8
- 8
0_python/0-ipython_notebook.ipynb
File diff suppressed because it is too large
View File


+ 69
- 77
0_python/1_Basics.ipynb View File

@@ -22,28 +22,28 @@
{
"data": {
"text/plain": [
"['0-ipython_notebook.ipynb',\n",
" '0-ipython_notebook_EN.ipynb',\n",
" '1_Basics.ipynb',\n",
"['.ipynb_checkpoints',\n",
" 'Python.pdf',\n",
" '1_Basics_EN.ipynb',\n",
" '2_Print_Statement.ipynb',\n",
" '2_Print_Statement_EN.ipynb',\n",
" '3_Data_Structure_1.ipynb',\n",
" '3_Data_Structure_1_EN.ipynb',\n",
" '4_Data_Structure_2.ipynb',\n",
" '4_Data_Structure_2_EN.ipynb',\n",
" '5_Control_Flow.ipynb',\n",
" '5_Control_Flow_EN.ipynb',\n",
" '6_Function.ipynb',\n",
" '6_Function_EN.ipynb',\n",
" '7_Class.ipynb',\n",
" '3_Data_Structure_1_EN.ipynb',\n",
" '7_Class_EN.ipynb',\n",
" 'Python.pdf',\n",
" 'README.md',\n",
" 'README_ENG.md',\n",
" 'images',\n",
" '0-ipython_notebook_EN.ipynb',\n",
" 'test.txt',\n",
" '.ipynb_checkpoints']"
" 'README_ENG.md',\n",
" '0-ipython_notebook.ipynb',\n",
" '1_Basics.ipynb',\n",
" '2_Print_Statement.ipynb',\n",
" '3_Data_Structure_1.ipynb',\n",
" '4_Data_Structure_2.ipynb',\n",
" '5_Control_Flow.ipynb',\n",
" '6_Function.ipynb',\n",
" '7_Class.ipynb',\n",
" 'README.md']"
]
},
"execution_count": 1,
@@ -135,9 +135,7 @@
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"x = 2\n",
@@ -171,10 +169,8 @@
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"execution_count": 6,
"metadata": {},
"outputs": [],
"source": [
"x = y = 1"
@@ -182,7 +178,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -228,7 +224,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -237,7 +233,7 @@
"3"
]
},
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"output_type": "execute_result"
}
@@ -248,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -257,7 +253,7 @@
"1"
]
},
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -268,7 +264,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -277,7 +273,7 @@
"2"
]
},
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -288,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -297,7 +293,7 @@
"0.5"
]
},
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -315,7 +311,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -324,7 +320,7 @@
"0.5"
]
},
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -335,7 +331,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -344,7 +340,7 @@
"0.5"
]
},
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -355,7 +351,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -364,7 +360,7 @@
"5"
]
},
"execution_count": 13,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -382,7 +378,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -391,7 +387,7 @@
"1.0"
]
},
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -423,10 +419,8 @@
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
"z = 1"
@@ -434,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -443,7 +437,7 @@
"True"
]
},
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@@ -454,7 +448,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -463,7 +457,7 @@
"False"
]
},
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
@@ -495,10 +489,8 @@
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"a = 2 #10\n",
@@ -507,7 +499,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -526,7 +518,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -535,7 +527,7 @@
"2"
]
},
"execution_count": 20,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -557,7 +549,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -566,7 +558,7 @@
"10"
]
},
"execution_count": 21,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -616,7 +608,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -625,7 +617,7 @@
"'0xaa'"
]
},
"execution_count": 22,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -636,7 +628,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -645,7 +637,7 @@
"170"
]
},
"execution_count": 23,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -656,7 +648,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 26,
"metadata": {},
"outputs": [
{
@@ -665,7 +657,7 @@
"'0o10'"
]
},
"execution_count": 24,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -683,7 +675,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -711,7 +703,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -737,7 +729,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@@ -746,7 +738,7 @@
"'b'"
]
},
"execution_count": 27,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
@@ -757,7 +749,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -766,7 +758,7 @@
"98"
]
},
"execution_count": 28,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
@@ -791,7 +783,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 31,
"metadata": {
"scrolled": false
},
@@ -819,7 +811,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -844,7 +836,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 34,
"metadata": {},
"outputs": [
{
@@ -853,7 +845,7 @@
"(4, 1)"
]
},
"execution_count": 31,
"execution_count": 34,
"metadata": {},
"output_type": "execute_result"
}
@@ -871,7 +863,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -925,7 +917,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 36,
"metadata": {},
"outputs": [
{
@@ -960,14 +952,14 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 39,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Type something here and it will be stored in variable abc \thello\n"
"Type something here and it will be stored in variable abc \t10\n"
]
}
],
@@ -977,7 +969,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -986,7 +978,7 @@
"str"
]
},
"execution_count": 36,
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
}


+ 24
- 24
0_python/2_Print_Statement.ipynb View File

@@ -84,7 +84,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -112,7 +112,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {
"scrolled": true
},
@@ -143,7 +143,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -174,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -207,7 +207,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -239,7 +239,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -270,7 +270,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -287,7 +287,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -304,7 +304,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -328,7 +328,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -352,7 +352,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -369,7 +369,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -411,7 +411,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -420,7 +420,7 @@
"'3.121312'"
]
},
"execution_count": 16,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -438,7 +438,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -447,7 +447,7 @@
"'3.12131'"
]
},
"execution_count": 17,
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
@@ -465,7 +465,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -474,7 +474,7 @@
"'-33.12131'"
]
},
"execution_count": 18,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@@ -492,7 +492,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -501,7 +501,7 @@
"'00000000000003.12131'"
]
},
"execution_count": 19,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -519,7 +519,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -545,7 +545,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -571,7 +571,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -580,7 +580,7 @@
"'3.121 '"
]
},
"execution_count": 16,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}


+ 347
- 130
0_python/3_Data_Structure_1.ipynb
File diff suppressed because it is too large
View File


+ 59
- 65
0_python/4_Data_Structure_2.ipynb View File

@@ -18,10 +18,8 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"String0 = 'Taj Mahal is beautiful'\n",
@@ -63,7 +61,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -71,13 +69,15 @@
"output_type": "stream",
"text": [
"M\n",
"Mahal is beautiful\n"
"Mahal is beautiful\n",
"l\n"
]
}
],
"source": [
"print(String0[4])\n",
"print(String0[4:])"
"print(String0[4:])\n",
"print(String0[-1])"
]
},
{
@@ -96,7 +96,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -124,7 +124,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -148,7 +148,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -174,7 +174,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -199,7 +199,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -208,7 +208,7 @@
"' Taj Mahal is beautiful '"
]
},
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -226,7 +226,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -235,7 +235,7 @@
"'------------------------Taj Mahal is beautiful------------------------'"
]
},
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -253,7 +253,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -262,7 +262,7 @@
"'00000000Taj Mahal is beautiful'"
]
},
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -280,7 +280,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -327,8 +327,8 @@
"evalue": "substring not found",
"output_type": "error",
"traceback": [
"\u001b[0;31m------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0mTraceback (most recent call last)",
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mValueError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-a7d6b97b4839>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Taj'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m0\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 3\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mString0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mindex\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'Mahal'\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m10\u001b[0m\u001b[0;34m,\u001b[0m\u001b[0;36m20\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mValueError\u001b[0m: substring not found"
]
@@ -557,7 +557,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -584,7 +584,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -610,7 +610,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -619,7 +619,7 @@
"'TAJ MAHAL IS BEAUTIFUL'"
]
},
"execution_count": 24,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -637,7 +637,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -646,7 +646,7 @@
"'Bengaluru is beautiful'"
]
},
"execution_count": 25,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
@@ -664,10 +664,8 @@
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {
"collapsed": true
},
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
"f = ' hello '"
@@ -682,7 +680,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -691,7 +689,7 @@
"'hello'"
]
},
"execution_count": 28,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -709,10 +707,8 @@
},
{
"cell_type": "code",
"execution_count": 29,
"metadata": {
"collapsed": true
},
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
"f = ' ***----hello---******* '"
@@ -720,22 +716,22 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"' ***----hello---******* '"
"'hello'"
]
},
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"f.strip('*')"
"f.strip(' *-')"
]
},
{
@@ -813,7 +809,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 33,
"metadata": {},
"outputs": [
{
@@ -839,7 +835,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -858,7 +854,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 37,
"metadata": {},
"outputs": [
{
@@ -883,7 +879,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 38,
"metadata": {},
"outputs": [
{
@@ -907,10 +903,8 @@
},
{
"cell_type": "code",
"execution_count": 5,
"metadata": {
"collapsed": true
},
"execution_count": 39,
"metadata": {},
"outputs": [],
"source": [
"names = ['One', 'Two', 'Three', 'Four', 'Five']\n",
@@ -926,7 +920,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -944,7 +938,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 42,
"metadata": {},
"outputs": [
{
@@ -1005,7 +999,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@@ -1031,7 +1025,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 44,
"metadata": {},
"outputs": [
{
@@ -1049,7 +1043,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 45,
"metadata": {},
"outputs": [
{
@@ -1075,7 +1069,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 46,
"metadata": {},
"outputs": [
{
@@ -1084,7 +1078,7 @@
"dict_values([1, 2, 3, 4, 5])"
]
},
"execution_count": 10,
"execution_count": 46,
"metadata": {},
"output_type": "execute_result"
}
@@ -1102,7 +1096,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 47,
"metadata": {},
"outputs": [
{
@@ -1111,7 +1105,7 @@
"dict_keys(['One', 'Two', 'Three', 'Four', 'Five'])"
]
},
"execution_count": 11,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
@@ -1129,7 +1123,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 48,
"metadata": {},
"outputs": [
{
@@ -1160,23 +1154,23 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 52,
"metadata": {},
"outputs": [
{
"ename": "TypeError",
"evalue": "pop expected at least 1 arguments, got 0",
"ename": "KeyError",
"evalue": "'One'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-a0907f1327c5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mTypeError\u001b[0m: pop expected at least 1 arguments, got 0"
"\u001b[0;31mKeyError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-52-436d4737aff5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0ma2\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0ma1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpop\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m'One'\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0ma2\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mKeyError\u001b[0m: 'One'"
]
}
],
"source": [
"a2 = a1.pop()\n",
"a2 = a1.pop('One')\n",
"print(a1)\n",
"print(a2)"
]


+ 31
- 19
0_python/5_Control_Flow.ipynb View File

@@ -26,7 +26,9 @@
{
"cell_type": "code",
"execution_count": 1,
"metadata": {},
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
@@ -45,6 +47,16 @@
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"x = 4\n",
"if x > 10: print(\"Hello\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -66,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -194,7 +206,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -216,7 +228,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -245,7 +257,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -273,7 +285,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -321,7 +333,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -375,7 +387,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -416,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -489,7 +501,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -498,7 +510,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 13,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -523,7 +535,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -532,7 +544,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 14,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -543,7 +555,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 13,
"metadata": {
"scrolled": true
},
@@ -563,7 +575,7 @@
" '270': 270}"
]
},
"execution_count": 15,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -574,7 +586,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -583,7 +595,7 @@
"(27, 54, 81, 108, 135, 162, 189, 216, 243, 270)"
]
},
"execution_count": 16,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -601,7 +613,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -610,7 +622,7 @@
"[1, 2, 3, 4, 28, 29, 30, 31, 55, 56, 57, 58]"
]
},
"execution_count": 17,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}


+ 65
- 63
0_python/6_Function.ipynb View File

@@ -109,7 +109,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [],
"source": [
@@ -120,14 +120,14 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : Jack\n"
"Please enter your name : Tom\n"
]
}
],
@@ -144,15 +144,15 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Hey Jack!\n",
"Jack, How do you do?\n"
"Hey Tom!\n",
"Tom, How do you do?\n"
]
}
],
@@ -169,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -183,16 +183,16 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : Tom\n",
"Hey Tom!\n",
"Tom, How do you do?\n"
"Please enter your name : Jack\n",
"Hey Jack!\n",
"Jack, How do you do?\n"
]
}
],
@@ -216,7 +216,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@@ -234,7 +234,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -266,7 +266,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -277,7 +277,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -302,8 +302,10 @@
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"execution_count": 14,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stdout",
@@ -323,7 +325,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 15,
"metadata": {},
"outputs": [],
"source": [
@@ -339,7 +341,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -348,7 +350,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
@@ -369,7 +371,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -387,7 +389,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -422,7 +424,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
@@ -446,7 +448,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -455,7 +457,7 @@
"7"
]
},
"execution_count": 20,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
}
@@ -473,7 +475,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -482,7 +484,7 @@
"8"
]
},
"execution_count": 21,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -493,7 +495,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -502,7 +504,7 @@
"11"
]
},
"execution_count": 22,
"execution_count": 23,
"metadata": {},
"output_type": "execute_result"
}
@@ -527,7 +529,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
@@ -549,7 +551,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 25,
"metadata": {},
"outputs": [
{
@@ -565,7 +567,7 @@
"15"
]
},
"execution_count": 24,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -576,7 +578,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"outputs": [
{
@@ -592,7 +594,7 @@
"6"
]
},
"execution_count": 25,
"execution_count": 26,
"metadata": {},
"output_type": "execute_result"
}
@@ -610,7 +612,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -626,7 +628,7 @@
"60"
]
},
"execution_count": 26,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@@ -659,7 +661,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
@@ -675,7 +677,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
@@ -695,7 +697,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 31,
"metadata": {},
"outputs": [
{
@@ -741,7 +743,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 32,
"metadata": {},
"outputs": [],
"source": [
@@ -750,7 +752,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 33,
"metadata": {},
"outputs": [
{
@@ -759,7 +761,7 @@
"64"
]
},
"execution_count": 31,
"execution_count": 33,
"metadata": {},
"output_type": "execute_result"
}
@@ -770,7 +772,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 35,
"metadata": {},
"outputs": [
{
@@ -779,7 +781,7 @@
"(6, 8)"
]
},
"execution_count": 32,
"execution_count": 35,
"metadata": {},
"output_type": "execute_result"
}
@@ -791,7 +793,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 36,
"metadata": {},
"outputs": [
{
@@ -800,7 +802,7 @@
"function"
]
},
"execution_count": 33,
"execution_count": 36,
"metadata": {},
"output_type": "execute_result"
}
@@ -811,7 +813,7 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 37,
"metadata": {},
"outputs": [
{
@@ -820,7 +822,7 @@
"function"
]
},
"execution_count": 34,
"execution_count": 37,
"metadata": {},
"output_type": "execute_result"
}
@@ -848,7 +850,7 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 38,
"metadata": {},
"outputs": [],
"source": [
@@ -857,7 +859,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 40,
"metadata": {},
"outputs": [
{
@@ -875,7 +877,7 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 41,
"metadata": {},
"outputs": [
{
@@ -900,7 +902,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 42,
"metadata": {},
"outputs": [],
"source": [
@@ -909,7 +911,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 43,
"metadata": {},
"outputs": [
{
@@ -966,7 +968,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
@@ -982,7 +984,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 45,
"metadata": {},
"outputs": [
{
@@ -1007,22 +1009,22 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 47,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<map at 0x7f7bb06c6590>"
"[True, True, True, True, False, False, False, False, False]"
]
},
"execution_count": 43,
"execution_count": 47,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"map(lambda x:x<5, list1)"
"list(map(lambda x:x<5, list1))"
]
},
{
@@ -1034,22 +1036,22 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 49,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<filter at 0x7f7bb06ddf10>"
"[4, 8]"
]
},
"execution_count": 44,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"filter(lambda x:x%4==0,list1)"
"list(filter(lambda x:x%4==0,list1))"
]
}
],


+ 51
- 51
0_python/7_Class.ipynb View File

@@ -34,7 +34,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -59,7 +59,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@@ -68,7 +68,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -77,7 +77,7 @@
"__main__.FirstClass"
]
},
"execution_count": 3,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -88,7 +88,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -97,7 +97,7 @@
"type"
]
},
"execution_count": 4,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -138,7 +138,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -159,7 +159,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -169,7 +169,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -197,7 +197,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {
"scrolled": false
},
@@ -234,7 +234,7 @@
" 'class_var']"
]
},
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -245,7 +245,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -254,7 +254,7 @@
"'My first class'"
]
},
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -272,7 +272,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -309,7 +309,7 @@
" 'value']"
]
},
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -327,7 +327,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@@ -346,7 +346,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
@@ -356,7 +356,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -366,7 +366,7 @@
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-4ab7dec1c737>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-15-4ab7dec1c737>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0mprint\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msymbol\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'name'"
]
}
@@ -385,7 +385,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -421,7 +421,7 @@
" 'v']"
]
},
"execution_count": 15,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -432,7 +432,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -512,7 +512,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@@ -522,7 +522,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -559,7 +559,7 @@
" 'v']"
]
},
"execution_count": 21,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -583,7 +583,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
@@ -603,7 +603,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 22,
"metadata": {},
"outputs": [
{
@@ -623,7 +623,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 23,
"metadata": {},
"outputs": [
{
@@ -647,7 +647,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 24,
"metadata": {},
"outputs": [],
"source": [
@@ -665,7 +665,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
@@ -674,7 +674,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 26,
"metadata": {},
"outputs": [
{
@@ -693,7 +693,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -702,7 +702,7 @@
"10"
]
},
"execution_count": 28,
"execution_count": 27,
"metadata": {},
"output_type": "execute_result"
}
@@ -720,7 +720,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -729,7 +729,7 @@
"10"
]
},
"execution_count": 29,
"execution_count": 28,
"metadata": {},
"output_type": "execute_result"
}
@@ -761,7 +761,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 29,
"metadata": {},
"outputs": [],
"source": [
@@ -776,7 +776,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
@@ -785,7 +785,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 31,
"metadata": {},
"outputs": [
{
@@ -802,7 +802,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 32,
"metadata": {},
"outputs": [
{
@@ -837,7 +837,7 @@
" 'salary']"
]
},
"execution_count": 33,
"execution_count": 32,
"metadata": {},
"output_type": "execute_result"
}
@@ -967,7 +967,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
@@ -976,7 +976,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 41,
"metadata": {},
"outputs": [
{
@@ -1012,7 +1012,7 @@
" 'salary']"
]
},
"execution_count": 40,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@@ -1023,7 +1023,7 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 42,
"metadata": {},
"outputs": [
{
@@ -1049,7 +1049,7 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": 44,
"metadata": {},
"outputs": [],
"source": [
@@ -1065,7 +1065,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": 45,
"metadata": {},
"outputs": [],
"source": [
@@ -1074,7 +1074,7 @@
},
{
"cell_type": "code",
"execution_count": 44,
"execution_count": 46,
"metadata": {},
"outputs": [
{
@@ -1101,7 +1101,7 @@
},
{
"cell_type": "code",
"execution_count": 45,
"execution_count": 47,
"metadata": {},
"outputs": [],
"source": [
@@ -1112,7 +1112,7 @@
},
{
"cell_type": "code",
"execution_count": 46,
"execution_count": 48,
"metadata": {},
"outputs": [],
"source": [
@@ -1121,7 +1121,7 @@
},
{
"cell_type": "code",
"execution_count": 47,
"execution_count": 49,
"metadata": {},
"outputs": [
{
@@ -1130,7 +1130,7 @@
"'I Do Not Know What To Type'"
]
},
"execution_count": 47,
"execution_count": 49,
"metadata": {},
"output_type": "execute_result"
}


+ 19
- 19
6_pytorch/1_NN/1-linear-regression-gradient-descend.ipynb
File diff suppressed because it is too large
View File


+ 64
- 64
6_pytorch/1_NN/3-nn-sequential-module.ipynb
File diff suppressed because it is too large
View File


+ 38
- 59
6_pytorch/1_NN/4-deep-nn.ipynb View File

@@ -65,7 +65,7 @@
},
{
"cell_type": "code",
"execution_count": 1,
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
@@ -79,7 +79,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -97,7 +97,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [],
"source": [
@@ -106,17 +106,17 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"image/png": "iVBORw0KGgoAAAANSUhEUgAAABwAAAAcCAAAAABXZoBIAAABAElEQVR4nGNgGMyAWUhIqK5jvdSy/9/rGRgYGFhgEnJsVjYCwQwMDAxPJgV+vniQgYGBgREqZ7iXH8r6l/SV4dn7m8gmCt3++/fv37/Htn3/iMW+gDnZf/+e5WbQnoXNNXyMs/5GoQoxwVmf/n9kSGFiwAW49/11wynJoPzx4YIcRlyygR/+/i2XxCWru+vv32nSuGQFYv/83Y3b4p9/fzpAmSyoMnohpiwM1w5h06Q+5enfv39/bcMiJVF09+/fv39P+mFKiTtd/fv3799jgZiBJLT69t+/f/8eDuDEkDJf8+jv379/v7Ryo4qzMDAwMAQGMjBc3/y35wM2V1IfAABFF16Aa0wAOwAAAABJRU5ErkJggg==\n",
"text/plain": [
"<PIL.Image.Image image mode=L size=28x28 at 0x7FF1658A5278>"
"<PIL.Image.Image image mode=L size=28x28 at 0x7F552C598970>"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -127,16 +127,16 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"tensor(5)"
"5"
]
},
"execution_count": 5,
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
@@ -154,7 +154,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 7,
"metadata": {},
"outputs": [
{
@@ -179,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -260,7 +260,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
@@ -277,7 +277,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -285,7 +285,7 @@
"output_type": "stream",
"text": [
"torch.Size([784])\n",
"tensor(5)\n"
"5\n"
]
}
],
@@ -297,7 +297,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@@ -316,7 +316,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -325,7 +325,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -345,7 +345,7 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
@@ -363,7 +363,7 @@
},
{
"cell_type": "code",
"execution_count": 39,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -380,7 +380,7 @@
")"
]
},
"execution_count": 39,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -398,7 +398,7 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -409,45 +409,24 @@
},
{
"cell_type": "code",
"execution_count": 42,
"execution_count": null,
"metadata": {
"scrolled": true
},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:22: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n",
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:25: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n",
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:41: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n",
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:44: UserWarning: invalid index of a 0-dim tensor. This will be an error in PyTorch 0.5. Use tensor.item() to convert a 0-dim tensor to a Python number\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch: 0, Train Loss: 0.166705, Train Acc: 0.947978, Eval Loss: 0.129106, Eval Acc: 0.959157\n",
"epoch: 1, Train Loss: 0.117714, Train Acc: 0.962836, Eval Loss: 0.097123, Eval Acc: 0.969838\n",
"epoch: 2, Train Loss: 0.092098, Train Acc: 0.970532, Eval Loss: 0.098194, Eval Acc: 0.969541\n",
"epoch: 3, Train Loss: 0.074442, Train Acc: 0.975880, Eval Loss: 0.077213, Eval Acc: 0.975574\n",
"epoch: 4, Train Loss: 0.062742, Train Acc: 0.979594, Eval Loss: 0.149892, Eval Acc: 0.955301\n",
"epoch: 5, Train Loss: 0.052319, Train Acc: 0.983276, Eval Loss: 0.124755, Eval Acc: 0.961531\n",
"epoch: 6, Train Loss: 0.045134, Train Acc: 0.985091, Eval Loss: 0.085263, Eval Acc: 0.975178\n",
"epoch: 7, Train Loss: 0.038610, Train Acc: 0.987423, Eval Loss: 0.063986, Eval Acc: 0.980123\n",
"epoch: 8, Train Loss: 0.033068, Train Acc: 0.988906, Eval Loss: 0.074201, Eval Acc: 0.977453\n",
"epoch: 9, Train Loss: 0.029478, Train Acc: 0.990155, Eval Loss: 0.066254, Eval Acc: 0.980123\n",
"epoch: 10, Train Loss: 0.024885, Train Acc: 0.992237, Eval Loss: 0.067818, Eval Acc: 0.979727\n",
"epoch: 11, Train Loss: 0.020706, Train Acc: 0.993237, Eval Loss: 0.174131, Eval Acc: 0.958070\n",
"epoch: 12, Train Loss: 0.019527, Train Acc: 0.993553, Eval Loss: 0.066838, Eval Acc: 0.982199\n",
"epoch: 13, Train Loss: 0.016248, Train Acc: 0.994620, Eval Loss: 0.080457, Eval Acc: 0.978738\n",
"epoch: 14, Train Loss: 0.017617, Train Acc: 0.994603, Eval Loss: 0.064320, Eval Acc: 0.982496\n",
"epoch: 15, Train Loss: 0.012970, Train Acc: 0.995985, Eval Loss: 0.079791, Eval Acc: 0.979925\n",
"epoch: 16, Train Loss: 0.012162, Train Acc: 0.995736, Eval Loss: 0.083829, Eval Acc: 0.979727\n",
"epoch: 17, Train Loss: 0.011916, Train Acc: 0.996185, Eval Loss: 0.079493, Eval Acc: 0.981507\n",
"epoch: 18, Train Loss: 0.008972, Train Acc: 0.997385, Eval Loss: 0.074135, Eval Acc: 0.981507\n",
"epoch: 19, Train Loss: 0.008857, Train Acc: 0.997018, Eval Loss: 0.074056, Eval Acc: 0.983188\n"
"epoch: 0, Train Loss: 0.511304, Train Acc: 0.830540, Eval Loss: 0.232364, Eval Acc: 0.925732\n",
"epoch: 1, Train Loss: 0.167128, Train Acc: 0.948744, Eval Loss: 0.171745, Eval Acc: 0.942148\n",
"epoch: 2, Train Loss: 0.118102, Train Acc: 0.963420, Eval Loss: 0.107683, Eval Acc: 0.965882\n",
"epoch: 3, Train Loss: 0.092869, Train Acc: 0.971565, Eval Loss: 0.090614, Eval Acc: 0.970728\n",
"epoch: 4, Train Loss: 0.073340, Train Acc: 0.977229, Eval Loss: 0.081820, Eval Acc: 0.972805\n",
"epoch: 5, Train Loss: 0.060981, Train Acc: 0.980727, Eval Loss: 0.087822, Eval Acc: 0.972211\n",
"epoch: 6, Train Loss: 0.051884, Train Acc: 0.982809, Eval Loss: 0.127961, Eval Acc: 0.958564\n",
"epoch: 7, Train Loss: 0.044878, Train Acc: 0.985741, Eval Loss: 0.102081, Eval Acc: 0.967366\n",
"epoch: 8, Train Loss: 0.039214, Train Acc: 0.987223, Eval Loss: 0.067912, Eval Acc: 0.977551\n"
]
}
],
@@ -473,10 +452,10 @@
" loss.backward()\n",
" optimizer.step()\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" # 计算分类的准确率\n",
" _, pred = out.max(1)\n",
" num_correct = float((pred == label).sum().data[0])\n",
" num_correct = float((pred == label).sum().item())\n",
" acc = num_correct / im.shape[0]\n",
" train_acc += acc\n",
" \n",
@@ -492,10 +471,10 @@
" out = net(im)\n",
" loss = criterion(out, label)\n",
" # 记录误差\n",
" eval_loss += loss.data[0]\n",
" eval_loss += loss.item()\n",
" # 记录准确率\n",
" _, pred = out.max(1)\n",
" num_correct = float((pred == label).sum().data[0])\n",
" num_correct = float((pred == label).sum().item())\n",
" acc = num_correct / im.shape[0]\n",
" eval_acc += acc\n",
" \n",
@@ -515,7 +494,7 @@
},
{
"cell_type": "code",
"execution_count": 43,
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
@@ -674,7 +653,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -688,7 +667,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 52
- 64
6_pytorch/1_NN/5-param_initialize.ipynb View File

@@ -71,13 +71,13 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
"tensor([[ 0.0276, -0.1197, -0.0397, ..., 0.0759, -0.1630, 0.1599],\n",
" [ 0.1419, 0.0903, -0.1630, ..., -0.0615, 0.1502, 0.0596],\n",
" [-0.0451, 0.1103, 0.1070, ..., -0.1506, -0.1346, 0.1284],\n",
"tensor([[-0.0784, 0.1559, 0.0451, ..., 0.0432, 0.0325, -0.0626],\n",
" [ 0.0436, 0.0976, 0.1529, ..., -0.1601, -0.1227, -0.0831],\n",
" [ 0.0890, 0.0343, 0.1744, ..., -0.0332, 0.0897, 0.0002],\n",
" ...,\n",
" [-0.0975, -0.1264, 0.0738, ..., -0.1058, -0.1396, 0.1800],\n",
" [-0.1352, 0.0287, 0.0779, ..., 0.1773, -0.1585, 0.1046],\n",
" [-0.1194, 0.1526, -0.0018, ..., 0.0946, -0.1453, -0.1512]],\n",
" [-0.1447, -0.0411, -0.0851, ..., 0.0117, 0.1457, 0.0585],\n",
" [ 0.1642, 0.0744, -0.1118, ..., 0.0623, -0.0591, 0.0512],\n",
" [-0.1610, 0.0070, 0.0184, ..., -0.1529, -0.0314, 0.1748]],\n",
" requires_grad=True)\n"
]
}
@@ -113,13 +113,13 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
"tensor([[3.0403, 4.7550, 4.9311, ..., 3.0626, 4.3593, 3.9823],\n",
" [4.4812, 4.5463, 4.4052, ..., 3.7669, 3.4201, 4.6582],\n",
" [3.7711, 3.3997, 4.1416, ..., 3.4086, 3.1681, 4.0410],\n",
"tensor([[3.5493, 3.2984, 4.3041, ..., 4.5181, 3.7561, 4.5633],\n",
" [4.4523, 3.7956, 3.7448, ..., 3.5031, 3.9477, 4.8617],\n",
" [3.5174, 4.1082, 4.6358, ..., 3.5759, 4.5291, 3.9545],\n",
" ...,\n",
" [4.4137, 4.1779, 4.8741, ..., 3.4678, 3.4457, 4.7489],\n",
" [3.8246, 4.2699, 4.9944, ..., 4.8576, 3.8945, 4.5525],\n",
" [3.4959, 3.6991, 4.4047, ..., 4.7308, 3.5796, 3.2013]],\n",
" [3.6757, 4.2100, 3.9763, ..., 3.2017, 3.4422, 4.0191],\n",
" [3.0283, 3.8147, 3.1705, ..., 3.9442, 4.1054, 4.9491],\n",
" [3.5879, 3.7237, 4.0656, ..., 3.2279, 3.1818, 4.7489]],\n",
" dtype=torch.float64, requires_grad=True)\n"
]
}
@@ -173,9 +173,7 @@
{
"cell_type": "code",
"execution_count": 8,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"class sim_net(nn.Module):\n",
@@ -208,9 +206,7 @@
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"net2 = sim_net()"
@@ -226,15 +222,15 @@
"output_type": "stream",
"text": [
"Sequential(\n",
" (0): Linear(in_features=30, out_features=40)\n",
" (0): Linear(in_features=30, out_features=40, bias=True)\n",
" (1): ReLU()\n",
")\n",
"Sequential(\n",
" (0): Linear(in_features=40, out_features=50)\n",
" (0): Linear(in_features=40, out_features=50, bias=True)\n",
" (1): ReLU()\n",
")\n",
"Sequential(\n",
" (0): Linear(in_features=50, out_features=10)\n",
" (0): Linear(in_features=50, out_features=10, bias=True)\n",
" (1): ReLU()\n",
")\n"
]
@@ -257,35 +253,35 @@
"text": [
"sim_net(\n",
" (l1): Sequential(\n",
" (0): Linear(in_features=30, out_features=40)\n",
" (0): Linear(in_features=30, out_features=40, bias=True)\n",
" (1): ReLU()\n",
" )\n",
" (l2): Sequential(\n",
" (0): Linear(in_features=40, out_features=50)\n",
" (0): Linear(in_features=40, out_features=50, bias=True)\n",
" (1): ReLU()\n",
" )\n",
" (l3): Sequential(\n",
" (0): Linear(in_features=50, out_features=10)\n",
" (0): Linear(in_features=50, out_features=10, bias=True)\n",
" (1): ReLU()\n",
" )\n",
")\n",
"Sequential(\n",
" (0): Linear(in_features=30, out_features=40)\n",
" (0): Linear(in_features=30, out_features=40, bias=True)\n",
" (1): ReLU()\n",
")\n",
"Linear(in_features=30, out_features=40)\n",
"Linear(in_features=30, out_features=40, bias=True)\n",
"ReLU()\n",
"Sequential(\n",
" (0): Linear(in_features=40, out_features=50)\n",
" (0): Linear(in_features=40, out_features=50, bias=True)\n",
" (1): ReLU()\n",
")\n",
"Linear(in_features=40, out_features=50)\n",
"Linear(in_features=40, out_features=50, bias=True)\n",
"ReLU()\n",
"Sequential(\n",
" (0): Linear(in_features=50, out_features=10)\n",
" (0): Linear(in_features=50, out_features=10, bias=True)\n",
" (1): ReLU()\n",
")\n",
"Linear(in_features=50, out_features=10)\n",
"Linear(in_features=50, out_features=10, bias=True)\n",
"ReLU()\n"
]
}
@@ -334,7 +330,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
@@ -343,7 +339,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -351,13 +347,13 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
"tensor([[3.0403, 4.7550, 4.9311, ..., 3.0626, 4.3593, 3.9823],\n",
" [4.4812, 4.5463, 4.4052, ..., 3.7669, 3.4201, 4.6582],\n",
" [3.7711, 3.3997, 4.1416, ..., 3.4086, 3.1681, 4.0410],\n",
"tensor([[ 0.2725, -0.2262, -0.4229, ..., -0.2451, 0.2344, 0.1583],\n",
" [ 0.1886, 0.3226, -0.5023, ..., -0.2228, 0.5089, -0.6994],\n",
" [-0.4689, 0.2612, 0.3464, ..., -0.0423, -0.2999, -0.5813],\n",
" ...,\n",
" [4.4137, 4.1779, 4.8741, ..., 3.4678, 3.4457, 4.7489],\n",
" [3.8246, 4.2699, 4.9944, ..., 4.8576, 3.8945, 4.5525],\n",
" [3.4959, 3.6991, 4.4047, ..., 4.7308, 3.5796, 3.2013]],\n",
" [ 0.4200, 0.2091, -0.3690, ..., 0.4142, 0.1120, 0.0771],\n",
" [ 0.6540, 0.0475, 0.0594, ..., 0.1726, -0.2264, 0.1510],\n",
" [-1.0729, -0.2862, 0.4953, ..., 0.4702, 0.5555, -0.2246]],\n",
" dtype=torch.float64, requires_grad=True)\n"
]
}
@@ -368,43 +364,35 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 16,
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"/home/bushuhui/.virtualenv/dl/lib/python3.5/site-packages/ipykernel_launcher.py:1: UserWarning: nn.init.xavier_uniform is now deprecated in favor of nn.init.xavier_uniform_.\n",
" \"\"\"Entry point for launching an IPython kernel.\n"
]
},
{
"data": {
"text/plain": [
"Parameter containing:\n",
"tensor([[-0.0889, 0.2279, 0.1816, ..., 0.1091, 0.0207, -0.2063],\n",
" [ 0.0394, 0.1860, 0.1261, ..., 0.2250, -0.2881, 0.0727],\n",
" [-0.2252, -0.0639, 0.2077, ..., 0.0328, -0.0075, 0.0339],\n",
"tensor([[ 0.1173, -0.0864, 0.1008, ..., -0.1053, 0.2642, -0.1045],\n",
" [-0.0244, 0.1722, 0.1330, ..., 0.2443, -0.2385, 0.1613],\n",
" [-0.1767, 0.0678, 0.1282, ..., 0.1033, -0.2423, -0.0864],\n",
" ...,\n",
" [-0.0932, 0.2806, -0.2377, ..., -0.2087, 0.0325, 0.0504],\n",
" [-0.2305, 0.2866, -0.1872, ..., 0.2127, 0.1487, 0.0645],\n",
" [-0.0072, 0.2771, 0.0928, ..., -0.0234, -0.1238, 0.1197]],\n",
" [-0.1673, -0.1338, -0.0839, ..., 0.0267, 0.1693, -0.2911],\n",
" [ 0.2146, 0.0194, 0.2873, ..., 0.1486, 0.2775, 0.2740],\n",
" [-0.0400, 0.2231, 0.0800, ..., 0.2804, 0.2121, 0.2764]],\n",
" dtype=torch.float64, requires_grad=True)"
]
},
"execution_count": 9,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"init.xavier_uniform(net1[0].weight) # 这就是上面我们讲过的 Xavier 初始化方法,PyTorch 直接内置了其实现"
"init.xavier_uniform_(net1[0].weight) # 这就是上面我们讲过的 Xavier 初始化方法,PyTorch 直接内置了其实现"
]
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -412,13 +400,13 @@
"output_type": "stream",
"text": [
"Parameter containing:\n",
"tensor([[-0.0889, 0.2279, 0.1816, ..., 0.1091, 0.0207, -0.2063],\n",
" [ 0.0394, 0.1860, 0.1261, ..., 0.2250, -0.2881, 0.0727],\n",
" [-0.2252, -0.0639, 0.2077, ..., 0.0328, -0.0075, 0.0339],\n",
"tensor([[ 0.1173, -0.0864, 0.1008, ..., -0.1053, 0.2642, -0.1045],\n",
" [-0.0244, 0.1722, 0.1330, ..., 0.2443, -0.2385, 0.1613],\n",
" [-0.1767, 0.0678, 0.1282, ..., 0.1033, -0.2423, -0.0864],\n",
" ...,\n",
" [-0.0932, 0.2806, -0.2377, ..., -0.2087, 0.0325, 0.0504],\n",
" [-0.2305, 0.2866, -0.1872, ..., 0.2127, 0.1487, 0.0645],\n",
" [-0.0072, 0.2771, 0.0928, ..., -0.0234, -0.1238, 0.1197]],\n",
" [-0.1673, -0.1338, -0.0839, ..., 0.0267, 0.1693, -0.2911],\n",
" [ 0.2146, 0.0194, 0.2873, ..., 0.1486, 0.2775, 0.2740],\n",
" [-0.0400, 0.2231, 0.0800, ..., 0.2804, 0.2121, 0.2764]],\n",
" dtype=torch.float64, requires_grad=True)\n"
]
}
@@ -448,7 +436,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -462,7 +450,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


6_pytorch/1_NN/6-nn_summary.ipynb → 6_pytorch/1_NN/backup/6-nn_summary.ipynb View File

@@ -1933,7 +1933,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -1947,7 +1947,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,

+ 129
- 33
6_pytorch/1_NN/optimizer/6_1-sgd.ipynb
File diff suppressed because it is too large
View File


+ 4
- 4
6_pytorch/1_NN/optimizer/6_2-momentum.ipynb View File

@@ -104,8 +104,8 @@
" x = torch.from_numpy(x)\n",
" return x\n",
"\n",
"train_set = MNIST('./data', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('./data', train=False, transform=data_tf, download=True)\n",
"train_set = MNIST('../../../data/mnist', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('../../../data/mnist', train=False, transform=data_tf, download=True)\n",
"\n",
"# 定义 loss 函数\n",
"criterion = nn.CrossEntropyLoss()"
@@ -374,7 +374,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -388,7 +388,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 5
- 5
6_pytorch/1_NN/optimizer/6_3-adagrad.ipynb View File

@@ -132,9 +132,9 @@
" loss.backward()\n",
" sgd_adagrad(net.parameters(), sqrs, 1e-2) # 学习率设为 0.01\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" if idx % 30 == 0:\n",
" losses.append(loss.data[0])\n",
" losses.append(loss.item())\n",
" idx += 1\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
@@ -232,7 +232,7 @@
" loss.backward()\n",
" optimizer.step()\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
"end = time.time() # 计时结束\n",
@@ -242,7 +242,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -256,7 +256,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 9
- 9
6_pytorch/1_NN/optimizer/6_4-rmsprop.ipynb View File

@@ -66,8 +66,8 @@
" x = torch.from_numpy(x)\n",
" return x\n",
"\n",
"train_set = MNIST('./data', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('./data', train=False, transform=data_tf, download=True)\n",
"train_set = MNIST('../../../data/mnist', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('../../../data/mnist', train=False, transform=data_tf, download=True)\n",
"\n",
"# 定义 loss 函数\n",
"criterion = nn.CrossEntropyLoss()"
@@ -122,9 +122,9 @@
" loss.backward()\n",
" rmsprop(net.parameters(), sqrs, 1e-3, 0.9) # 学习率设为 0.001,alpha 设为 0.9\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" if idx % 30 == 0:\n",
" losses.append(loss.data[0])\n",
" losses.append(loss.item())\n",
" idx += 1\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
@@ -214,9 +214,9 @@
" loss.backward()\n",
" rmsprop(net.parameters(), sqrs, 1e-3, 0.999) # 学习率设为 0.001,alpha 设为 0.999\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" if idx % 30 == 0:\n",
" losses.append(loss.data[0])\n",
" losses.append(loss.item())\n",
" idx += 1\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
@@ -315,7 +315,7 @@
" loss.backward()\n",
" optimizer.step()\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
"end = time.time() # 计时结束\n",
@@ -325,7 +325,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -339,7 +339,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 7
- 7
6_pytorch/1_NN/optimizer/6_5-adadelta.ipynb View File

@@ -77,8 +77,8 @@
" x = torch.from_numpy(x)\n",
" return x\n",
"\n",
"train_set = MNIST('./data', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('./data', train=False, transform=data_tf, download=True)\n",
"train_set = MNIST('../../../data/mnist', train=True, transform=data_tf, download=True) # 载入数据集,申明定义的数据变换\n",
"test_set = MNIST('../../../data/mnist', train=False, transform=data_tf, download=True)\n",
"\n",
"# 定义 loss 函数\n",
"criterion = nn.CrossEntropyLoss()"
@@ -135,9 +135,9 @@
" loss.backward()\n",
" adadelta(net.parameters(), sqrs, deltas, 0.9) # rho 设置为 0.9\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" if idx % 30 == 0:\n",
" losses.append(loss.data[0])\n",
" losses.append(loss.item())\n",
" idx += 1\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
@@ -242,7 +242,7 @@
" loss.backward()\n",
" optimizer.step()\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
"end = time.time() # 计时结束\n",
@@ -259,7 +259,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -273,7 +273,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 5
- 5
6_pytorch/1_NN/optimizer/6_6-adam.ipynb View File

@@ -143,9 +143,9 @@
" adam(net.parameters(), vs, sqrs, 1e-3, t) # 学习率设为 0.001\n",
" t += 1\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" if idx % 30 == 0:\n",
" losses.append(loss.data[0])\n",
" losses.append(loss.item())\n",
" idx += 1\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
@@ -238,7 +238,7 @@
" loss.backward()\n",
" optimizer.step()\n",
" # 记录误差\n",
" train_loss += loss.data[0]\n",
" train_loss += loss.item()\n",
" print('epoch: {}, Train Loss: {:.6f}'\n",
" .format(e, train_loss / len(train_data)))\n",
"end = time.time() # 计时结束\n",
@@ -267,7 +267,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -281,7 +281,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 3
- 3
6_pytorch/2_CNN/2-batch-normalization.ipynb View File

@@ -431,7 +431,7 @@
" return x\n",
"\n",
"train_set = mnist.MNIST('../../data/mnist', train=True, transform=data_tf, download=True) # 重新载入数据集,申明定义的数据变换\n",
"test_set = mnist.MNIST('../../data/mnist', train=False, transform=data_tf, download=True)\n",
"test_set = mnist.MNIST('../../data/mnist', train=False, transform=data_tf, download=True)\n",
"train_data = DataLoader(train_set, batch_size=64, shuffle=True)\n",
"test_data = DataLoader(test_set, batch_size=128, shuffle=False)"
]
@@ -558,7 +558,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -572,7 +572,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 4
- 4
6_pytorch/2_CNN/3-lr-decay.ipynb View File

@@ -201,9 +201,9 @@
" x = im_aug(x)\n",
" return x\n",
"\n",
"train_set = CIFAR10('./data', train=True, transform=train_tf)\n",
"train_set = CIFAR10('../../data', train=True, transform=train_tf)\n",
"train_data = torch.utils.data.DataLoader(train_set, batch_size=256, shuffle=True, num_workers=4)\n",
"valid_set = CIFAR10('./data', train=False, transform=test_tf)\n",
"valid_set = CIFAR10('../../data', train=False, transform=test_tf)\n",
"valid_data = torch.utils.data.DataLoader(valid_set, batch_size=256, shuffle=False, num_workers=4)\n",
"\n",
"net = resnet(3, 10)\n",
@@ -389,7 +389,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -403,7 +403,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 55
- 71
6_pytorch/2_CNN/4-data-augumentation.ipynb
File diff suppressed because it is too large
View File


+ 16
- 32
6_pytorch/2_CNN/4-regularization.ipynb View File

@@ -51,8 +51,7 @@
"ExecuteTime": {
"end_time": "2017-12-24T08:02:11.903459Z",
"start_time": "2017-12-24T08:02:11.383170Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -76,8 +75,7 @@
"ExecuteTime": {
"end_time": "2017-12-24T08:02:13.120502Z",
"start_time": "2017-12-24T08:02:11.905617Z"
},
"collapsed": true
}
},
"outputs": [],
"source": [
@@ -90,9 +88,9 @@
" x = im_aug(x)\n",
" return x\n",
"\n",
"train_set = CIFAR10('./data', train=True, transform=data_tf)\n",
"train_set = CIFAR10('../../data', train=True, transform=data_tf)\n",
"train_data = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True, num_workers=4)\n",
"test_set = CIFAR10('./data', train=False, transform=data_tf)\n",
"test_set = CIFAR10('../../data', train=False, transform=data_tf)\n",
"test_data = torch.utils.data.DataLoader(test_set, batch_size=128, shuffle=False, num_workers=4)\n",
"\n",
"net = resnet(3, 10)\n",
@@ -102,7 +100,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {
"ExecuteTime": {
"end_time": "2017-12-24T08:11:36.106177Z",
@@ -111,29 +109,15 @@
},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Epoch 0. Train Loss: 1.429834, Train Acc: 0.476982, Valid Loss: 1.261334, Valid Acc: 0.546776, Time 00:00:26\n",
"Epoch 1. Train Loss: 0.994539, Train Acc: 0.645400, Valid Loss: 1.310620, Valid Acc: 0.554688, Time 00:00:27\n",
"Epoch 2. Train Loss: 0.788570, Train Acc: 0.723585, Valid Loss: 1.256101, Valid Acc: 0.577433, Time 00:00:28\n",
"Epoch 3. Train Loss: 0.629832, Train Acc: 0.780411, Valid Loss: 1.222015, Valid Acc: 0.609474, Time 00:00:27\n",
"Epoch 4. Train Loss: 0.500406, Train Acc: 0.825288, Valid Loss: 0.831702, Valid Acc: 0.720332, Time 00:00:27\n",
"Epoch 5. Train Loss: 0.388376, Train Acc: 0.868646, Valid Loss: 0.829582, Valid Acc: 0.726760, Time 00:00:27\n",
"Epoch 6. Train Loss: 0.291237, Train Acc: 0.902094, Valid Loss: 1.499777, Valid Acc: 0.623714, Time 00:00:28\n",
"Epoch 7. Train Loss: 0.222401, Train Acc: 0.925072, Valid Loss: 1.832660, Valid Acc: 0.558643, Time 00:00:28\n",
"Epoch 8. Train Loss: 0.157753, Train Acc: 0.947990, Valid Loss: 1.255313, Valid Acc: 0.668117, Time 00:00:28\n",
"Epoch 9. Train Loss: 0.111407, Train Acc: 0.963595, Valid Loss: 1.004693, Valid Acc: 0.724782, Time 00:00:27\n",
"Epoch 10. Train Loss: 0.084960, Train Acc: 0.972926, Valid Loss: 0.867961, Valid Acc: 0.775119, Time 00:00:27\n",
"Epoch 11. Train Loss: 0.066854, Train Acc: 0.979280, Valid Loss: 1.011263, Valid Acc: 0.749604, Time 00:00:28\n",
"Epoch 12. Train Loss: 0.048280, Train Acc: 0.985534, Valid Loss: 2.438345, Valid Acc: 0.576938, Time 00:00:27\n",
"Epoch 13. Train Loss: 0.046176, Train Acc: 0.985614, Valid Loss: 1.008425, Valid Acc: 0.756527, Time 00:00:27\n",
"Epoch 14. Train Loss: 0.039515, Train Acc: 0.988411, Valid Loss: 0.945017, Valid Acc: 0.766317, Time 00:00:27\n",
"Epoch 15. Train Loss: 0.025882, Train Acc: 0.992667, Valid Loss: 0.918691, Valid Acc: 0.784217, Time 00:00:27\n",
"Epoch 16. Train Loss: 0.018592, Train Acc: 0.994985, Valid Loss: 1.507427, Valid Acc: 0.680281, Time 00:00:27\n",
"Epoch 17. Train Loss: 0.021062, Train Acc: 0.994246, Valid Loss: 2.976452, Valid Acc: 0.558940, Time 00:00:27\n",
"Epoch 18. Train Loss: 0.021458, Train Acc: 0.993926, Valid Loss: 0.927871, Valid Acc: 0.785898, Time 00:00:27\n",
"Epoch 19. Train Loss: 0.015656, Train Acc: 0.995824, Valid Loss: 0.962502, Valid Acc: 0.782832, Time 00:00:27\n"
"ename": "IndexError",
"evalue": "invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mIndexError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m/tmp/ipykernel_10317/3705871991.py\u001b[0m in \u001b[0;36m<module>\u001b[0;34m\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0;32mfrom\u001b[0m \u001b[0mutils\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0mtrain\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0mtrain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mnet\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtrain_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mtest_data\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0;36m20\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcriterion\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m~-data/msdk/my_progs/pi-lab/courses/machine_learning/machinelearning_notebook/6_pytorch/2_CNN/utils.py\u001b[0m in \u001b[0;36mtrain\u001b[0;34m(net, train_data, valid_data, num_epochs, optimizer, criterion)\u001b[0m\n\u001b[1;32m 37\u001b[0m \u001b[0moptimizer\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstep\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 38\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m---> 39\u001b[0;31m \u001b[0mtrain_loss\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mloss\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mitem\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 40\u001b[0m \u001b[0mtrain_acc\u001b[0m \u001b[0;34m+=\u001b[0m \u001b[0mget_acc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0moutput\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mlabel\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 41\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mIndexError\u001b[0m: invalid index of a 0-dim tensor. Use `tensor.item()` in Python or `tensor.item<T>()` in C++ to convert a 0-dim tensor to a number"
]
}
],
@@ -145,7 +129,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -159,7 +143,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 4
- 4
6_pytorch/2_CNN/6-vgg.ipynb View File

@@ -340,9 +340,9 @@
" x = torch.from_numpy(x)\n",
" return x\n",
" \n",
"train_set = CIFAR10('./data', train=True, transform=data_tf)\n",
"train_set = CIFAR10('../../data', train=True, transform=data_tf)\n",
"train_data = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)\n",
"test_set = CIFAR10('./data', train=False, transform=data_tf)\n",
"test_set = CIFAR10('../../data', train=False, transform=data_tf)\n",
"test_data = torch.utils.data.DataLoader(test_set, batch_size=128, shuffle=False)\n",
"\n",
"net = vgg()\n",
@@ -401,7 +401,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -415,7 +415,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 4
- 4
6_pytorch/2_CNN/7-googlenet.ipynb View File

@@ -296,9 +296,9 @@
" x = torch.from_numpy(x)\n",
" return x\n",
" \n",
"train_set = CIFAR10('./data', train=True, transform=data_tf)\n",
"train_set = CIFAR10('../../data', train=True, transform=data_tf)\n",
"train_data = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)\n",
"test_set = CIFAR10('./data', train=False, transform=data_tf)\n",
"test_set = CIFAR10('../../data', train=False, transform=data_tf)\n",
"test_data = torch.utils.data.DataLoader(test_set, batch_size=128, shuffle=False)\n",
"\n",
"net = googlenet(3, 10)\n",
@@ -363,7 +363,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -377,7 +377,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.8"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 4
- 4
6_pytorch/2_CNN/8-resnet.ipynb View File

@@ -294,9 +294,9 @@
" x = torch.from_numpy(x)\n",
" return x\n",
" \n",
"train_set = CIFAR10('./data', train=True, transform=data_tf)\n",
"train_set = CIFAR10('../../data', train=True, transform=data_tf)\n",
"train_data = torch.utils.data.DataLoader(train_set, batch_size=64, shuffle=True)\n",
"test_set = CIFAR10('./data', train=False, transform=data_tf)\n",
"test_set = CIFAR10('../../data', train=False, transform=data_tf)\n",
"test_data = torch.utils.data.DataLoader(test_set, batch_size=128, shuffle=False)\n",
"\n",
"net = resnet(3, 10)\n",
@@ -359,7 +359,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -373,7 +373,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.6.9"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 2
- 2
6_pytorch/2_CNN/9-densenet.ipynb View File

@@ -368,7 +368,7 @@
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"display_name": "Python 3 (ipykernel)",
"language": "python",
"name": "python3"
},
@@ -382,7 +382,7 @@
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.5.2"
"version": "3.9.7"
}
},
"nbformat": 4,


+ 3
- 3
6_pytorch/2_CNN/utils.py View File

@@ -9,7 +9,7 @@ from torch.autograd import Variable
def get_acc(output, label):
total = output.shape[0]
_, pred_label = output.max(1)
num_correct = (pred_label == label).sum().data[0]
num_correct = (pred_label == label).sum().item()
return num_correct / total


@@ -36,7 +36,7 @@ def train(net, train_data, valid_data, num_epochs, optimizer, criterion):
loss.backward()
optimizer.step()

train_loss += loss.data[0]
train_loss += loss.item()
train_acc += get_acc(output, label)

cur_time = datetime.now()
@@ -56,7 +56,7 @@ def train(net, train_data, valid_data, num_epochs, optimizer, criterion):
label = Variable(label, volatile=True)
output = net(im)
loss = criterion(output, label)
valid_loss += loss.data[0]
valid_loss += loss.item()
valid_acc += get_acc(output, label)
epoch_str = (
"Epoch %d. Train Loss: %f, Train Acc: %f, Valid Loss: %f, Valid Acc: %f, "


Loading…
Cancel
Save