Artificial Intelligence with PHP
  • Getting Started
    • Introduction
    • Audience
    • How to Read This Book
    • Glossary
    • Contributors
    • Resources
    • Changelog
  • Artificial Intelligence
    • Introduction
    • Overview of AI
      • History of AI
      • How Does AI Work?
      • Structure of AI
      • Will AI Take Over the World?
      • Types of AI
        • Limited Memory AI
        • Reactive AI
        • Theory of Mind AI
        • Self-Aware AI
    • AI Capabilities in PHP
      • Introduction to LLM Agents PHP SDK
      • Overview of AI Libraries in PHP
    • AI Agents
      • Introduction to AI Agents
      • Structure of AI Agent
      • Components of AI Agents
      • Types of AI Agents
      • AI Agent Architecture
      • AI Agent Environment
      • Application of Agents in AI
      • Challenges in AI Agent Development
      • Future of AI Agents
      • Turing Test in AI
      • LLM AI Agents
        • Introduction to LLM AI Agents
        • Implementation in PHP
          • Sales Analyst Agent
          • Site Status Checker Agent
    • Theoretical Foundations of AI
      • Introduction to Theoretical Foundations of AI
      • Problem Solving in AI
        • Introduction
        • Types of Search Algorithms
          • Comparison of Search Algorithms
          • Informed (Heuristic) Search
            • Global Search
              • Beam Search
              • Greedy Search
              • Iterative Deepening A* Search
              • A* Search
                • A* Graph Search
                • A* Graph vs A* Tree Search
                • A* Tree Search
            • Local Search
              • Hill Climbing Algorithm
                • Introduction
                • Best Practices and Optimization
                • Practical Applications
                • Implementation in PHP
              • Simulated Annealing Search
              • Local Beam Search
              • Genetic Algorithms
              • Tabu Search
          • Uninformed (Blind) Search
            • Global Search
              • Bidirectional Search (BDS)
              • Breadth-First Search (BFS)
              • Depth-First Search (DFS)
              • Iterative Deepening Depth-First Search (IDDFS)
              • Uniform Cost Search (UCS)
            • Local Search
              • Depth-Limited Search (DLS)
              • Random Walk Search (RWS)
          • Adversarial Search
          • Means-Ends Analysis
      • Knowledge & Uncertainty in AI
        • Knowledge-Based Agents
        • Knowledge Representation
          • Introduction
          • Approaches to KR in AI
          • The KR Cycle in AI
          • Types of Knowledge in AI
          • KR Techniques
            • Logical Representation
            • Semantic Network Representation
            • Frame Representation
            • Production Rules
        • Reasoning in AI
        • Uncertain Knowledge Representation
        • The Wumpus World
        • Applications and Challenges
      • Cybernetics and AI
      • Philosophical and Ethical Foundations of AI
    • Mathematics for AI
      • Computational Theory in AI
      • Logic and Reasoning
        • Classification of Logics
        • Formal Logic
          • Propositional Logic
            • Basics of Propositional Logic
            • Implementation in PHP
          • Predicate Logic
            • Basics of Predicate Logic
            • Implementation in PHP
          • Second-order and Higher-order Logic
          • Modal Logic
          • Temporal Logic
        • Informal Logic
        • Semi-formal Logic
      • Set Theory and Discrete Mathematics
      • Decision Making in AI
    • Key Application of AI
      • AI in Astronomy
      • AI in Agriculture
      • AI in Automotive Industry
      • AI in Data Security
      • AI in Dating
      • AI in E-commerce
      • AI in Education
      • AI in Entertainment
      • AI in Finance
      • AI in Gaming
      • AI in Healthcare
      • AI in Robotics
      • AI in Social Media
      • AI in Software Development
      • AI in Adult Entertainment
      • AI in Criminal Justice
      • AI in Criminal World
      • AI in Military Domain
      • AI in Terrorist Activities
      • AI in Transforming Our World
      • AI in Travel and Transport
    • Practice
  • Machine Learning
    • Introduction
    • Overview of ML
      • History of ML
        • Origins and Early Concepts
        • 19th Century
        • 20th Century
        • 21st Century
        • Coming Years
      • Key Terms and Principles
      • Machine Learning Life Cycle
      • Problems and Challenges
    • ML Capabilities in PHP
      • Overview of ML Libraries in PHP
      • Configuring an Environment for PHP
        • Direct Installation
        • Using Docker
        • Additional Notes
      • Introduction to PHP-ML
      • Introduction to Rubix ML
    • Mathematics for ML
      • Linear Algebra
        • Scalars
          • Definition and Operations
          • Scalars with PHP
        • Vectors
          • Definition and Operations
          • Vectors in Machine Learning
          • Vectors with PHP
        • Matrices
          • Definition and Types
          • Matrix Operations
          • Determinant of a Matrix
          • Inverse Matrices
          • Cofactor Matrices
          • Adjugate Matrices
          • Matrices in Machine Learning
          • Matrices with PHP
        • Tensors
          • Definition of Tensors
          • Tensor Properties
            • Tensor Types
            • Tensor Dimension
            • Tensor Rank
            • Tensor Shape
          • Tensor Operations
          • Practical Applications
          • Tensors in Machine Learning
          • Tensors with PHP
        • Linear Transformations
          • Introduction
          • LT with PHP
          • LT Role in Neural Networks
        • Eigenvalues and Eigenvectors
        • Norms and Distances
        • Linear Algebra in Optimization
      • Calculus
      • Probability and Statistics
      • Information Theory
      • Optimization Techniques
      • Graph Theory and Networks
      • Discrete Mathematics and Combinatorics
      • Advanced Topics
    • Data Fundamentals
      • Data Types and Formats
        • Data Types
        • Structured Data Formats
        • Unstructured Data Formats
        • Implementation with PHP
      • General Data Processing
        • Introduction
        • Storage and Management
          • Data Security and Privacy
          • Data Serialization and Deserialization in PHP
          • Data Versioning and Management
          • Database Systems for AI
          • Efficient Data Storage Techniques
          • Optimizing Data Retrieval for AI Algorithms
          • Big Data Considerations
            • Introduction
            • Big Data Techniques in PHP
      • ML Data Processing
        • Introduction
        • Types of Data in ML
        • Stages of Data Processing
          • Data Acquisition
            • Data Collection
            • Ethical Considerations in Data Preparation
          • Data Cleaning
            • Data Cleaning Examples
            • Data Cleaning Types
            • Implementation with PHP
          • Data Transformation
            • Data Transformation Examples
            • Data Transformation Types
            • Implementation with PHP ?..
          • Data Integration
          • Data Reduction
          • Data Validation and Testing
            • Data Splitting and Sampling
          • Data Representation
            • Data Structures in PHP
            • Data Visualization Techniques
          • Typical Problems with Data
    • ML Algorithms
      • Classification of ML Algorithms
        • By Methods Used
        • By Learning Types
        • By Tasks Resolved
        • By Feature Types
        • By Model Depth
      • Supervised Learning
        • Regression
          • Linear Regression
            • Types of Linear Regression
            • Finding Best Fit Line
            • Gradient Descent
            • Assumptions of Linear Regression
            • Evaluation Metrics for Linear Regression
            • How It Works by Math
            • Implementation in PHP
              • Multiple Linear Regression
              • Simple Linear Regression
          • Polynomial Regression
            • Introduction
            • Implementation in PHP
          • Support Vector Regression
        • Classification
        • Recommendation Systems
          • Matrix Factorization
          • User-Based Collaborative Filtering
      • Unsupervised Learning
        • Clustering
        • Dimension Reduction
        • Search and Optimization
        • Recommendation Systems
          • Item-Based Collaborative Filtering
          • Popularity-Based Recommendations
      • Semi-Supervised Learning
        • Regression
        • Classification
        • Clustering
      • Reinforcement Learning
      • Distributed Learning
    • Integrating ML into Web
      • Open-Source Projects
      • Introduction to EasyAI-PHP
    • Key Applications of ML
    • Practice
  • Neural Networks
    • Introduction
    • Overview of NN
      • History of NN
      • Basic Components of NN
        • Activation Functions
        • Connections and Weights
        • Inputs
        • Layers
        • Neurons
      • Problems and Challenges
      • How NN Works
    • NN Capabilities in PHP
    • Mathematics for NN
    • Types of NN
      • Classification of NN Types
      • Linear vs Non-Linear Problems in NN
      • Basic NN
        • Simple Perceptron
        • Implementation in PHP
          • Simple Perceptron with Libraries
          • Simple Perceptron with Pure PHP
      • NN with Hidden Layers
      • Deep Learning
      • Bayesian Neural Networks
      • Convolutional Neural Networks (CNN)
      • Recurrent Neural Networks (RNN)
    • Integrating NN into Web
    • Key Applications of NN
    • Practice
  • Natural Language Processing
    • Introduction
    • Overview of NLP
      • History of NLP
        • Ancient Times
        • Medieval Period
        • 15th-16th Century
        • 17th-18th Century
        • 19th Century
        • 20th Century
        • 21st Century
        • Coming Years
      • NLP and Text
      • Key Concepts in NLP
      • Common Challenges in NLP
      • Machine Learning Role in NLP
    • NLP Capabilities in PHP
      • Overview of NLP Libraries in PHP
      • Challenges in NLP with PHP
    • Mathematics for NLP
    • NLP Techniques
      • Basic Text Processing with PHP
      • NLP Workflow
      • Popular Tools and Frameworks for NLP
      • Techniques and Algorithms in NLP
        • Basic NLP Techniques
        • Advanced NLP Techniques
      • Advanced NLP Topics
    • Integrating NLP into Web
    • Key Applications of NLP
    • Practice
  • Computer Vision
    • Introduction
  • Overview of CV
    • History of CV
    • Common Use Cases
  • CV Capabilities in PHP
  • Mathematics for CV
  • CV Techniques
  • Integrating CV into Web
  • Key Applications of CV
  • Practice
  • Robotics
    • Introduction
  • Overview of Robotics
    • History and Evolution of Robotics
    • Core Components
      • Sensors (Perception)
      • Actuators (Action)
      • Controllers (Processing and Logic)
    • The Role of AI in Robotics
      • Object Detection and Recognition
      • Path Planning and Navigation
      • Decision Making and Learning
  • Robotics Capabilities in PHP
  • Mathematics for Robotics
  • Building Robotics
  • Integration Robotics into Web
  • Key Applications of Robotics
  • Practice
  • Expert Systems
    • Introduction
    • Overview of ES
      • History of ES
        • Origins and Early ES
        • Milestones in the Evolution of ES
        • Expert Systems in Modern AI
      • Core Components and Architecture
      • Challenges and Limitations
      • Future Trends
    • ES Capabilities in PHP
    • Mathematics for ES
    • Building ES
      • Knowledge Representation Approaches
      • Inference Mechanisms
      • Best Practices for Knowledge Base Design and Inference
    • Integration ES into Web
    • Key Applications of ES
    • Practice
  • Cognitive Computing
    • Introduction
    • Overview of CC
      • History of CC
      • Differences Between CC and AI
    • CC Compatibilities in PHP
    • Mathematics for CC
    • Building CC
      • Practical Implementation
    • Integration CC into Web
    • Key Applications of CC
    • Practice
  • AI Ethics and Safety
    • Introduction
    • Overview of AI Ethics
      • Core Principles of AI Ethics
      • Responsible AI Development
      • Looking Ahead: Ethical AI Governance
    • Building Ethics & Safety AI
      • Fairness, Bias, and Transparency
        • Bias in AI Models
        • Model Transparency and Explainability
        • Auditing, Testing, and Continuous Monitoring
      • Privacy and Security in AI
        • Data Privacy and Consent
        • Safety Mechanisms in AI Integration
        • Preventing and Handling AI Misuse
      • Ensuring AI Accountability
        • Ethical AI in Decision Making
        • Regulations & Compliance
        • AI Risk Assessment
    • Key Applications of AI Ethics
    • Practice
  • Epilog
    • Summing-up
Powered by GitBook
On this page
  • Compatibility Conditions and Properties
  • Matrix Operations
  • Addition and Subtraction
  • Scalar Multiplication
  • Matrix Multiplication
  • Transposition
  1. Machine Learning
  2. Mathematics for ML
  3. Linear Algebra
  4. Matrices

Matrix Operations

Compatibility Conditions and Properties

Understanding the compatibility conditions and properties of matrix operations is crucial in machine learning, especially when dealing with neural networks and other complex models.

Compatibility Conditions

Matrix operations have specific requirements for the dimensions of the matrices involved. This is particularly important for matrix multiplication.

Matrix Operations

Matrix operations are fundamental to many machine learning algorithms and techniques. Understanding these operations is crucial for implementing and optimizing ML models efficiently.

Properties of Matrix Operations

Understanding these properties helps in optimizing computations and designing efficient algorithms.

1. Non-commutativity of Matrix Multiplication

Unlike scalar multiplication, matrix multiplication is not commutative. In general, AB≠BAAB ≠ BAAB=BA.

Example: A=[12]A = \begin{bmatrix} 1 & 2 \end{bmatrix}A=[1​2​], B=[563478]B = \begin{bmatrix} 5 & 6 \\ 3 & 4 \\ 7 & 8 \end{bmatrix}B=​537​648​​

AB=[1922]≠BA=[233443503146]AB = \begin{bmatrix} 19 & 22 \end{bmatrix} \neq BA = \begin{bmatrix} 23 & 34 \\ 43 & 50 \\ 31 & 46 \end{bmatrix}AB=[19​22​]=BA=​234331​345046​​\

ML Application: The order of operations matters in neural network computations. For instance, applying activation functions before or after matrix multiplication can lead to different results.

2. Associativity of Matrix Multiplication

(AB)C=A(BC)(AB)C = A(BC)(AB)C=A(BC) for matrices with compatible dimensions.

Example: A(2×2)∗(B(2×2)∗C(2×1))=(A(2×2)∗B(2×2))∗C(2×1)A (2×2) * (B (2×2) * C (2×1)) = (A (2×2) * B (2×2)) * C (2×1)A(2×2)∗(B(2×2)∗C(2×1))=(A(2×2)∗B(2×2))∗C(2×1)

ML Application: This property allows for optimizing computations in deep neural networks by grouping operations efficiently.

3. Distributivity of Matrix Multiplication over Addition

A(B+C)=AB+ACA(B + C) = AB + ACA(B+C)=AB+AC and (A+B)C=AC+BC(A + B)C = AC + BC(A+B)C=AC+BC for matrices with compatible dimensions.

Example: A∗(B+C)=(A∗B)+(A∗C)A * (B + C) = (A * B) + (A * C)A∗(B+C)=(A∗B)+(A∗C)

ML Application: This property is useful in backpropagation when computing gradients with respect to multiple parameters.

Addition and Subtraction

Matrix addition and subtraction are performed element-wise between matrices of the same dimensions.

Examples (2 x 2 matrices):

Given: A=[1234],B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}A=[13​24​],B=[57​68​] then A+B=[1+52+63+74+8]=[681012]A + B = \begin{bmatrix} 1 + 5 & 2 + 6 \\ 3 + 7 & 4 + 8 \end{bmatrix} = \begin{bmatrix} 6 & 8 \\ 10 & 12 \end{bmatrix}A+B=[1+53+7​2+64+8​]=[610​812​]

Step by step explanation

Step 1: Add corresponding elements

  • (1,1):1+5=6(1,1): 1 + 5 = 6(1,1):1+5=6

  • (1,2):2+6=8(1,2): 2 + 6 = 8(1,2):2+6=8

  • (2,1):3+7=10(2,1): 3 + 7 = 10(2,1):3+7=10

  • (2,2):4+8=12(2,2): 4 + 8 = 12(2,2):4+8=12

Step 2: Write the result A+B=[681012]A + B = \begin{bmatrix} 6 & 8 \\ 10 & 12 \end{bmatrix}A+B=[610​812​]

Given: A=[1278],B=[5634]A = \begin{bmatrix} 1 & 2 \\ 7 & 8 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 3 & 4 \end{bmatrix}A=[17​28​],B=[53​64​] then A−B=[1−52−67−38−4]=[−4−244]A - B = \begin{bmatrix} 1 - 5 & 2 - 6 \\ 7 - 3 & 8 - 4 \end{bmatrix} = \begin{bmatrix} -4 & -2 \\ 4 & 4 \end{bmatrix}A−B=[1−57−3​2−68−4​]=[−44​−24​]

Step by step explanation

Step 1: Add corresponding elements

  • (1,1):1−5=−4(1,1): 1 - 5 = -4(1,1):1−5=−4

  • (1,2):2−6=−2(1,2): 2 - 6 = -2(1,2):2−6=−2

  • (2,1):7−3=4(2,1): 7 - 3 = 4(2,1):7−3=4

  • (2,2):8−4=4(2,2): 8 -4 = 4(2,2):8−4=4

Step 2: Write the result A−B=[−4−244]A - B = \begin{bmatrix} -4 & -2 \\ 4 & 4 \end{bmatrix}A−B=[−44​−24​]

Example in ML: Updating weights in neural networks. In gradient descent, we update parameters by subtracting the gradient multiplied by the learning rate:

W=[0.10.20.30.4],gradient=[0.010.020.030.04],learning rate=0.1W = \begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix}, \quad \text{gradient} = \begin{bmatrix} 0.01 & 0.02 \\ 0.03 & 0.04 \end{bmatrix}, \quad \text{learning rate} = 0.1W=[0.10.3​0.20.4​],gradient=[0.010.03​0.020.04​],learning rate=0.1

Wnew=W−(learning rate×gradient)W_{\text{new}} = W - (\text{learning rate} \times \text{gradient})Wnew​=W−(learning rate×gradient)

Wnew=[0.10.20.30.4]−0.1⋅[0.010.020.030.04]=[0.0990.1980.2970.396]W_{\text{new}} = \begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix} - 0.1 \cdot \begin{bmatrix} 0.01 & 0.02 \\ 0.03 & 0.04 \end{bmatrix} = \begin{bmatrix} 0.099 & 0.198 \\ 0.297 & 0.396 \end{bmatrix}Wnew​=[0.10.3​0.20.4​]−0.1⋅[0.010.03​0.020.04​]=[0.0990.297​0.1980.396​]

Step by step explanation

Step 1: Multiply gradient by learning rate

0.1⋅[0.010.020.030.04]=[0.0010.0020.0030.004]0.1 \cdot \begin{bmatrix} 0.01 & 0.02 \\ 0.03 & 0.04 \end{bmatrix} = \begin{bmatrix} 0.001 & 0.002 \\ 0.003 & 0.004 \end{bmatrix}0.1⋅[0.010.03​0.020.04​]=[0.0010.003​0.0020.004​]

Step 2: Subtract from

[0.10.20.30.4]−[0.0010.0020.0030.004]=[0.0990.1980.2970.396]\begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix} - \begin{bmatrix} 0.001 & 0.002 \\ 0.003 & 0.004 \end{bmatrix} = \begin{bmatrix} 0.099 & 0.198 \\ 0.297 & 0.396 \end{bmatrix}[0.10.3​0.20.4​]−[0.0010.003​0.0020.004​]=[0.0990.297​0.1980.396​]

Scalar Multiplication

Scalar multiplication involves multiplying each element of a matrix by a scalar value.

Example (3x3 matrix):

Let's multiply a 3x3 matrix by a scalar:

Given: k=2,A=[123456789]k = 2, \quad A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}k=2,A=​147​258​369​​

Then, k⋅A=2⋅[123456789]=[24681012141618]k \cdot A = 2 \cdot \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix} = \begin{bmatrix} 2 & 4 & 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18 \end{bmatrix}k⋅A=2⋅​147​258​369​​=​2814​41016​61218​​

Step by step explanation

Step 1: Multiply each element by kkk

  • (1,1):2∗1=2(1,1): 2 * 1 = 2(1,1):2∗1=2

  • (1,2):2∗2=4(1,2): 2 * 2 = 4(1,2):2∗2=4

  • (1,3):2∗3=6(1,3): 2 * 3 = 6(1,3):2∗3=6

  • (2,1):2∗4=8(2,1): 2 * 4 = 8(2,1):2∗4=8

  • (2,2):2∗5=10(2,2): 2 * 5 = 10(2,2):2∗5=10

  • (2,3):2∗6=12(2,3): 2 * 6 = 12(2,3):2∗6=12

  • (3,1):2∗7=14(3,1): 2 * 7 = 14(3,1):2∗7=14

  • (3,2):2∗8=16(3,2): 2 * 8 = 16(3,2):2∗8=16

  • (3,3):2∗9=18(3,3): 2 * 9 = 18(3,3):2∗9=18

Step 2: Write the result A=[24681012141618]A = \begin{bmatrix} 2 & 4 & 6 \\ 8 & 10 & 12 \\ 14 & 16 & 18 \end{bmatrix}A=​2814​41016​61218​​

Matrix Multiplication

Matrix multiplication is a crucial operation in many ML computations, including neural network layers and linear transformations.

Matrix Multiplication Compatibility

For two matrices A and B to be multiplied:

  • The number of columns in matrix must equal the number of rows in matrix BBB.

  • If A is an m×nm × nm×n matrix and B is a p×qp × qp×q matrix, then n must equal p.

  • The resulting matrix will have dimensions m×qm × qm×q.

Example (2x2 matrices):

Given: A=[1234],B=[5678]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}, \quad B = \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix}A=[13​24​],B=[57​68​]

Then, A⋅B=[1234]⋅[5678]=[19224350]A \cdot B = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} \cdot \begin{bmatrix} 5 & 6 \\ 7 & 8 \end{bmatrix} = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}A⋅B=[13​24​]⋅[57​68​]=[1943​2250​]

Step by step explanation

Step 1: Multiply row 1 of A with columns of B

  • (1,1):(15)+(27)=5+14=19(1,1): (15) + (27) = 5 + 14 = 19(1,1):(15)+(27)=5+14=19

  • (1,2):(16)+(28)=6+16=22(1,2): (16) + (28) = 6 + 16 = 22(1,2):(16)+(28)=6+16=22

Step 2: Multiply row 2 of A with columns of B

  • (2,1):(35)+(47)=15+28=43(2,1): (35) + (47) = 15 + 28 = 43(2,1):(35)+(47)=15+28=43

  • (2,2):(36)+(48)=18+32=50(2,2): (36) + (48) = 18 + 32 = 50(2,2):(36)+(48)=18+32=50

Step 3: Write the result AB=[19224350]AB = \begin{bmatrix} 19 & 22 \\ 43 & 50 \end{bmatrix}AB=[1943​2250​]

Example in ML. Forward pass in a neural network layer:

Given:

W=[0.10.20.30.4],X=[23],b=[0.10.2]W = \begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix}, \quad X = \begin{bmatrix} 2 \\ 3 \end{bmatrix}, \quad b = \begin{bmatrix} 0.1 \\ 0.2 \end{bmatrix}W=[0.10.3​0.20.4​],X=[23​],b=[0.10.2​]

Then,

Z=W⋅X+b=[(0.1⋅2+0.2⋅3)(0.3⋅2+0.4⋅3)]+[0.10.2]=[0.82.0]+[0.10.2]=[0.92.2]Z = W \cdot X + b = \begin{bmatrix} (0.1 \cdot 2 + 0.2 \cdot 3) \\ (0.3 \cdot 2 + 0.4 \cdot 3) \end{bmatrix} + \begin{bmatrix} 0.1 \\ 0.2 \end{bmatrix} = \begin{bmatrix} 0.8 \\ 2.0 \end{bmatrix} + \begin{bmatrix} 0.1 \\ 0.2 \end{bmatrix} = \begin{bmatrix} 0.9 \\ 2.2 \end{bmatrix}Z=W⋅X+b=[(0.1⋅2+0.2⋅3)(0.3⋅2+0.4⋅3)​]+[0.10.2​]=[0.82.0​]+[0.10.2​]=[0.92.2​]

Step by step explanation

Step 1: Multiply WWW and XXX

Z = W \cdot X = \begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix} \cdot \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} (0.1 \cdot 2 + 0.2 \cdot 3) \\ (0.3 \cdot 2 + 0.4 \cdot 3) \end{bmatrix}$$$$Z = W \cdot X = \begin{bmatrix} 0.1 & 0.2 \\ 0.3 & 0.4 \end{bmatrix} \cdot \begin{bmatrix} 2 \\ 3 \end{bmatrix} = \begin{bmatrix} (0.1 \cdot 2 + 0.2 \cdot 3) \\ (0.3 \cdot 2 + 0.4 \cdot 3) \end{bmatrix}

Step 2: Add bias b

Z = \begin{bmatrix} 0.8 \\ 1.8 \end{bmatrix} + \begin{bmatrix} 0.1 \\ 0.2 \end{bmatrix} = \begin{bmatrix} 0.8 + 0.1 \\ 1.8 + 0.2 \end{bmatrix} = \begin{bmatrix} 0.9 \\ 2.0 \end{bmatrix}$$$$[0.8] + [0.1] = [0.9] [1.8] [0.2] [2.0]

Transposition

Transposition is the operation of flipping a matrix over its diagonal, switching its rows with its columns.

Transpose Properties

(AT)T=A(AB)T=BTAT(A+B)T=AT+BT(A^T)^T = A (AB)^T = B^T A^T (A + B)^T = A^T + B^T(AT)T=A(AB)T=BTAT(A+B)T=AT+BT

Example:

Given: A=[1234]A = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix}A=[13​24​]

Taking the transpose of AAA: AT=[1324]A^T = \begin{bmatrix} 1 & 3 \\ 2 & 4 \end{bmatrix}AT=[12​34​]

Then, taking the transpose again: (AT)T=[1234]=A(A^T)^T = \begin{bmatrix} 1 & 2 \\ 3 & 4 \end{bmatrix} = A(AT)T=[13​24​]=A

ML Application: These properties are often used in deriving gradient descent algorithms and in simplifying complex matrix expressions in various ML models.

Understanding these compatibility conditions and properties is essential for:

  1. Correctly implementing machine learning algorithms

  2. Optimizing computations for better performance

  3. Debugging issues related to matrix dimensions in neural networks

  4. Deriving new algorithms or simplifying existing ones

In practice, many machine learning libraries handle these compatibility checks automatically, but understanding the underlying principles helps in designing and troubleshooting models effectively.

Example (3x3 matrix):

Given: A=[123456789]A = \begin{bmatrix} 1 & 2 & 3 \\ 4 & 5 & 6 \\ 7 & 8 & 9 \end{bmatrix}A=​147​258​369​​

Then, the transpose of AAA , denoted ATA^TAT, is: AT=[147258369]A^T = \begin{bmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}AT=​123​456​789​​

Step by step explanation

Step 1: Swap rows and columns

  • New(1,1)=Old(1,1):1New (1,1) = Old (1,1): 1New(1,1)=Old(1,1):1

  • New(1,2)=Old(2,1):4New (1,2) = Old (2,1): 4New(1,2)=Old(2,1):4

  • New(1,3)=Old(3,1):7New (1,3) = Old (3,1): 7New(1,3)=Old(3,1):7

  • New(2,1)=Old(1,2):2New (2,1) = Old (1,2): 2New(2,1)=Old(1,2):2

  • New(2,2)=Old(2,2):5New (2,2) = Old (2,2): 5New(2,2)=Old(2,2):5

  • New(2,3)=Old(3,2):8New (2,3) = Old (3,2): 8New(2,3)=Old(3,2):8

  • New(3,1)=Old(1,3):3New (3,1) = Old (1,3): 3New(3,1)=Old(1,3):3

  • New(3,2)=Old(2,3):6New (3,2) = Old (2,3): 6New(3,2)=Old(2,3):6

  • New(3,3)=Old(3,3):9New (3,3) = Old (3,3): 9New(3,3)=Old(3,3):9

Step 2: Write the result

AT=[147258369]A^T = \begin{bmatrix} 1 & 4 & 7 \\ 2 & 5 & 8 \\ 3 & 6 & 9 \end{bmatrix}AT=​123​456​789​​

Example in ML. Computing the gradient in linear regression:

( gradient=XT∗(ypred−y)/nsamplesgradient = X^T * (y_{pred} - y) / n_{samples}gradient=XT∗(ypred​−y)/nsamples​ )

Given: X=[123456],y=[51117],w=[0.51.5]X = \begin{bmatrix} 1 & 2 \\ 3 & 4 \\ 5 & 6 \end{bmatrix}, \quad y = \begin{bmatrix} 5 \\ 11 \\ 17 \end{bmatrix}, \quad w = \begin{bmatrix} 0.5 \\ 1.5 \end{bmatrix}X=​135​246​​,y=​51117​​,w=[0.51.5​]

Transpose of XXX: XT=[135246]X^T = \begin{bmatrix} 1 & 3 & 5 \\ 2 & 4 & 6 \end{bmatrix}XT=[12​34​56​]

Predicted values ypredy_{\text{pred}}ypred​:

ypred=X⋅w=[1⋅0.5+2⋅1.53⋅0.5+4⋅1.55⋅0.5+6⋅1.5]=[3.57.511.5]y_{\text{pred}} = X \cdot w = \begin{bmatrix} 1 \cdot 0.5 + 2 \cdot 1.5 \\ 3 \cdot 0.5 + 4 \cdot 1.5 \\ 5 \cdot 0.5 + 6 \cdot 1.5 \end{bmatrix} = \begin{bmatrix} 3.5 \\ 7.5 \\ 11.5 \end{bmatrix}ypred​=X⋅w=​1⋅0.5+2⋅1.53⋅0.5+4⋅1.55⋅0.5+6⋅1.5​​=​3.57.511.5​​

Error calculation:

error=ypred−y=[3.5−57.5−1111.5−17]=[−1.5−3.5−5.5]\text{error} = y_{\text{pred}} - y = \begin{bmatrix} 3.5 - 5 \\ 7.5 - 11 \\ 11.5 - 17 \end{bmatrix} = \begin{bmatrix} -1.5 \\ -3.5 \\ -5.5 \end{bmatrix}error=ypred​−y=​3.5−57.5−1111.5−17​​=​−1.5−3.5−5.5​​

Gradient calculation:

gradient=XT⋅error=[1⋅(−1.5)+3⋅(−3.5)+5⋅(−5.5)2⋅(−1.5)+4⋅(−3.5)+6⋅(−5.5)]=[−40.5−58.5]\text{gradient} = X^T \cdot \text{error} = \begin{bmatrix} 1 \cdot (-1.5) + 3 \cdot (-3.5) + 5 \cdot (-5.5) \\ 2 \cdot (-1.5) + 4 \cdot (-3.5) + 6 \cdot (-5.5) \end{bmatrix} = \begin{bmatrix} -40.5 \\ -58.5 \end{bmatrix}gradient=XT⋅error=[1⋅(−1.5)+3⋅(−3.5)+5⋅(−5.5)2⋅(−1.5)+4⋅(−3.5)+6⋅(−5.5)​]=[−40.5−58.5​]\

Step by step explanation

Step 1: Calculate error:

error=ypred−y=[3.5−57.5−1111.5−17]=[−1.5−3.5−5.5]\text{error} = y_{\text{pred}} - y = \begin{bmatrix} 3.5 - 5 \\ 7.5 - 11 \\ 11.5 - 17 \end{bmatrix} = \begin{bmatrix} -1.5 \\ -3.5 \\ -5.5 \end{bmatrix}error=ypred​−y=​3.5−57.5−1111.5−17​​=​−1.5−3.5−5.5​​

Step 2: Transpose XXX:

XT=[135246]X^T = \begin{bmatrix} 1 & 3 & 5 \\ 2 & 4 & 6 \end{bmatrix}XT=[12​34​56​]

Step 3: Multiply XTX^TXT by error:

gradient=XT⋅error=[1⋅(−1.5)+3⋅(−3.5)+5⋅(−5.5)2⋅(−1.5)+4⋅(−3.5)+6⋅(−5.5)]=[−40.5−58.5]\text{gradient} = X^T \cdot \text{error} = \begin{bmatrix} 1 \cdot (-1.5) + 3 \cdot (-3.5) + 5 \cdot (-5.5) \\ 2 \cdot (-1.5) + 4 \cdot (-3.5) + 6 \cdot (-5.5) \end{bmatrix} = \begin{bmatrix} -40.5 \\ -58.5 \end{bmatrix}gradient=XT⋅error=[1⋅(−1.5)+3⋅(−3.5)+5⋅(−5.5)2⋅(−1.5)+4⋅(−3.5)+6⋅(−5.5)​]=[−40.5−58.5​]

Step 4: Divide by nsamplesn_{samples}nsamples​ (3 in this case):

gradient3=[−40.53−58.53]=[−13.5−19.5]\frac{\text{gradient}}{3} = \begin{bmatrix} \frac{-40.5}{3} \\ \frac{-58.5}{3} \end{bmatrix} = \begin{bmatrix} -13.5 \\ -19.5 \end{bmatrix}3gradient​=[3−40.5​3−58.5​​]=[−13.5−19.5​]

This step-by-step breakdown illustrates how each matrix operation is performed and how it applies in machine learning contexts.

PreviousDefinition and TypesNextDeterminant of a Matrix

Last updated 1 month ago