You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

7-param_initialize.ipynb 15 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "metadata": {},
  6. "source": [
  7. "# 参数初始化\n",
  8. "参数初始化对模型具有较大的影响,不同的初始化方式可能会导致截然不同的结果,所幸的是很多深度学习的先驱们已经帮我们探索了各种各样的初始化方式,所以我们只需要学会如何对模型的参数进行初始化的赋值即可。"
  9. ]
  10. },
  11. {
  12. "cell_type": "markdown",
  13. "metadata": {},
  14. "source": [
  15. "PyTorch 的初始化方式并没有那么显然,如果你使用最原始的方式创建模型,那么需要定义模型中的所有参数,当然这样可以非常方便地定义每个变量的初始化方式。但是对于复杂的模型,这并不容易,而且推荐使用 Sequential 和 Module 来定义模型,所以这个时候就需要知道如何来自定义初始化方式。"
  16. ]
  17. },
  18. {
  19. "cell_type": "markdown",
  20. "metadata": {},
  21. "source": [
  22. "## 1. 使用 NumPy 来初始化\n",
  23. "因为 PyTorch 是一个非常灵活的框架,理论上能够对所有的 Tensor 进行操作,所以我们能够通过定义新的 Tensor 来初始化,直接看下面的例子"
  24. ]
  25. },
  26. {
  27. "cell_type": "code",
  28. "execution_count": 1,
  29. "metadata": {
  30. "collapsed": true
  31. },
  32. "outputs": [],
  33. "source": [
  34. "import numpy as np\n",
  35. "import torch\n",
  36. "from torch import nn"
  37. ]
  38. },
  39. {
  40. "cell_type": "code",
  41. "execution_count": 2,
  42. "metadata": {
  43. "collapsed": true
  44. },
  45. "outputs": [],
  46. "source": [
  47. "# 定义一个 Sequential 模型\n",
  48. "net1 = nn.Sequential(\n",
  49. " nn.Linear(30, 40),\n",
  50. " nn.ReLU(),\n",
  51. " nn.Linear(40, 50),\n",
  52. " nn.ReLU(),\n",
  53. " nn.Linear(50, 10)\n",
  54. ")"
  55. ]
  56. },
  57. {
  58. "cell_type": "code",
  59. "execution_count": 3,
  60. "metadata": {
  61. "collapsed": true
  62. },
  63. "outputs": [],
  64. "source": [
  65. "# 访问第一层的参数\n",
  66. "w1 = net1[0].weight\n",
  67. "b1 = net1[0].bias"
  68. ]
  69. },
  70. {
  71. "cell_type": "code",
  72. "execution_count": 4,
  73. "metadata": {},
  74. "outputs": [
  75. {
  76. "name": "stdout",
  77. "output_type": "stream",
  78. "text": [
  79. "Parameter containing:\n",
  80. "tensor([[-0.0784, 0.1559, 0.0451, ..., 0.0432, 0.0325, -0.0626],\n",
  81. " [ 0.0436, 0.0976, 0.1529, ..., -0.1601, -0.1227, -0.0831],\n",
  82. " [ 0.0890, 0.0343, 0.1744, ..., -0.0332, 0.0897, 0.0002],\n",
  83. " ...,\n",
  84. " [-0.1447, -0.0411, -0.0851, ..., 0.0117, 0.1457, 0.0585],\n",
  85. " [ 0.1642, 0.0744, -0.1118, ..., 0.0623, -0.0591, 0.0512],\n",
  86. " [-0.1610, 0.0070, 0.0184, ..., -0.1529, -0.0314, 0.1748]],\n",
  87. " requires_grad=True)\n"
  88. ]
  89. }
  90. ],
  91. "source": [
  92. "print(w1)"
  93. ]
  94. },
  95. {
  96. "cell_type": "markdown",
  97. "metadata": {},
  98. "source": [
  99. "注意,这是一个 Parameter,也就是一个特殊的 Variable,我们可以访问其 `.data`属性得到其中的数据,然后直接定义一个新的 Tensor 对其进行替换,我们可以使用 PyTorch 中的一些随机数据生成的方式,比如 `torch.randn`,如果要使用更多 PyTorch 中没有的随机化方式,可以使用 numpy"
  100. ]
  101. },
  102. {
  103. "cell_type": "code",
  104. "execution_count": 5,
  105. "metadata": {
  106. "collapsed": true
  107. },
  108. "outputs": [],
  109. "source": [
  110. "# 定义一个 Tensor 直接对其进行替换\n",
  111. "net1[0].weight.data = torch.from_numpy(np.random.uniform(3, 5, size=(40, 30)))"
  112. ]
  113. },
  114. {
  115. "cell_type": "code",
  116. "execution_count": 6,
  117. "metadata": {},
  118. "outputs": [
  119. {
  120. "name": "stdout",
  121. "output_type": "stream",
  122. "text": [
  123. "Parameter containing:\n",
  124. "tensor([[3.5493, 3.2984, 4.3041, ..., 4.5181, 3.7561, 4.5633],\n",
  125. " [4.4523, 3.7956, 3.7448, ..., 3.5031, 3.9477, 4.8617],\n",
  126. " [3.5174, 4.1082, 4.6358, ..., 3.5759, 4.5291, 3.9545],\n",
  127. " ...,\n",
  128. " [3.6757, 4.2100, 3.9763, ..., 3.2017, 3.4422, 4.0191],\n",
  129. " [3.0283, 3.8147, 3.1705, ..., 3.9442, 4.1054, 4.9491],\n",
  130. " [3.5879, 3.7237, 4.0656, ..., 3.2279, 3.1818, 4.7489]],\n",
  131. " dtype=torch.float64, requires_grad=True)\n"
  132. ]
  133. }
  134. ],
  135. "source": [
  136. "print(net1[0].weight)"
  137. ]
  138. },
  139. {
  140. "cell_type": "markdown",
  141. "metadata": {},
  142. "source": [
  143. "可以看到这个参数的值已经被改变了,也就是说已经被定义成了我们需要的初始化方式,如果模型中某一层需要我们手动去修改,那么我们可以直接用这种方式去访问,但是更多的时候是模型中相同类型的层都需要初始化成相同的方式,这个时候一种更高效的方式是使用循环去访问,比如"
  144. ]
  145. },
  146. {
  147. "cell_type": "code",
  148. "execution_count": 7,
  149. "metadata": {
  150. "collapsed": true
  151. },
  152. "outputs": [],
  153. "source": [
  154. "for layer in net1:\n",
  155. " if isinstance(layer, nn.Linear): # 判断是否是线性层\n",
  156. " param_shape = layer.weight.shape\n",
  157. " layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5, size=param_shape)) \n",
  158. " # 定义为均值为 0,方差为 0.5 的正态分布"
  159. ]
  160. },
  161. {
  162. "cell_type": "markdown",
  163. "metadata": {},
  164. "source": [
  165. "一种非常流行的初始化方式叫 Xavier,方法来源于 2010 年的一篇论文 [Understanding the difficulty of training deep feedforward neural networks](http://proceedings.mlr.press/v9/glorot10a.html),其通过数学的推到,证明了这种初始化方式可以使得每一层的输出方差是尽可能相等。这种初始化的公式为:\n",
  166. "\n",
  167. "$$\n",
  168. "w\\ \\sim \\ Uniform[- \\frac{\\sqrt{6}}{\\sqrt{n_j + n_{j+1}}}, \\frac{\\sqrt{6}}{\\sqrt{n_j + n_{j+1}}}]\n",
  169. "$$\n",
  170. "\n",
  171. "其中 $n_j$ 和 $n_{j+1}$ 表示该层的输入和输出数目,所以请尝试实现以下这种初始化方式"
  172. ]
  173. },
  174. {
  175. "cell_type": "markdown",
  176. "metadata": {},
  177. "source": [
  178. "对于 Module 的参数初始化,其实也非常简单,如果想对其中的某层进行初始化,可以直接像 Sequential 一样对其 Tensor 进行重新定义,其唯一不同的地方在于,如果要用循环的方式访问,需要介绍两个属性,children 和 modules,下面我们举例来说明"
  179. ]
  180. },
  181. {
  182. "cell_type": "code",
  183. "execution_count": 8,
  184. "metadata": {
  185. "collapsed": true
  186. },
  187. "outputs": [],
  188. "source": [
  189. "class sim_net(nn.Module):\n",
  190. " def __init__(self):\n",
  191. " super(sim_net, self).__init__()\n",
  192. " self.l1 = nn.Sequential(\n",
  193. " nn.Linear(30, 40),\n",
  194. " nn.ReLU()\n",
  195. " )\n",
  196. " \n",
  197. " self.l1[0].weight.data = torch.randn(40, 30) # 直接对某一层初始化\n",
  198. " \n",
  199. " self.l2 = nn.Sequential(\n",
  200. " nn.Linear(40, 50),\n",
  201. " nn.ReLU()\n",
  202. " )\n",
  203. " \n",
  204. " self.l3 = nn.Sequential(\n",
  205. " nn.Linear(50, 10),\n",
  206. " nn.ReLU()\n",
  207. " )\n",
  208. " \n",
  209. " def forward(self, x):\n",
  210. " x = self.l1(x)\n",
  211. " x =self.l2(x)\n",
  212. " x = self.l3(x)\n",
  213. " return x"
  214. ]
  215. },
  216. {
  217. "cell_type": "code",
  218. "execution_count": 9,
  219. "metadata": {
  220. "collapsed": true
  221. },
  222. "outputs": [],
  223. "source": [
  224. "net2 = sim_net()"
  225. ]
  226. },
  227. {
  228. "cell_type": "code",
  229. "execution_count": 10,
  230. "metadata": {},
  231. "outputs": [
  232. {
  233. "name": "stdout",
  234. "output_type": "stream",
  235. "text": [
  236. "Sequential(\n",
  237. " (0): Linear(in_features=30, out_features=40, bias=True)\n",
  238. " (1): ReLU()\n",
  239. ")\n",
  240. "Sequential(\n",
  241. " (0): Linear(in_features=40, out_features=50, bias=True)\n",
  242. " (1): ReLU()\n",
  243. ")\n",
  244. "Sequential(\n",
  245. " (0): Linear(in_features=50, out_features=10, bias=True)\n",
  246. " (1): ReLU()\n",
  247. ")\n"
  248. ]
  249. }
  250. ],
  251. "source": [
  252. "# 访问 children\n",
  253. "for i in net2.children():\n",
  254. " print(i)"
  255. ]
  256. },
  257. {
  258. "cell_type": "code",
  259. "execution_count": 11,
  260. "metadata": {},
  261. "outputs": [
  262. {
  263. "name": "stdout",
  264. "output_type": "stream",
  265. "text": [
  266. "sim_net(\n",
  267. " (l1): Sequential(\n",
  268. " (0): Linear(in_features=30, out_features=40, bias=True)\n",
  269. " (1): ReLU()\n",
  270. " )\n",
  271. " (l2): Sequential(\n",
  272. " (0): Linear(in_features=40, out_features=50, bias=True)\n",
  273. " (1): ReLU()\n",
  274. " )\n",
  275. " (l3): Sequential(\n",
  276. " (0): Linear(in_features=50, out_features=10, bias=True)\n",
  277. " (1): ReLU()\n",
  278. " )\n",
  279. ")\n",
  280. "Sequential(\n",
  281. " (0): Linear(in_features=30, out_features=40, bias=True)\n",
  282. " (1): ReLU()\n",
  283. ")\n",
  284. "Linear(in_features=30, out_features=40, bias=True)\n",
  285. "ReLU()\n",
  286. "Sequential(\n",
  287. " (0): Linear(in_features=40, out_features=50, bias=True)\n",
  288. " (1): ReLU()\n",
  289. ")\n",
  290. "Linear(in_features=40, out_features=50, bias=True)\n",
  291. "ReLU()\n",
  292. "Sequential(\n",
  293. " (0): Linear(in_features=50, out_features=10, bias=True)\n",
  294. " (1): ReLU()\n",
  295. ")\n",
  296. "Linear(in_features=50, out_features=10, bias=True)\n",
  297. "ReLU()\n"
  298. ]
  299. }
  300. ],
  301. "source": [
  302. "# 访问 modules\n",
  303. "for i in net2.modules():\n",
  304. " print(i)"
  305. ]
  306. },
  307. {
  308. "cell_type": "markdown",
  309. "metadata": {},
  310. "source": [
  311. "通过上面的例子,看到区别了吗?\n",
  312. "\n",
  313. "children 只会访问到模型定义中的第一层,因为上面的模型中定义了三个 Sequential,所以只会访问到三个 Sequential,而 modules 会访问到最后的结构,比如上面的例子,modules 不仅访问到了 Sequential,也访问到了 Sequential 里面,这就对我们做初始化非常方便,比如"
  314. ]
  315. },
  316. {
  317. "cell_type": "code",
  318. "execution_count": 12,
  319. "metadata": {
  320. "collapsed": true
  321. },
  322. "outputs": [],
  323. "source": [
  324. "for layer in net2.modules():\n",
  325. " if isinstance(layer, nn.Linear):\n",
  326. " param_shape = layer.weight.shape\n",
  327. " layer.weight.data = torch.from_numpy(np.random.normal(0, 0.5, size=param_shape)) "
  328. ]
  329. },
  330. {
  331. "cell_type": "markdown",
  332. "metadata": {},
  333. "source": [
  334. "这上面实现了和 Sequential 相同的初始化,同样非常简便"
  335. ]
  336. },
  337. {
  338. "cell_type": "markdown",
  339. "metadata": {},
  340. "source": [
  341. "## 2. `torch.nn.init`\n",
  342. "因为 PyTorch 灵活的特性,可以直接对 Tensor 进行操作从而初始化,PyTorch 也提供了初始化的函数帮助我们快速初始化,就是 `torch.nn.init`,其操作层面仍然在 Tensor 上,下面我们举例说明"
  343. ]
  344. },
  345. {
  346. "cell_type": "code",
  347. "execution_count": 13,
  348. "metadata": {
  349. "collapsed": true
  350. },
  351. "outputs": [],
  352. "source": [
  353. "from torch.nn import init"
  354. ]
  355. },
  356. {
  357. "cell_type": "code",
  358. "execution_count": 14,
  359. "metadata": {},
  360. "outputs": [
  361. {
  362. "name": "stdout",
  363. "output_type": "stream",
  364. "text": [
  365. "Parameter containing:\n",
  366. "tensor([[ 0.2725, -0.2262, -0.4229, ..., -0.2451, 0.2344, 0.1583],\n",
  367. " [ 0.1886, 0.3226, -0.5023, ..., -0.2228, 0.5089, -0.6994],\n",
  368. " [-0.4689, 0.2612, 0.3464, ..., -0.0423, -0.2999, -0.5813],\n",
  369. " ...,\n",
  370. " [ 0.4200, 0.2091, -0.3690, ..., 0.4142, 0.1120, 0.0771],\n",
  371. " [ 0.6540, 0.0475, 0.0594, ..., 0.1726, -0.2264, 0.1510],\n",
  372. " [-1.0729, -0.2862, 0.4953, ..., 0.4702, 0.5555, -0.2246]],\n",
  373. " dtype=torch.float64, requires_grad=True)\n"
  374. ]
  375. }
  376. ],
  377. "source": [
  378. "print(net1[0].weight)"
  379. ]
  380. },
  381. {
  382. "cell_type": "code",
  383. "execution_count": 16,
  384. "metadata": {},
  385. "outputs": [
  386. {
  387. "data": {
  388. "text/plain": [
  389. "Parameter containing:\n",
  390. "tensor([[ 0.1173, -0.0864, 0.1008, ..., -0.1053, 0.2642, -0.1045],\n",
  391. " [-0.0244, 0.1722, 0.1330, ..., 0.2443, -0.2385, 0.1613],\n",
  392. " [-0.1767, 0.0678, 0.1282, ..., 0.1033, -0.2423, -0.0864],\n",
  393. " ...,\n",
  394. " [-0.1673, -0.1338, -0.0839, ..., 0.0267, 0.1693, -0.2911],\n",
  395. " [ 0.2146, 0.0194, 0.2873, ..., 0.1486, 0.2775, 0.2740],\n",
  396. " [-0.0400, 0.2231, 0.0800, ..., 0.2804, 0.2121, 0.2764]],\n",
  397. " dtype=torch.float64, requires_grad=True)"
  398. ]
  399. },
  400. "execution_count": 16,
  401. "metadata": {},
  402. "output_type": "execute_result"
  403. }
  404. ],
  405. "source": [
  406. "init.xavier_uniform_(net1[0].weight) # 这就是上面我们讲过的 Xavier 初始化方法,PyTorch 直接内置了其实现"
  407. ]
  408. },
  409. {
  410. "cell_type": "code",
  411. "execution_count": 17,
  412. "metadata": {},
  413. "outputs": [
  414. {
  415. "name": "stdout",
  416. "output_type": "stream",
  417. "text": [
  418. "Parameter containing:\n",
  419. "tensor([[ 0.1173, -0.0864, 0.1008, ..., -0.1053, 0.2642, -0.1045],\n",
  420. " [-0.0244, 0.1722, 0.1330, ..., 0.2443, -0.2385, 0.1613],\n",
  421. " [-0.1767, 0.0678, 0.1282, ..., 0.1033, -0.2423, -0.0864],\n",
  422. " ...,\n",
  423. " [-0.1673, -0.1338, -0.0839, ..., 0.0267, 0.1693, -0.2911],\n",
  424. " [ 0.2146, 0.0194, 0.2873, ..., 0.1486, 0.2775, 0.2740],\n",
  425. " [-0.0400, 0.2231, 0.0800, ..., 0.2804, 0.2121, 0.2764]],\n",
  426. " dtype=torch.float64, requires_grad=True)\n"
  427. ]
  428. }
  429. ],
  430. "source": [
  431. "print(net1[0].weight)"
  432. ]
  433. },
  434. {
  435. "cell_type": "markdown",
  436. "metadata": {},
  437. "source": [
  438. "可以看到参数已经被修改了\n",
  439. "\n",
  440. "`torch.nn.init` 提供了更多的内置初始化方式,避免了重复去实现一些相同的操作。"
  441. ]
  442. },
  443. {
  444. "cell_type": "markdown",
  445. "metadata": {},
  446. "source": [
  447. "上面讲了两种初始化方式,其实它们的本质都是一样的,就是去修改某一层参数的实际值,而 `torch.nn.init` 提供了更多成熟的深度学习相关的初始化方式。\n"
  448. ]
  449. }
  450. ],
  451. "metadata": {
  452. "kernelspec": {
  453. "display_name": "Python 3 (ipykernel)",
  454. "language": "python",
  455. "name": "python3"
  456. },
  457. "language_info": {
  458. "codemirror_mode": {
  459. "name": "ipython",
  460. "version": 3
  461. },
  462. "file_extension": ".py",
  463. "mimetype": "text/x-python",
  464. "name": "python",
  465. "nbconvert_exporter": "python",
  466. "pygments_lexer": "ipython3",
  467. "version": "3.9.7"
  468. }
  469. },
  470. "nbformat": 4,
  471. "nbformat_minor": 2
  472. }

机器学习越来越多应用到飞行器、机器人等领域,其目的是利用计算机实现类似人类的智能,从而实现装备的智能化与无人化。本课程旨在引导学生掌握机器学习的基本知识、典型方法与技术,通过具体的应用案例激发学生对该学科的兴趣,鼓励学生能够从人工智能的角度来分析、解决飞行器、机器人所面临的问题和挑战。本课程主要内容包括Python编程基础,机器学习模型,无监督学习、监督学习、深度学习基础知识与实现,并学习如何利用机器学习解决实际问题,从而全面提升自我的《综合能力》。