Skip to main content

一个用于深度学习验证的工具包

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 Tensor class 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 a Module base class for building custom models.
  • Training Utilities: Provides DataLoader for batching data, training / validation loop helpers (train_on_batch, valid_on_batch), and optimization with Adam optimizer.
  • Early Stopping: Built-in EarlyStopping callback 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:

  1. Data Loading: Parses krkopt.data into numerical features.
  2. Data Preparation: Splits data into training, validation, and test sets.
  3. Model Definition: Uses a 3-layer fully connected network (KRKClassifier).
  4. Training: Uses Adam optimizer with early stopping.
  5. 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 Dense is implemented).
  • Basic optimizer support (only Adam is available).
  • No GPU acceleration; all operations are CPU-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 CUDA for 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

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tensorplay-0.0.1.tar.gz (26.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

TensorPlay-0.0.1-py3-none-any.whl (24.5 kB view details)

Uploaded Python 3

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

Hashes for tensorplay-0.0.1.tar.gz
Algorithm Hash digest
SHA256 1b52e9e44951563bafdf13b45bdd7bfff1ff4ed88316a69fc1a7c38d4edcaaf4
MD5 ced53e563247982d99d6345fb3ef8951
BLAKE2b-256 9d598a13a7237f6ba056fd085955fa3b017ad613603142b1546b4011aa0afa59

See more details on using hashes here.

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

Hashes for TensorPlay-0.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 3238af71e7695caff7a553f8510060cdbd3620b78d69ee95284cc280b9ed7e1c
MD5 e865ddb6b1869a875c44a7b808fd88cb
BLAKE2b-256 01a0ebd21d8181962a35f02369c3872c5168cac69c8e98000017f9b3f0e2d01c

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page