Skip to content

corticalstack/TensorOpsInPython

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 

Repository files navigation

🧮 TensorOpsInPython

A Python repository demonstrating tensor operations implemented from scratch, compared with NumPy's efficient implementations.

📝 Description

TensorOpsInPython provides educational examples of fundamental tensor operations implemented using naive Python loops alongside their NumPy equivalents. This repository serves as a learning resource for understanding the underlying mechanics of tensor operations commonly used in machine learning and deep learning applications.

✨ Features

  • Basic Tensor Operations:

    • ReLU activation function
    • Addition, subtraction, and multiplication of tensors
    • Broadcasting (adding a matrix and a vector)
  • Advanced Tensor Operations:

    • Vector dot product
    • Matrix-vector dot product (two implementations)
    • Matrix-matrix dot product
  • Tensor Transformations:

    • Reshaping tensors
    • Transposing tensors (exchanging rows and columns)
  • Comparative Learning:

    • Each operation is implemented both with naive Python loops and using NumPy's optimized functions
    • Output comparison between custom implementations and NumPy equivalents

🔧 Prerequisites

  • Python 3.x
  • NumPy library

🚀 Usage

Run the main script to see all tensor operations in action:

python main.py

Example Code Snippets

Implementing a Naive ReLU Function

@staticmethod
def naive_relu(x):
    assert len(x.shape) == 2
    x = x.copy()
    for i in range(x.shape[0]):
        for j in range(x.shape[1]):
            x[i, j] = max(x[i, j], 0)
    return x

Matrix-Vector Dot Product

@staticmethod
def naive_matrix_vector_dot(x, y):
    assert len(x.shape) == 2  # Numpy matrix
    assert len(y.shape) == 1  # Numpy vector
    assert x.shape[1] == y.shape[0]

    z = np.zeros(x.shape[0])
    for i in range(x.shape[0]):
        for j in range(x.shape[1]):
            z[i] += x[i, j] * y[j]
    return z

📄 License

This project is licensed under the MIT License - see the LICENSE file for details.

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages