A simple deep learning framework designed for educational purposes and small-scale experiments.
Project description
TensorPlay is a learner-friendly, DIY-ready deep learning framework designed for educational purposes and small-scale experiments.
TensorPlay
TensorPlay provides basic building blocks for constructing and training neural networks, including tensor operations, layers, optimizers, and training utilities.
🚀 Quick Install
PyPI Installation (Recommended)
pip install tensorplay --upgrade
Source Installation (For Development with OneAPI CUDA Support)
# Clone the repository
git clone https://github.com/bluemoon-o2/TensorPlay.git
cd TensorPlay
# Install in editable mode
pip install -e .
🎯 Core Features
| Module | Key Features |
|---|---|
tensorplay.Tensor |
Automatic gradient computation (autograd), basic arithmetic ops, broadcasting, and activation functions (ReLU, Sigmoid, Tanh, Softmax, GELU) |
tensorplay.nn |
Modular neural network layers (Linear/Dense, Conv2d), Module base class for custom models, and loss functions (MSE, NLL, CrossEntropy) |
tensorplay.optim |
Optimization algorithms (Adam, SGD, AdamW) with parameter update logic |
tensorplay.data |
DataLoader for batching, shuffling, and data iteration |
⚡Basic Usage
TensorPlay’s API is intentionally designed to match PyTorch, so if you know PyTorch, you already know most of TensorPlay!
1. Tensors and Automatic Differentiation
import tensorplay as tp
# Create a tensor with requires_grad=True (for autograd)
x = tp.Tensor([[1.0, 2.0], [3.0, 4.0]], requires_grad=True)
y = tp.Tensor([[5.0, 6.0], [7.0, 8.0]], requires_grad=True)
# Perform operations
z = x.matmul(y) + tp.ones_like(x)
loss = z.sum()
# Backpropagation (compute gradients)
loss.backward()
# Inspect gradients (aligned with PyTorch's grad behavior)
print(x.grad) # Gradient of loss w.r.t. x
print(y.grad) # Gradient of loss w.r.t. y
2. Define a Neural Network
Inherit from tp.nn.Module (just like torch.nn.Module) and define the forward pass:
import tensorplay as tp
from tensorplay.nn import Module, Linear, ReLU, Sigmoid
class MLP(Module):
def __init__(self, input_dim: int, hidden_dim: int, output_dim: int):
super().__init__()
# Linear layer (alias: Dense, same as torch.nn.Linear)
self.fc1 = Linear(input_dim, hidden_dim)
self.fc2 = Linear(hidden_dim, output_dim)
# Activation functions (functional or module-based)
self.relu = ReLU()
self.sigmoid = Sigmoid()
def forward(self, x: tp.Tensor) -> tp.Tensor:
# Forward pass (exactly like PyTorch)
x = self.relu(self.fc1(x))
x = self.sigmoid(self.fc2(x))
return x
# Initialize the model
model = MLP(input_dim=10, hidden_dim=32, output_dim=1)
# Print model structure (automatically implemented by Module)
print(model)
3. Prepare Data with DataLoader
Train the model using the train_on_batch function. A typical training loop includes batch training, validation, and early stopping judgment:
import tensorplay as tp
from tensorplay.data import DataLoader, TensorDataset
# Sample data (features: (N, 10), labels: (N, 1))
train_data = TensorDataset(tp.randn(100, 10), tp.randn(100, 1))
# DataLoader (batch_size, shuffle)
train_loader = DataLoader(
dataset=train_data,
batch_size=8,
num_workers=2,
prefetch_factor=2,
shuffle=True,
drop_last=False
)
4. Evaluate the Model
def test(model, test_loader):
correct = 0
total = 0
for batch_x, batch_y in test_loader:
for x, y in zip(batch_x, batch_y):
# Make predictions on a single sample
# Set the threshold according to the task type
# (taking binary classification as an example here)
pred = 1 if model(x).item() > 0.5 else 0 # For binary classification
if pred == y.item():
correct += 1
total += 1
print(f"Test Accuracy: {correct/total:.4f}")
♨️ Benchmark
We provide benchmark results on several standard datasets to demonstrate TensorPlay's performance and usability. Detailed benchmark results and comparisons with other frameworks can be found in the Benchmark Report.
👩👩👧👦Contributing
Contributions are welcome! Feel free to open issues for bugs or feature requests, or submit pull requests with improvements.
📄 License
This project is released under the Apache 2.0 license.
🔗Links
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distributions
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file tensorplay-1.0.0rc0-cp313-cp313-win_amd64.whl.
File metadata
- Download URL: tensorplay-1.0.0rc0-cp313-cp313-win_amd64.whl
- Upload date:
- Size: 82.6 MB
- Tags: CPython 3.13, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d42e1f35bd2024afa2996d590cd2ee0e2e5dc6e603778427b5075e44d87b06f5
|
|
| MD5 |
7e5cae1c2c9e6f39b4595645beb91947
|
|
| BLAKE2b-256 |
9501b7fc8444342e61f87f4cd4753f746921648c5f38f54469e2d37338c776e2
|
File details
Details for the file tensorplay-1.0.0rc0-cp312-cp312-win_amd64.whl.
File metadata
- Download URL: tensorplay-1.0.0rc0-cp312-cp312-win_amd64.whl
- Upload date:
- Size: 82.4 MB
- Tags: CPython 3.12, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7160a540d82738d577a3899b2c0c4f5cec7487d97e3387df2564ddce21d7bd8e
|
|
| MD5 |
eff8573064bc63d677bd7b2187a62778
|
|
| BLAKE2b-256 |
78723b66570109a238b51b0cb59db0e8ba76097fb5fe77cfcb2c7d4de3133d04
|
File details
Details for the file tensorplay-1.0.0rc0-cp311-cp311-win_amd64.whl.
File metadata
- Download URL: tensorplay-1.0.0rc0-cp311-cp311-win_amd64.whl
- Upload date:
- Size: 82.1 MB
- Tags: CPython 3.11, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
f6498222a198686e17af60f0d653f47be8a6bfdcfe2943a0baeaae48b0476afe
|
|
| MD5 |
2d66c35828bd64dbeb8ab2535e590fe0
|
|
| BLAKE2b-256 |
fd96e325fda2d5e21ed5e2deb735a773d0fb214577618a094bda2dc5cf238b76
|
File details
Details for the file tensorplay-1.0.0rc0-cp310-cp310-win_amd64.whl.
File metadata
- Download URL: tensorplay-1.0.0rc0-cp310-cp310-win_amd64.whl
- Upload date:
- Size: 81.9 MB
- Tags: CPython 3.10, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8ce97902fa5d336a372c47245624257f26f5a95286887fda628c41de2f4c0452
|
|
| MD5 |
91ec89c7302a9a5a8079dd3e32bedbd5
|
|
| BLAKE2b-256 |
1c971bec2b79a8ed2b02594a2a08afa76458c9d13ed05940a3de2f8454b867dc
|
File details
Details for the file tensorplay-1.0.0rc0-cp39-cp39-win_amd64.whl.
File metadata
- Download URL: tensorplay-1.0.0rc0-cp39-cp39-win_amd64.whl
- Upload date:
- Size: 81.6 MB
- Tags: CPython 3.9, Windows x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c836666594aa4d8928f6e92ab9d94125f626eb588108e5fd7842d6589ae4c609
|
|
| MD5 |
f5f325af84e80dee8990c826b7775374
|
|
| BLAKE2b-256 |
a5161c4f1d1d2603a6c30f5708300e894d983f3c88a01ceb56bf5d5e70b1863e
|