Lightning AI Studios: Never set up a local environment again →

Log in or create a free Lightning.ai account to track your progress and access additional course materials  

2.4 Improving Code Efficiency with Linear Algebra (Parts 1-4)

Code

What we covered in this video lecture

In this video, we saw how we could replace a Python for-loop computing of a weighted sum with a linear algebra concept called dot product. Given two vectors with the same number of elements, the dot product multiplies pairs of elements and sums the results. A common use case is computing the dot product between a feature vector and a weight vector.

Now, if we have n training examples, we can compute n dot products — the dot product between each training example and the weight vector. We can accomplish this via a for-loop over the n dot products. To make this more efficient, we can use matrix-vector multiplication.

What if we have n training examples and m feature vectors — we will encounter this later when we work with multilayer perceptrons. In this case, we can use matrix multiplication to multiply two matrices, a training example matrix, and a weight matrix.

Additional resources if you want to learn more

Linear algebra is a big topic, and there is a vast number of courses and textbooks out there. An exceptionally good course is Gilbert Strang’s Linear Algebra. However, good news is that we do not have to become linear algebra experts before we can get started leveraging it for deep learning. As we have seen in this lecture, we can regard linear algebra as a means of implementing computations more efficiently.

Log in or create a free Lightning.ai account to access:

  • Quizzes
  • Completion badges
  • Progress tracking
  • Additional downloadable content
  • Additional AI education resources
  • Notifications when new units are released
  • Free cloud computing credits

Quiz: 2.4 - Part 1: From For-Loops to Dot Products

What is the dot product between the two vectors [1.2, 5.1, -4.6] and [-2.1, 3.1, 5.5]

Correct. You can confirm this by creating PyTorch tensors a and b and carrying out the computation a.dot(b).

Incorrect. Hint: Try to implement the tensors in PyTorch and use the dot method to compute the result.

Incorrect. Hint: you perhaps added the two vectors by accident.

Incorrect. Hint: Try to implement the tensors in PyTorch and use the dot method to compute the result.

Please answer all questions to proceed.

Quiz: 2.4 - Part 2: Dealing with Multiple Training Examples via Matrix Multiplication

Suppose we have a 2×3 matrix A. Can we multiply it with a vector v, i.e., Av? If yes, what dimensionality should the vector have?

Incorrect. Hint: Write the matrix and vector side by side and then look at what dimensions should match.

Incorrect. Hint: Write the matrix and vector side by side and then look at what dimensions should match.

Correct. The number of columns of the matrix on the left

Incorrect. Hint: Write the matrix and vector side by side and then look at what dimensions should match.

Incorrect. Assuming that we have a rank-2 representation of the vector (e.g., 2×1 or 1×2; only one of the two is correct), it is possible to multiply a matrix and a vector.

Please answer all questions to proceed.

Quiz: 2.4 - Part 3: Multiplying Two Matrices

Suppose we have two matrices,

A = [[1, 2], [3, 4]]

and

B = [[5, 6], [7, 8]].

If we carry out the matrix multiplication C = AB, which results in a 2×2 matrix, what is the output value in the upper right?

Correct. 1*6 + 2*8 = 22.

Incorrect. You may have used the 2nd row in matrix an instead of the first row.

Incorrect. You may have added instead of multiplied values.

Incorrect. Hint: computing the value involves a dot product.

Please answer all questions to proceed.

Quiz: 2.4 - Part 4: Broadcasting -- Computations with Unequal Tensor Shapes

Suppose we have the following tensor:

a = torch.tensor(
[[1., 2.],
[3., 4.]])

which of the following operations is invalid?

Incorrect. This operation is valid. Via broadcasting, it increments each element in the tensor by 1.

Incorrect. This operation is valid. Via broadcasting, doubles each element in the tensor.

Incorrect. This operation is valid. Via broadcasting, we add the vector [1., 2.] to the first row of a as well as the second row of [1., 2.].

Incorrect. Here, both tensors have the same shape so there should be no issue in adding them.

Correct. In this case, we have a 2×2 tensor we want to add to a 4×1 tensor. The 4×1 shape matches non of the dimensions in the 2×2 tensor, and it is also not a tensor of a single element. So, broadcasting does not work here.

Please answer all questions to proceed.
Watch Video 1

Unit 2.4

Videos
Follow along in a Lightning Studio

DL Fundamentals 2: Tensors and PyTorch

Sebastian
Launch Studio →
Questions or Feedback?

Join the Discussion