A Python repository demonstrating tensor operations implemented from scratch, compared with NumPy's efficient implementations.
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.
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
- Python 3.x
- NumPy library
Run the main script to see all tensor operations in action:
python main.py@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@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]
returnzThis project is licensed under the MIT License - see the LICENSE file for details.