Skip to content

Commit c9ecf98

Browse files
committed
added regularization
1 parent 968c062 commit c9ecf98

26 files changed

+16
-15
lines changed

Neural Networks/OneVsAll-Digit Recognition.ipynb

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
},
3535
{
3636
"cell_type": "code",
37-
"execution_count": 153,
37+
"execution_count": 2,
3838
"id": "aeaa3bba",
3939
"metadata": {},
4040
"outputs": [
@@ -240,7 +240,7 @@
240240
"[5 rows x 785 columns]"
241241
]
242242
},
243-
"execution_count": 153,
243+
"execution_count": 2,
244244
"metadata": {},
245245
"output_type": "execute_result"
246246
}
@@ -257,7 +257,7 @@
257257
},
258258
{
259259
"cell_type": "code",
260-
"execution_count": 5,
260+
"execution_count": 3,
261261
"id": "6c91a3f5",
262262
"metadata": {},
263263
"outputs": [
@@ -324,7 +324,7 @@
324324
},
325325
{
326326
"cell_type": "code",
327-
"execution_count": 1,
327+
"execution_count": 4,
328328
"id": "26597be5",
329329
"metadata": {},
330330
"outputs": [],
@@ -336,7 +336,7 @@
336336
},
337337
{
338338
"cell_type": "code",
339-
"execution_count": 141,
339+
"execution_count": 5,
340340
"id": "a1f43228",
341341
"metadata": {},
342342
"outputs": [
@@ -346,7 +346,7 @@
346346
"(5000, 785)"
347347
]
348348
},
349-
"execution_count": 141,
349+
"execution_count": 5,
350350
"metadata": {},
351351
"output_type": "execute_result"
352352
}
@@ -361,37 +361,38 @@
361361
},
362362
{
363363
"cell_type": "code",
364-
"execution_count": 142,
364+
"execution_count": 10,
365365
"id": "fa042365",
366366
"metadata": {},
367367
"outputs": [],
368368
"source": [
369-
"def gradDesc(theta,x,y,m):\n",
369+
"def gradDesc(theta,x,y,m,lambda_):\n",
370370
" h = sigmoid(x*theta)\n",
371371
" grad = np.transpose(x)*(h-np.transpose(y))\n",
372+
" grad[1:,:] = grad[1:,:] + (lambda_/m)*theta[1:,:]\n",
372373
" return grad/m\n",
373374
"\n",
374-
"def oneVall(x,y,alpha,itr):\n",
375+
"def oneVall(x,y,alpha,itr,lambda_):\n",
375376
" m,n = x.shape\n",
376377
" theta = np.transpose(np.matrix(np.zeros(n)))\n",
377378
" x = np.matrix(x)\n",
378379
" y = np.matrix(y)\n",
379380
" for i in range(itr):\n",
380-
" theta = theta - alpha*gradDesc(theta,x,y,m)\n",
381+
" theta = theta - alpha*gradDesc(theta,x,y,m,lambda_)\n",
381382
" return np.array(np.transpose(theta))"
382383
]
383384
},
384385
{
385386
"cell_type": "code",
386-
"execution_count": 147,
387+
"execution_count": 15,
387388
"id": "d31d0b48",
388389
"metadata": {},
389390
"outputs": [],
390391
"source": [
391392
"all_theta = np.array([np.zeros(785)])\n",
392393
"for j in range(num_labels):\n",
393394
" yIn = (y == j).astype(int)\n",
394-
" temp = oneVall(x,yIn,0.00000001,1000)\n",
395+
" temp = oneVall(x,yIn,0.00000001,1000,100)\n",
395396
" all_theta = np.append(all_theta, temp, axis=0)\n",
396397
" #all_theta = np.vstack([all_theta,temp])\n",
397398
"all_theta.shape\n",
@@ -400,7 +401,7 @@
400401
},
401402
{
402403
"cell_type": "code",
403-
"execution_count": 154,
404+
"execution_count": 16,
404405
"id": "1b610409",
405406
"metadata": {},
406407
"outputs": [
@@ -433,7 +434,7 @@
433434
},
434435
{
435436
"cell_type": "code",
436-
"execution_count": 163,
437+
"execution_count": 17,
437438
"id": "37e5c59c",
438439
"metadata": {},
439440
"outputs": [],
@@ -443,7 +444,7 @@
443444
},
444445
{
445446
"cell_type": "code",
446-
"execution_count": 164,
447+
"execution_count": 18,
447448
"id": "fbb3bb20",
448449
"metadata": {},
449450
"outputs": [

0 commit comments

Comments
 (0)