Python Encyclopedia for Academics
  • Course Outline
  • Artificial Intelligence
    • Data Science Foundation
      • Python Programming
        • Introduction and Basics
          • Variables
          • Print Function
          • Input From User
          • Data Types
          • Type Conversion
        • Operators
          • Arithmetic Operators
          • Relational Operators
          • Bitwise Operators
          • Logical Operators
          • Assignment Operators
          • Compound Operators
          • Membership Operators
          • Identity Operators
      • Numpy
        • Vectors, Matrix
        • Operations on Matrix
        • Mean, Variance, and Standard Deviation
        • Reshaping Arrays
        • Transpose and Determinant of Matrix
      • Pandas
        • Series and DataFrames
        • Slicing, Rows, and Columns
        • Operations on DataFrames
        • Different wayes to creat DataFrame
        • Read, Write Operations with CSV files
      • Matplotlib
        • Graph Basics
        • Format Strings in Plots
        • Label Parameters, Legend
        • Bar Chart, Pie Chart, Histogram, and Scatter Plot
  • Machine Learning Algorithms
    • Regression Analysis In ML
      • Regression Analysis in Machine Learning
      • Proof of Linear Regression Formulas
      • Simple Linear Regression Implementation
      • Multiple Linear Regression
      • Advertising Dataset Example
      • Bike Sharing Dataset
      • Wine Quality Dataset
      • Auto MPG Dataset
    • Classification Algorithms in ML
      • Proof of Logistic Regression
      • Simplified Mathematical Proof of SVM
      • Iris Dataset
  • Machine Learning Laboratory
    • Lab 1: Titanic Dataset
      • Predicting Survival on the Titanic with Machine Learning
    • Lab 2: Dow Jones Index Dataset
      • Dow Jones Index Predictions Using Machine Learning
    • Lab 3: Diabetes Dataset
      • Numpy
      • Pandas
      • Matplotlib
      • Simple Linear Regression
      • Simple Non-linear Regression
      • Performance Matrix
      • Preprocessing
      • Naive Bayes Classification
      • K-Nearest Neighbors (KNN) Classification
      • Decision Tree & Random Forest
      • SVM Classifier
      • Logistic Regression
      • Artificial Neural Network
      • K means Clustering
    • Lab 4: MAGIC Gamma Telescope Dataset
      • Classification in ML-MAGIC Gamma Telescope Dataset
    • Lab 5: Seoul Bike Sharing Demand Dataset
      • Regression in ML-Seoul Bike Sharing Demand Dataset
    • Lab 6: Medical Cost Personal Datasets
      • Predict Insurance Costs with Linear Regression in Python
    • Lab 6: Predict The S&P 500 Index With Machine Learning And Python
      • Predict The S&P 500 Index With Machine Learning And Python
  • Artificial Neural Networks
    • Biological Inspiration vs. Artificial Neurons
    • Review linear algebra and calculus essentials for ANNs
    • Activation Function
  • Mathematics
    • Pre-Calculus
      • Factorials
      • Roots of Polynomials
      • Complex Numbers
      • Polar Coordinates
      • Graph of a Function
    • Calculus 1
      • Limit of a Function
      • Derivative of Function
      • Critical Points
      • Indefinite Integrals
  • Calculus 2
    • 3D Coordinates and Vectors
    • Vectors and Vector Operations
    • Lines and Planes in Space (3D)
    • Partial Derivatives
    • Optimization Problems (Maxima/Minima) in Multivariable Functions
    • Gradient Vectors
  • Engineering Mathematics
    • Laplace Transform
  • Electrical & electronics Eng
    • Resistor
      • Series Resistors
      • Parallel Resistors
    • Nodal Analysis
      • Example 1
      • Example 2
    • Transient State
      • RC Circuit Equations in the s-Domain
      • RL Circuit Equations in the s-Domain
      • LC Circuit Equations in the s-Domain
      • Series RLC Circuit with DC Source
  • Computer Networking
    • Fundamental
      • IPv4 Addressing
      • Network Diagnostics
  • Cybersecurity
    • Classical Ciphers
      • Caesar Cipher
      • Affine Cipher
      • Atbash Cipher
      • Vigenère Cipher
      • Gronsfeld Cipher
      • Alberti Cipher
      • Hill Cipher
Powered by GitBook
On this page
  • 1. Linear Algebra Essentials for ANNs
  • 2. Calculus Essentials
  1. Artificial Neural Networks

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

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)

Dot Product (Inner Product)

  • Computes weighted sums for neuron inputs:

z=W⋅x+bz=W⋅x+bz=W⋅x+b
import numpy as np

# Define inputs (x), weights (W), and bias (b)
x = np.array([0.5, -1.2, 2.1])  # Input vector (3 features)
W = np.array([[0.1, -0.3, 0.5],  # Weight matrix (2 neurons x 3 inputs)
              [0.7, 0.2, -0.1]]) 
b = np.array([0.2, -0.4])        # Bias vector (1 per neuron)

# Compute z = W·x + b
z = np.dot(W, x) + b  # Matrix multiplication + broadcasted bias

print("Weighted sum (z):", z)

Output:

Weighted sum (z): [ 1.66 -0.5 ]

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}zi​=j=1∑3​Wi,j​.xj​+bi​

Matrix Multiplication

  • Critical for forward propagation in ANNs.

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]])

# Dot product
dot_product = np.dot(v1, v2)
print("Dot product of v1 and v2:", dot_product)

# Matrix multiplication
mat_product = np.matmul(m1, m2)  # or m1 @ m2
print("Matrix product of m1 and m2:\n", mat_product)

Output

Dot product of v1 and v2: 32
Matrix product of m1 and m2:
 [[19 22]
 [43 50]]

Matrix Transpose

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

Python Example:

import numpy as np

# Matrices
m1 = np.array([[1, 2], [3, 4]])

print("Original matrix:\n", m1)
print("Transposed matrix:\n", m1.T)

Output

Original matrix:
 [[1 2]
 [3 4]]
Transposed matrix:
 [[1 3]
 [2 4]]

Special Matrices

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

Python Example:

import numpy as np

identity = np.eye(3)  # 3x3 identity matrix
print("Identity matrix:\n", identity)

diagonal = np.diag([1, 2, 3])
print("Diagonal matrix:\n", diagonal)

Output

Identity matrix:
 [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]
Diagonal matrix:
 [[1 0 0]
 [0 2 0]
 [0 0 3]]

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)=x2⇒fˊ(x)=2xf(x)=x^{2}\Rightarrow f\acute{}(x)=2xf(x)=x2⇒fˊ(x)=2x

Python Example:

import numpy as np
import matplotlib.pyplot as plt

def f(x):
    return x**2

def df(x):
    return 2*x

x = np.linspace(-3, 3, 100)
plt.plot(x, f(x), label='f(x) = x²')
plt.plot(x, df(x), label="f'(x) = 2x")
plt.legend()
plt.title('Function and its Derivative')
plt.xlabel('x')
plt.ylabel('y')
plt.grid(True)
plt.show()

Output

Partial Derivatives

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

Math:

f(x,y)=x2y+y3⇒∂f∂x=2xy    and    ∂f∂y=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}f(x,y)=x2y+y3⇒∂x∂f​=2xyand∂y∂f​=x2+3y2

Python Example:

import sympy as sp

# Define the variables
x, y = sp.symbols('x y')

# Define the function
f = x**2 * y + y**3

# Compute partial derivatives
df_dx = sp.diff(f, x)
df_dy = sp.diff(f, y)

# Display the results
print("∂f/∂x =", df_dx)
print("∂f/∂y =", df_dy)

Output:

∂f/∂x = 2*x*y
∂f/∂y = x**2 + 3*y**2

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))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))hˊ(x)=gˊ(x)fˊ(g(x))

Example

Given:

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

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})h(x)=fog(x)=sin(2e3x)⇒hˊ(x)=6e3xcos(2e3x)

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

Python Example:

import sympy as sp

# Define the variable
x = sp.symbols('x')

# Define inner and outer functions
g = sp.exp(3 * x)         # g(x) = e^(3x)
f = sp.sin(2 * x)         # f(x) = sin(2x)

# Define h(x) = f(g(x)) = sin(2 * e^(3x))
h = f.subs(x, g)

# Differentiate h using chain rule automatically
h_prime = sp.diff(h, x)

# Display results
print("h(x) =", h)
print("h'(x) =", sp.simplify(h_prime))

Output

h(x) = sin(2*exp(3*x))
h'(x) = 6*exp(3*x)*cos(2*exp(3*x))

PreviousBiological Inspiration vs. Artificial NeuronsNextActivation Function

Last updated 1 month ago