liveBook · Manning

Inner product

As explained in Deep Learning with Python, Third Edition

An inner-product operation used as a similarity score between vectors.

An inner product multiplies corresponding entries in two equally sized vectors and adds the results, producing a scalar. In tensor libraries, the same operation appears as a dot product or matmul. section 2.3.3

From vectors to attention

For vectors, implement the operation with NumPy's np.matmul, Keras's keras.ops.matmul, or Python's @ operator. In attention, use the resulting dot product as a relevance score between target and source vectors, normalize scores with softmax, and compute a weighted sum of source vectors. section 2.3.3 section 15.3.1

Keep these shape and interpretation rules in mind:

  • Require vectors to have the same number of elements; their product returns a scalar. section 2.3.3
  • Treat a matrix-vector product as a vector of inner products between the matrix rows and the vector. section 2.3.3
  • For matrix multiplication, require the first matrix's width to match the second matrix's height; the result combines row-column inner products. section 2.3.3
  • Do not assume matmul is symmetric once either operand has more than one dimension. section 2.3.3
x = np.random.random((32,))
y = np.random.random((32,))

z = np.matmul(x, y)                                    #1
z = x @ y                                               #2

Run the compact NumPy and @ forms to see a vector inner product produce a scalar. section 2.3.3

def dot_product_attention(target, source):
    scores = np.einsum("btd,bsd->bts", target, source)               #1
    scores = softmax(scores, axis=-1)
    return np.einsum("bts,bsd->btd", scores, source)                 #2
 
dot_product_attention(target, source)

Inspect batched dot products in attention: einsum creates target-source scores, softmax normalizes them, and a second einsum forms the weighted output. section 15.3.1

How Deep Learning with Python, Third Edition covers it

How deep it goes
Explains vector, matrix-vector, matrix, and higher-dimensional products with shape rules, but avoids deeper mathematical derivation.
Examples
Demonstrates NumPy matmul, Python's @ operator, naive products, einsum attention, and parameterized query-key-value projections.
What it assumes
Builds from vectors and tensor shapes, teaching compatibility before extending the operation to matrices and attention sequences.
What you can do after
Apply it via NumPy, Keras operations, Python's @ operator, or einsum-based attention implementations.
How it connects
Connects inner products to matrix multiplication, tensor shapes, softmax, attention, and weighted sums.

Also explained in

Compare how 12 books explain inner product →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Deep Learning with Python, Third Edition. The section links point to the parts of the book it draws on.