Review linear algebra and calculus essentials for ANNs

Nerd Cafe

1. Linear Algebra Essentials for ANNs

Vectors & Matrices

  • Vectors: 1D arrays (e.g., input features).

import numpy as np
x = np.array([1, 2, 3])  # Input vector
  • Matrices: 2D arrays (e.g., weights in a layer).

W = np.array([[1, 2], [3, 4]])  # Weight matrix

Python Example:

import numpy as np

# Vectors
v1 = np.array([1, 2, 3])
v2 = np.array([4, 5, 6])

# Matrices
m1 = np.array([[1, 2], [3, 4]])
m2 = np.array([[5, 6], [7, 8]])

print("Vector v1:", v1)
print("Matrix m1:\n", m1)

Output

Dot Product (Inner Product)

  • Computes weighted sums for neuron inputs:

z=Wx+bz=W⋅x+b

Output:

Key Notes:

  • Dimensions:

    • x: Shape (3,) (3 input features).

    • W: Shape (2, 3) (2 neurons, each with 3 weights).

    • b: Shape (2,) (1 bias per neuron).

    • Output z: Shape (2,) (output of 2 neurons).

  • What This Represents:

    • Simulates a dense layer in ANNs.

    • Each neuron computes:

zi=j=13Wi,j.xj+biz_{i}=\sum_{j=1}^{3}W_{i,j}.x_{j}+b_{i}

Matrix Multiplication

  • Critical for forward propagation in ANNs.

Output

Matrix Transpose

Concept: Flipping a matrix over its diagonal, swapping row and column indices.

Python Example:

Output

Special Matrices

Concept: Identity matrices (I), diagonal matrices, and their properties.

Python Example:

Output

2. Calculus Essentials

Derivatives and Gradients

Concept: The derivative measures how a function changes as its input changes. The gradient generalizes this to multiple dimensions.

Math:

f(x)=x2fˊ(x)=2xf(x)=x^{2}\Rightarrow f\acute{}(x)=2x

Python Example:

Output

Partial Derivatives

Concept: How a multi-variable function changes when only one variable changes.

Math:

f(x,y)=x2y+y3fx=2xy    and    fy=x2+3y2f(x,y)=x^{2}y+y^{3}\Rightarrow \frac{\partial f}{\partial x}=2xy\;\;and\;\;\frac{\partial f}{\partial y}=x^{2}+3y^{2}

Python Example:

Output:

The Chain Rule

Concept: Essential for backpropagation in neural networks. Allows computation of derivatives of composite functions.

Math:

If

h(x)=fog(x)=f(g(x))h(x)=fog(x)=f(g(x))

then

hˊ(x)=gˊ(x)fˊ(g(x))h\acute{}(x)=g\acute{}(x)f\acute{}(g(x))

Example

Given:

f(x)=sin(2x)    and    g(x)=e3xf(x)=sin(2x)\;\;and\;\;g(x)=e^{3x}

Then:

h(x)=fog(x)=sin(2e3x)hˊ(x)=6e3xcos(2e3x)h(x)=fog(x)=sin(2e^{3x})\Rightarrow h\acute{}(x)=6e^{3x}cos(2e^{3x})

We’ll use SymPy to define the functions and compute the derivative step by step.

Python Example:

Output

Last updated