Tensors and Vectors

Everything Prof Ng shows does follow the standard rules of matrix multiplication that you quote. If you think you have a counterexample, it just means you need to look a bit more closely to make sure you understand what you are seeing. Note that he chooses the convention that any standalone predefined vector is oriented as a column vector. So both the weights w and each input sample vector x in the case of Logistic Regression in Week 2 are column vectors of dimensions n_x x 1, where n_x is the number of input features (elements in each input sample). So when he defines the linear activation as a purely mathematical expression, it is:

z = \displaystyle \sum_{i = 1}^{n_x} w_i x_i + b

Then when he expresses that as a vector computation it becomes:

z = w^T \cdot x + b

You can see why the transpose is required there: it results in the matrix multiply being 1 x n_x dot n_x x 1, which yields a 1 x 1 or scalar result.

The hierarchy is this containment relationship:

scalars \subset vectors \subset matrices \subset arrays

A matrix is an array with 2 dimensions, but you can have arrays with more than 2 dimensions like the input images in the Logistic Regression assignment: they are m x 64 x 64 x 3 arrays with 4 dimensions and then we “flatten” them into 2D matrices. Here’s a thread which talks about how the flattening works.

One other important thing to mention is that Prof Ng also uses “elementwise” multiply operations in some cases. Those usually aren’t talked about that much in Linear Algebra math classes, but when they do they are called Hadamard Products. That is a completely different operation than the normal “dot product” style matrix multiply. Here’s a thread which discusses how to tell the difference. Prof Ng always uses “*” as the operator when he means “elementwise”.