๐Ÿ”ฅLimited Offer: Get 50% OFFon AI & Full Stack Courses๐Ÿ”ฅ
Back to Deep Learning Notes
Topic #14

Matrix Addition

Matrix addition combines two matrices of the same shape by adding corresponding elements. It's the simplest matrix operation โ€” and it's the exact mechanism behind adding a bias term and behind residual (skip) connections in modern architectures.

Formula

\[ (\mathbf{A} + \mathbf{B})_{ij} = A_{ij} + B_{ij} \]

Each entry of the result is simply the sum of the corresponding entries โ€” this is called an element-wise operation. \(\mathbf{A}\) and \(\mathbf{B}\) must have identical shapes (unless one is broadcastable โ€” see Broadcasting).

Numerical Example

\[ \begin{bmatrix}1 & 2\\3 & 4\end{bmatrix} + \begin{bmatrix}5 & 6\\7 & 8\end{bmatrix} = \begin{bmatrix}1+5 & 2+6\\3+7 & 4+8\end{bmatrix} = \begin{bmatrix}6 & 8\\10 & 12\end{bmatrix} \]

Code

import numpy as np
A = np.array([[1, 2], [3, 4]])
B = np.array([[5, 6], [7, 8]])
print(A + B)
# [[ 6  8]
#  [10 12]]
import torch
A = torch.tensor([[1., 2.], [3., 4.]])
B = torch.tensor([[5., 6.], [7., 8.]])
print(A + B)

Where This Shows Up in Deep Learning

  • Bias addition: every linear layer computes \(\mathbf{W}\mathbf{x} + \mathbf{b}\) โ€” the \(+\mathbf{b}\) is matrix/vector addition (broadcast across the batch).
  • Residual connections: ResNet's core idea is \(\text{output} = \mathbf{x} + F(\mathbf{x})\) โ€” the input is added directly to the transformed output, letting gradients flow through the addition unimpeded. This single operation is central to training very deep networks (see ResNet).

Common Mistakes

  • Trying to add matrices of incompatible shapes and expecting an error to explain itself clearly โ€” always check .shape first when addition fails.
  • Confusing matrix addition with matrix multiplication โ€” addition is element-wise and requires equal shapes; multiplication has completely different shape rules (see Matrix Multiplication).

Interview Relevance

Q: "What role does matrix addition play in a residual connection?" A residual block computes \(\mathbf{x} + F(\mathbf{x})\) โ€” the original input is added directly to the block's output. Because addition passes gradients through unchanged (derivative of a sum is 1), this addition is what allows gradients to flow to early layers in very deep networks, addressing vanishing gradients.

Practice Question

Given \(\mathbf{A} = \begin{bmatrix}2 & -1\\0 & 3\end{bmatrix}\) and \(\mathbf{B} = \begin{bmatrix}-2 & 1\\4 & 4\end{bmatrix}\), compute \(\mathbf{A} + \mathbf{B}\) by hand, then verify with code.

Want to go beyond the notes?

Join CodingNow 2.0's Deep Learning course โ€” live mentorship, real projects, and 100% placement support.

Enroll Now โ€” Free Demo Available

Matrix Addition โ€“ FAQs

Quick answers about learning Matrix Addition in Deep Learning.

This free note from CodingNow 2.0 explains Matrix Addition in Deep Learning โ€” concept, syntax and worked code examples you can copy, run and revise before interviews.
Yes. Every Deep Learning topic on CodingNow 2.0, including Matrix Addition, is 100% free with no signup required.
With focused practice, most students grasp Matrix Addition in 1โ€“3 days from these notes; pairing it with CodingNow 2.0's mentor-led course takes you to job-ready depth faster.
Use the code examples in this note, then ask doubts for free on the CodingNow 2.0 Community (/community) โ€” expert instructors answer within 24 hours.
WhatsApp
Call NowEnroll Now