Deep (learning) like Jacques Cousteau – Part 6 – Dot products

[This article was first published on Embracing the Random | R, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

(TL;DR: Start with two vectors with equal numbers of elements. Multiply them element-wise. Sum the results. This is the dot product.)

LaTeX and MathJax warning for those viewing my feed: please view directly on website!

Kendrick Lamar 2011

Hmmm…this is a tricky one!

Uhhh…did you know that Kendrick Lamar’s stage name used to be “K.Dot”?

Moi

Last time, we learnt how to add vectors. It’s time to learn about dot products!

Today’s topic: dot products

Let’s define two vectors:

Let’s multiply these vectors element-wise. We’ll take the first elements of our vectors and multiply them:

Let’s take the second elements and multiply them:

Now add the element-wise products:

This, my friends, is the dot product of our vectors.

More generally, if we have an arbitrary vector of elements and another arbitrary vector also of elements, then the dot product is:

The dot product is equivalent to . Let’s come back to this next time when we talk about matrix multiplication.

What is that angular ‘E’ looking thing?

For anyone who doesn’t know how to read the dot product equation, let’s dissect its right-hand side!

is the uppercase form of the Greek letter ‘sigma’. In this context, means ‘sum’. So we know that we’ll need to add some things.

We have and . In an earlier post, we learnt that this refers to the th element of some vector. So we can refer to the first element of our vector as . We notice that also shares the same subscript . So we know that whenever we refer to the second element in (i.e. ), we will be referring to the second element in (i.e. ).

We notice that is next to . So we’re going to be multiplying elements of our vectors which occur in the same position, .

We see that below our uppercase sigma there is a little . We also notice that there is a little above it. These mean “Let . Keep incrementing until you reach ”.

What is ? It’s the number of elements in our vectors!

If we expand the right-hand side, we get:

This looks somewhat similar to the equation from the example earlier:

Easy! These are the mechanics of dot products.

What the hell does this all mean anyway?

For a deeper understanding of dot products (which is unfortunately beyond me right at this moment!) please refer to this video:


The entire series in the playlist is so beautifully done. They are mesmerising!

How can we perform dot products in R?

Let’s define two vectors:

x <- c(1, 2, 3)
y <- c(4, 5, 6)

We can find the dot product of these two vectors using the %*% operator:

x %*% y

##      [,1]
## [1,]   32

What does R do if we simply multiply one vector by the other?

x * y

## [1]  4 10 18

This is the element-wise product! If the dot product is simply the sum of the element-wise product, then x %*% y is equivalent to doing this:

sum(x * y)

## [1] 32

In our previous posts, R allowed us to multiply vectors of different lengths. Notice how R doesn’t allow us to calculate the dot product of vectors with different lengths:

x <- c(1, 2)
y <- c(3, 4, 5)

x %*% y

This is the exception that gets raised:

Error in x %*% y : non-conformable arguments

Conclusion

We have learnt the mechanics of calculating dot products. We can now finally move onto matrices. Ooooooh yeeeeeah.

Justin

To leave a comment for the author, please follow the link and comment on their blog: Embracing the Random | R.

R-bloggers.com offers daily e-mail updates about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.
Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)