一个用于深度学习验证的工具包
Project description
TensorPlay
A simple deep learning framework designed for educational purposes and small-scale experiments. TensorPlay provides basic building blocks for constructing and training neural networks, including tensor operations, layers, optimizers, and training utilities.
Features
- Core Tensor Structure: Implements a
Tensorclass with automatic gradient computation, supporting basic arithmetic operations and common activation functions (ReLU,Sigmoid,Tanh,Softmax,GELU). - Neural Network Components: Includes essential layers like
Dense(fully connected layer) and aModulebase class for building custom models. - Training Utilities: Provides
DataLoaderfor batching data, training / validation loop helpers (train_on_batch,valid_on_batch), and optimization withAdamoptimizer. - Early Stopping: Built-in
EarlyStoppingcallback to prevent overfitting. - Loss Functions: Implements common loss functions such as
MSE(Mean Squared Error),SSE(Sum of Squared Errors), and NLL (Negative Log Likelihood).
Basic Usage
1. Define a Model
Create custom models by inheriting from Module and defining the forward pass:
from TensorPlay import Module, Dense
class MyModel(Module):
def __init__(self, input_size, hidden_size, output_size):
super().__init__()
self.fc1 = Dense(input_size, hidden_size)
self.fc2 = Dense(hidden_size, output_size)
def forward(self, x):
x = self.fc1(x).relu() # Apply ReLU activation after first layer
x = self.fc2(x).sigmoid() # Apply Sigmoid for binary classification
return x
2. Prepare Data
Use DataLoader to handle batching and shuffling:
from TensorPlay import DataLoader
# Assume data is a list of (input_features, label) tuples
train_data = [(x1, y1), (x2, y2), ...]
train_loader = DataLoader(train_data, batch_size=32, shuffle=True)
3. Train the Model
Train the model using the train_on_batch function. A typical training loop includes batch training, validation, and early stopping judgment:
from TensorPlay import Adam, EarlyStopping
def train(model, loader, val_data, epochs=50, lr=0.01):
optimizer = Adam(model.params(), lr=lr)
stoper = EarlyStopping(patience=5, delta=0.1, verbose=True)
for epoch in range(epochs):
# Training phase
total_loss = 0
correct = 0
total_samples = 0
for batch in loader:
batch_size = len(batch[0])
total_samples += batch_size
# Batch training and obtaining loss and accuracy
loss, acc = train_on_batch(model, batch, optimizer)
total_loss += loss * batch_size # 累计总损失
correct += acc * batch_size # 累计正确样本数
# Calculate the average metrics of the training set
avg_loss = total_loss / total_samples
accuracy = correct / total_samples
# Verification phase
total_loss = 0
correct = 0
total_samples = 0
for batch in val_data:
batch_size = len(batch[0])
val_loss, val_acc = valid_on_batch(model, batch)
total_samples += batch_size
correct += val_acc * batch_size
total_loss += val_loss * batch_size
# Calculate the average metrics of the validation set
val_avg_loss = total_loss / total_samples
val_accuracy = correct / total_samples
# Print training information
print(f"Epoch {epoch + 1}/{epochs}")
print(f"Loss: {avg_loss:.4f} | Accuracy: {accuracy:.4f} "
f"Val Loss: {val_avg_loss:.4f} | Val Accuracy: {val_accuracy:.4f}")
# Early stopping check
stoper(val_avg_loss, model)
if stoper.early_stop:
break
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).data[0] > 0.5 else 0 # For binary classification
if pred == y.data[0]:
correct += 1
total += 1
print(f"Test Accuracy: {correct/total:.4f}")
Example: KRK Chess Endgame Classification
The demo/KRK_classify.py script demonstrates classifying chess endgame positions (King-Rook-King) as either a draw or not. Key steps:
- Data Loading: Parses
krkopt.datainto numerical features. - Data Preparation: Splits data into training, validation, and test sets.
- Model Definition: Uses a 3-layer fully connected network (
KRKClassifier). - Training: Uses
Adamoptimizer with early stopping. - Evaluation: Computes test accuracy.
Run the example:
python demo/KRK_classify.py
Limitations and Future Improvements
Current Limitations
- Only supports 1D
tensors, no higher-dimensional data (matrices, images). - Limited layer types (only
Denseis implemented). - Basic optimizer support (only
Adamis available). - No
GPUacceleration; all operations areCPU-bound. - Limited debugging tools for computation graphs.
Planned Improvements
- Add support for n-dimensional tensors (
matrices,3D tensors). - Implement more layers (
Dropout,BatchNorm). - Support GPU acceleration via
CUDAfor faster training. - Improve automatic differentiation efficiency.
- Include more loss functions (
Cross-Entropy,MAE). - Add visualization tools for computation graphs and training metrics.
Contributing
Contributions are welcome! Feel free to open issues for bugs or feature requests, or submit pull requests with improvements.
License
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 Distribution
Built Distribution
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-0.0.1.tar.gz.
File metadata
- Download URL: tensorplay-0.0.1.tar.gz
- Upload date:
- Size: 26.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1b52e9e44951563bafdf13b45bdd7bfff1ff4ed88316a69fc1a7c38d4edcaaf4
|
|
| MD5 |
ced53e563247982d99d6345fb3ef8951
|
|
| BLAKE2b-256 |
9d598a13a7237f6ba056fd085955fa3b017ad613603142b1546b4011aa0afa59
|
File details
Details for the file TensorPlay-0.0.1-py3-none-any.whl.
File metadata
- Download URL: TensorPlay-0.0.1-py3-none-any.whl
- Upload date:
- Size: 24.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3238af71e7695caff7a553f8510060cdbd3620b78d69ee95284cc280b9ed7e1c
|
|
| MD5 |
e865ddb6b1869a875c44a7b808fd88cb
|
|
| BLAKE2b-256 |
01a0ebd21d8181962a35f02369c3872c5168cac69c8e98000017f9b3f0e2d01c
|