This example uses the Jupyter Notebook and python to understand the feedforward and backpropagation methods in an ANN.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

415 lines
11 KiB

1 year ago
  1. {
  2. "cells": [
  3. {
  4. "cell_type": "markdown",
  5. "id": "77c1109a-ca7c-4005-908e-e89b22de92ff",
  6. "metadata": {},
  7. "source": [
  8. "# Exam\n",
  9. "$$ \\frac{\\partial E}{\\partial w_{jk}}= -e_j\\cdot \\sigma\\left(\\sum_i w_{ij} o_i\\right) \\left(1-\\sigma\\left(\\sum_i w_{ij} o_i\\right) \\right) o_i $$\n",
  10. "\n",
  11. "$$w_{New} = w_{old}-\\alpha \\frac{\\partial E}{\\partial w} $$"
  12. ]
  13. },
  14. {
  15. "cell_type": "markdown",
  16. "id": "9878b172-ce84-4d7d-822b-a2c0bda6b7cf",
  17. "metadata": {},
  18. "source": [
  19. "## Doing step by step"
  20. ]
  21. },
  22. {
  23. "cell_type": "code",
  24. "execution_count": 1,
  25. "id": "bafc3aff-e2eb-43cf-9d8e-6a7e252be593",
  26. "metadata": {},
  27. "outputs": [],
  28. "source": [
  29. "def af(x):\n",
  30. " sigmoid = 1/(1+np.exp(-x))\n",
  31. " return sigmoid"
  32. ]
  33. },
  34. {
  35. "cell_type": "code",
  36. "execution_count": 3,
  37. "id": "7a83cf35-172a-436c-8e68-ed6ae9e2eeba",
  38. "metadata": {},
  39. "outputs": [
  40. {
  41. "name": "stdout",
  42. "output_type": "stream",
  43. "text": [
  44. "Wih: [[ 0.34666241 0.06116554 -0.0451246 ]\n",
  45. " [-0.14782509 0.08585138 0.03574974]\n",
  46. " [ 0.32745628 -0.2354578 -0.02162094]\n",
  47. " [-0.15221498 -0.36552168 -0.24002265]]\n",
  48. "Who: [[-0.45236532 -0.1057067 -0.12838381 0.05673292]\n",
  49. " [ 0.39749455 -0.33265411 -0.09279358 0.15235334]\n",
  50. " [ 0.06774908 0.06651886 0.0243551 0.10758002]]\n"
  51. ]
  52. }
  53. ],
  54. "source": [
  55. "import numpy as np\n",
  56. "\n",
  57. "inN = 3\n",
  58. "hiN= 4\n",
  59. "outN=3\n",
  60. "lr= 0.4\n",
  61. " \n",
  62. "#weight W11 W21 W31\n",
  63. "# W12 W22 W32\n",
  64. "# .....\n",
  65. "np.random.seed(53) \n",
  66. "wih=np.random.rand(hiN, inN)-0.5\n",
  67. "who=np.random.rand(outN, hiN)-0.5\n",
  68. "print(\"Wih: \", wih)\n",
  69. "print(\"Who: \", who)"
  70. ]
  71. },
  72. {
  73. "cell_type": "markdown",
  74. "id": "8c2a8d42-b5e2-40c4-9220-50e8cc42ed7e",
  75. "metadata": {},
  76. "source": [
  77. "## Feedforward"
  78. ]
  79. },
  80. {
  81. "cell_type": "code",
  82. "execution_count": 4,
  83. "id": "c3e66fc9-a311-4d30-b312-08e6519f4069",
  84. "metadata": {},
  85. "outputs": [
  86. {
  87. "name": "stdout",
  88. "output_type": "stream",
  89. "text": [
  90. "Xh: [[ 0.11932424]\n",
  91. " [-0.0176892 ]\n",
  92. " [ 0.03732063]\n",
  93. " [-0.19060372]]\n",
  94. "Oh: [[0.52979571]\n",
  95. " [0.49557782]\n",
  96. " [0.50932908]\n",
  97. " [0.45249281]]\n",
  98. "Xo: [[-0.33176547]\n",
  99. " [ 0.06741123]\n",
  100. " [ 0.12994239]]\n",
  101. "Oo: [[0.41781112]\n",
  102. " [0.51684643]\n",
  103. " [0.53243996]]\n"
  104. ]
  105. }
  106. ],
  107. "source": [
  108. "inputList = [0.32, 0.27, 0.18]\n",
  109. "inputs = np.array(inputList, ndmin=2).T\n",
  110. "Xh = np.dot(wih, inputs)\n",
  111. "print('Xh: ', Xh)\n",
  112. "\n",
  113. "Oh = af(Xh)\n",
  114. "print('Oh:', Oh)\n",
  115. "\n",
  116. "# computing output \n",
  117. "Xo = np.dot(who, Oh)\n",
  118. "print('Xo: ', Xo)\n",
  119. "Oo = af(Xo)\n",
  120. "print('Oo: ', Oo)"
  121. ]
  122. },
  123. {
  124. "cell_type": "markdown",
  125. "id": "87b902d1-0046-425f-8ade-def73a605374",
  126. "metadata": {},
  127. "source": [
  128. "## Backpropagation"
  129. ]
  130. },
  131. {
  132. "cell_type": "code",
  133. "execution_count": 6,
  134. "id": "9703ca9c-9a8d-4f4a-bc89-f3984620ef1f",
  135. "metadata": {},
  136. "outputs": [],
  137. "source": [
  138. "inputList = [0.32, 0.27, 0.18]\n",
  139. "targetList = [0.82, 0.25, 0.44]"
  140. ]
  141. },
  142. {
  143. "cell_type": "code",
  144. "execution_count": 7,
  145. "id": "b335a7d5-33bf-4af0-930b-794c77bdc116",
  146. "metadata": {},
  147. "outputs": [],
  148. "source": [
  149. "inputs = np.array(inputList, ndmin=2).T\n",
  150. "target = np.array(targetList, ndmin=2).T\n",
  151. " \n",
  152. "#computting hidden layer\n",
  153. "Xh = np.dot(wih, inputs)\n",
  154. "Oh = af(Xh)\n",
  155. " \n",
  156. "# computing output \n",
  157. "Xo = np.dot(who, Oh)\n",
  158. "Oo = af(Xo)\n",
  159. " \n",
  160. "# Output error\n",
  161. "oe = target-Oo\n",
  162. "# E propagation\n",
  163. "hiddenE = np.dot(who.T, oe)\n",
  164. " \n",
  165. "# updating weights\n",
  166. "#who+=lr*np.dot(oe*Oo*(1-Oo), Oh.T) \n",
  167. "#wih+=lr*np.dot(hiddenE*Oh*(1-Oh), inputs.T) \n",
  168. "#print('New wih: ', wih)\n",
  169. "#print('New who: ', who)"
  170. ]
  171. },
  172. {
  173. "cell_type": "code",
  174. "execution_count": 16,
  175. "id": "0164f74a-ee41-4cf4-92d3-72dfdc85d3bc",
  176. "metadata": {},
  177. "outputs": [
  178. {
  179. "data": {
  180. "text/plain": [
  181. "array([[-0.43163327, -0.08631366, -0.10845266, 0.07443995],\n",
  182. " [ 0.38337319, -0.34586342, -0.10636942, 0.14029244],\n",
  183. " [ 0.06287227, 0.06195702, 0.01966668, 0.10341479]])"
  184. ]
  185. },
  186. "execution_count": 16,
  187. "metadata": {},
  188. "output_type": "execute_result"
  189. }
  190. ],
  191. "source": [
  192. "NewW=who-lr*np.dot(-oe*Oo*(1-Oo),Oh.T)\n",
  193. "NewW"
  194. ]
  195. },
  196. {
  197. "cell_type": "code",
  198. "execution_count": 36,
  199. "id": "becd14fd-c1bf-400e-995d-c3ffee8167f5",
  200. "metadata": {},
  201. "outputs": [
  202. {
  203. "data": {
  204. "text/plain": [
  205. "array([[-0.43163327, -0.08631366, -0.10845266, 0.07443995],\n",
  206. " [ 0.38337319, -0.34586342, -0.10636942, 0.14029244],\n",
  207. " [ 0.06287227, 0.06195702, 0.01966668, 0.10341479]])"
  208. ]
  209. },
  210. "execution_count": 36,
  211. "metadata": {},
  212. "output_type": "execute_result"
  213. }
  214. ],
  215. "source": [
  216. "newWho=who-lr*np.dot(-oe*Oo*(1-Oo), Oh.T)\n",
  217. "newWho"
  218. ]
  219. },
  220. {
  221. "cell_type": "markdown",
  222. "id": "03cba5b5-7d31-464a-b56f-04004efe67b2",
  223. "metadata": {},
  224. "source": [
  225. "## Using class"
  226. ]
  227. },
  228. {
  229. "cell_type": "code",
  230. "execution_count": 2,
  231. "id": "de8798ec-ddf2-40d2-bdd3-ed9d82059829",
  232. "metadata": {},
  233. "outputs": [],
  234. "source": [
  235. "import numpy as np\n",
  236. "\n",
  237. "\n",
  238. "class NeuralNetwork:\n",
  239. " # init method\n",
  240. " def __init__(self, inputN,hiddenN, outputN, lr):\n",
  241. " # creates a NN with three layers (input, hidden, output)\n",
  242. " # inputN - Number of input nodes\n",
  243. " # hiddenN - Number of hidden nodes\n",
  244. " self.inN=inputN\n",
  245. " self.hiN=hiddenN\n",
  246. " self.outN=outputN\n",
  247. " self.lr=lr\n",
  248. " \n",
  249. " #weight W11 W21 W31\n",
  250. " # W12 W22 W32\n",
  251. " # .....\n",
  252. " np.random.seed(53) \n",
  253. " self.wih=np.random.rand(self.hiN, self.inN)-0.5\n",
  254. " self.who=np.random.rand(self.outN,self.hiN)-0.5\n",
  255. " print(\"Wih: \", self.wih)\n",
  256. " print(\"Who: \", self.who)\n",
  257. " pass\n",
  258. " \n",
  259. " # NN computing method\n",
  260. " def feedforward(self, inputList):\n",
  261. " # computing hidden output\n",
  262. " inputs = np.array(inputList, ndmin=2).T\n",
  263. " self.Xh = np.dot(self.wih, inputs)\n",
  264. " print('Xh: ', self.Xh)\n",
  265. " self.af = lambda x:1/(1+np.exp(-x))\n",
  266. " self.Oh = self.af(self.Xh)\n",
  267. " print('Oh:', self.Oh)\n",
  268. " \n",
  269. " # computing output \n",
  270. " self.Xo = np.dot(self.who, self.Oh)\n",
  271. " print('Xo: ', self.Xo)\n",
  272. " self.Oo = self.af(self.Xo)\n",
  273. " print('Oo: ', self.Oo)\n",
  274. " pass\n",
  275. " \n",
  276. " # NN trainning method \n",
  277. " def backpropagation(self, inputList, targetList):\n",
  278. " # data\n",
  279. " lr = self.lr \n",
  280. " inputs = np.array(inputList, ndmin=2).T\n",
  281. " target = np.array(targetList, ndmin=2).T\n",
  282. " \n",
  283. " #computting hidden layer\n",
  284. " Xh = np.dot(self.wih, inputs)\n",
  285. " af = lambda x:1/(1+np.exp(-x))\n",
  286. " Oh = af(Xh)\n",
  287. " \n",
  288. " # computing output \n",
  289. " Xo = np.dot(self.who, Oh)\n",
  290. " Oo = af(Xo)\n",
  291. " \n",
  292. " # Output error\n",
  293. " oe = target-Oo\n",
  294. " # E propagation\n",
  295. " hiddenE = np.dot(self.who.T, oe)\n",
  296. " \n",
  297. " # updating weights\n",
  298. " self.who+=lr*np.dot(oe*Oo*(1-Oo), Oh.T) \n",
  299. " self.wih+=lr*np.dot(hiddenE*Oh*(1-Oh), inputs.T) \n",
  300. " return self.wih, self.who"
  301. ]
  302. },
  303. {
  304. "cell_type": "code",
  305. "execution_count": 3,
  306. "id": "e38c98a0-2edc-4733-a2e0-d911f44236a1",
  307. "metadata": {},
  308. "outputs": [
  309. {
  310. "name": "stdout",
  311. "output_type": "stream",
  312. "text": [
  313. "Wih: [[ 0.34666241 0.06116554 -0.0451246 ]\n",
  314. " [-0.14782509 0.08585138 0.03574974]\n",
  315. " [ 0.32745628 -0.2354578 -0.02162094]\n",
  316. " [-0.15221498 -0.36552168 -0.24002265]]\n",
  317. "Who: [[-0.45236532 -0.1057067 -0.12838381 0.05673292]\n",
  318. " [ 0.39749455 -0.33265411 -0.09279358 0.15235334]\n",
  319. " [ 0.06774908 0.06651886 0.0243551 0.10758002]]\n"
  320. ]
  321. }
  322. ],
  323. "source": [
  324. "NN = NeuralNetwork(3,4,3,0.4)"
  325. ]
  326. },
  327. {
  328. "cell_type": "code",
  329. "execution_count": 4,
  330. "id": "87a9faaa-d8f8-4ea7-b098-d3e8cf06fea8",
  331. "metadata": {},
  332. "outputs": [
  333. {
  334. "name": "stdout",
  335. "output_type": "stream",
  336. "text": [
  337. "Xh: [[ 0.11932424]\n",
  338. " [-0.0176892 ]\n",
  339. " [ 0.03732063]\n",
  340. " [-0.19060372]]\n",
  341. "Oh: [[0.52979571]\n",
  342. " [0.49557782]\n",
  343. " [0.50932908]\n",
  344. " [0.45249281]]\n",
  345. "Xo: [[-0.33176547]\n",
  346. " [ 0.06741123]\n",
  347. " [ 0.12994239]]\n",
  348. "Oo: [[0.41781112]\n",
  349. " [0.51684643]\n",
  350. " [0.53243996]]\n"
  351. ]
  352. }
  353. ],
  354. "source": [
  355. "NN.feedforward([0.32, 0.27, 0.18])"
  356. ]
  357. },
  358. {
  359. "cell_type": "code",
  360. "execution_count": 5,
  361. "id": "6ef426d3-3f6d-448d-9fe0-41c044a30fed",
  362. "metadata": {},
  363. "outputs": [
  364. {
  365. "data": {
  366. "text/plain": [
  367. "(array([[ 0.33727924, 0.05324849, -0.05040263],\n",
  368. " [-0.14654184, 0.08693412, 0.03647157],\n",
  369. " [ 0.32652462, -0.23624388, -0.02214499],\n",
  370. " [-0.15309598, -0.36626503, -0.24051822]]),\n",
  371. " array([[-0.43163327, -0.08631366, -0.10845266, 0.07443995],\n",
  372. " [ 0.38337319, -0.34586342, -0.10636942, 0.14029244],\n",
  373. " [ 0.06287227, 0.06195702, 0.01966668, 0.10341479]]))"
  374. ]
  375. },
  376. "execution_count": 5,
  377. "metadata": {},
  378. "output_type": "execute_result"
  379. }
  380. ],
  381. "source": [
  382. "NN.backpropagation([0.32, 0.27, 0.18], [0.82, 0.25, 0.44])"
  383. ]
  384. },
  385. {
  386. "cell_type": "code",
  387. "execution_count": null,
  388. "id": "ab4fc050-df3d-470c-aede-629357e44df5",
  389. "metadata": {},
  390. "outputs": [],
  391. "source": []
  392. }
  393. ],
  394. "metadata": {
  395. "kernelspec": {
  396. "display_name": "Python 3 (ipykernel)",
  397. "language": "python",
  398. "name": "python3"
  399. },
  400. "language_info": {
  401. "codemirror_mode": {
  402. "name": "ipython",
  403. "version": 3
  404. },
  405. "file_extension": ".py",
  406. "mimetype": "text/x-python",
  407. "name": "python",
  408. "nbconvert_exporter": "python",
  409. "pygments_lexer": "ipython3",
  410. "version": "3.8.10"
  411. }
  412. },
  413. "nbformat": 4,
  414. "nbformat_minor": 5
  415. }