Skip to content

Commit

Permalink
Activation Function Code
Browse files Browse the repository at this point in the history
  • Loading branch information
Alluxia-F committed Jul 25, 2017
1 parent a7c66bd commit 2c2f907
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion Chapter 9/2. Activation Function Code.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"\n",
"def softmax(x):\n",
" temp=np.exp(x)\n",
" return temp/np.sum(temp,azis=1,keepdims=True)"
" return temp/np.sum(temp,axis=1,keepdims=True)"
]
},
{
Expand All @@ -47,6 +47,55 @@
"weights_0_1=0.02*np.random.random((pixels_per_image,hidden_size))-0.01\n",
"weights_1_2=0.2*np.random.random((hidden_size,num_lables))-0.1"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"collapsed": true
},
"outputs": [],
"source": [
"for j in xrange(iterations):\n",
" correct_cnt=0\n",
" for i in xrange(len(images)/batch_size):\n",
" batch_start,batch_end=((i*batch_size),((i+1)*batch_size))\n",
" layer_0=images[batch_start:batch_end]\n",
" layer_1=tanh(np.dot(layer_0,weights_0_1))\n",
" dropout_mask=np.random.randint(2,size=layer_1.shape)\n",
" layer_1*=dropout_masl*2\n",
" layer_2=softmax(np.dot(layer_1,weights_1_1))\n",
" \n",
" for k in xrange(batch_size):\n",
" correct_cnt+=int(np.argmax(layer_2[k:k+1])==\\\n",
" np.argmax(labels[batch_start+k:batch_start+K+1]))\n",
" layer_2_delta=(labels[batch_start:batch_end]-layer_2)/(batch_size*layer_2.shape[0])\n",
" layer_1_detal=layer_2_delta.dot(weights_1_2.T)*tanh2deriv(layer_1)\n",
" layer_1_delta*=dropout_mask\n",
" \n",
" weights_1_2+=alpha*layer_1.T.dot(layer_2_delta)\n",
" weights_0_1+=alpha*layer_0.T.dot(layer_1_delta)\n",
" \n",
" test_correct_cnt=0\n",
" \n",
" for i in xrange(len(test_images)):\n",
" layer_0=test_images[i:i+1]\n",
" layer_1=relu(np.dot(layer_0,weights_0_1))\n",
" layer_2=np.dot(layer_1,weights_1_2)\n",
" \n",
" test_correct_cnt+=int(np.argmax(layer_2)==np.argmax(test_labels[i:i+1]))\n",
" \n",
" if(j%10==0):\n",
" sys.stdout.write('\\n'+\\\n",
" 'I:'+str(j)+\\\n",
" 'Test-Acc:'+str(test_correct_cnt/float(len(test_images)))+\\\n",
" 'Train-Acc:'+str(correct_cnt/float((len(images)))))\n",
" \n",
" \n",
" \n",
" \n",
" "
]
}
],
"metadata": {
Expand Down

0 comments on commit 2c2f907

Please sign in to comment.