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
@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
@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
This project is licensed under the MIT License - see the LICENSE file for details.