Skip to content

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

@staticmethoddefnaive_relu(x):
assertlen(x.shape) ==2x=x.copy()
foriinrange(x.shape[0]):
forjinrange(x.shape[1]):
x[i, j] =max(x[i, j], 0)
returnx

Matrix-Vector Dot Product

@staticmethoddefnaive_matrix_vector_dot(x, y):
assertlen(x.shape) ==2# Numpy matrixassertlen(y.shape) ==1# Numpy vectorassertx.shape[1] ==y.shape[0]
z=np.zeros(x.shape[0])
foriinrange(x.shape[0]):
forjinrange(x.shape[1]):
z[i] +=x[i, j] *y[j]
returnz

📄 License

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

About

No description, website, or topics provided.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages