Browse Source

Add some references

pull/1/MERGE
Shuhui Bu 6 years ago
parent
commit
008fd37b15
27 changed files with 2737 additions and 1071 deletions
  1. +71
    -58
      0_python/1_Basics.ipynb
  2. +7
    -7
      0_python/2_Print_Statement.ipynb
  3. +530
    -105
      0_python/3_Data_Structure_1.ipynb
  4. +77
    -35
      0_python/4_Data_Structure_2.ipynb
  5. +82
    -9
      0_python/5_Control_Flow.ipynb
  6. +157
    -93
      0_python/6_Function.ipynb
  7. +162
    -52
      0_python/7_Class.ipynb
  8. +18
    -18
      1_numpy_matplotlib_scipy_sympy/bokeh_tutorial.ipynb
  9. +69
    -32
      1_numpy_matplotlib_scipy_sympy/matplotlib_simple_tutorial.ipynb
  10. +349
    -290
      1_numpy_matplotlib_scipy_sympy/numpy_tutorial.ipynb
  11. +729
    -92
      1_numpy_matplotlib_scipy_sympy/sympy_tutorial.ipynb
  12. +11
    -11
      2_knn/knn_classification.ipynb
  13. +2
    -2
      3_kmeans/ClusteringAlgorithms.ipynb
  14. +84
    -47
      3_kmeans/k-means.ipynb
  15. +26
    -20
      3_kmeans/kmeans-color-vq.ipynb
  16. +50
    -50
      4_logistic_regression/Least_squares.ipynb
  17. +13
    -13
      4_logistic_regression/Logistic_regression.ipynb
  18. +4
    -5
      5_nn/Perceptron.ipynb
  19. +19
    -19
      5_nn/mlp_bp.ipynb
  20. +15
    -13
      6_pytorch/0_basic/Tensor-and-Variable.ipynb
  21. +23
    -23
      6_pytorch/1_NN/linear-regression-gradient-descend.ipynb
  22. +63
    -71
      6_pytorch/PyTorch_quick_intro.ipynb
  23. +4
    -4
      README.md
  24. +1
    -0
      References.md
  25. BIN
      tips/The Matrix Calculus You Need For Deep Learning.pdf
  26. +150
    -0
      tips/构建深度神经网络的一些实战建议.ipynb
  27. +21
    -2
      tips/构建深度神经网络的一些实战建议.md

+ 71
- 58
0_python/1_Basics.ipynb View File

@@ -67,10 +67,8 @@
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"collapsed": true
},
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
"x = 2\n",
@@ -80,7 +78,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -92,7 +90,7 @@
}
],
"source": [
"print x+y, xy"
"print(x+y, xy)"
]
},
{
@@ -105,9 +103,7 @@
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"x = y = 1"
@@ -127,7 +123,7 @@
}
],
"source": [
"print x,y"
"print(x,y)"
]
},
{
@@ -248,6 +244,26 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"0.5"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"1/2"
]
},
{
"cell_type": "code",
"execution_count": 10,
"metadata": {},
"outputs": [
@@ -408,10 +424,8 @@
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {
"collapsed": true
},
"execution_count": 9,
"metadata": {},
"outputs": [],
"source": [
"a = 2 #10\n",
@@ -420,7 +434,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -433,13 +447,13 @@
}
],
"source": [
"print a & b\n",
"print bin(a&b)"
"print(a & b)\n",
"print(bin(a&b))"
]
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 12,
"metadata": {},
"outputs": [
{
@@ -448,7 +462,7 @@
"2"
]
},
"execution_count": 18,
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
@@ -470,7 +484,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -479,7 +493,7 @@
"10"
]
},
"execution_count": 19,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -529,7 +543,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -538,7 +552,7 @@
"'0xaa'"
]
},
"execution_count": 20,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -569,16 +583,16 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 15,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'010'"
"'0o10'"
]
},
"execution_count": 22,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -644,7 +658,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -657,8 +671,8 @@
}
],
"source": [
"print int(7.7)\n",
"print int('7')"
"print(int(7.7))\n",
"print(int('7'))"
]
},
{
@@ -724,7 +738,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 17,
"metadata": {
"scrolled": false
},
@@ -733,14 +747,14 @@
"name": "stdout",
"output_type": "stream",
"text": [
"6.0\n",
"6\n",
"4.56\n"
]
}
],
"source": [
"print round(5.6231) \n",
"print round(4.55892, 2)"
"print(round(5.6231))\n",
"print(round(4.55892, 2))"
]
},
{
@@ -752,20 +766,20 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 18,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"5.38516480713\n"
"5.385164807134504\n"
]
}
],
"source": [
"c =complex('5+2j')\n",
"print abs(c)"
"print(abs(c))"
]
},
{
@@ -804,7 +818,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -818,9 +832,9 @@
}
],
"source": [
"print isinstance(1, int)\n",
"print isinstance(1.0,int)\n",
"print isinstance(1.0,(int,float))"
"print(isinstance(1, int))\n",
"print(isinstance(1.0,int))\n",
"print(isinstance(1.0,(int,float)))"
]
},
{
@@ -838,23 +852,22 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"-1\n",
"1\n",
"0\n"
"True\n"
]
}
],
"source": [
"print cmp(1,2)\n",
"print cmp(2,1)\n",
"print cmp(2,2)"
"print(1<2)\n",
"#print(cmp(1,2))\n",
"#print(cmp(2,1))\n",
"#print(cmp(2,2))"
]
},
{
@@ -892,23 +905,23 @@
},
{
"cell_type": "code",
"execution_count": 34,
"execution_count": 22,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[0, 1, 2]\n",
"[2, 3, 4, 5, 6, 7, 8]\n",
"[2, 10, 18, 26]\n"
"range(0, 3)\n",
"range(2, 9)\n",
"range(2, 27, 8)\n"
]
}
],
"source": [
"print range(3)\n",
"print range(2,9)\n",
"print range(2,27,8)"
"print(range(3))\n",
"print(range(2,9))\n",
"print(range(2,27,8))"
]
},
{
@@ -927,14 +940,14 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Type something here and it will be stored in variable abc \tHey\n"
"Type something here and it will be stored in variable abc \tHello world!\n"
]
}
],
@@ -944,7 +957,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -953,7 +966,7 @@
"str"
]
},
"execution_count": 36,
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}


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

@@ -99,7 +99,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 1,
"metadata": {
"scrolled": true
},
@@ -130,7 +130,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -187,7 +187,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 3,
"metadata": {},
"outputs": [
{
@@ -219,7 +219,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -231,7 +231,7 @@
}
],
"source": [
"print \"Hello %s %s\" %(string1,string2)"
"print(\"Hello %s %s\" % (string1,string2))"
]
},
{
@@ -267,7 +267,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -284,7 +284,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 6,
"metadata": {},
"outputs": [
{


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


+ 77
- 35
0_python/4_Data_Structure_2.ipynb View File

@@ -66,7 +66,7 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
@@ -99,7 +99,7 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -182,12 +182,12 @@
"name": "stdout",
"output_type": "stream",
"text": [
"Observe the first letter in this sentence.\n"
"Observe the first letter in this sentence. can you change this sentence\n"
]
}
],
"source": [
"String3 = 'observe the first letter in this sentence.'\n",
"String3 = 'observe the first letter in this sentence. can you change this sentence'\n",
"print(String3.capitalize())"
]
},
@@ -426,7 +426,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 11,
"metadata": {},
"outputs": [
{
@@ -435,7 +435,7 @@
"'*a_a-'"
]
},
"execution_count": 16,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -445,6 +445,26 @@
]
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'1\\n2'"
]
},
"execution_count": 12,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"'\\n'.join(['1', '2'])"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -460,7 +480,7 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -488,7 +508,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -513,7 +533,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -792,14 +812,14 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<type 'dict'> <type 'dict'>\n"
"<class 'dict'> <class 'dict'>\n"
]
}
],
@@ -818,14 +838,14 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 23,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'OneTwo': 12, 'One': 1}\n"
"{'One': 1, 'OneTwo': 12}\n"
]
}
],
@@ -836,6 +856,24 @@
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'key2': [1, 2, 4], 3: (1, 4, 6), 'key1': 1}\n"
]
}
],
"source": [
"d1 = {\"key1\":1, \"key2\":[1,2,4], 3:(1, 4, 6)}\n",
"print(d1)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -844,7 +882,7 @@
},
{
"cell_type": "code",
"execution_count": 32,
"execution_count": 24,
"metadata": {},
"outputs": [
{
@@ -868,7 +906,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 25,
"metadata": {},
"outputs": [],
"source": [
@@ -885,20 +923,20 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 27,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<zip object at 0x7f33306cb8c8>\n"
"{'Two': 2, 'One': 1, 'Five': 5, 'Four': 4, 'Three': 3}\n"
]
}
],
"source": [
"d2 = zip(names,numbers)\n",
"print(d2)"
"print(dict(d2))"
]
},
{
@@ -912,14 +950,14 @@
},
{
"cell_type": "code",
"execution_count": 35,
"execution_count": 29,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n"
"{}\n"
]
}
],
@@ -944,7 +982,7 @@
},
{
"cell_type": "code",
"execution_count": 36,
"execution_count": 30,
"metadata": {},
"outputs": [
{
@@ -969,14 +1007,14 @@
},
{
"cell_type": "code",
"execution_count": 37,
"execution_count": 31,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Four': 4, 'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n"
"{'Two': 2, 'One': 1, 'Five': 5, 'Four': 4, 'Three': 3}\n"
]
}
],
@@ -1049,22 +1087,26 @@
},
{
"cell_type": "code",
"execution_count": 40,
"execution_count": 33,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[('Four', 4), ('Five', 5), ('Three', 3), ('Two', 2), ('One', 1)]"
]
},
"execution_count": 40,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"[ Two] 2\n",
"[ One] 1\n",
"[ Five] 5\n",
"[ Four] 4\n",
"[ Three] 3\n"
]
}
],
"source": [
"a1.items()"
"a1.items()\n",
"\n",
"for (k,v) in a1.items():\n",
" print(\"[%6s] %d\" % (k, v))"
]
},
{
@@ -1076,14 +1118,14 @@
},
{
"cell_type": "code",
"execution_count": 41,
"execution_count": 34,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"{'Five': 5, 'Three': 3, 'Two': 2, 'One': 1}\n",
"{'Two': 2, 'One': 1, 'Five': 5, 'Three': 3}\n",
"4\n"
]
}


+ 82
- 9
0_python/5_Control_Flow.ipynb View File

@@ -199,7 +199,7 @@
},
{
"cell_type": "code",
"execution_count": 5,
"execution_count": 2,
"metadata": {},
"outputs": [
{
@@ -220,6 +220,28 @@
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"1\n",
"2\n",
"5\n",
"6\n"
]
}
],
"source": [
"a = [1, 2, 5, 6]\n",
"for i in a:\n",
" print(i)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -228,7 +250,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -256,7 +278,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 6,
"metadata": {},
"outputs": [
{
@@ -430,7 +452,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -446,7 +468,7 @@
"for i in range(1,11):\n",
" x = 27*i\n",
" res.append(x)\n",
"print res"
"print(res)"
]
},
{
@@ -458,7 +480,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"outputs": [
{
@@ -467,7 +489,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 12,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -492,7 +514,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -501,7 +523,7 @@
"[27, 54, 81, 108, 135, 162, 189, 216, 243, 270]"
]
},
"execution_count": 13,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -511,6 +533,57 @@
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"scrolled": true
},
"outputs": [
{
"data": {
"text/plain": [
"{'108': 108,\n",
" '135': 135,\n",
" '162': 162,\n",
" '189': 189,\n",
" '216': 216,\n",
" '243': 243,\n",
" '27': 27,\n",
" '270': 270,\n",
" '54': 54,\n",
" '81': 81}"
]
},
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"{str(27*x):27*x for x in range(1,20) if x<=10}"
]
},
{
"cell_type": "code",
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(27, 54, 81, 108, 135, 162, 189, 216, 243, 270)"
]
},
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"tuple((27*x for x in range(1,20) if x<=10))"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [


+ 157
- 93
0_python/6_Function.ipynb View File

@@ -179,7 +179,7 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 1,
"metadata": {},
"outputs": [],
"source": [
@@ -193,16 +193,16 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 2,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Please enter your name : karthik\n",
"Hey karthik!\n",
"karthik, How do you do?\n"
"Please enter your name : Tom\n",
"Hey Tom!\n",
"Tom, How do you do?\n"
]
}
],
@@ -226,10 +226,8 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {
"collapsed": true
},
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
"def times(x,y):\n",
@@ -246,7 +244,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 5,
"metadata": {},
"outputs": [
{
@@ -259,7 +257,7 @@
],
"source": [
"c = times(4,5)\n",
"print c"
"print(c)"
]
},
{
@@ -278,7 +276,7 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
@@ -289,7 +287,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 8,
"metadata": {},
"outputs": [
{
@@ -302,7 +300,7 @@
],
"source": [
"c = times(4,5)\n",
"print c"
"print(c)"
]
},
{
@@ -314,7 +312,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 10,
"metadata": {},
"outputs": [
{
@@ -334,6 +332,15 @@
]
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
"times?"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -342,10 +349,8 @@
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {
"collapsed": true
},
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
"eglist = [10,50,30,12,6,8,100]"
@@ -353,10 +358,8 @@
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {
"collapsed": true
},
"execution_count": 13,
"metadata": {},
"outputs": [],
"source": [
"def egfunc(eglist):\n",
@@ -380,18 +383,16 @@
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(100, 6, 10, 100)"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"(100, 6, 10, 100)\n"
]
}
],
"source": [
"egfunc(eglist)"
"a = egfunc(eglist)\n",
"print(a)"
]
},
{
@@ -431,10 +432,8 @@
},
{
"cell_type": "code",
"execution_count": 18,
"metadata": {
"collapsed": true
},
"execution_count": 17,
"metadata": {},
"outputs": [],
"source": [
"def implicitadd(x,y=3):\n",
@@ -457,7 +456,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 18,
"metadata": {},
"outputs": [
{
@@ -466,7 +465,7 @@
"7"
]
},
"execution_count": 19,
"execution_count": 18,
"metadata": {},
"output_type": "execute_result"
}
@@ -484,7 +483,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 19,
"metadata": {},
"outputs": [
{
@@ -493,7 +492,7 @@
"8"
]
},
"execution_count": 20,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
}
@@ -503,6 +502,26 @@
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"11"
]
},
"execution_count": 20,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"implicitadd(5, y=6)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -519,9 +538,7 @@
{
"cell_type": "code",
"execution_count": 21,
"metadata": {
"collapsed": true
},
"metadata": {},
"outputs": [],
"source": [
"def add_n(*args):\n",
@@ -610,7 +627,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 36,
"metadata": {},
"outputs": [],
"source": [
@@ -626,53 +643,47 @@
},
{
"cell_type": "code",
"execution_count": 25,
"metadata": {
"collapsed": true
},
"execution_count": 37,
"metadata": {},
"outputs": [],
"source": [
"def egfunc1():\n",
" def thirdfunc(arg1):\n",
" eg2 = arg1[:]\n",
" #eg2 = arg1\n",
" eg2.append(6)\n",
" print(\"This is happening inside the function :\", eg2)\n",
" \n",
" # what's the difference between following two lines?\n",
" eg1.append(7)\n",
" #eg1 = [1, 3, 5, 6]\n",
" \n",
" print(\"This is happening before the function is called : \", eg1)\n",
" thirdfunc(eg1)\n",
" \n",
" print(\"This is happening outside the function :\", eg1) \n",
" print(\"Accessing a variable declared inside the function from outside :\" , eg2)"
" #print(\"Accessing a variable declared inside the function from outside :\" , eg2)"
]
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 38,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is happening before the function is called : [1, 2, 3, 4, 5]\n",
"This is happening inside the function : [1, 2, 3, 4, 5, 6]\n",
"This is happening outside the function : [1, 2, 3, 4, 5]\n",
"Accessing a variable declared inside the function from outside :"
]
},
{
"ename": "NameError",
"evalue": "global name 'eg2' is not defined",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mNameError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-26-949117e1ddc5>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0megfunc1\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<ipython-input-25-0da329480da9>\u001b[0m in \u001b[0;36megfunc1\u001b[0;34m()\u001b[0m\n\u001b[1;32m 7\u001b[0m \u001b[0mthirdfunc\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0meg1\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 8\u001b[0m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"This is happening outside the function :\"\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0meg1\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 9\u001b[0;31m \u001b[0;32mprint\u001b[0m \u001b[0;34m\"Accessing a variable declared inside the function from outside :\"\u001b[0m \u001b[0;34m,\u001b[0m \u001b[0meg2\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mNameError\u001b[0m: global name 'eg2' is not defined"
"This is happening before the function is called : [1, 3, 5, 6]\n",
"This is happening inside the function : [1, 3, 5, 6, 6]\n",
"This is happening outside the function : [1, 3, 5, 6]\n",
"[1, 2, 3, 4, 5]\n"
]
}
],
"source": [
"egfunc1()"
"egfunc1()\n",
"print(eg1)"
]
},
{
@@ -755,10 +766,8 @@
},
{
"cell_type": "code",
"execution_count": 30,
"metadata": {
"collapsed": true
},
"execution_count": 40,
"metadata": {},
"outputs": [],
"source": [
"z = lambda x: x * x"
@@ -766,7 +775,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 41,
"metadata": {},
"outputs": [
{
@@ -775,7 +784,7 @@
"64"
]
},
"execution_count": 31,
"execution_count": 41,
"metadata": {},
"output_type": "execute_result"
}
@@ -785,6 +794,49 @@
]
},
{
"cell_type": "code",
"execution_count": 42,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 42,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"type(z)"
]
},
{
"cell_type": "code",
"execution_count": 43,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"function"
]
},
"execution_count": 43,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"def sqr(x):\n",
" return x*x\n",
"\n",
"type(sqr)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -800,10 +852,8 @@
},
{
"cell_type": "code",
"execution_count": 32,
"metadata": {
"collapsed": true
},
"execution_count": 46,
"metadata": {},
"outputs": [],
"source": [
"list1 = [1,2,3,4,5,6,7,8,9]"
@@ -811,7 +861,7 @@
},
{
"cell_type": "code",
"execution_count": 33,
"execution_count": 48,
"metadata": {},
"outputs": [
{
@@ -824,7 +874,25 @@
],
"source": [
"eg = map(lambda x:x+2, list1)\n",
"print(eg)"
"print(list(eg))"
]
},
{
"cell_type": "code",
"execution_count": 49,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 4, 9, 16, 25, 36, 49, 64, 81]\n"
]
}
],
"source": [
"eg = map(sqr, list1)\n",
"print(list(eg))"
]
},
{
@@ -902,10 +970,8 @@
},
{
"cell_type": "code",
"execution_count": 37,
"metadata": {
"collapsed": true
},
"execution_count": 50,
"metadata": {},
"outputs": [],
"source": [
"list1 = [1,2,3,4,5,6,7,8,9]"
@@ -920,22 +986,20 @@
},
{
"cell_type": "code",
"execution_count": 38,
"execution_count": 52,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[1, 2, 3, 4]"
]
},
"execution_count": 38,
"metadata": {},
"output_type": "execute_result"
"name": "stdout",
"output_type": "stream",
"text": [
"[1, 2, 3, 4]\n"
]
}
],
"source": [
"filter(lambda x:x<5,list1)"
"res = filter(lambda x:x<5,list1)\n",
"print(list(res))"
]
},
{


+ 162
- 52
0_python/7_Class.ipynb View File

@@ -39,10 +39,8 @@
},
{
"cell_type": "code",
"execution_count": 1,
"metadata": {
"collapsed": true
},
"execution_count": 2,
"metadata": {},
"outputs": [],
"source": [
"class FirstClass:\n",
@@ -65,7 +63,7 @@
},
{
"cell_type": "code",
"execution_count": 2,
"execution_count": 3,
"metadata": {},
"outputs": [],
"source": [
@@ -74,16 +72,16 @@
},
{
"cell_type": "code",
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"instance"
"__main__.FirstClass"
]
},
"execution_count": 3,
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
@@ -94,16 +92,16 @@
},
{
"cell_type": "code",
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"classobj"
"type"
]
},
"execution_count": 4,
"execution_count": 5,
"metadata": {},
"output_type": "execute_result"
}
@@ -137,9 +135,21 @@
},
{
"cell_type": "code",
"execution_count": null,
"execution_count": 6,
"metadata": {},
"outputs": [],
"outputs": [
{
"ename": "AttributeError",
"evalue": "'FirstClass' object has no attribute 'init'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-6-d15e7b8e3d78>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 1\u001b[0m \u001b[0meg0\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mFirstClass\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m----> 2\u001b[0;31m \u001b[0meg0\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0minit\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'init'"
]
}
],
"source": [
"eg0 = FirstClass()\n",
"eg0.init()"
@@ -163,13 +173,12 @@
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"collapsed": true
},
"execution_count": 19,
"metadata": {},
"outputs": [],
"source": [
"class FirstClass:\n",
" \"\"\"My first class\"\"\"\n",
" def __init__(self,name,symbol):\n",
" self.name = name\n",
" self.symbol = symbol"
@@ -184,10 +193,8 @@
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {
"collapsed": true
},
"execution_count": 20,
"metadata": {},
"outputs": [],
"source": [
"eg1 = FirstClass('one',1)\n",
@@ -196,7 +203,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 21,
"metadata": {},
"outputs": [
{
@@ -222,16 +229,42 @@
},
{
"cell_type": "code",
"execution_count": 9,
"metadata": {},
"execution_count": 22,
"metadata": {
"scrolled": false
},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__']"
]
},
"execution_count": 9,
"execution_count": 22,
"metadata": {},
"output_type": "execute_result"
}
@@ -241,6 +274,26 @@
]
},
{
"cell_type": "code",
"execution_count": 24,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"'My first class'"
]
},
"execution_count": 24,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"FirstClass.__doc__"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
@@ -249,16 +302,42 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 25,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__', 'name', 'symbol']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'name',\n",
" 'symbol']"
]
},
"execution_count": 10,
"execution_count": 25,
"metadata": {},
"output_type": "execute_result"
}
@@ -276,10 +355,8 @@
},
{
"cell_type": "code",
"execution_count": 11,
"metadata": {
"collapsed": true
},
"execution_count": 26,
"metadata": {},
"outputs": [],
"source": [
"class FirstClass:\n",
@@ -297,10 +374,8 @@
},
{
"cell_type": "code",
"execution_count": 12,
"metadata": {
"collapsed": true
},
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"eg1 = FirstClass('one',1)\n",
@@ -309,18 +384,18 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 28,
"metadata": {},
"outputs": [
{
"ename": "AttributeError",
"evalue": "FirstClass instance has no attribute 'name'",
"evalue": "'FirstClass' object has no attribute 'name'",
"output_type": "error",
"traceback": [
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-13-3717d682d1cf>\u001b[0m in \u001b[0;36m<module>\u001b[0;34m()\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mprint\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[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mprint\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[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: FirstClass instance has no attribute 'name'"
"\u001b[0;32m<ipython-input-28-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[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[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'FirstClass' object has no attribute 'name'"
]
}
],
@@ -338,16 +413,42 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 29,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['__doc__', '__init__', '__module__', 'n', 's']"
"['__class__',\n",
" '__delattr__',\n",
" '__dict__',\n",
" '__dir__',\n",
" '__doc__',\n",
" '__eq__',\n",
" '__format__',\n",
" '__ge__',\n",
" '__getattribute__',\n",
" '__gt__',\n",
" '__hash__',\n",
" '__init__',\n",
" '__le__',\n",
" '__lt__',\n",
" '__module__',\n",
" '__ne__',\n",
" '__new__',\n",
" '__reduce__',\n",
" '__reduce_ex__',\n",
" '__repr__',\n",
" '__setattr__',\n",
" '__sizeof__',\n",
" '__str__',\n",
" '__subclasshook__',\n",
" '__weakref__',\n",
" 'n',\n",
" 's']"
]
},
"execution_count": 14,
"execution_count": 29,
"metadata": {},
"output_type": "execute_result"
}
@@ -485,7 +586,7 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 30,
"metadata": {},
"outputs": [],
"source": [
@@ -505,18 +606,27 @@
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {
"collapsed": true
},
"outputs": [],
"execution_count": 36,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"test4\n"
]
}
],
"source": [
"eg3 = FirstClass('Three',3)"
"eg3 = FirstClass('Three',3)\n",
"eg4 = FirstClass('Four', 4)\n",
"eg4.test = 'test4'\n",
"print(eg4.test)"
]
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 35,
"metadata": {},
"outputs": [
{


+ 18
- 18
1_numpy_matplotlib_scipy_sympy/bokeh_tutorial.ipynb
File diff suppressed because it is too large
View File


+ 69
- 32
1_numpy_matplotlib_scipy_sympy/matplotlib_simple_tutorial.ipynb
File diff suppressed because it is too large
View File


+ 349
- 290
1_numpy_matplotlib_scipy_sympy/numpy_tutorial.ipynb
File diff suppressed because it is too large
View File


+ 729
- 92
1_numpy_matplotlib_scipy_sympy/sympy_tutorial.ipynb
File diff suppressed because it is too large
View File


+ 11
- 11
2_knn/knn_classification.ipynb
File diff suppressed because it is too large
View File


+ 2
- 2
3_kmeans/ClusteringAlgorithms.ipynb
File diff suppressed because it is too large
View File


+ 84
- 47
3_kmeans/k-means.ipynb
File diff suppressed because it is too large
View File


+ 26
- 20
3_kmeans/kmeans-color-vq.ipynb
File diff suppressed because it is too large
View File


+ 50
- 50
4_logistic_regression/Least_squares.ipynb
File diff suppressed because it is too large
View File


+ 13
- 13
4_logistic_regression/Logistic_regression.ipynb
File diff suppressed because it is too large
View File


+ 4
- 5
5_nn/Perceptron.ipynb View File

@@ -150,11 +150,10 @@
"name": "stdout",
"output_type": "stream",
"text": [
"update weight and bias: 1.0 3.0 0.5\n",
"update weight and bias: -0.5 2.5 0.0\n",
"update weight and bias: -2.5 2.0 -0.5\n",
"w = [-2.5, 2.0]\n",
"b = -0.5\n",
"update weight and bias: 1.0 2.5 0.5\n",
"update weight and bias: -2.0 1.5 0.0\n",
"w = [-2.0, 1.5]\n",
"b = 0.0\n",
"[ 1 1 1 1 -1 -1 -1 -1]\n",
"[1, 1, 1, 1, -1, -1, -1, -1]\n"
]


+ 19
- 19
5_nn/mlp_bp.ipynb View File

@@ -1003,15 +1003,15 @@
"epoch [ 590] L = 38.645959, acc = 0.850000\n",
"epoch [ 591] L = 38.645621, acc = 0.850000\n",
"epoch [ 592] L = 38.645284, acc = 0.850000\n",
"epoch [ 593] L = 38.644948, acc = 0.850000\n"
"epoch [ 593] L = 38.644948, acc = 0.850000\n",
"epoch [ 594] L = 38.644612, acc = 0.850000\n",
"epoch [ 595] L = 38.644278, acc = 0.850000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch [ 594] L = 38.644612, acc = 0.850000\n",
"epoch [ 595] L = 38.644278, acc = 0.850000\n",
"epoch [ 596] L = 38.643945, acc = 0.850000\n",
"epoch [ 597] L = 38.643612, acc = 0.850000\n",
"epoch [ 598] L = 38.643281, acc = 0.850000\n",
@@ -1607,13 +1607,7 @@
"epoch [1188] L = 38.527754, acc = 0.845000\n",
"epoch [1189] L = 38.527630, acc = 0.845000\n",
"epoch [1190] L = 38.527507, acc = 0.845000\n",
"epoch [1191] L = 38.527384, acc = 0.845000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch [1191] L = 38.527384, acc = 0.845000\n",
"epoch [1192] L = 38.527261, acc = 0.845000\n",
"epoch [1193] L = 38.527139, acc = 0.845000\n",
"epoch [1194] L = 38.527016, acc = 0.845000\n",
@@ -1632,7 +1626,13 @@
"epoch [1207] L = 38.525434, acc = 0.845000\n",
"epoch [1208] L = 38.525313, acc = 0.845000\n",
"epoch [1209] L = 38.525192, acc = 0.845000\n",
"epoch [1210] L = 38.525072, acc = 0.845000\n",
"epoch [1210] L = 38.525072, acc = 0.845000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch [1211] L = 38.524951, acc = 0.845000\n",
"epoch [1212] L = 38.524831, acc = 0.845000\n",
"epoch [1213] L = 38.524711, acc = 0.845000\n",
@@ -2206,13 +2206,7 @@
"epoch [1781] L = 38.470511, acc = 0.845000\n",
"epoch [1782] L = 38.470432, acc = 0.845000\n",
"epoch [1783] L = 38.470353, acc = 0.845000\n",
"epoch [1784] L = 38.470275, acc = 0.845000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch [1784] L = 38.470275, acc = 0.845000\n",
"epoch [1785] L = 38.470196, acc = 0.845000\n",
"epoch [1786] L = 38.470118, acc = 0.845000\n",
"epoch [1787] L = 38.470039, acc = 0.845000\n",
@@ -2266,7 +2260,13 @@
"epoch [1835] L = 38.466317, acc = 0.845000\n",
"epoch [1836] L = 38.466240, acc = 0.845000\n",
"epoch [1837] L = 38.466164, acc = 0.845000\n",
"epoch [1838] L = 38.466087, acc = 0.845000\n",
"epoch [1838] L = 38.466087, acc = 0.845000\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"epoch [1839] L = 38.466011, acc = 0.845000\n",
"epoch [1840] L = 38.465934, acc = 0.845000\n",
"epoch [1841] L = 38.465858, acc = 0.845000\n",


+ 15
- 13
6_pytorch/0_basic/Tensor-and-Variable.ipynb
File diff suppressed because it is too large
View File


+ 23
- 23
6_pytorch/1_NN/linear-regression-gradient-descend.ipynb View File

@@ -128,7 +128,7 @@
{
"data": {
"text/plain": [
"<torch._C.Generator at 0x7ff9fe679ab0>"
"<torch._C.Generator at 0x7fe7240c4cd0>"
]
},
"execution_count": 1,
@@ -146,7 +146,7 @@
},
{
"cell_type": "code",
"execution_count": 6,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -162,16 +162,16 @@
},
{
"cell_type": "code",
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"[<matplotlib.lines.Line2D at 0x7ff9ca7aa240>]"
"[<matplotlib.lines.Line2D at 0x7fe6d149d0f0>]"
]
},
"execution_count": 7,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
},
@@ -198,7 +198,7 @@
},
{
"cell_type": "code",
"execution_count": 8,
"execution_count": 10,
"metadata": {},
"outputs": [],
"source": [
@@ -213,7 +213,7 @@
},
{
"cell_type": "code",
"execution_count": 9,
"execution_count": 11,
"metadata": {},
"outputs": [],
"source": [
@@ -227,7 +227,7 @@
},
{
"cell_type": "code",
"execution_count": 10,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -243,16 +243,16 @@
},
{
"cell_type": "code",
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x7ff9ca7696a0>"
"<matplotlib.legend.Legend at 0x7fe6d1458128>"
]
},
"execution_count": 11,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
},
@@ -295,7 +295,7 @@
},
{
"cell_type": "code",
"execution_count": 12,
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
@@ -308,7 +308,7 @@
},
{
"cell_type": "code",
"execution_count": 13,
"execution_count": 15,
"metadata": {},
"outputs": [
{
@@ -338,7 +338,7 @@
},
{
"cell_type": "code",
"execution_count": 14,
"execution_count": 16,
"metadata": {},
"outputs": [],
"source": [
@@ -348,7 +348,7 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 17,
"metadata": {},
"outputs": [
{
@@ -368,7 +368,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 18,
"metadata": {},
"outputs": [],
"source": [
@@ -386,16 +386,16 @@
},
{
"cell_type": "code",
"execution_count": 17,
"execution_count": 19,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x7ff9ca73dac8>"
"<matplotlib.legend.Legend at 0x7fe6d14283c8>"
]
},
"execution_count": 17,
"execution_count": 19,
"metadata": {},
"output_type": "execute_result"
},
@@ -428,7 +428,7 @@
},
{
"cell_type": "code",
"execution_count": 18,
"execution_count": 20,
"metadata": {},
"outputs": [
{
@@ -472,16 +472,16 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<matplotlib.legend.Legend at 0x7ff9c86fd8d0>"
"<matplotlib.legend.Legend at 0x7fe6d163f6d8>"
]
},
"execution_count": 19,
"execution_count": 21,
"metadata": {},
"output_type": "execute_result"
},


+ 63
- 71
6_pytorch/PyTorch_quick_intro.ipynb View File

@@ -41,8 +41,8 @@
{
"data": {
"text/plain": [
"tensor([[5.0275e-38, 0.0000e+00, 5.7453e-44],\n",
" [0.0000e+00, nan, 4.5886e-41],\n",
"tensor([[1.2563e-37, 0.0000e+00, 5.7453e-44],\n",
" [0.0000e+00, nan, 4.5814e-41],\n",
" [1.3733e-14, 6.4076e+07, 2.0706e-19],\n",
" [7.3909e+22, 2.4176e-12, 1.1625e+33],\n",
" [8.9605e-01, 1.1632e+33, 5.6003e-02]])"
@@ -67,11 +67,11 @@
{
"data": {
"text/plain": [
"tensor([[0.7334, 0.3729, 0.2952],\n",
" [0.0380, 0.1581, 0.2454],\n",
" [0.6000, 0.1633, 0.7892],\n",
" [0.1951, 0.5389, 0.3149],\n",
" [0.6041, 0.8072, 0.5542]])"
"tensor([[0.7149, 0.6065, 0.8056],\n",
" [0.2450, 0.1942, 0.5305],\n",
" [0.6735, 0.7798, 0.6060],\n",
" [0.1072, 0.8325, 0.8617],\n",
" [0.5117, 0.2246, 0.4984]])"
]
},
"execution_count": 3,
@@ -128,11 +128,11 @@
{
"data": {
"text/plain": [
"tensor([[1.7112, 1.2969, 0.3289],\n",
" [0.7841, 1.0128, 0.7596],\n",
" [1.1364, 1.1541, 0.8970],\n",
" [0.8831, 0.7063, 0.3158],\n",
" [1.5160, 1.3610, 0.8437]])"
"tensor([[1.6605, 1.1155, 1.2724],\n",
" [0.6727, 0.6428, 1.0969],\n",
" [1.4898, 1.7437, 1.3258],\n",
" [0.8030, 1.5725, 1.4709],\n",
" [0.6847, 0.4828, 0.6183]])"
]
},
"execution_count": 5,
@@ -393,18 +393,18 @@
},
{
"cell_type": "code",
"execution_count": 15,
"execution_count": 6,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"tensor([[2.4446, 1.6699, 0.6242],\n",
" [0.8222, 1.1709, 1.0050],\n",
" [1.7364, 1.3174, 1.6862],\n",
" [1.0782, 1.2452, 0.6307],\n",
" [2.1201, 2.1682, 1.3979]], device='cuda:0')\n"
"tensor([[1.6605, 1.1155, 1.2724],\n",
" [0.6727, 0.6428, 1.0969],\n",
" [1.4898, 1.7437, 1.3258],\n",
" [0.8030, 1.5725, 1.4709],\n",
" [0.6847, 0.4828, 0.6183]], device='cuda:0')\n"
]
}
],
@@ -441,7 +441,7 @@
},
{
"cell_type": "code",
"execution_count": 16,
"execution_count": 8,
"metadata": {},
"outputs": [],
"source": [
@@ -450,7 +450,7 @@
},
{
"cell_type": "code",
"execution_count": 19,
"execution_count": 9,
"metadata": {
"scrolled": true
},
@@ -462,7 +462,7 @@
" [1., 1.]], requires_grad=True)"
]
},
"execution_count": 19,
"execution_count": 9,
"metadata": {},
"output_type": "execute_result"
}
@@ -475,7 +475,7 @@
},
{
"cell_type": "code",
"execution_count": 20,
"execution_count": 10,
"metadata": {
"scrolled": true
},
@@ -486,7 +486,7 @@
"tensor(4., grad_fn=<SumBackward0>)"
]
},
"execution_count": 20,
"execution_count": 10,
"metadata": {},
"output_type": "execute_result"
}
@@ -498,16 +498,16 @@
},
{
"cell_type": "code",
"execution_count": 21,
"execution_count": 11,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"<SumBackward0 at 0x7fe8cf72c908>"
"<SumBackward0 at 0x7fb610129c88>"
]
},
"execution_count": 21,
"execution_count": 11,
"metadata": {},
"output_type": "execute_result"
}
@@ -518,7 +518,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 12,
"metadata": {},
"outputs": [],
"source": [
@@ -527,7 +527,7 @@
},
{
"cell_type": "code",
"execution_count": 23,
"execution_count": 13,
"metadata": {},
"outputs": [
{
@@ -537,7 +537,7 @@
" [1., 1.]])"
]
},
"execution_count": 23,
"execution_count": 13,
"metadata": {},
"output_type": "execute_result"
}
@@ -557,7 +557,7 @@
},
{
"cell_type": "code",
"execution_count": 24,
"execution_count": 14,
"metadata": {},
"outputs": [
{
@@ -567,7 +567,7 @@
" [2., 2.]])"
]
},
"execution_count": 24,
"execution_count": 14,
"metadata": {},
"output_type": "execute_result"
}
@@ -579,7 +579,7 @@
},
{
"cell_type": "code",
"execution_count": 22,
"execution_count": 15,
"metadata": {
"scrolled": true
},
@@ -587,13 +587,11 @@
{
"data": {
"text/plain": [
"Variable containing:\n",
" 3 3\n",
" 3 3\n",
"[torch.FloatTensor of size 2x2]"
"tensor([[3., 3.],\n",
" [3., 3.]])"
]
},
"execution_count": 22,
"execution_count": 15,
"metadata": {},
"output_type": "execute_result"
}
@@ -605,7 +603,7 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 16,
"metadata": {},
"outputs": [
{
@@ -615,7 +613,7 @@
" [0., 0.]])"
]
},
"execution_count": 25,
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
@@ -656,34 +654,29 @@
},
{
"cell_type": "code",
"execution_count": 25,
"execution_count": 17,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Variable containing:\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
"[torch.FloatTensor of size 4x5]\n",
"\n"
"tensor([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403],\n",
" [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],\n",
" [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],\n",
" [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]])\n"
]
},
{
"data": {
"text/plain": [
"\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
" 0.5403 0.5403 0.5403 0.5403 0.5403\n",
"[torch.FloatTensor of size 4x5]"
"tensor([[0.5403, 0.5403, 0.5403, 0.5403, 0.5403],\n",
" [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],\n",
" [0.5403, 0.5403, 0.5403, 0.5403, 0.5403],\n",
" [0.5403, 0.5403, 0.5403, 0.5403, 0.5403]])"
]
},
"execution_count": 25,
"execution_count": 17,
"metadata": {},
"output_type": "execute_result"
}
@@ -715,7 +708,7 @@
},
{
"cell_type": "code",
"execution_count": 26,
"execution_count": 27,
"metadata": {},
"outputs": [
{
@@ -723,11 +716,11 @@
"output_type": "stream",
"text": [
"Net(\n",
" (conv1): Conv2d (1, 6, kernel_size=(5, 5), stride=(1, 1))\n",
" (conv2): Conv2d (6, 16, kernel_size=(5, 5), stride=(1, 1))\n",
" (fc1): Linear(in_features=400, out_features=120)\n",
" (fc2): Linear(in_features=120, out_features=84)\n",
" (fc3): Linear(in_features=84, out_features=10)\n",
" (conv1): Conv2d(1, 6, kernel_size=(5, 5), stride=(1, 1))\n",
" (conv2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))\n",
" (fc1): Linear(in_features=400, out_features=120, bias=True)\n",
" (fc2): Linear(in_features=120, out_features=84, bias=True)\n",
" (fc3): Linear(in_features=84, out_features=10, bias=True)\n",
")\n"
]
}
@@ -777,7 +770,7 @@
},
{
"cell_type": "code",
"execution_count": 27,
"execution_count": 28,
"metadata": {},
"outputs": [
{
@@ -795,7 +788,7 @@
},
{
"cell_type": "code",
"execution_count": 28,
"execution_count": 29,
"metadata": {},
"outputs": [
{
@@ -829,7 +822,7 @@
},
{
"cell_type": "code",
"execution_count": 29,
"execution_count": 30,
"metadata": {
"scrolled": true
},
@@ -840,7 +833,7 @@
"torch.Size([1, 10])"
]
},
"execution_count": 29,
"execution_count": 30,
"metadata": {},
"output_type": "execute_result"
}
@@ -853,7 +846,7 @@
},
{
"cell_type": "code",
"execution_count": 30,
"execution_count": 31,
"metadata": {},
"outputs": [],
"source": [
@@ -879,7 +872,7 @@
},
{
"cell_type": "code",
"execution_count": 31,
"execution_count": 39,
"metadata": {
"scrolled": true
},
@@ -887,19 +880,18 @@
{
"data": {
"text/plain": [
"Variable containing:\n",
" 28.5536\n",
"[torch.FloatTensor of size 1]"
"tensor(28.3834, grad_fn=<MseLossBackward>)"
]
},
"execution_count": 31,
"execution_count": 39,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"output = net(input)\n",
"target = Variable(t.arange(0,10)) \n",
"target = Variable(t.arange(0,10).float().unsqueeze(0)) \n",
"criterion = nn.MSELoss()\n",
"loss = criterion(output, target)\n",
"loss"


+ 4
- 4
README.md View File

@@ -1,8 +1,8 @@
# Python与机器学习
# 机器学习

本教程包含了一些使用Python来学习机器学习的notebook,通过本教程的引导来快速学习Python、Python的常用库、机器学习的理论知识与实际编程,并学习如何解决实际问题。

由于**本课程需要大量的编程练习才能取得比较好的学习效果**,因此需要认真把作业和报告完成,写作业的过程可以查阅网上的资料,但是不能直接照抄,需要自己独立思考并独立写出代码。
由于**本课程需要大量的编程练习才能取得比较好的学习效果**,因此需要认真把[作业和报告](https://gitee.com/bushuhui/machinelearning_homework)完成,写作业的过程可以查阅网上的资料,但是不能直接照抄,需要自己独立思考并独立写出代码。



@@ -47,15 +47,15 @@
- [optim/sgd](6_pytorch/1_NN/optimizer/sgd.ipynb)
- [optim/adam](6_pytorch/1_NN/optimizer/adam.ipynb)
- CNN
- 加一个基本的用法介绍
- [cnn/basic_conv](6_pytorch/2_CNN/basic_conv.ipynb)
- [cnn/minist (demo code)](./demo_code/3_CNN_MNIST.py)
- [cnn/batch-normalization](6_pytorch/2_CNN/batch-normalization.ipynb)
- [cnn/regularization](6_pytorch/2_CNN/regularization.ipynb)
- [cnn/lr-decay](6_pytorch/2_CNN/lr-decay.ipynb)
- [cnn/vgg](6_pytorch/2_CNN/vgg.ipynb)
- [cnn/googlenet](6_pytorch/2_CNN/googlenet.ipynb)
- [cnn/densenet](6_pytorch/2_CNN/densenet.ipynb)
- [cnn/resnet](6_pytorch/2_CNN/resnet.ipynb)
- [cnn/densenet](6_pytorch/2_CNN/densenet.ipynb)
- RNN
- [rnn/pytorch-rnn](6_pytorch/3_RNN/pytorch-rnn.ipynb)
- [rnn/rnn-for-image](6_pytorch/3_RNN/rnn-for-image.ipynb)


+ 1
- 0
References.md View File

@@ -24,6 +24,7 @@
* [pytorch](https://pytorch.org/)
* [tensorflow](https://www.tensorflow.org/)
* [keras](https://keras.io/)
* [bokeh](https://bokeh.pydata.org/)





BIN
tips/The Matrix Calculus You Need For Deep Learning.pdf View File


+ 150
- 0
tips/构建深度神经网络的一些实战建议.ipynb View File

@@ -0,0 +1,150 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# 构建深度神经网络的一些实战建议\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"我们将整理在训练神经网络实战过程中用到的很多有用的tips(主要基于Tensorflow平台)。一些建议可能对你非常用有用,有些可能没有太大的启发意义。一些建议可能对于你不太实用,限于个人都统一整理,大家根据个人经验、场景选择。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 常见的一些tips\n",
"\n",
"**1、使用Adam作为优化器。** Adam优化效果非常好。与传统的优化器(optimizer),如传统的梯度下降法,它应该是首选。Tensorflow实践笔记:当保存和回复模型参数时,设置AdamOptimizer之后,一定记得设置Saver,因为Adam有些state也需要恢复(即每个weight的学习率)。\n",
"\n",
"**2、采用ReLu作为非线性激活函数。** Relu训练速度非常快,简单,而且训练效果非常好,不存在梯度消失的问题。尽管sigmoid是最常见的激活函数,但其存在梯度消失的问题,随着梯度反传深度的加深,梯度传递效率大大降低。\n",
"\n",
"**3、在网络的输出层不要使用激活函数。** 这很明显,但如果你默认在每一层都设置激活函数就很容易犯这个错误,所以一定记得在输出层取消掉激活函数。\n",
"\n",
"**4、在每一层都添加偏置项(bias)。** 因为偏置项很重要,可以把一个平面转换成一个best-fitting position。比如,y=mx+b,b就是偏置项,它使得一条直线上线移动,以便找到最优的position。\n",
"\n",
"**5、使用variance-scaled 初始化。** 在Tensorflow中,就是这个接口:`tf.contrib.layers.variance_scaling_initializer()`。在我们的经验中,这种generalizes/scales比其他常用的初始化方法要好,如Gaussian,truncated normal和Xavier。一般来说,方差调整初始化方法基于每一层输入和输出的神经元(Tensorflow中默认输入神经元数目)的数目来调整随机初始化的权重的偏差。因此,在没有额外的“hacks”,如clipping或batch normalization,帮助下,可以帮助signals传递到更深的网络中。Xavier也比较类似,除非所有的层都有相似的偏差,但网络不同的层具有非常不同的形状(shapes)(在CNN中很常见),所有的层具有相同的偏差不能处理这样的场景。\n",
"\n",
"**6、Whiten(规范化,normalize)输入数据。** 训练时,减去输入数据的均值,然后除以输入数据的方差。模型的权重延伸和拓展的角度越小,网络学习更容易且速度越快。保持输入数据是零均值的(mean-centered)且具有恒大的方差,可以帮助实现这一点。对于所有的测试数据也需要执行这一点,所以一定要确保你的训练数据与真实数据高度相似。\n",
"\n",
"**7、在保留输入数据dynamic range的情况下,对输入数据进行尺度变换。** 这个操作与normalization相关,但应先于normalization执行。举个例子,数据X实际的变化范围是[0, 140000000],这可以被激活函数tanh(x)或tanh(x/c)所驯服(c是一个常量,延展曲线,在输入数据的变化范围匹配(fit)输入的动态特性,即tanh函数倾斜(激活)部分)。特别是你的输入数据根本没有一个上下限变化范围时,神经网络可以在(0,1)范围内,学得更好。\n",
"\n",
"**8、不要改变learning rate的衰减。** 学习速率的衰减在SGD中很常见,而且ADAM中也会自然调整它。如果你想直接一点点的调整学习速率,比如,在训练一段时间后,减小学习速率,误差曲线可能会突然drop一点点,随后很快恢复平整。\n",
"\n",
"**9、如果你使用的卷积层使用64或128的卷积核,这已经足够了。** 特别是对于深度网络,确实,128已经很大了。如果你已经有了一个更大的卷积核了,增加更多的卷积核,并不会带来效果的提升。\n",
"\n",
"**10、Pooling(池化)以保持转移不变性。** Pooling主要是让网络对于图片输入的“该部分”具有一个“普适的作用”。比如说,最大池化(Max pooling)可以帮助CNN对于发生旋转、偏置和特征尺度变换的输入图像,也能够具有的鲁棒性。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 神经网络诊断tips\n",
"\n",
"如果网络不学习(意思是:loss/accuracy在训练过程中不收敛,或者没有得到你预期的结果),尝试下面的tips:\n",
"\n",
"**1、过拟合。** 如果你的模型不学习,首先应该想到模型是否陷入了过拟合情况。在一个很小的数据集上训练模型,准确率达到了100%或99.99%,或者error接近于0。如果你的神经网络不能实现过拟合,说明你的网络结构存在一些严重的问题,但也可能不是很明显。如果你的模型在小数据上过拟合,但在大数据集上任然不收敛,试一试下面的suggestion。\n",
"\n",
"**2、减小学习速率。** 模型的学习速度会变慢,但模型可以到达一个更小的局部极小值,这个点因为之前的步长过大而跳不进来。\n",
"\n",
"**3、增大学习速率。** 这可以加速模型的训练,尽快收敛,帮助模型跳出局部极小值。尽管神经网络很快就收敛了,但是其结果并不是最好的,其“收敛”得到的结果可能会及其稳定,即不同训练,得到结果差别很大。(使用Adam,我们发现0.001是一个很好的初始值)。\n",
"\n",
"**4、减小(mini)batch size。** 减小batch size至1可以得到模型参数调整最细粒度的变化,你可以在Tensorboard(或其他debugging/可视化工具)中观察到,确定梯度更新是否存在问题。\n",
"\n",
"**5、去除batch normalization。** 当你把batch size减小至1时,起初BN可以帮助你发现模型是否存在梯度爆炸或梯度消失的问题。曾经,我们有一个神经网络不收敛,仅当我们去除BN之后我们才发现模型的输入在第二个iteration是变成了NaN。BN是一种锦上添花的措施,它只有在你确定你的模型不存在其他问题时才能正常发挥其强大的功能。\n",
"\n",
"**6、增大(mini-)batch size。** 增加batch size是必须的,越大的batch size可以减小梯度更新的方差,使得每一轮的梯度更新更加准确。换句话说,梯度更新会沿着准确的方向移动。但是!我们不可能无限制的增加batch size,因为计算机的物理内存是有限的。经验证明,这一点没有之前提出的两个suggestion重要,即减小batch size和去除BN。\n",
"\n",
"**7、检查你的reshape操作。** 频繁的进行reshape操作(比如,改变图像X和Y的维度)可能会破环空间的局部特性(spatially local features),使得神经网络几乎不能准确学习,因为它们必须学习错误的reshape。(自然的features变得破碎不堪,因为CNN的卷积操作高度依赖这些自然情况下的局部空间特征)同时对多个图片/channels进行reshape操作时必须非常的小心,使用numpy.stack()进行正确的对齐。\n",
"\n",
"**8、监视你loss函数的变化。** 如果使用的时一个复杂的函数作为目标函数,尽量使用L1或L2约束去去简化它。我们发现,L1对于边界没有那么敏感,当模型遇到噪声训练数据时,模型参数调整没有那么剧烈。\n",
"\n",
"**9、尽可能的进行模型训练可视化。** 如果你有可视化的工具,如matplotlib、OpenCV、Tensorboard等等,尽量可视化网络中值得scale、clipping等得变化,并保证不同参数着色策略得一致性。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## 给出一些实际得例子\n",
"\n",
"为了帮助你理解上面所讲得tips,我们这里给出我们构建了CNN网络,得到的一些loss得图(通过TensorBoard给出)来辅助说明。\n",
"\n",
"首先,模型根本没有学习情况:\n",
"\n",
"![dnn_tips_01.jpg](images/dnn_tips_01.jpeg)\n",
"\n",
"我们尝试着对value进行clipping(裁剪)操作,保证它们不会超过一个固定得bounds,得到以下结果:\n",
"\n",
"然而,loss曲线仍然不够平滑。学习速率是否任然过大呢?我们又尝试对学习速率进行衰减操作,同时采用一个训练样本进行训练:\n",
"\n",
"可以发现,大约在第300和3000 steps时,学习速率的变化情况。很明显,学习速率下降太快了。所以,需要让学习速率每一次衰减的时间间隔变得更长,这样得到的结果更好:\n",
"\n",
"![dnn_tips_02.jpg](images/dnn_tips_02.jpeg)\n",
"\n",
"可以发现,在step 2000和5000的时候,进行decay,这样得到的结果更好,但也并不非常理想,loss并没有下降至0。\n",
"\n",
"下面,我们去除掉LR衰减策略,把输入经过一个tanh函数处理,把输入数据最大最小值压缩到一个更小的范围。尽管很明显,这个操作对于小于1的值会带来一些误差,但我们人道没能实现达到过拟合的目标。\n",
"\n",
"![dnn_tips_03.jpg](images/dnn_tips_03.jpeg)\n",
"\n",
"通过实践我们发现,去掉BN之后,在1或2个iteration之后,网络的输出就变成了NaN。我们去除了BN,同时对参数初始化策略添加方差幅度进行约束(variance scaling)。这一操作使得结果发生了巨大变化。对于几个输入训练样本,模型终于过拟合了。尽管误差值超过了5,但是最终的误差降低了4个数量级。\n",
"\n",
"![dnn_tips_04.jpg](images/dnn_tips_04.jpeg)\n",
"\n",
"上面这张图得loss曲线非常平滑,可以发现它在测试数据上过拟合了,在训练数据上得loss曲线只是降到了0.01。这是因为没有对学习速率进行衰减操作,所以loss降不下去了。随后我们把学习速率降低了一个数量级,得到了更好得结果:\n",
"\n",
"![dnn_tips_05.jpg](images/dnn_tips_05.jpeg)\n",
"\n",
"![dnn_tips_06.jpg](images/dnn_tips_06.jpeg)\n",
"\n",
"可以发现,上面得结果更好了!但倘若只是衰减学习速率,而不把训练分为两个部分呢?\n",
"\n",
"通过给学习率每一个step都乘以一个衰减系数0.9995,得到的结果并不是很好:\n",
"\n",
"![dnn_tips_07.jpg](images/dnn_tips_07.jpeg)\n",
"\n",
"出现这种情况,可能是因为衰减过快了。因此我们把衰减系数变成0.999995,结果则更好一点,但最终的结果也基本类似,loss并没有继续减小。通过这一些列的实验,我们推测可能是因为BN掩盖了由于poor参数初始化导致的梯度爆炸问题。而且,对于Adam优化器来说,衰减学习速率并不能带来很大的帮助,除非是在训练快结束时进行deliberate的衰减。当使用BN时,对权值等value进行裁减只会掩盖真正的问题。我们同样可以使用tanh来处理具有high-variance的输入数据。\n",
"\n",
"我们希望上面tips可以对你有所帮助,助你掌握构建深度神经网络的一些基本方法。很多时候,仅仅一些简单的操作就能导致巨大的差别。\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## References\n",
"\n",
"* https://www.toutiao.com/a6611803073100644868"
]
}
],
"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.5.2"
},
"main_language": "python"
},
"nbformat": 4,
"nbformat_minor": 2
}

+ 21
- 2
tips/构建深度神经网络的一些实战建议.md View File

@@ -1,3 +1,22 @@
---
jupyter:
jupytext_format_version: '1.0'
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.5.2
---

# 构建深度神经网络的一些实战建议


@@ -22,7 +41,7 @@

**7、在保留输入数据dynamic range的情况下,对输入数据进行尺度变换。** 这个操作与normalization相关,但应先于normalization执行。举个例子,数据X实际的变化范围是[0, 140000000],这可以被激活函数tanh(x)或tanh(x/c)所驯服(c是一个常量,延展曲线,在输入数据的变化范围匹配(fit)输入的动态特性,即tanh函数倾斜(激活)部分)。特别是你的输入数据根本没有一个上下限变化范围时,神经网络可以在(0,1)范围内,学得更好。

**8、不要影响learning rate的衰减。** 学习速率的衰减在SGD中很常见,而且ADAM中也会自然调整它。如果你想直接一点点的调整学习速率,比如,在训练一段时间后,减小学习速率,误差曲线可能会突然drop一点点,随后很快恢复平整。
**8、不要改变learning rate的衰减。** 学习速率的衰减在SGD中很常见,而且ADAM中也会自然调整它。如果你想直接一点点的调整学习速率,比如,在训练一段时间后,减小学习速率,误差曲线可能会突然drop一点点,随后很快恢复平整。

**9、如果你使用的卷积层使用64或128的卷积核,这已经足够了。** 特别是对于深度网络,确实,128已经很大了。如果你已经有了一个更大的卷积核了,增加更多的卷积核,并不会带来效果的提升。

@@ -100,4 +119,4 @@

## References

* https://www.toutiao.com/a6611803073100644868
* https://www.toutiao.com/a6611803073100644868

Loading…
Cancel
Save