Skip to content

Commit

Permalink
blog2
Browse files Browse the repository at this point in the history
  • Loading branch information
nveshaan committed Aug 2, 2024
1 parent 37cca4a commit e09f880
Show file tree
Hide file tree
Showing 85 changed files with 1,574 additions and 36 deletions.
2 changes: 1 addition & 1 deletion config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ colorScheme = "blowfish"
defaultAppearance = "light"
autoSwitchAppearance = true
enableSearch = true
enableCodeCopy = false
enableCodeCopy = true
disableImageOptimization = false
disableTextInHeader = false

Expand Down
134 changes: 134 additions & 0 deletions content/blog/vectorised-digit-classifier/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
---
title: "Vectorising the Digit Classifier"
date: 2024-07-30
draft: false
tags: ["machine learning"]
---

## Overview

In the previous post, I trained a neural network to classify hand-written digits. I got an accuracy of ~85% on the test data. It took more than an hour to train the model. I will now vectorise the implementation, to speed up the training process.

## Implementation

Instead of iterating through the training examples one by one, I will stack them up in a matrix `X` and one-hot encode the labels in `Y`. Then I can just multiply `X` with `W` and add `B` to get the output `y`.

Before vectorising, I multiplied the weights with the training examples and added the bias values, in the following way

{{< katex >}}

$$
\{
\begin{bmatrix}
w_{11} & w_{12} & w_{13} & w_{14}\newline
w_{21} & w_{22} & w_{23} & w_{24}\newline
w_{31} & w_{32} & w_{33} & w_{34}
\end{bmatrix}
\begin{bmatrix}
x_{11}\newline
x_{12}\newline
x_{13}\newline
x_{14}
\end{bmatrix}
+
\begin{bmatrix}
b_1\newline
b_2\newline
b_3
\end{bmatrix}
\}
$$

$$
\{
=
\begin{bmatrix}
w_{11}x_{11}+w_{12}x_{12}+w_{13}x_{13}+w_{14}x_{14}+b_1\newline
w_{21}x_{11}+w_{22}x_{12}+w_{23}x_{13}+w_{24}x_{14}+b_2\newline
w_{31}x_{11}+w_{32}x_{12}+w_{33}x_{13}+w_{34}x_{14}+b_3
\end{bmatrix}
=
\begin{bmatrix}
a_1\newline
a_2\newline
a_3
\end{bmatrix}
\}
$$

Now, I can multiply all the training examples with the weights and add the bias values, in one step

$$
\{
\begin{bmatrix}
w_{11} & w_{12} & w_{13} & w_{14}\newline
w_{21} & w_{22} & w_{23} & w_{24}\newline
w_{31} & w_{32} & w_{33} & w_{34}
\end{bmatrix}
\begin{bmatrix}
x_{11} & x_{21} & x_{31} & x_{41}\newline
x_{12} & x_{22} & x_{32} & x_{42}\newline
x_{13} & x_{23} & x_{33} & x_{43}\newline
x_{14} & x_{24} & x_{34} & x_{44}
\end{bmatrix}
+
\begin{bmatrix}
b_1\newline
b_2\newline
b_3
\end{bmatrix}
\}
$$

$$
\{
=
\begin{bmatrix}
a_{11} & a_{21} & a_{31} & a_{41}\newline
a_{12} & a_{22} & a_{32} & a_{42}\newline
a_{13} & a_{23} & a_{33} & a_{43}
\end{bmatrix}
\}
$$

In the code, I got rid of one `for` loop.

```py
for run in range(epoch):

# forward prop
Z1 = np.dot(W1, X) + B1
A1 = 1/(1+np.exp(-Z1+1e-5))

Z2 = np.dot(W2, A1) + B2
A2 = 1/(1+np.exp(-Z2+1e-5))

Z3 = np.dot(W3, A2) + B3
y = np.exp(Z3+1e-5)
y /= sum(y)

# back prop
dz3 = y - Y_one
dw3 = np.dot(dz3, A2.T)/m
db3 = sum(dz3.T)/m
da2 = np.matmul(W3.T, dz3)
dz2 = A2*(1-A2)*da2
dw2 = np.dot(dz2, A1.T)/m
db2 = sum(dz2.T)/m
da1 = np.matmul(W2.T, dz2)
dz1 = A1*(1-A1)*da1
dw1 = np.dot(dz1, X.T)/m
db1 = sum(dz1.T)/m

# update params
W3 -= alpha*dw3
B3 -= alpha*db3.reshape(-1, 1)
W2 -= alpha*dw2
B2 -= alpha*db2.reshape(-1, 1)
W1 -= alpha*dw1
B1 -= alpha*db1.reshape(-1, 1)
```

## Conclusion

In the vectorised form, the training process became 15x faster, producing similar results to the non-vectorised form.
7 changes: 5 additions & 2 deletions public/404.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.6ece09b43917cb66380f5ee610feb0c6d1b73798d171ccd7d73ac38dd7b4a5326ef0ac3f9a557ff5cc67c7d9e484f4ddd1f3f23301f277206665da75c58261e9.js"
integrity="sha512-bs4JtDkXy2Y4D17mEP6wxtG3N5jRcczX1zrDjde0pTJu8Kw/mlV/9cxnx9nkhPTd0fPyMwHydyBmZdp1xYJh6Q==" data-copy="" data-copied=""></script>



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.8b56b0fd46524eaecbfe33f576b8ba2cd2a122521a89f8229a95be0b635a7ac8e774eb1d8b8b93b32bbd677c3deb734aeea62917443f124d5bf5478859767380.js"
integrity="sha512-i1aw/UZSTq7L/jP1dri6LNKhIlIaifgimpW&#43;C2NaesjndOsdi4uTsyu9Z3w963NK7qYpF0Q/Ek1b9UeIWXZzgA==" data-copy="" data-copied=""></script>


<script src="/js/zoom.min.js"></script>
Expand Down
7 changes: 5 additions & 2 deletions public/about/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.6ece09b43917cb66380f5ee610feb0c6d1b73798d171ccd7d73ac38dd7b4a5326ef0ac3f9a557ff5cc67c7d9e484f4ddd1f3f23301f277206665da75c58261e9.js"
integrity="sha512-bs4JtDkXy2Y4D17mEP6wxtG3N5jRcczX1zrDjde0pTJu8Kw/mlV/9cxnx9nkhPTd0fPyMwHydyBmZdp1xYJh6Q==" data-copy="" data-copied=""></script>



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.8b56b0fd46524eaecbfe33f576b8ba2cd2a122521a89f8229a95be0b635a7ac8e774eb1d8b8b93b32bbd677c3deb734aeea62917443f124d5bf5478859767380.js"
integrity="sha512-i1aw/UZSTq7L/jP1dri6LNKhIlIaifgimpW&#43;C2NaesjndOsdi4uTsyu9Z3w963NK7qYpF0Q/Ek1b9UeIWXZzgA==" data-copy="" data-copied=""></script>


<script src="/js/zoom.min.js"></script>
Expand Down
7 changes: 5 additions & 2 deletions public/authors/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.6ece09b43917cb66380f5ee610feb0c6d1b73798d171ccd7d73ac38dd7b4a5326ef0ac3f9a557ff5cc67c7d9e484f4ddd1f3f23301f277206665da75c58261e9.js"
integrity="sha512-bs4JtDkXy2Y4D17mEP6wxtG3N5jRcczX1zrDjde0pTJu8Kw/mlV/9cxnx9nkhPTd0fPyMwHydyBmZdp1xYJh6Q==" data-copy="" data-copied=""></script>



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.8b56b0fd46524eaecbfe33f576b8ba2cd2a122521a89f8229a95be0b635a7ac8e774eb1d8b8b93b32bbd677c3deb734aeea62917443f124d5bf5478859767380.js"
integrity="sha512-i1aw/UZSTq7L/jP1dri6LNKhIlIaifgimpW&#43;C2NaesjndOsdi4uTsyu9Z3w963NK7qYpF0Q/Ek1b9UeIWXZzgA==" data-copy="" data-copied=""></script>


<script src="/js/zoom.min.js"></script>
Expand Down
44 changes: 42 additions & 2 deletions public/blog/digit-classifier/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.6ece09b43917cb66380f5ee610feb0c6d1b73798d171ccd7d73ac38dd7b4a5326ef0ac3f9a557ff5cc67c7d9e484f4ddd1f3f23301f277206665da75c58261e9.js"
integrity="sha512-bs4JtDkXy2Y4D17mEP6wxtG3N5jRcczX1zrDjde0pTJu8Kw/mlV/9cxnx9nkhPTd0fPyMwHydyBmZdp1xYJh6Q==" data-copy="" data-copied=""></script>



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.8b56b0fd46524eaecbfe33f576b8ba2cd2a122521a89f8229a95be0b635a7ac8e774eb1d8b8b93b32bbd677c3deb734aeea62917443f124d5bf5478859767380.js"
integrity="sha512-i1aw/UZSTq7L/jP1dri6LNKhIlIaifgimpW&#43;C2NaesjndOsdi4uTsyu9Z3w963NK7qYpF0Q/Ek1b9UeIWXZzgA==" data-copy="" data-copied=""></script>


<script src="/js/zoom.min.js"></script>
Expand Down Expand Up @@ -1027,6 +1030,43 @@ <h2 class="relative group">Results






<div class="pt-8">
<hr class="border-dotted border-neutral-300 dark:border-neutral-600" />
<div class="flex justify-between pt-3">
<span>

</span>
<span>

<a class="flex text-right group ml-3" href="/blog/vectorised-digit-classifier/">
<span class="flex flex-col">
<span
class="mt-[0.1rem] leading-6 group-hover:underline group-hover:decoration-primary-500"
>Vectorising the Digit Classifier</span
>
<span class="mt-[0.1rem] text-xs text-neutral-500 dark:text-neutral-400">

<time datetime="2024-07-30 00:00:00 &#43;0000 UTC">30 July 2024</time>

</span>
</span>
<span
class="ml-3 text-neutral-700 group-hover:text-primary-600 ltr:inline rtl:hidden dark:text-neutral dark:group-hover:text-primary-400"
>&rarr;</span
>
<span
class="mr-3 text-neutral-700 group-hover:text-primary-600 ltr:hidden rtl:inline dark:text-neutral dark:group-hover:text-primary-400"
>&larr;</span
>
</a>

</span>
</div>
</div>




Expand Down
99 changes: 97 additions & 2 deletions public/blog/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,11 @@



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.6ece09b43917cb66380f5ee610feb0c6d1b73798d171ccd7d73ac38dd7b4a5326ef0ac3f9a557ff5cc67c7d9e484f4ddd1f3f23301f277206665da75c58261e9.js"
integrity="sha512-bs4JtDkXy2Y4D17mEP6wxtG3N5jRcczX1zrDjde0pTJu8Kw/mlV/9cxnx9nkhPTd0fPyMwHydyBmZdp1xYJh6Q==" data-copy="" data-copied=""></script>



<script defer type="text/javascript" id="script-bundle" src="/js/main.bundle.min.8b56b0fd46524eaecbfe33f576b8ba2cd2a122521a89f8229a95be0b635a7ac8e774eb1d8b8b93b32bbd677c3deb734aeea62917443f124d5bf5478859767380.js"
integrity="sha512-i1aw/UZSTq7L/jP1dri6LNKhIlIaifgimpW&#43;C2NaesjndOsdi4uTsyu9Z3w963NK7qYpF0Q/Ek1b9UeIWXZzgA==" data-copy="" data-copied=""></script>


<script src="/js/zoom.min.js"></script>
Expand Down Expand Up @@ -510,6 +513,98 @@ <h1 class="mt-5 text-4xl font-extrabold text-neutral-900 dark:text-neutral">Blog










<a class="flex flex-wrap article " href="/blog/vectorised-digit-classifier/">
<div class=" mt-3 md:mt-0">
<div class="items-center text-left text-xl font-semibold">

<div class="font-bold text-xl text-neutral-800 decoration-primary-500 hover:underline hover:underline-offset-2 dark:text-neutral"
href="/blog/vectorised-digit-classifier/">Vectorising the Digit Classifier</div>



</div>
<div class="text-sm text-neutral-500 dark:text-neutral-400">




































<div class="flex flex-row flex-wrap items-center">


<time datetime="2024-07-30 00:00:00 &#43;0000 UTC">30 July 2024</time><span class="px-2 text-primary-500">&middot;</span><span>408 words</span><span class="px-2 text-primary-500">&middot;</span><span title="Reading time">2 mins</span>




</div>







</div>

</div>
</a>





















Expand Down
12 changes: 11 additions & 1 deletion public/blog/index.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,17 @@
<generator>Hugo -- gohugo.io</generator>
<language>en</language>
<copyright>© 2024 Eshaan</copyright>
<lastBuildDate>Sun, 21 Jul 2024 00:00:00 +0000</lastBuildDate><atom:link href="http://localhost:1313/blog/index.xml" rel="self" type="application/rss+xml" />
<lastBuildDate>Tue, 30 Jul 2024 00:00:00 +0000</lastBuildDate><atom:link href="http://localhost:1313/blog/index.xml" rel="self" type="application/rss+xml" />
<item>
<title>Vectorising the Digit Classifier</title>
<link>http://localhost:1313/blog/vectorised-digit-classifier/</link>
<pubDate>Tue, 30 Jul 2024 00:00:00 +0000</pubDate>

<guid>http://localhost:1313/blog/vectorised-digit-classifier/</guid>
<description>Overview #In the previous post, I trained a neural network to classify hand-written digits.</description>

</item>

<item>
<title>Coding a Neural Network from scratch</title>
<link>http://localhost:1313/blog/digit-classifier/</link>
Expand Down
Loading

0 comments on commit e09f880

Please sign in to comment.