Matrix Multiplication (PyTorch)

Recently we discovered a neat website that visualizes how matrix multiplication works. You can adjust the values and dimensions, then start the visualization. It’s an excellent tool for learning the concept or explaining it to others. 🧮👨‍🏫

🌐 Website: http://matrixmultiplication.xyz/ (No SSL, sorry)

On a side note, we came across this while preparing a training session on using PyTorch with Python for AI-based image classification. This approach can also be integrated into #LabVIEW. If you’re interested, don’t hesitate to get in touch.

import torch

# Define two matrices
matrix_a = torch.tensor([[1, 2], 
                         [3, 4]], dtype=torch.float32)
matrix_b = torch.tensor([[5, 6], 
                         [7, 8]], dtype=torch.float32)

# Perform matrix multiplication
result = torch.matmul(matrix_a, matrix_b)

# Print the result
print("Matrix A:")
print(matrix_a)

print("\nMatrix B:")
print(matrix_b)

print("\nResult of Matrix Multiplication:")
print(result)

Explanation:

  1. torch.tensor: Creates the matrices matrix_a and matrix_b.
  2. torch.matmul: Performs matrix multiplication.
  3. Result: The resulting matrix is calculated using the dot product of rows from matrix_a and columns from matrix_b.

Matrix A:
tensor([[1., 2.],
        [3., 4.]])

Matrix B:
tensor([[5., 6.],
        [7., 8.]])

Result of Matrix Multiplication:
tensor([[19., 22.],
        [43., 50.]])

The result matrix is calculated as follows:

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert